Godot Version
4.3 stable
Question
I have a visual shader in .tres file and Sprite2D object. How can I add this visual shader to my object and change visual shader variables from code?
4.3 stable
I have a visual shader in .tres file and Sprite2D object. How can I add this visual shader to my object and change visual shader variables from code?
Easy. Preload your shader resource and set the shader to the sprite2d
const SHADER = preload("res://shaders/visual_shader.tres") ##Location of your visual shader
func _ready() -> void:
sprite_2d.set_shader(SHADER)
You can also save the shader with its parameters as a material and set material instead.
The correct answer is below How to add a shader on object from code? - #3 by gertkeno
Need to give the sprite a shader material first, set_shader
isn’t a method of Sprite2d/CanvasItem
const SHADER = preload("res://shaders/visual_shader.tres") ##Location of your visual shader
func _ready() -> void:
sprite_2d.material = ShaderMaterial.new()
sprite_2d.material.shader = SHADER
You’re right, I missed that.
Thanks, but thats only half of problem. I still dont know How I can change shader parameters
Use shader material’s set_shader_parameter
method
sprite_2d.material.set_shader_parameter("uniform_name", new_value)
Everything works, Thanks a lot
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.