Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active July 7, 2025 13:52
Show Gist options
  • Save partybusiness/62b245d033e789541107678b2ecc5984 to your computer and use it in GitHub Desktop.
Save partybusiness/62b245d033e789541107678b2ecc5984 to your computer and use it in GitHub Desktop.
Godot shader that will let you replace colours (usually full-saturation RGB, Yellow, Cyan, Magenta) with new colours based on a texture.
shader_type canvas_item;
// expects this as 4x2
// black, red, blue, magenta
// green, yellow, cyan, white
// set alpha to 0 for sections you don't want replaced
// usually black pixel is alpha 0 because most colours are below threshold
uniform sampler2D colours:source_color;
// threshold for what counts as full saturation
// usually keep it pretty high so most colours can be treated as black
uniform float threshold = 0.99;
void fragment() {
vec3 o = COLOR.rgb;
ivec2 coord = ivec2(vec2(step(threshold, o.r) + step(threshold, o.b) * 2.0, step(threshold, o.g)));
vec4 swap = texelFetch(colours, coord, 0);
COLOR.rgb = mix(COLOR.rgb, swap.rgb, swap.a);
}
shader_type spatial;
uniform sampler2D main_texture:source_color, filter_nearest;
// expects this as 4x2
// black, red, blue, magenta
// green, yellow, cyan, white
// set alpha to 0 for sections you don't want replaced
// usually black pixel is alpha 0 because most colours are below threshold
uniform sampler2D colours:source_color;
// threshold for what counts as full saturation
// usually keep it pretty high so most colours can be treated as black
uniform float threshold = 0.99;
void fragment() {
vec3 o = texture(main_texture, UV).rgb;
ivec2 coord = ivec2(vec2(step(threshold, o.r) + step(threshold, o.b) * 2.0, step(threshold, o.g)));
vec4 swap = texelFetch(colours, coord, 0);
ALBEDO.rgb = mix(o.rgb, swap.rgb, swap.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment