How to change a specific color using shaders

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rustyStriker
:warning: Old Version Published before Godot 3 was released.

Hello,
I’ve a map for my game and i want a specific color to flick from one color to another to give the game a little life, is there a way to do it in shaders?

You may want to search for a ChromaKey shader

timoschwarzer | 2017-06-15 06:18

:bust_in_silhouette: Reply From: Omicron

Check : http://docs.godotengine.org/en/stable/learning/features/shading/shading_language.html#examples

Make a fragmentShader, then all you need is to test color value, then change it how you like (or return it unchanged).

The link is broken…

Lightning_A | 2020-12-09 13:37

This is the new URL: Shading language — Godot Engine (latest) documentation in English

Calinou | 2020-12-09 13:37

:bust_in_silhouette: Reply From: eons

At least on Godot 2, you can work over COLOR in fragment (on 3 I think is only output).
Using operations to modify the color (some square wave maybe) can be used for flicker effect.

Look at the demos and docs for better reference on the language and how to affect outputs.

:bust_in_silhouette: Reply From: jarlowrey

This should change all opaque black colors to opaque red in Godot 3.0.2

shader_type canvas_item;

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

	if (curr_color == vec4(0,0,0,1)){
    	COLOR = vec4(1,0,0,1);
	}else{
		COLOR = curr_color;
	}
}

thanks, you really saved me.

anllucah | 2019-12-17 14:33

When using a uniform to set the colors, I was getting float error. Using this approx_equal function worked, but there may be a better way:
bool approx_eq(vec4 c1, vec4 c2) { return all( lessThan( abs( c1 - c2 ), vec4( 0.001, 0.001, 0.001, 0.001) ) ); }

MonsterVial | 2020-09-18 12:41