How can I load a shader file through script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By YP

How can I load a shader file using script, just like we do it in the editor?

shader load

:bust_in_silhouette: Reply From: MysteryGM

The shader file is a resource so you can load it like any resource.

Once you have the resource you just assign it like this:

onready var WaterShader = preload("res://Water.shader")

func _ready():
	self.material.shader = WaterShader

Hope this helps

Thank you that worked

YP | 2018-10-30 09:05

how can you access the material in 3d?

yellowsir | 2019-01-19 16:08

to supplement MysteryGM’s answer:

onready var WaterShader = preload("res://Water.shader")

func _ready():
    some_sprite_node.material = ShaderMaterial.new()
    some_sprite_node.material.shader = WaterShader

whoji | 2020-06-21 07:53

1 Like