Best way to change colors on a character and access the shader parameters from the script

Godot Version

4.6

Question

Best way to change colors on a character for example this character I have:

I assume I have to create a shader and change the colors, the closest i’ve seen to this was this shader. Another question was how to access the shader parameters from the script?

You can use set_shader_parameter("uniform_name", uniform_value) on your ShaderMaterial resource to set shader parameters from a script.

In your case, using the RGB channels as a sort of color mask may work best

shader_type canvas_item;

uniform vec3 red_color: source_color;
uniform vec3 green_color: source_color;
uniform vec3 blue_color: source_color;

void fragment() {
	vec4 origin_color = texture(TEXTURE, UV);
	
	vec3 r = red_color * origin_color.r;
	vec3 g = green_color * origin_color.g;
	vec3 b = blue_color * origin_color.b;
	
	COLOR = vec4(r + g + b, origin_color.a);
}

6 Likes

Well that’s cool.

Ah thank you, I’ll try that out immediately!

It works but how do I change the colors from the script? Also forgot to mention it’s an AnimatedSprite2D

You can try with set_shader_parameter("uniform_name", uniform_value) on your ShaderMaterial. You can get your shader material from the same property .material

var shader_mat: ShaderMaterial = $AnimatedSprite2D.material
shader_mat.set_shader_parameter("red_color", Color.BLANCHED_ALMOND)
shader_mat.set_shader_parameter("green_color", Color.CORNFLOWER_BLUE)
shader_mat.set_shader_parameter("blue_color", Color.MEDIUM_VIOLET_RED)
1 Like

Ah thank you but I’m getting this error:
Invalid call. Nonexistent function ‘set_shader_param’ in base ‘ShaderMaterial’.

Sorry yeah it’s set_shader_parameter

1 Like

Yep that worked! Thank you