| 1 | #version 300 es |
| 2 | precision highp float; // CRITICAL: Prevents many artifacts |
| 3 | |
| 4 | in vec2 v_texcoord; |
| 5 | uniform sampler2D tex; |
| 6 | out vec4 fragColor; |
| 7 | |
| 8 | // --- CONFIGURATION --- |
| 9 | const float dot_spacing = 4.0; // 3.0-6.0 recommended |
| 10 | const int color_levels = 4; // Posterization levels |
| 11 | const vec3 paper_color = vec3(0.95, 0.92, 0.85); |
| 12 | const float paper_texture_strength = 0.04; |
| 13 | const float dot_softness = 1.5; // Higher = softer dot edges (anti-aliasing) |
| 14 | const float ink_darkness = 0.95; // How dark the ink appears (0.0-1.0) |
| 15 | const bool use_dithering = true; // Reduces posterization banding |
| 16 | // --------------------- |
| 17 | |
| 18 | // Improved hash function - less pattern artifacts than sin-based |
| 19 | float hash12(vec2 p) { |
| 20 | vec3 p3 = fract(vec3(p.xyx) * 0.1031); |
| 21 | p3 += dot(p3, p3.yzx + 33.33); |
| 22 | return fract((p3.x + p3.y) * p3.z); |
| 23 | } |
| 24 | |
| 25 | // Smoother 2D noise for paper texture |
| 26 | float valueNoise(vec2 p) { |
| 27 | vec2 i = floor(p); |
| 28 | vec2 f = fract(p); |
| 29 | |
| 30 | // Smooth interpolation |
| 31 | vec2 u = f * f * (3.0 - 2.0 * f); |
| 32 | |
| 33 | float a = hash12(i); |
| 34 | float b = hash12(i + vec2(1.0, 0.0)); |
| 35 | float c = hash12(i + vec2(0.0, 1.0)); |
| 36 | float d = hash12(i + vec2(1.0, 1.0)); |
| 37 | |
| 38 | return mix(mix(a, b, u.x), mix(c, d, u.x), u.y); |
| 39 | } |
| 40 | |
| 41 | float luminance(vec3 color) { |
| 42 | return dot(color, vec3(0.2126, 0.7152, 0.0722)); |
| 43 | } |
| 44 | |
| 45 | // Bayer 4x4 dithering matrix - reduces posterization banding |
| 46 | float bayerDither(vec2 pos) { |
| 47 | ivec2 p = ivec2(mod(pos, 4.0)); |
| 48 | int index = p.x + p.y * 4; |
| 49 | |
| 50 | // Bayer matrix values |
| 51 | float matrix[16] = float[16]( |
| 52 | 0.0, 8.0, 2.0, 10.0, |
| 53 | 12.0, 4.0, 14.0, 6.0, |
| 54 | 3.0, 11.0, 1.0, 9.0, |
| 55 | 15.0, 7.0, 13.0, 5.0 |
| 56 | ); |
| 57 | |
| 58 | return (matrix[index] / 16.0) - 0.5; |
| 59 | } |
| 60 | |
| 61 | void main() { |
| 62 | vec2 screen_res = vec2(textureSize(tex, 0)); |
| 63 | vec2 pixel_coords = v_texcoord * screen_res; |
| 64 | |
| 65 | // 1. Sample original with clamped coordinates (prevents edge artifacts) |
| 66 | vec2 safe_uv = clamp(v_texcoord, 0.0, 1.0); |
| 67 | vec3 original_color = texture(tex, safe_uv).rgb; |
| 68 | |
| 69 | // 2. Posterize with optional dithering to reduce banding |
| 70 | float levels = float(color_levels); |
| 71 | vec3 posterized_color; |
| 72 | |
| 73 | if (use_dithering) { |
| 74 | // Add dither before quantization |
| 75 | float dither = bayerDither(pixel_coords) / (levels * 2.0); |
| 76 | posterized_color = floor((original_color + dither) * levels) / (levels - 1.0); |
| 77 | } else { |
| 78 | posterized_color = floor(original_color * levels) / (levels - 1.0); |
| 79 | } |
| 80 | posterized_color = clamp(posterized_color, 0.0, 1.0); |
| 81 | |
| 82 | // 3. Create layered paper texture (more natural looking) |
| 83 | float paper_noise = 0.0; |
| 84 | paper_noise += valueNoise(pixel_coords * 0.5) * 0.6; // Large grain |
| 85 | paper_noise += valueNoise(pixel_coords * 1.5) * 0.3; // Medium grain |
| 86 | paper_noise += hash12(pixel_coords) * 0.1; // Fine grain |
| 87 | paper_noise *= paper_texture_strength; |
| 88 | |
| 89 | vec3 textured_paper = max(paper_color - paper_noise, 0.0); // Prevent negative |
| 90 | |
| 91 | // 4. Create halftone grid |
| 92 | vec2 cell_coords = pixel_coords / dot_spacing; |
| 93 | vec2 grid_uv = fract(cell_coords); |
| 94 | |
| 95 | // 5. Calculate halftone dot |
| 96 | // Use sqrt for area-proportional dots (perceptually correct) |
| 97 | float lum = luminance(posterized_color); |
| 98 | float darkness = 1.0 - lum; |
| 99 | float dot_radius = sqrt(darkness) * 0.5; // Area proportional to darkness |
| 100 | |
| 101 | // Distance from cell center |
| 102 | float dist = length(grid_uv - 0.5); |
| 103 | |
| 104 | // Anti-aliased dot edge using screen-space derivatives |
| 105 | // This adapts to any resolution automatically |
| 106 | float pixel_width = fwidth(dist) * dot_softness; |
| 107 | float dot_mask = smoothstep(dot_radius - pixel_width, dot_radius + pixel_width, dist); |
| 108 | |
| 109 | // 6. Combine: ink color where dots are, paper elsewhere |
| 110 | vec3 ink_color = posterized_color * ink_darkness; |
| 111 | vec3 final_color = mix(ink_color, textured_paper, dot_mask); |
| 112 | |
| 113 | fragColor = vec4(final_color, 1.0); |
| 114 | } |