Palette Swap using two textures

Shader to change specific colors for another specific color using a texture map instead of multiple variables.

Heavily based on

Shader code
shader_type canvas_item;

uniform bool screen_mode = true;
uniform int num_colors;
uniform float precision : hint_range(0.0, 1.0, 0.1) = 0.1;

uniform sampler2D palette_in;
uniform sampler2D palette_out;

vec4 swap_color(vec4 color) {
	float inc = 1.0 / float(num_colors); 
	for (float i = 0.0; i < 1.0; i += inc) {
		vec2 uv = vec2(i, 0.0);
		vec4 color_in = texture(palette_in, uv);
		if (distance(color, color_in) <= precision) {
			return texture(palette_out, uv);
	return color;

void fragment() {
	vec4 color = texture(TEXTURE, UV);
	if (screen_mode) {
		color = texture(SCREEN_TEXTURE, SCREEN_UV);
	COLOR = swap_color(color);
Color, palette, pixelart
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.

More from afk

Tilemap Wind shader

Circle progress mirror

Sokpop Skybox

Related shaders

2 Colour Palette Swap

Palette Swap (no recolor / recolor)

Combine two Shaders

Notify of

Newest Most Voted
Inline Feedbacks
View all comments
1 year ago

beware: don’t set “num_colors” to 0… the zero div will crash godot.

8 months ago
Reply to  ColorauGuiyino

Did you get this working for you? It doesnt work for me in GODOT 4

8 months ago

I know this should be a simple thing to use but for some reason the colors do not swap for me in GODOT 4

2 months ago

in 4.0 just replace line 14 with vec2 uv = vec2(i + (inc/2.0), 0.0);’
the problem is its sampling the texture right at the edge of the color split. this moves it to the center.