Skip to content

Instantly share code, notes, and snippets.

@kevinthompson
Created April 3, 2025 21:01
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;
// Insert a palette from lospec for instance
uniform sampler2D palette : hint_black;
uniform int palette_size = 16;
void fragment(){
vec4 color = texture(SCREEN_TEXTURE, SCREEN_UV);
vec4 new_color = vec4(.0);
for (int i = 0; i < palette_size; i++) {
vec4 palette_color = texture(palette, vec2(1.0 / float(palette_size) * (float(i) + 0.5), .0));
if (distance(palette_color, color) < distance(new_color, color)) {
new_color = palette_color;
}
}
COLOR = new_color;
COLOR.a = color.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment