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 https://godotshaders.com/shader/palette-swap-no-recolor-recolor/
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);
}
beware: don’t set “num_colors” to 0… the zero div will crash godot.
Did you get this working for you? It doesnt work for me in GODOT 4
I know this should be a simple thing to use but for some reason the colors do not swap for me in GODOT 4
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.