Simple shader not working

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

I am trying to turn all invisible pixels (alpha == 0) red. But this code turns the entire image white. If I comment out everything the image goes back to normal. If I remove the conditional and keep the COLOR = ... the entire image is red.

shader_type canvas_item;

void fragment() {
	if (COLOR[3] == 0.0){ #alpha channel
    	COLOR = vec4(255,0,0,255);
	}
}
:bust_in_silhouette: Reply From: jarlowrey

Working code below. I guess you cant use COLOR to get the current color, only to set it, even though the docs say this is a read-only variable for fragment built-ins

shader_type canvas_item;

void fragment() {
	vec4 curr_color = texture(TEXTURE,UV); // Get current color of pixel

	if (curr_color[3] == 0.0){
    	COLOR = vec4(1,0,0,1);
	}else{
		COLOR = curr_color;
	}
}

docs are correct.COLOR is readonly variable in spatial shaders in canvas_item shader COLOR is output variable. please fix your answer

Bartosz | 2018-03-23 00:40