0 votes

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)
Godot version Newest (3.2)
in Engine by (330 points)

1 Answer

+2 votes
Best answer

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.

by (2,058 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.