Ordered dithering with Bayer matrices
Quantizes each pixel against a threshold taken from a tiled Bayer matrix, producing a crisp, regular dither pattern at very low cost.
Also known as Bayer dithering
In the family Stylized & non-photorealistic rendering, dithering
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Official add-on | import { bayerDither } from 'three/addons/tsl/math/Bayer.js' docs |
| Babylon.js 9.38.0 | None found | ImageProcessingConfiguration.ditheringEnabled applies pseudo-random screen-space dithering rather than a tiled Bayer matrix. |
| Godot 4.7 | None found | Viewport.use_debanding applies Valve interleaved RGB screen-space dither (tonemap.glsl), not a tiled Bayer matrix. |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
A Bayer matrix of size 2^n is a recursively built grid of threshold ranks (for 4×4: values 0 to 15). For each pixel, look up the threshold at (x mod n, y mod n), normalise it to (0,1), add it to the scaled input luminance, and floor to the target number of levels. For a 1-bit result this is simply lum > threshold. The recursion guarantees that the dots turn on in a spread-out, balanced order as brightness rises, so the pattern shows as the familiar crosshatch-grid texture. Per-channel dithering with limited palettes works the same way. Applying it at a lower virtual resolution than the screen, with nearest-neighbour upscaling, gives the retro look.
When to choose it
Reach for it when
- need retro dithered look with cheap per-pixel cost
- banding when reducing colour depth
- dither pattern must not shimmer between frames
- limited palette output with stable pattern
- 1-bit or low-colour image conversion in a shader
Choose it when you want a deterministic, stable, retro pattern that does not shimmer with camera motion and is trivially cheap. Prefer Blue-noise dithering for less patterned, more natural-looking results, and Error-diffusion dithering (Floyd-Steinberg) for best static quality where temporal stability does not matter.
Field marks
- Visible regular crosshatch pattern at low level counts
- Pattern is screen-fixed, so it appears to slide over moving 3D objects (shower door)
- Banding on smooth gradients at very small matrix sizes
- Colour dithering with unequal per-channel thresholds shows hue shifts
Presets
4x4 Bayer 1-bit dither of a lit sphere
{
"levels": 2,
"cell_px": 3
}Show GLSL (32 lines)
// GLSL ES 3.0, Shadertoy-style: 4x4 Bayer ordered dithering of a procedural gradient
const float LEVELS = 2.0; // output levels per channel (2 = pure 1-bit look)
const float PIXEL = 3.0; // size of one dither cell in screen pixels
const vec3 INK = vec3(0.07, 0.06, 0.12);
const vec3 PAPER = vec3(0.93, 0.90, 0.78);
const int BAYER4[16] = int[16](0,8,2,10, 12,4,14,6, 3,11,1,9, 15,7,13,5);
float bayer4(ivec2 p){
return (float(BAYER4[(p.y & 3)*4 + (p.x & 3)]) + 0.5) / 16.0;
}
float scene(vec2 uv){
vec2 c = uv - vec2(0.0, 0.02);
float r = length(c);
float sphere = 0.0;
if(r < 0.32){
vec3 n = vec3(c, sqrt(0.32*0.32 - r*r)) / 0.32;
vec3 l = normalize(vec3(-0.5 + 0.2*sin(iTime), 0.6, 0.7));
sphere = clamp(dot(n, l), 0.0, 1.0);
}
float bg = 0.15 + 0.5*smoothstep(-0.5, 0.5, uv.x + 0.4*uv.y);
return mix(bg, 0.05 + 0.95*sphere, step(r, 0.32));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord){
vec2 uv = (fragCoord - 0.5*iResolution.xy) / iResolution.y;
ivec2 cell = ivec2(floor(fragCoord / PIXEL));
vec2 uvq = (vec2(cell)*PIXEL + 0.5*PIXEL - 0.5*iResolution.xy) / iResolution.y;
float lum = scene(uvq);
float th = bayer4(cell);
float q = floor(lum*(LEVELS-1.0) + th) / (LEVELS-1.0);
fragColor = vec4(mix(INK, PAPER, clamp(q, 0.0, 1.0)), 1.0);
}
Attribution: written for this index
8×8 Bayer ordered dithering (WGSL)
{
"LEVELS": 3,
"CELL_SIZE": 3
}Show WGSL (35 lines)
// WebGPU WGSL fragment specimen: recursive 8x8 Bayer threshold matrix
const LEVELS: f32 = 3.0000; // P
const CELL_SIZE: f32 = 3.0000; // P
fn bayer2(a: vec2f) -> f32 {
let f = floor(a);
return fract(dot(f, vec2f(0.5, f.y * 0.75)));
}
fn bayer8(p: vec2f) -> f32 {
let f = floor(p) - 8.0 * floor(floor(p) / 8.0);
let b2 = bayer2(f);
let b4 = bayer2(floor(f * 0.5));
let b8 = bayer2(floor(f * 0.25));
return (b8 * 0.25 + b4) * 0.25 + b2;
}
@fragment
fn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f {
let cell = max(1.0, floor(CELL_SIZE + 0.5));
let p = floor(pos.xy / cell) * cell + 0.5 * cell;
let uv = (p - 0.5 * u.resolution) / u.resolution.y;
let d = length(uv);
var src = mix(vec3f(0.95, 0.94, 0.90), vec3f(0.14, 0.17, 0.22), clamp(uv.x + 0.55, 0.0, 1.0));
if (d < 0.36) {
let n = normalize(vec3f(uv.x, -uv.y, sqrt(max(0.0, 0.36 * 0.36 - d * d))));
let l = normalize(vec3f(cos(u.time * 0.5) * 0.7, 0.65, 0.6));
let shade = max(dot(n, l), 0.0) * 0.85 + 0.12;
src = vec3f(shade * 0.96, shade * 0.86, shade * 0.72);
}
let thr = bayer8(pos.xy / cell) - 0.5;
let lv = max(1.0, floor(LEVELS + 0.5));
let q = clamp(floor(src * lv + 0.5 + thr) / lv, vec3f(0.0), vec3f(1.0));
return vec4f(q, 1.0);
}Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| bayer-4x4-1bit | Tesla T4 1920x1080 | 1.32× baseline (feather) · 0.043 ms / 0.044 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| bayer-4x4-1bit | NVIDIA L4 1920x1080 | 1.03× baseline (feather) · 0.016 ms / 0.017 ms · 2.7× faster on L4 | opengl-timer-query · Cross-architecture validation |
| bayer-4x4-1bit | NVIDIA A100-SXM4-40GB 1920x1080 | 1.00× baseline (feather) · 0.029 ms / 0.029 ms · 1.5× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
Crosses
- OKLab palette quantization: Bayer threshold applied before nearest-palette lookup in a perceptual space gives clean palette dithering.