Raymarching 2: glow, fog and light

glint · Examples · Tutorials · Gallery

After Raymarching, step by step: glow from near misses (per object, on the beat), fog that hugs the ground, light shafts marched through fog and done in 2D, things that light up, bloom with a threshold, tone mapping, and what glow costs.

A free interactive course of 11 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. Glow from near misses

This course picks up where Raymarching, step by step leaves off: the light and air that make a scene feel like a place. Start with the easiest effect raymarching gives: glow.

A marching ray already knows, at every step, how close it is to the nearest surface (d). Rays that pass close to the ring get small ds for several steps; rays far away never do. So adding up something that's big when d is small, at every step, gives a glow that hugs the shape: a halo, for one line of code.

0.01 / (0.002 + d * d) is how thick the glow is at a point: 5 right on the surface, falling away smoothly (0.8 at d = 0.1, 0.1 at d = 0.3). The 0.002 sets how wide it is.

It's multiplied by d, the length of the step about to be taken, so it adds up glow per distance travelled, not per step. Without that, rays that hit (which end in dozens of tiny steps) would pile up glow in rings on the surface, and the picture would change with the step count.

Your turn: In the loop, before the hit test, gather it: glow += d * 0.01 / (0.002 + d * d);

2. Each object its own glow

With one d for everything, every glow is the same colour. To colour each object's glow differently, keep each object's own distance (a for the ring, b for the ball) and gather a glow for each from its own. The step is still d = min(a, b), the nearest of the two: that's how far the ray can safely move.

The ring already gathers gA and is drawn pink. The ball has gB and is drawn cyan, but nothing ever adds to it.

The same trick works for any number of objects, and the glows add like light: where pink and cyan overlap they make lilac.

Your turn: Give the ball its glow: under the gA line add gB += d * 0.01 / (0.002 + b * b);

3. Glow on the beat

Glow is the easiest thing to make move with music: it's a single number at the end, so scaling it costs nothing and nothing has to be marched again.

kick is beat: 1 on each beat (with audio on, Inputs panel), fading away after. With no audio it stays 0; to try it without music, use float kick = exp(-fract(time * 2.0) * 5.0);, a pulse twice a second.

A small base (0.3) keeps the glow alive between beats; the kick adds a flash on top.

Your turn: Make the glow pulse: col += (PINK * gA + CYAN * gB) * (0.3 + 2.0 * kick);

4. Fog that hugs the ground

Distance fog (the Raymarching course's last step) fades everything by how far the ray went: 1.0 - exp(-t * 0.08). Fine for things, wrong for the sky: rays that miss go 60 units, so the sky turns solid fog colour too.

Real fog lies low: thick at the ground, thinning going up. Then a ray looking up passes through hardly any, and one skimming the ground passes through lots. heightFog works out exactly how much fog a ray passes through on its way to t, in one go, with no extra marching (the maths is the integral of a * exp(-b * height) along the ray).

Its a is the thickness at the ground and b how quickly it thins. Try them after.

Your turn: Swap the distance fog for height fog: float fog = heightFog(ro, rd, t);

5. Light shafts through fog

A lamp above a turning plate full of holes. Fog glows where light passes through it, so a ray picks up light all along its way, not just where it ends. The second loop walks along the ray in 32 steps and, at each point, adds the lamp's light that would scatter towards us there: brighter near the lamp (1 / (1 + dL²), light fading with distance).

Right now every point counts, so the fog is a plain glow round the lamp. But the plate is in the way for most of them. vis marches from each point to the lamp (a shadow ray, as in the Raymarching course) and is 0 where something blocks it. Multiply by it and the fog only glows where light gets through: shafts, the shape of the holes.

This costs: 32 points × up to 16 steps each, for every pixel. That's why this pass draws at half resolution (Pass panel › Render › Resolution 50%: a quarter of the pixels; fog is soft, so it hardly shows), and why the next step does it in 2D.

Your turn: Only count the light that gets there: fogLight += vis * DENSITY / (1.0 + dL * dL) * dt;

6. Light shafts in 2D

The cheap way, used in games and in most demos: do it on the finished picture. The Scene pass draws the ring against the sun and keeps, in alpha, how much sun each pixel sees (0 where something's in front).

Pass 1, Rays (open), walks from each pixel towards the sun on screen and adds up those alphas. Pixels whose path to the sun is mostly clear get a lot; ones behind the ring get little. That's a blur radiating out from the sun, and it looks like shafts.

With 4 samples you can see the four copies it's made of. More samples blend them into smooth rays. It's still far cheaper than the last step: a few dozen texture reads, no marching.

Your turn: In the Rays pass, take more samples: const int SAMPLES = 48;

7. Things that light up

Neon tubes, screens and lava don't need light shining on them: they give out their own. That's emission, and it's added after the lighting, not multiplied by it.

Each pillar has rings in its own colour. Now they're just painted on (albedo), so they're as dark as the rest of the pillar in the moonlight and black in shadow. emit is the light they give out, 4 times brighter than white: that's fine, a later step deals with numbers over 1.

Your turn: Add the light they give: col = albedo * light + emit;

8. Bloom, only where it's bright

Bloom is light spilling round bright things, as it does in a camera lens or your eye. Three passes: Scene (last step's), Bright (open) picks out what should spill, and Finish adds blurred copies of Bright (its mipmaps) on top of the scene.

Bright passes on everything, so the whole picture is blurred over itself: a hazy, washed-out look. Only light brighter than white should bloom: max(c - 1.0, 0.0) keeps just the part over 1, which here is only the neon.

This is why the scene works in values over 1 (the pass targets hold them): with everything clipped at 1 there'd be no telling a white wall from a neon tube.

Your turn: In the Bright pass: vec3 bright = max(c - 1.0, 0.0);

9. Tone mapping: fitting the light in

The scene has light from 0.01 (the sky) to over 4 (the neon) and more where bloom piles up. A screen shows 0 to 1. clamp just cuts off everything over 1: the neon goes flat, and bright colours lose their hue (a bright orange clips to yellow, then white).

A tone curve squeezes the whole range in instead, gently: dark stays as it was, bright rolls off towards white without ever hitting a wall. aces is the curve film-style renderers use; the * 0.8 is exposure, as on a camera.

(The Colour and light course compares several curves.)

Your turn: In the Finish pass, swap the clamp for the curve: col = aces(col * 0.8);

10. What glow costs

SHOW_COST is on: each pixel's colour is how many steps its ray took (brighter = more). The bright rims are rays skimming past the ring and the ball, creeping along in tiny steps. That's what makes glow nice (they gather a lot) and what makes it slow: those rays never hit, so they never stop early.

MIN_STEP puts a floor under the step: never move less than this. Skimming rays then pass in a few strides. The cost is accuracy right at the surface (a ray can step a little way in, which a thin enough object would let it pass through), so keep it smaller than the thinnest thing.

The Profiler (Alt G, or the fps readout) shows the same per loop for any shader, and the inspector's flame button paints the cost map.

Your turn: Set MIN_STEP to 0.02 and watch the rims go dark. Then turn SHOW_COST off to check the picture.

11. Pull it apart: neon in the fog

Everything together: emissive neon, moonlight with soft shadows, height fog, a Bright pass with a threshold, bloom from mipmaps and ACES. Read through the three passes and see how they hand on to each other.

But the bloom is wrong. The neon is too bright and sharp-edged, with no soft light spilling round it. Something in the Finish pass isn't reading what it should.

Hint: the loop is meant to read Bright's picture at five levels of blur.

Your turn: Fix the Finish pass so the glow spreads.

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 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 · Blur and glow