Skip to content

Instantly share code, notes, and snippets.

@kevinthompson
Last active June 23, 2025 23:03
Show Gist options
  • Save kevinthompson/2b91a16c38734163f34e698e7808c366 to your computer and use it in GitHub Desktop.
Save kevinthompson/2b91a16c38734163f34e698e7808c366 to your computer and use it in GitHub Desktop.
Godot Palette Shader

A simple shader that takes a 1px tall png as a palette uniform (as you might download from lospec.com) and enforces that palette in screen space when applied to a ColorRect.

shader_type canvas_item;
uniform sampler2D palette : hint_default_transparent;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
void fragment(){
int palette_size = textureSize(palette, 0).x;
vec4 color = texture(screen_texture, SCREEN_UV);
vec4 new_color = vec4(.0);
for (int i = 0; i < palette_size; i++) {
float color_uv_width = 1.0 / float(palette_size);
float palette_uv_x = color_uv_width * (float(i) + 0.5);
float palette_uv_y = 0.5;
vec4 palette_color = texture(palette, vec2(palette_uv_x, palette_uv_y));
if (distance(palette_color, color) < distance(new_color, color)) {
new_color = palette_color;
}
}
COLOR = new_color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment