Blur and glow

glint · Examples · Tutorials · Gallery

Blurs that stay smooth however big they get: why spaced reads and flat weights go blocky, the Gaussian, reaching 3σ, two pixels a read, blurring smaller pictures, reading them back smoothly, bloom in several sizes, mipmaps read round a circle, then zoom (radial) blur, jitter, light shafts, spin blur and bokeh.

A free interactive course of 14 steps in glint, a live shader editor in the browser: each step changes a running shader, and ticks itself when you've done its task.

1. Why a big blur goes blocky

A blur makes each pixel the average of the pixels round it. Done in two passes (along x, then along y: see Passes and feedback) it's cheap, so it's how most glows are made. This one is in the Common pass, and both Blur passes call it: Blur x with one pixel across as dir, Blur y with one pixel up.

It reads 9 pixels in a row. To make it bigger without reading more, a common trick is to space the reads out: SPACING pixels apart. Try it and watch the lights.

Each light turns into a grid of 9 × 9 copies: the reads skip the pixels in between, so the blur is made of separate pictures, laid out in a square. That's the blockiness. The rest of the course is about blurs that stay smooth however big they get.

Your turn: In Common, space the reads out: const float SPACING = 6.0;

2. A bell, not a box

Now every pixel is read (no gaps), 25 in a row, all with the same weight: w = 1.0. That's a box blur, and done along x then y it makes a square. Look at a light: a soft square, with hard edges.

A Gaussian blur weights each read by a bell curve: exp(-0.5 * i² / σ²), 1 in the middle, falling smoothly to nearly 0 about 3σ out. Along x then along y, a Gaussian makes a perfectly round blur, which is why it's the one everyone uses: it's the only blur that's both separable and round.

Dividing by total, the weights added up, keeps the picture as bright as it was, whatever the weights.

Your turn: In Common, weight the reads by a bell: float w = exp(-0.5 * float(i * i) / (SIGMA * SIGMA));

3. Reaching far enough

To make a Gaussian wider, make σ (SIGMA) bigger, never the spacing. But the reads have to reach far enough: the bell is still well above 0 at 1σ, so stopping there cuts it off, and a cut-off bell is a box again, with hard edges.

Here σ is 8 pixels but the loop stops at R = 8: the lights come out as soft squares again. Reach 3σ, where the bell has fallen to 1 %.

That's 49 reads a pass. The next steps make big blurs cheaper.

Your turn: In Common, reach three σ: const int R = 24;

4. Two pixels for one read

A texture read between two pixels gets their mix, for free: the GPU's bilinear filtering blends the four nearest pixels. So pixels i and i+1 can be read at once, from a point between them, nearer the one with the bigger weight.

If the weights are w1 and w2, the point is i + w2 / (w1 + w2) and the read counts w1 + w2. Half the reads, the same blur, exactly.

Right now o is just i, so each read gets pixel i twice and i+1 never: the blur comes out narrower and lumpy.

Your turn: In Common, read between the pair: float o = float(i) + w2 / (w1 + w2);

5. Blur a smaller picture

The biggest saving: blur a smaller picture. A pass's Resolution (in the Pass panel) sets the size of its picture: at 25 % it has 1/16 of the pixels, and σ counts that pass's own pixels, so the same blur covers four times as far for a sixteenth of the work.

Blur y already runs at 25 %. Blur x doesn't, so the blur is still narrow across. Make it match.

A blur hides the lost detail: it was going to be smoothed away anyway. Shrinking only goes wrong in how the small picture is read back, as the next step shows.

Your turn: Select the Blur x tab, open the Pass panel and set Resolution to 25 %.

6. Pull it apart: the blocky glow

A bloom: Bright keeps what's brighter than white, Blur x and Blur y blur it at 12.5 % (a Gaussian, as before), and Combine adds it to the scene. But the glow comes out in blocks, little squares round every light, and the bigger the glow the worse.

The blur passes are fine (the Show steps before looked smooth). Look at how Combine reads the blurred picture.

(Bright runs at 25 %, so the picture shrinks in two steps: straight from full size to 12.5 %, the blur would skip pixels in between and thin things would flicker.)

Your turn: Make the glow smooth.

7. Bloom in several sizes

