Skip to content

Instantly share code, notes, and snippets.

@cenullum
Last active December 4, 2025 21:03
Show Gist options
  • Select an option

  • Save cenullum/eecb020390d17d4a1b8669f6eea170dc to your computer and use it in GitHub Desktop.

Select an option

Save cenullum/eecb020390d17d4a1b8669f6eea170dc to your computer and use it in GitHub Desktop.
godot engine 4.4.1 1bit dithering shader
shader_type canvas_item;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
uniform vec3 color_A : source_color = vec3(1.0, 1.0, 1.0);
uniform vec3 color_B : source_color = vec3(0.0, 0.0, 0.0);
uniform float threshold : hint_range(0.0, 2.0, 0.01) = 1.0;
uniform bool use_dithering = true;
uniform float contrast : hint_range(0.0, 4.0, 0.01) = 1.0;
uniform float brightness : hint_range(-1.0, 1.0, 0.01) = 0.0;
// 4x4 Bayer matrix
const mat4 BAYER_MATRIX = mat4(
vec4(1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0),
vec4(13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0),
vec4(4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0),
vec4(16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0)
);
float get_luminance(vec3 color) {
return dot(color, vec3(0.2126, 0.7152, 0.0722));
}
void fragment() {
vec3 screen_color = texture(screen_texture, SCREEN_UV).rgb;
screen_color = (screen_color - 0.5) * contrast + 0.5;
screen_color += brightness;
screen_color = clamp(screen_color, 0.0, 1.0);
float luma = get_luminance(screen_color);
vec3 result;
if (use_dithering) {
// BAYER DITHERING
ivec2 pixel_pos = ivec2(FRAGCOORD.xy);
float bayer_value = BAYER_MATRIX[pixel_pos.x % 4][pixel_pos.y % 4];
if (luma > bayer_value * threshold) {
result = color_A;
} else {
result = color_B;
}
} else {
if (luma > threshold) {
result = color_A;
} else {
result = color_B;
}
}
COLOR.rgb = result;
COLOR.a = 1.0;
}
@cenullum
Copy link
Author

cenullum commented Dec 4, 2025

Screenshot from 2025-12-04 23-46-32

recording_20251204_233535

Screenshot from 2025-12-04 23-48-47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment