cirnogodot/Shaders/GodotRetro/Screen Shaders/Aditional Shaders/Dithering.gdshader
2025-12-28 22:53:31 +01:00

64 lines
No EOL
1.9 KiB
Text

//SHADER ORIGINALY CREADED BY "abelcamarena" FROM SHADERTOY
//MODIFIED AND PORTED TO GODOT BY AHOPNESS (@ahopness)
//LICENSE : CC0
//COMATIBLE WITH : GLES2, GLES3, WEBGL
//SHADERTOY LINK : https://www.shadertoy.com/view/tsKGDm
shader_type canvas_item;
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
uniform float SCREEN_WIDTH = 320.; // Lower num - bigger pixels (this will be the screen width)
uniform float COLOR_FACTOR :hint_range(0., 10.) = 4.; // Higher num - higher colors quality
uniform float DITHERING_STRENTH :hint_range(0., .07) = 0.005; // Be carefull with this one, dithering can get messy really easily
int PSXDither(ivec2 fragcoord) {
const int dither_table[16] = {
-4, +0, -3, +1,
+2, -2, +3, -1,
-3, +1, -4, +0,
+3, -1, +2, -2
};
const int dither_table2[16] = {
+0, +8, 2, +10,
+12, -4, +14, +6,
+3, +11, +1, +9,
+15, +7, +13, +5
};
const int dither_table_8[64] = {
0, 32, 8, 40, 2, 34, 10, 42,
48, 16, 56, 24, 50, 18, 58, 26,
12, 44, 4, 36, 14, 46, 6, 38,
60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41,
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21
};
int x = fragcoord.x % 4;
int y = fragcoord.y % 4;
return dither_table_8[y * 4 + x];
}
void fragment(){
// Reduce pixels
vec2 size = SCREEN_WIDTH * SCREEN_PIXEL_SIZE.xy/SCREEN_PIXEL_SIZE.x;
vec2 coor = floor( UV * size) ;
vec2 uv = FRAGCOORD.xy / (1.0 / SCREEN_PIXEL_SIZE).xy;
// Get source color
vec3 col = texture(SCREEN_TEXTURE, uv).xyz;
// Dithering
col += float(PSXDither(ivec2(FRAGCOORD.xy))) * DITHERING_STRENTH;
// Reduce colors
col = floor(col * COLOR_FACTOR) / COLOR_FACTOR;
// Output to screen
COLOR = vec4(col,1.);
}