I want to remove 3 Colors from my Sprite with a Shader

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

I want to remove 3 Colors from my Sprite with a Shader.

This is what I tried, but it didnt worked well.

Thanks for answers on how I can fix it/ do it!

shader_type canvas_item;

uniform vec4 color1;
uniform vec4 color2;
uniform vec4 color3;

void fragment() {
    if (COLOR == color1 ||COLOR == color2 || COLOR == color3) {
	    COLOR = vec4(0.0,0.0,0.0,0.0)
:bust_in_silhouette: Reply From: estebanmolca

Some time ago I answered a similar question:

Mainly the problem is the precision of the color and how the shader interprets it. So using the == comparator means that the color has to be exactly the same, and due to the floating point calculations and the precision of the shader this never happens. Then > and < are used to set a range.

shader_type canvas_item;

uniform vec4 color1 :hint_color;
uniform vec4 color2 :hint_color;
uniform vec4 color3 :hint_color;
uniform float precision = 0.05;

vec4 replace_color(vec4 color, vec4 curr_color, float threshold){
	if((color.r + threshold >= curr_color.r && color.r - threshold <= curr_color.r) &&
    (color.g + threshold >= curr_color.g && color.g - threshold <= curr_color.g) &&
    (color.b + threshold >= curr_color.b && color.b - threshold <= curr_color.b)){
		return vec4(0.0,0.0,0.0,0.0);
		return curr_color;

void fragment() {
	vec4 tex = texture(TEXTURE,UV);
    tex = replace_color(color1,tex, precision);
	tex = replace_color(color2,tex, precision);
	tex = replace_color(color3,tex, precision);

This should work, and best if you have an unfiltered pixelart texture. Increase or decrease the precision parameter according to the range of colors you want to cover.