summaryrefslogtreecommitdiffstats
path: root/mat_palette.gdshader
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-11-26 18:41:11 +0200
committerJan Tuomi <jan@jantuomi.fi>2025-11-26 18:41:11 +0200
commit194f6e369c9f31df6e6ab4b46c7423d031f26264 (patch)
treea6d748b1b8f48ae35d8c8f4e0edb552075d192ca /mat_palette.gdshader
parenta991d59e784cc0a44a63be5e8f8e98fe27b05224 (diff)
Improve shader
Diffstat (limited to 'mat_palette.gdshader')
-rw-r--r--mat_palette.gdshader64
1 files changed, 20 insertions, 44 deletions
diff --git a/mat_palette.gdshader b/mat_palette.gdshader
index e24a6ee..03c664a 100644
--- a/mat_palette.gdshader
+++ b/mat_palette.gdshader
@@ -1,54 +1,30 @@
shader_type canvas_item;
-uniform vec4 palette_0 : source_color;
-uniform vec4 palette_1 : source_color;
-uniform vec4 palette_2 : source_color;
-uniform vec4 palette_3 : source_color;
-uniform vec4 palette_4 : source_color;
-uniform vec4 palette_5 : source_color;
-uniform vec4 palette_6 : source_color;
-uniform vec4 palette_7 : source_color;
-uniform vec4 palette_8 : source_color;
-uniform vec4 palette_9 : source_color;
-uniform vec4 palette_10 : source_color;
-uniform vec4 palette_11 : source_color;
+uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
-const int N_COLORS = 12;
-
-uniform sampler2D screen_texture : hint_screen_texture;
+uniform float brightness : hint_range(-1, 1) = 0;
+uniform sampler2D source_palette;
+uniform sampler2D target_palette;
void fragment() {
- vec4 palette[N_COLORS] = { palette_0, palette_1, palette_2, palette_3, palette_4, palette_5, palette_6, palette_7, palette_8, palette_9, palette_10, palette_11 };
vec4 screen_color = texture(screen_texture, SCREEN_UV);
- // Compute source color value
- float sv = 0.f;
- for (int i = 0; i < 3; i++) {
- if (screen_color[i] > sv) { sv = screen_color[i]; }
- }
+ ivec2 pal_size = textureSize(source_palette, 0);
- int pal_i = int(floor((sv * 0.99f) * float(N_COLORS)));
- vec4 result = palette[pal_i];
- /*
- float min_dist = 999.f; // always bigger than 1.0
- vec4 result;
- for (int i = 0; i < N_COLORS; i++) {
- vec4 pc = palette[i];
- // Compute palette color value
- float pv = 0.f;
- for (int j = 0; j < 3; j++) {
- if (pc[j] > pv) { pv = pc[j]; }
- }
- float dist = abs(pv - sv);
- if (dist < min_dist) {
- min_dist = dist;
- result = pc;
+ float color_diff = 1.0;
+ int nearest_swatch = 0;
+
+ for (int swatch=0; swatch < pal_size.x; swatch++) {
+ vec4 sampled_color = texelFetch(source_palette, ivec2(swatch,0), 0);
+ float new_color_diff = distance(sampled_color, screen_color);
+
+ if (new_color_diff < color_diff) {
+ color_diff = new_color_diff;
+ nearest_swatch = swatch;
}
}
- */
- COLOR = vec4(result.rgb, COLOR.a);
+
+ int swatch_offset = int(brightness * float(pal_size.x));
+ int target_swatch = clamp(nearest_swatch + swatch_offset, 0, pal_size.x -1);
+
+ COLOR = texelFetch(target_palette, ivec2(target_swatch, 0), 0);
}
-
-//void light() {
-// // Called for every pixel for every light affecting the CanvasItem.
-// // Uncomment to replace the default light processing function with this one.
-//}