Why dither at all
A smooth gradient is one of the hardest things to show on an 8-bit display. Step through a sky slowly enough and the rounding errors line up into visible bands. The usual fix is to add noise, which trades banding for grain.
Dithering makes the trade on purpose. Reduce the palette to a deliberately small number of levels, then choose which level each pixel gets based on its brightness and a threshold pattern. At a distance the eye averages the pattern back into a tone; up close it is a texture. That texture is the point, not a side effect.
The hero uses twelve levels. Few enough that the pattern is visible, many enough that skin, wood and water still read as themselves.
Bayer, without the lookup table
Ordered dithering uses a fixed matrix of thresholds: an 8×8 grid of values that repeats across the screen. The usual implementation stores 64 numbers in an array or a texture. The shader builds the same pattern recursively instead, from one two-by-two function:
float bayer2(vec2 a) {
a = floor(a);
return fract(a.x / 2.0 + a.y * a.y * 0.75);
}
#define bayer4(a) (bayer2(0.5 * (a)) * 0.25 + bayer2(a))
#define bayer8(a) (bayer4(0.5 * (a)) * 0.25 + bayer2(a))
Each step halves the coordinate, evaluates the smaller pattern, and folds it back in at a quarter of the weight. The result is a 64-value threshold field in the range 0–1, generated from a few instructions and no memory. Subtracting 0.5 turns it into the signed offset the quantiser wants.
The pattern is why the result reads as print rather than television static. Noise varies per pixel and per frame; Bayer repeats, so the eye can lock onto it and the image holds still even when the camera moves.
One pass, after the tone map
The scene is rendered normally, tone-mapped and converted to display space by an
OutputPass, and only then handed to the dither pass, so the levels are
quantised in display space rather than the linear space the scene was lit in.
The pass does not sample each pixel independently. It snaps to a block first, then samples the centre of that block:
vec2 block = floor(gl_FragCoord.xy / uPixelSize);
vec2 blockUv = (block * uPixelSize + uPixelSize * 0.5) / uResolution;
vec3 color = texture2D(tDiffuse, blockUv).rgb;
That detail matters. If every pixel sampled its own colour and then quantised, the pattern would sit on top of the full-resolution image and the edges would turn to mush. Sampling the block centre first means the whole block shares one input colour, so the dither grid itself becomes the resolution of the image. It is a deliberate drop in spatial detail to gain a stable, crisp texture.
The grid is two CSS pixels wide
The block size is set in device pixels and scaled by the device pixel ratio, because
gl_FragCoord is measured in device pixels too. Without that scale, the same
shader would produce a grid half as wide on a retina screen and the dither would almost
disappear.
The pixel ratio used by the renderer is capped at 1.5, and the block is set to
2.0 × dpr (2.2 on narrow screens). So the grid stays roughly two CSS pixels
on a phone, a laptop and a 4K monitor.
The palette moves with the scene
Dithering is only half of the look. The other half is that the twelve levels are not fixed colours: the scene's atmosphere is sampled across four keyframes — daylight at 0, golden dusk at 0.46, twilight at 0.66, midnight at 1 — and every colour in the scene is interpolated between them with a smoothstep.
The same scalar drives the page. A separate pair of light and dark palettes is sampled on the CPU and written to CSS custom properties, so the paper, ink and accent of the sections below the hero move with the sky. The favicon and the browser theme colour change too. One number, one light.
That is also why the dial can be dragged instead of toggling a theme. There is no discrete dark mode to switch: there is a time of day, and everything samples it.
What it costs
The post pass is cheap; the scene is not. A full Three.js scene with a custom ripple
plane, particles and a composer is a lot of work for a background, so the module is
loaded on idle rather than in the first payload, the renderer pauses when the hero
scrolls out of view or the tab is hidden, and visitors with Save-Data get a static
background instead. With prefers-reduced-motion, the clock stops: no drift,
no entrance, no ripples, but the dither remains.
One anti-flicker detail worth keeping: the paper grain added after quantisation is hashed from the block coordinate, not from time. Per-frame noise on a static pattern sparkles; per-block noise looks like paper tooth.
Read it
The pass is in src/hero/dither.js, the palette in
src/hero/atmosphere.js, and the order of passes in
src/hero/HeroScene.js. The
source for the site
is on GitHub.