Error-diffusion dithering (Floyd-Steinberg)
Quantizes pixels one at a time and spreads each pixel's rounding error to unvisited neighbours, giving the best tonal accuracy among simple dithers.
In the family Stylized & non-photorealistic rendering, dithering
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | No built-in Floyd-Steinberg error-diffusion dithering post-process. |
| Unity | None found | |
| Unreal 5.7 | None found | No built-in Floyd-Steinberg error-diffusion dithering post-process. |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Pixels are scanned in raster order. For each pixel, the colour plus accumulated error is rounded to the nearest palette entry, and the difference between wanted and output is distributed to neighbours not yet visited: for Floyd-Steinberg, 7/16 to the right, 3/16 below-left, 5/16 below and 1/16 below-right. Serpentine scanning reverses direction every row to reduce directional artefacts. Alternative kernels (Jarvis-Judice-Ninke, Stucki, Atkinson) trade smoothness against sharpness. The sequential dependence makes it hard to parallelise, so on GPUs it needs wavefront or tile tricks, and the pattern changes across the whole image whenever a single pixel changes.
When to choose it
Reach for it when
- banding when reducing colors to a small palette
- retro limited palette image looks posterized
- convert to 1-bit image that keeps tones
- smooth gradients on low bit depth displays
Choose it for offline conversion, texture baking, and stills where detail fidelity matters. Prefer Blue-noise dithering or Ordered dithering with Bayer matrices for real-time 3D, because error diffusion crawls and shimmers with any change in the image.
Field marks
- Pattern changes everywhere when the source changes, so animated output flickers badly
- Worm-like directional artefacts in near-flat regions
- Raster-order dependence blocks straightforward GPU parallelism
- Edge bleed of error across hard boundaries
Presets
Error-diffusion dithering — split continuous tone vs Floyd-Steinberg residual stipple
{
"P_LEVELS": 2,
"P_DIFFUSION": 1,
"P_PIXEL_SIZE": 2.5,
"P_SPLIT": 0.36
}Show GLSL (124 lines)
const float P_LEVELS = 2.0; // P Quant Levels [2.0, 6.0, 1.0]
const float P_DIFFUSION = 1.0; // P Error Diffusion [0.0, 1.4, 0.05]
const float P_PIXEL_SIZE = 2.5; // P Pixel Size [1.0, 6.0, 0.25]
const float P_SPLIT = 0.36; // P Split Position [0.0, 1.0, 0.02]
mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float sdTorus(vec3 p, vec2 t) {
vec2 q = vec2(length(p.xz) - t.x, p.y);
return length(q) - t.y;
}
float mapObj(vec3 p) {
vec3 q = p;
q.xz = rot2(iTime * 0.42) * q.xz;
q.xy = rot2(0.45 + 0.15 * sin(iTime * 0.6)) * q.xy;
float d1 = sdTorus(q, vec2(0.62, 0.22));
float d2 = length(p - vec3(0.0, 0.0, 0.0)) - 0.34;
float dFloor = p.y + 0.88;
return min(min(d1, d2), dFloor);
}
vec3 calcN(vec3 p) {
vec2 e = vec2(0.002, 0.0);
return normalize(vec3(
mapObj(p + e.xyy) - mapObj(p - e.xyy),
mapObj(p + e.yxy) - mapObj(p - e.yxy),
mapObj(p + e.yyx) - mapObj(p - e.yyx)
));
}
// Continuous luminance of a 3D classical still-life scene at normalized cell coord
float sampleTone(vec2 uv) {
vec3 ro = vec3(0.0, 0.35, 2.55);
vec3 rd = normalize(vec3(uv, -1.55));
float t = 0.0;
bool hit = false;
for (int i = 0; i < 42; i++) {
float d = mapObj(ro + rd * t);
if (d < 0.0025) { hit = true; break; }
t += d;
if (t > 8.0) break;
}
if (!hit) {
// Smooth studio gradient backdrop to showcase gradient dithering quality
return clamp(0.14 + 0.56 * smoothstep(-0.55, 0.55, uv.y) + 0.16 * exp(-length(uv - vec2(0.28, 0.26)) * 2.2), 0.0, 1.0);
}
vec3 p = ro + rd * t;
vec3 n = calcN(p);
vec3 l = normalize(vec3(0.65, 0.78, 0.55));
vec3 l2 = normalize(vec3(-0.7, 0.25, -0.4));
float diff = max(dot(n, l), 0.0);
float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.45;
vec3 h = normalize(l - rd);
float spec = pow(max(dot(n, h), 0.0), 32.0) * 0.55;
float back = max(dot(n, l2), 0.0) * 0.22;
float ao = clamp(0.55 + 0.45 * smoothstep(-0.88, -0.3, p.y), 0.0, 1.0);
return clamp((0.08 + 0.72 * diff + back + rim + spec) * ao, 0.0, 1.0);
}
// Local finite-horizon Floyd-Steinberg (7/16, 3/16, 5/16, 1/16) error-diffusion solver
// Runs a 5-column x 3-row causal error carry window ending at cell c
float floydSteinbergLocal(vec2 c, float pxScale, float levels, float diffWeight) {
float errRow[5];
for (int k = 0; k < 5; k++) errRow[k] = 0.0;
float stepVal = 1.0 / max(levels - 1.0, 1.0);
float outQ = 0.0;
for (int ry = -2; ry <= 0; ry++) {
float nextRow[5];
for (int k = 0; k < 5; k++) nextRow[k] = 0.0;
float carryRight = 0.0;
// Slight blue-like phase seed on top boundary to break periodic lock
for (int rx = 0; rx < 5; rx++) {
vec2 sc = c + vec2(float(rx - 4), float(ry));
vec2 suv = (sc * pxScale - 0.5 * iResolution.xy) / iResolution.y;
float val = sampleTone(suv);
// Add causal errors diffused from left (7/16) and previous row (3/16, 5/16, 1/16)
float accum = val + diffWeight * (carryRight + errRow[rx]);
float q = floor(accum / stepVal + 0.5) * stepVal;
q = clamp(q, 0.0, 1.0);
float err = accum - q;
carryRight = err * (7.0 / 16.0);
if (rx > 0) nextRow[rx - 1] += err * (3.0 / 16.0);
nextRow[rx] += err * (5.0 / 16.0);
if (rx < 4) nextRow[rx + 1] += err * (1.0 / 16.0);
if (ry == 0 && rx == 4) outQ = q;
}
for (int k = 0; k < 5; k++) errRow[k] = nextRow[k];
}
return outQ;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 screenUV = fragCoord / iResolution.xy;
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float pxSize = clamp(P_PIXEL_SIZE, 1.0, 8.0);
float levels = floor(clamp(P_LEVELS, 2.0, 8.0) + 0.5);
float diffW = clamp(P_DIFFUSION, 0.0, 1.5);
float splitX = clamp(P_SPLIT, 0.0, 1.0);
vec3 inkDark = vec3(0.055, 0.06, 0.085);
vec3 paperLight = vec3(0.95, 0.93, 0.87);
vec3 col;
if (screenUV.x < splitX) {
float tone = sampleTone(uv);
col = mix(inkDark, paperLight, tone);
} else {
vec2 cell = floor(fragCoord / pxSize) + 0.5;
float q = floydSteinbergLocal(cell, pxSize, levels, diffW);
col = mix(inkDark, paperLight, q);
}
if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) {
col = vec3(0.92, 0.36, 0.22);
}
fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| error-diffusion-dithering-live | Tesla T4 1920x1080 | 123× baseline (extreme) · 4.05 ms / 4.26 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| error-diffusion-dithering-live | NVIDIA L4 1920x1080 | 108× baseline (extreme) · 1.72 ms / 1.78 ms · 2.4× faster on L4 | opengl-timer-query · Cross-architecture validation |
| error-diffusion-dithering-live | NVIDIA A100-SXM4-40GB 1920x1080 | 73.1× baseline (extreme) · 2.11 ms / 2.14 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |