Shader uniform variable type breaks it

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

As soon as I change const vec4 REPLACE_1 = vec4(1.0); to uniform vec4 REPLACE_1 = vec4(1.0);, the shader turns into some random white rectangle. What’s the error here?

shader_type canvas_item;

const vec4 REPLACE_1 = vec4(1.0);
const vec4 REPLACE_2 = REPLACE_1 - vec4(.25, .25, .25, 0);
const vec4 REPLACE_3 = REPLACE_2 - vec4(.25, .25, .25, 0);
const vec4 REPLACE_4 = REPLACE_3 - vec4(.25, .25, .25, 0);
const vec4 COLOR_1 = vec4(230.0/255.0, 69.0/255.0, 57.0/255.0, 1);
const vec4 COLOR_2 = vec4(173.0/255.0, 47.0/255.0, 69.0/255.0, 1);
const vec4 COLOR_3 = vec4(120.0/255.0, 29.0/255.0, 79.0/255.0, 1);
const vec4 COLOR_4 = vec4(79.0/255.0, 29.0/255.0, 76.0/255.0, 1);

void fragment() {
	COLOR = texture(TEXTURE, UV);
	if (COLOR == COLOR_1){
		COLOR = REPLACE_1;
	} else if (COLOR == COLOR_2){
		COLOR = REPLACE_2;
	} else if (COLOR == COLOR_3){
		COLOR = REPLACE_3;
	} else if (COLOR == COLOR_4){
		COLOR = REPLACE_4;
	}
}
:bust_in_silhouette: Reply From: klaas

Hi,
you cannot use uniforms to calculate something in the head section of the shader

const vec4 REPLACE_2 = REPLACE_1 - vec4(.25, .25, .25, 0);

This works with constants cause they get replaced before compiling and the statement in the head wouldnt change.

You have to calcualte the replacements in a function body.