Godot color replacement shader not working

Godot Version

4.2

Question

In my Godot game I have a player sprite. With a shader I’m trying to make it so one color of the sprite will be changed to whatever I need it to be. In my shader it only works when I try to change the color black, but I am trying to change the color of the orange (not the light orange). I’ve tried looking at many tutorials but none of them work.

shader_type canvas_item;

uniform vec4 og_color : source_color;
uniform vec4 new_color : source_color;

void fragment() {
	vec4 curr_color = texture(TEXTURE, UV);

    // Define a threshold for the comparison
	if (COLOR == og_color){
		COLOR = new_color;
	}
	else{
		COLOR.rgb = curr_color.rgb;
	}
	
}

if (curr_color == og_color){
COLOR = new_color;
}

1 Like

Yup, tried that.

The shader is working. Did you try to attach a material with the shader to sprite2d?

If your sprite2d is a child of node2d to which the shader is attached, you can try enabling “use parent material” in the sprite2d settings.

There is another problem with my version of the engine 4.2. When I reload the project, the engine seems to forget what colors were selected in the shaders parameters, despite the fact that they are displayed reliably. Only when I pick the og_color color again the shader start working as expected.

I don’t think so. The only thing I have done is material: New shader material, and then added the shader. It is an animated sprite though.

Your problem may be that you are trying to compare floating point numbers: vec4.

You can try converting curr_color and og_color to uvec4 or do something else.

shader_type canvas_item;

uniform vec4 og_color : source_color;
uniform vec4 new_color : source_color;

void fragment() {
	vec4 curr_color = texture(TEXTURE, UV);

    // Define a threshold for the comparison
	uvec4 uint_curr_color = uvec4(round(curr_color * 255.0));
	uvec4 uint_og_color = uvec4(round(og_color * 255.0));
	if (uint_curr_color == uint_og_color){
		COLOR = new_color;
	}
	else{
		COLOR = curr_color;
	}
	
}

I found the issue. The problem was that I was using HDR 2d, and that ruins some shaders.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.