Real glow is a tight halo near a light and a wide haze further out. One Gaussian is one size, so good blooms add several: a small blur, then a blur of that at a lower resolution (blurring a blur makes a wider one: their σ² add up), and so on.

Passes 2 and 3 are the tight glow at 50 %; passes 4 and 5 blur that again at 12.5 %, eight times as wide for the same work. Combine adds only the tight one so far.

Your turn: In Combine, add the haze too: col += 1.0 * texture(pass5, uv).rgb;

8. A blur for free: mipmaps

With its sampler set to mipmap (Pass panel › Samplers), a pass's picture comes with smaller copies: half the size, a quarter and so on, each pixel the average of the four below it. textureLod(tex, uv, lod) reads level lod: level 4 is 1/16 the size, so it's already a blur, at no cost. Adding a few levels together gives a tight glow and a wide one.

Read once, it's blocky: each level's pixels are a box average of the ones below, and the GPU only blends straight lines between them. Read a few times round a small circle about a pixel of that level wide and average, and the squares melt into a round glow.

Not as good as a Gaussian, but one pass and no loops over pixels: handy for a quick glow, or for a blur whose size changes per pixel (depth of field, rough reflections).

Your turn: In Combine, read round a circle: const int TAPS = 6;

9. Zoom blur

A blur doesn't have to spread every way. A zoom (radial) blur averages along the line from each pixel towards the middle, as if the camera zoomed while the shutter was open: streaks pointing outwards.

mix(uv, CENTRE, t * STRENGTH) goes from the pixel (t = 0) part of the way to the middle (t = 1). With STRENGTH 0 every read is the pixel itself.

Your turn: Zoom: const float STRENGTH = 0.3;

10. Hiding the steps: jitter

Look closely at the streaks: they're made of 24 separate copies, stepped, the same problem as the spaced reads at the start. More reads would hide it, at a price.

The cheap fix is to start each pixel's reads at a random point between steps (j, 0..1, different for every pixel). Neighbouring pixels then fill in each other's gaps, and the steps turn into a fine grain the eye barely sees, smooth as a hundred reads.

Your turn: Jitter the reads: float j = hash(gl_FragCoord.xy);

11. Light shafts

The zoom blur aimed at a light instead of the middle, on the bright parts only, makes light shafts (god rays): from each pixel, read back towards the light and add up what's bright on the way.

DECAY dims each step further back, so the rays fade with distance. With 1.0 they never fade and run right across the picture.

Your turn: In Rays, fade them: const float DECAY = 0.96;

12. Spin blur

The other way round the middle: a spin blur reads along a circle through the pixel, a little either side, as if the picture turned while the shutter was open. Each read is the pixel's place turned by an angle.

The jitter is already in, so 24 reads look smooth. The further out, the longer the streaks: the same angle sweeps a bigger arc.

Your turn: Spin it: const float ANGLE = 0.4;

13. Bokeh: a disc, not a bell

A camera out of focus doesn't make a Gaussian: each light becomes a disc the shape of the lens opening (bokeh). A disc isn't separable, so it's done in one pass, reading points spread over it: each one turned by the golden angle from the last, its distance the square root of its number, so they cover the disc evenly with no pattern.

On a photo the discs show because lights are far brighter than everything round them. Weighting bright reads more makes them pop out the same way.

Your turn: Weight the bright reads: float w = 1.0 + 8.0 * smoothstep(1.0, 2.0, max(c.r, max(c.g, c.b)));

14. Pull it apart: the dim glow

Another bloom, all Gaussian and smooth. But the glow is far too faint, and making σ bigger makes it fainter still, until it all but vanishes.

Look at the blur in Common: what is the sum divided by?

Your turn: Make the glow as bright as the light it came from.

Other courses: Basics · Medium · Advanced · Maths for shaders · Raymarching, step by step · Matrices, from scratch · Sound you can see · Noise from scratch · Colour and light · Passes and feedback · Skies and reflections · Vertex shaders, step by step · Raymarching 2: glow, fog and light · Raymarching 3: worlds · Raymarching 4: modelling, materials and motion · Fractals, step by step · Vertex shaders 2: lines, particles and moving meshes · Fractals 2: further in · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment