2 Colour Palette Swap

Swaps 2 Colours

Shader code
shader_type canvas_item;

uniform vec4 pal_source_1 : source_color  = vec4(0.0);
uniform vec4 pal_source_2 : source_color  = vec4(0.0);

uniform vec4 pal_swap_1 : source_color  = vec4(0.0);
uniform vec4 pal_swap_2 : source_color  = vec4(0.0);

void fragment() {
	COLOR = texture(TEXTURE, UV);
	if (COLOR.a > 0.0)
	{
		if(distance(COLOR, pal_source_1) < 0.1)
		{
			COLOR = pal_swap_1;
		}

		if(distance(COLOR, pal_source_2) < 0.1)
		{
			COLOR = pal_swap_2;
		}
	}
}
Tags
Color, color swap, colour, colour swap, palette, palette swap, swap, switch
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Palette Swap using two textures

Palette Swap (no recolor / recolor)

Palette Swap: Post-Process, Image-Parametrized

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
GourabSN
GourabSN
9 months ago

For Godot 4, use this code.

shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture;

uniform vec4 pal_source_1 : source_color  = vec4(0.0);
uniform vec4 pal_source_2 : source_color  = vec4(0.0);

uniform vec4 pal_swap_1 : source_color  = vec4(0.0);
uniform vec4 pal_swap_2 : source_color  = vec4(0.0);

void fragment() {
    COLOR = texture(SCREEN_TEXTURE, SCREEN_UV);
    if (COLOR.a > 0.0)
    {
        if(distance(COLOR, pal_source_1) < 0.1)
        {
            COLOR = pal_swap_1;
        }

        if(distance(COLOR, pal_source_2) < 0.1)
        {
            COLOR = pal_swap_2;
        }
    }
}