2D Palette Swap (8 Colours, Tolerance, Smooth Blend)
Recolour sprites at runtime without duplicating textures: enemy variants, team colours, character skins, damage/poison tints or a retro palette change. Pick up to 8 colours in the sprite and swap each one for a new colour. Hard swap for crisp pixel art, or add smoothness for filtered / anti-aliased art. One canvas_item shader, no lookup texture needed.
USAGE:
Put a ShaderMaterial with this shader on a Sprite2D, AnimatedSprite2D, TextureRect or any CanvasItem. Set source_N to an exact colour from your art (use the colour picker’s eyedropper) and target_N to the new colour. Make the material “Local to Scene” (or duplicate it) when different instances need different palettes. From code:
var mat: ShaderMaterial = $Sprite2D.material
mat.set_shader_parameter(“target_0”, Color(“#3b82f6”))
Uniforms:
- source_0 … source_7 — colours to find in the sprite. Set a slot’s alpha to 0 to turn it off
- target_0 … target_7 — replacement colour for the matching source slot
- tolerance — how close (RGB distance) a pixel must be to a source colour to be swapped. Keep it low (0.02–0.08) for pixel art
- smoothness — soft falloff past the tolerance, for filtered or anti-aliased art (0 = hard swap)
- keep_shading — keeps the brightness difference between the pixel and the source colour, so one slot can recolour a whole shaded ramp
- mix_amount — 0 = original, 1 = fully swapped. Tween it for a quick palette flash or transition
Alpha and the node’s modulate are kept. Works in Godot 4.3+ (Compatibility, Forward+, Mobile).
AI-assisted (code + text)
Give your recoloured enemies some punch: free Combat SFX sampler: https://crtforge.pages.dev/combat-sfx-sampler/ and the full Fantasy RPG Combat SFX pack: https://assetsmithpixels.itch.io/fantasy-rpg-combat-sfx
Shader code
// 2D Palette Swap (up to 8 colours, tolerance, smooth blend) — Godot 4.3+
// MIT License — free for any use. AI-assisted (code + text).
shader_type canvas_item;
// Colours to look for in the sprite (set alpha to 0 to disable a slot).
uniform vec4 source_0 : source_color = vec4(1.0, 0.0, 0.0, 1.0);
uniform vec4 source_1 : source_color = vec4(0.0, 1.0, 0.0, 0.0);
uniform vec4 source_2 : source_color = vec4(0.0, 0.0, 1.0, 0.0);
uniform vec4 source_3 : source_color = vec4(1.0, 1.0, 0.0, 0.0);
uniform vec4 source_4 : source_color = vec4(1.0, 0.0, 1.0, 0.0);
uniform vec4 source_5 : source_color = vec4(0.0, 1.0, 1.0, 0.0);
uniform vec4 source_6 : source_color = vec4(1.0, 1.0, 1.0, 0.0);
uniform vec4 source_7 : source_color = vec4(0.0, 0.0, 0.0, 0.0);
// Colours to replace them with (same index).
uniform vec4 target_0 : source_color = vec4(1.0, 0.0, 0.0, 1.0);
uniform vec4 target_1 : source_color = vec4(0.0, 1.0, 0.0, 1.0);
uniform vec4 target_2 : source_color = vec4(0.0, 0.0, 1.0, 1.0);
uniform vec4 target_3 : source_color = vec4(1.0, 1.0, 0.0, 1.0);
uniform vec4 target_4 : source_color = vec4(1.0, 0.0, 1.0, 1.0);
uniform vec4 target_5 : source_color = vec4(0.0, 1.0, 1.0, 1.0);
uniform vec4 target_6 : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 target_7 : source_color = vec4(0.0, 0.0, 0.0, 1.0);
// How close (RGB distance) a pixel must be to a source colour to be swapped.
uniform float tolerance : hint_range(0.0, 1.0, 0.005) = 0.06;
// Soft edge past the tolerance, for anti-aliased / filtered art (0 = hard pixel-art swap).
uniform float smoothness : hint_range(0.0, 0.5, 0.005) = 0.0;
// Keep the original pixel's brightness variation (shading) on the new colour.
uniform bool keep_shading = false;
// Blend between original (0) and swapped (1). Tween it for flashes / transitions.
uniform float mix_amount : hint_range(0.0, 1.0) = 1.0;
float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
vec3 try_swap(vec3 col, vec4 src, vec4 dst, inout float best) {
if (src.a <= 0.0) return col;
float d = distance(col, src.rgb);
float w = 1.0 - smoothstep(tolerance, tolerance + smoothness + 0.0001, d);
if (w <= best) return col;
best = w;
vec3 rep = dst.rgb;
if (keep_shading) rep = clamp(rep + (luma(col) - luma(src.rgb)), 0.0, 1.0);
return rep;
}
void fragment() {
vec4 tex = COLOR; // in Godot 4, COLOR already = texture * modulate
vec3 orig = tex.rgb;
float best = 0.0;
vec3 rep = orig;
vec3 r;
r = try_swap(orig, source_0, target_0, best); if (best > 0.0) rep = r;
float b0 = best;
r = try_swap(orig, source_1, target_1, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_2, target_2, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_3, target_3, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_4, target_4, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_5, target_5, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_6, target_6, best); if (best > b0) { rep = r; b0 = best; }
r = try_swap(orig, source_7, target_7, best); if (best > b0) { rep = r; b0 = best; }
COLOR = vec4(mix(orig, mix(orig, rep, best), mix_amount), tex.a);
}

Comments 0
No comments yet.