This shader i wrote is suppose to take the background sample color provided by user, and make it transparent.
It seems to work while in the editor but when i run it, the background color is not transparent.

Shader is applied to an animated sprite.

shader_type canvas_item;
uniform vec4 bg_color : hint_color;

void fragment(){
	vec4 col = texture(TEXTURE,UV).rgba;
	if (col == bg_color){
		col.a = 0.0;
	}
	COLOR = col;
}
  • Looks like i found someone with a similar issue, and changed it to this. Works now.

    shader_type canvas_item;
    uniform vec4 bg_color : hint_color;
    
    void fragment(){
    	vec4 col = texture(TEXTURE,UV).rgba;
    	vec4 d4 = abs(col - bg_color);
    	float d = max(max(d4.r,d4.g),d4.b);
    	if (d < 0.01){
    		col.a = 0.0;
    	}
    	COLOR = col;
    }

Looks like i found someone with a similar issue, and changed it to this. Works now.

shader_type canvas_item;
uniform vec4 bg_color : hint_color;

void fragment(){
	vec4 col = texture(TEXTURE,UV).rgba;
	vec4 d4 = abs(col - bg_color);
	float d = max(max(d4.r,d4.g),d4.b);
	if (d < 0.01){
		col.a = 0.0;
	}
	COLOR = col;
}

    The reason it likely didn't work was because of floating point inaccuracy. For example, gray can be 0.5 (on all 3 channels). But in the computer, it might be stored as 0.50000001 or 0.500004 or something not quite exactly 0.5. So you can't compare floating point numbers that way (or composite objects, that are made up of floats/doubles like vector or color). However, it should not have worked in the editor either, the code was just incorrect. So I'm not sure how it ever would work.