#Changing shaders through code

13 messages · Page 1 of 1 (latest)

tepid anchor
#

You'll want to use some uniforms for this.

In your shader, do this:

shader_type canvas_item;

uniform vec4 main_color;
uniform vec4 secondary_color;
uniform vec4 accessory_color;

void fragment() {
  ...
}

Then, in your script you can set them like so:

// Where my_shader_material is your ShaderMaterial
// and main_color, secondary_color, accessory_color
// are set to their corresponding values

my_shader_material.set_shader_parameter("main_color", main_color)
my_shader_material.set_shader_parameter("secondary_color", secondary_color)
my_shader_material.set_shader_parameter("accessory_color", accessory_color)

You can see more about both here:
https://docs.godotengine.org/en/stable/tutorials/shaders/your_first_shader/your_first_3d_shader.html#uniforms
https://docs.godotengine.org/en/stable/classes/class_shadermaterial.html

wooden pasture
#

Shader autoloads?

tepid anchor
#

Just a reference to your ShaderMaterial

proud bear
tepid anchor
#

Is this on a Sprite2D

proud bear
tepid anchor
#

Just material then

proud bear
tepid anchor
#

It's because if you just access it like that autocomplete doesn't know it's a ShaderMaterial

proud bear
#

Ah

tepid anchor
#

If you want autocomplete try this:
(material as ShaderMaterial).set_shader_parameter