CGA 4-Color Shader
CGA 4 color post processing shader. I made this for use in the 2024 CGA Jam.
Shader code
shader_type spatial;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
uniform float offset;
uniform vec2 screenscale;
render_mode unshaded;
vec3 palettize(vec3 col) {
vec3 palette[4] = { vec3(1.0, 1.0, 1.0), vec3(0.0, 1.0, 1.0), vec3(1.0, 0.0, 1.0), vec3(0,0,0) };
int closest = 0;
float dist = 100000.0f;
for (int i = 0; i < 4; i++) {
float temp = (palette[i].r - col.r) * (palette[i].r - col.r) + (palette[i].g - col.g) * (palette[i].g - col.g) + (palette[i].b - col.b) * (palette[i].b - col.b);
if (temp < dist) {
dist = temp;
closest = i;
}
}
return palette[closest];
}
void vertex() {
// Called for every vertex the material is visible on.
}
void fragment() {
float bayer_matrix[16] = float[16] (
-0.5, 0.0, -0.375, 0.125,
0.25, -0.25, 0.375, -0.125,
-0.3125, 0.1875, -0.4375, 0.0625,
0.4375, -0.0625, 0.3125, -0.1875
);
vec2 uv = round(SCREEN_UV * screenscale);
int uvx = int(uv.x) % 4;
int uvy = int(uv.y) % 4;
uv /= screenscale;
vec3 col = texture(screen_texture, uv).rgb;
float dither = bayer_matrix[uvy * 4 + uvx];
vec3 value = vec3(0,0,0);
col *= offset;
value = palettize(col+dither*0.3);
ALBEDO = value;
}
//void light() {
// Called for every pixel for every light affecting the CanvasItem.
// Uncomment to replace the default light processing function with this one.
//}