| 1 | #version 300 es |
| 2 | // Pixelation Shader - OPTIMIZED |
| 3 | // Fixes: Sample from pixel CENTER (not corner), aspect ratio support |
| 4 | |
| 5 | precision highp float; |
| 6 | |
| 7 | in vec2 v_texcoord; |
| 8 | uniform sampler2D tex; |
| 9 | out vec4 fragColor; |
| 10 | |
| 11 | // --- CONFIGURATION --- |
| 12 | // Pixels across the shorter dimension (height usually) |
| 13 | const float PIXEL_COUNT = 350.0; |
| 14 | // Set to your aspect ratio, or 1.0 for square pixels |
| 15 | const float ASPECT_RATIO = 16.0 / 9.0; |
| 16 | |
| 17 | void main() { |
| 18 | // Calculate pixel dimensions accounting for aspect ratio |
| 19 | vec2 pixelCount = vec2(PIXEL_COUNT * ASPECT_RATIO, PIXEL_COUNT); |
| 20 | vec2 pixelSize = 1.0 / pixelCount; |
| 21 | |
| 22 | // FIXED: Sample from pixel CENTER, not corner |
| 23 | // This prevents the "swimming" artifact when content moves |
| 24 | vec2 pixelCoord = floor(v_texcoord * pixelCount) + 0.5; |
| 25 | vec2 sampleCoord = pixelCoord / pixelCount; |
| 26 | |
| 27 | fragColor = texture(tex, sampleCoord); |
| 28 | } |