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 cell UV

Joepli wobble

Tilemap Wind shader

Related shaders

Palette Swap (no recolor / recolor)

Texture population using Texture

Color Range Swap [UPDATED]


1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
2 months ago

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