Bayer matrix dithering in GLSL. Originally sourced from @oosmoxiecode's C64 Shader Demo, which in turn was based on the implementation in this article.
Each dithering function takes two arguments:
pos
: the position of the current pixel – you can usegl_FragCoord.xy
directly in most cases.brightness
: the current pixel's brightness, either as afloat
or avec3|vec4
color directly.
The returned value will be the same type as the brightness
parameter.
Dither using a 8x8 matrix.
Dither using a 4x4 matrix.
Dither using a 2x2 matrix.
precision mediump float;
uniform sampler2D uTexture;
varying vec2 vUv;
// Use any of the following:
#pragma glslify: dither = require(glsl-dither)
#pragma glslify: dither = require(glsl-dither/8x8)
#pragma glslify: dither = require(glsl-dither/4x4)
#pragma glslify: dither = require(glsl-dither/2x2)
void main() {
vec4 color = texture2D(uTexture, vUv);
gl_FragColor = dither(gl_FragCoord.xy, color);
}
MIT. See LICENSE.md for details.