Sharing a Palette-Based Color Replacement Shader

Hey everyone! I wanted to share a quick tool I’m using in my current project.

I’ve been working on a Palette-Based 2D Pixel Color Replacement Shader. It’s super useful for swapping character outfits or creating variations without redrawing sprites.

Feel free to grab the code and use it in your own projects—no strings attached!

Hope this saves someone some time!

shader_type canvas_item;

uniform sampler2D source_palette_ramp: filter_nearest;
uniform sampler2D target_palette_ramp: filter_nearest;
uniform float allowable_error: hint_range(0.0, 0.5, 0.01) = 0.1;

void fragment() {
	ivec2 ramp_size = textureSize(source_palette_ramp, 0);
	int palette_size = ramp_size.x;
	float palette_x_pixel_size = 1.0 / float(palette_size);
	vec3 current_color = texture(TEXTURE, UV).rgb;
	for (int i = 0; i < palette_size; i++) {
		float x_position = float(i) * palette_x_pixel_size;
		vec2 color_palette_position = vec2(x_position, 0.0);
		vec3 source_palette_color = texture(source_palette_ramp, color_palette_position).rgb;
		float color_distance = distance(current_color, source_palette_color);
		if (color_distance <= allowable_error) {
			vec3 target_palette_color = texture(target_palette_ramp, color_palette_position).rgb;
			COLOR.rgb = target_palette_color;
		}
	}
}

3 Likes

While I appreciate your sharing the cool things you are working on, this might be a better place to share your shader: https://godotshaders.com/

That plus I really like that site and do what I can to make people aware of it :slight_smile:

4 Likes

Also this should be in Resources, not Help.

1 Like

Yeah,this should be in Resource Category.

Amazing Place :grinning_face_with_smiling_eyes: