LearnPatterns and noise

Lesson 17 of 30 · 50 min

Light and finishing

Finish images with glow, additive light, tone mapping and vignettes

In this lesson you will

  • Make glows from a distance (k / d, exp(-d * k)) and choose their strength and spread
  • Add lights together to build scenes full of overlapping points of light
  • Bring brightness above 1.0 back into range naturally with tone mapping (1.0 - exp(-x), Reinhard)
  • Finish an image with gamma correction, a vignette, film grain and chromatic aberration, and explain their order

01Light glows

The shapes so far have been crisp fills. But things that give off light — street lamps, neon, stars — spread a soft brightness around them. It is just "brighter near the source, darker farther away", so the distance from Circles and distance is all we need. The simplest version divides by the distance.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.05 to 0.1 for a stronger, bigger glow. What changes if you replace 0.05 / d with 0.0025 / (d * d)?

0.05 / d is 1.0 at 0.05 from the center, 0.5 at 0.1, and 0.1 at 0.5. It gets bright quickly as you approach and never quite reaches 0 far away. That long tail is what makes it look like light diffusing through air. At the very center we divide by 0, so the value gets huge, but the screen clips it to 1.0, leaving a white-hot core.

There is another reason the core turns white. Multiply the color (1.0, 0.6, 0.3) by 3 and you get (3.0, 1.8, 0.9): red and green clip at 1.0 and it looks yellowish. Multiply by 10 and all three pass 1.0 — white. Real photos do the same thing: the middle of a bright light looks white.

02Choosing how light falls off

There are other formulas for turning distance into brightness (falloff formulas), and each gives light a different character. Below, three lights use three formulas, from left to right.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
▸Change 12.0 to 4.0 and the middle light spreads wide. Did you notice the tail of the left light reaching all the way to the middle one?
  • k / d … sharp core, very long tail; a big hazy glow. With many lights, the tails pile up and brighten the whole screen
  • exp(-d * k) … exactly 1.0 at the center, with no division by zero. The tail dies fast, so k sets the size of the light precisely
  • k / (d * d) … the inverse-square falloff of real light. A very intense core that fades quickly. Combined with gamma correction, coming up later, it spreads out just right

It's not just points that can glow. Any distance function from Drawing with distance can glow. To make an outline glow, use the distance to the edge, abs(d). That gives you a neon sign.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Lower 0.012 to 0.004 for thin neon tubes. Set flicker to 1.0 to stop the flicker

The middle of each tube looks white for the same reason as before: the values pass 1.0 and all three channels get clipped. Even pink (1.0, 0.2, 0.6) pushes green over 1.0 once it is multiplied by 5 or more. flicker is an old-school random number that changes every 0.083 seconds (1 ÷ 12), dimming the sign now and then like aging neon.

03Adding lights together

Light has a handy property: it adds up. Two bulbs give you the sum of their brightness. So we can draw many lights by looping and adding each one's brightness in turn. Let's use hash22 from Making randomness to pick each light's position and color, and let them drift.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Starts when it scrolls into view
Open in /edit ↗
▸Change i < 24 to i < 48 for more lights; the whole screen gets hazy. Raise 0.01 to 0.03 and the overlaps blow out to white

Where lights overlap, their colors add up and get brighter. Push it too far, though, and the overlaps become flat white patches where you can no longer tell which light had which color. That's because everything above 1.0 is cut off on screen. Let's fix that next.

04Tone mapping

A screen can only show 0.0 to 1.0, but light looks most natural when you compute it freely, past 1.0 if need be. Then, at the very end, squeeze large values smoothly into 0–1. That's tone mapping. Cameras do the same thing to fit a dazzling sky and a dark room into one photo.

A popular formula is 1.0 - exp(-x). It is 0 at x = 0, 0.39 at 0.5, 0.63 at 1, 0.86 at 2 and 0.98 at 4: approaching 1.0 as x grows, but never passing it. Take the color (2.0, 1.2, 0.4). Clipping turns it into yellow (1.0, 1.0, 0.4), but 1.0 - exp(-x) gives (0.86, 0.70, 0.33), which keeps the original orange feel. Below, to make the difference obvious, three big red, green and blue lights overlap, and we compare the two halves.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Compare the two halves. On the right the white patches are gone and every light keeps its color. Remove the // from // mapped = col / (1.0 + col); to switch to the Reinhard formula

Another well-known formula is x / (1.0 + x), called Reinhard after its inventor. It gives 0.5 at 1, 0.75 at 3 and 0.9 at 9. It approaches 1.0 more slowly than 1.0 - exp(-x), so differences among the bright parts survive better and the overall look is a little calmer.

Tone mapping also darkens the dim and middle tones a bit (0.5 becomes 0.39). Set the overall brightness by multiplying before tone mapping. That multiplier is called exposure: col *= 1.5;, then tone map.

05Gamma correction

Here's a surprise: screen brightness is not proportional to the numbers. A screen shows a value v as roughly v to the power of 2.2 in actual light. A value of 0.5 becomes 0.5 to the 2.2, about 0.22: only a fifth of the light of 1.0. The reason is that our eyes are more sensitive to differences in the dark, so screens spend more of their steps there.

Adding lights and fading them with distance, as we have been doing, are calculations in amounts of light. Send those straight to the screen and the middle tones get crushed dark. So at the very end we apply the inverse of the power of 2.2, pow(col, vec3(1.0 / 2.2)). It cancels the screen's power of 2.2, and you see the amounts of light you computed. This is gamma correction, which we touched on in Mixing colors.

1

2

3

4

5

6

7

8

9

10

11

Starts when it scrolls into view
Open in /edit ↗
▸Compare the middle of the blend and the spread of the light between the top and bottom. Change 2.2 to 1.0 and the halves match

In the top half (uncorrected), the middle of the red-green blend sinks into a dark olive, and the light huddles around its core. In the bottom half (corrected), the middle turns bright yellow and the light spreads large and soft. For example, the half-and-half blend (0.35, 0.35, 0.0) becomes (0.62, 0.62, 0.0) after correction. The light uses k / (d * d), just like real light. Uncorrected it is a small dot; corrected, it spreads naturally, much like the k / d glow we started with.

Watch out

Apply gamma correction once, after tone mapping, right before returning the color. Applying it twice makes everything washed out. Also, pow is undefined for negative inputs, so if a color might go negative, clamp it first with max(col, 0.0).

Tip

Gamma correction lifts dark colors: 0.02 becomes 0.17 and 0.1 becomes 0.35. To keep a background dark, make the value before correction much smaller (0.002 ends up around 0.06). For the same reason, the long tail of k / d gets lifted too and the screen turns hazy, so in gamma-corrected scenes use k / (d * d) or exp(-d * k).

06Vignette and film grain

A vignette darkens the corners of the frame a little. Like a photo taken through an old lens, it naturally pulls the eye toward the middle. Here is a commonly used one-liner:

1

col *= pow(16.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y), 0.2);

uv.x * (1.0 - uv.x) is 0 at the left and right edges and 0.25 in the middle (0.5). The same goes vertically, so the product of all four is 0 at the edges and 0.0625 (one sixteenth) at the center. Multiplying by 16.0 makes it exactly 1.0 at the center. The final pow(…, 0.2) pulls values close to 1.0 up and flattens them, so only the area near the edges darkens. A larger exponent than 0.2 gives a stronger vignette.

Film grain jiggles each pixel's brightness a tiny bit, imitating the texture of film. Add a random number that changes every frame, just a little (around ±0.02). It gives flat gradients some texture and hides the stair steps (banding) that dark gradients tend to show.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Raise 0.2 to 0.6 for a stronger vignette. Bump 0.04 up to 0.15 to see clearly what the grain does (then set it back)

The grain's random number mixes in fract(time), so the pattern changes every frame and the texture shimmers. We add the grain after gamma correction. Added before, it would be stretched a lot in the dark areas by the inverse power of 2.2 and look far too coarse there.

07Chromatic aberration

Photos from cheap lenses sometimes show red and blue fringes around bright objects. Different colors of light bend by slightly different amounts, so red, green and blue land in slightly different places. This is chromatic aberration, and a little of it on purpose gives the feeling of looking through a lens.

To imitate it in a shader, put the scene in a function such as vec3 scene(vec2 p), call it three times at slightly different spots, and take red from one, green from another and blue from the third. Shifting by scaling, as in p * 1.01, makes the offset zero at the center and larger toward the edges, just like a real lens.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Starts when it scrolls into view
Open in /edit ↗
▸Set amount to 0.0 and the neon turns back to plain white. At 0.05 the colors split apart like in 3D glasses

Watch out

Chromatic aberration computes the scene three times, so it costs three times as much. Skip it for heavy scenes, or trim the loop counts first. Keep the offset modest, around 0.01–0.02, for a tasteful look.

08The order of finishing

The order of these finishing steps changes the result. When in doubt, use the order below and you won't go far wrong. The 3D lessons ahead finish their images the same way.

  1. Compute the scene in amounts of light. Lights add up, and going over 1.0 is fine (for chromatic aberration, call the scene three times here)
  2. Exposure: multiply everything to set the overall brightness
  3. Tone mapping: squeeze into 0–1 with 1.0 - exp(-col) or similar
  4. Vignette: applied after tone mapping, the darkening is easy to control
  5. Gamma correction: pow(col, vec3(1.0 / 2.2)), exactly once
  6. Grain: add a tiny bit of randomness to the final screen values

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

Starts when it scrolls into view
Open in /edit ↗
▸Raise the exposure 1.4 to 3.0: thanks to tone mapping, nothing blows out. Comment out the finishing lines one at a time with // to see what each does

09Exercises

Try combining light and finishing by hand. Feel free to keep the list above open for the order of the finishing steps.

Exercise 1

A neon triangle

Turn this thin line-drawn triangle into a glowing neon sign. Light should bloom around the lines with a whitish core, in cyan. Finish with tone mapping and gamma correction, keeping the background dark.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

A spinning ring of lights

Place 12 lights evenly around a circle and rotate them slowly. Give each light its own hue to make a rainbow, and tone map so the overlaps don't blow out.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

The full finishing pass

Here is a scene of a planet in space with a star behind it. Right now it returns the raw values, so the area around the star blows out and everything else sinks into darkness. Add exposure, tone mapping, a vignette, gamma correction and grain in the right order to finish it like a photograph.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Starts when it scrolls into view
Open in /edit ↗

10Recap

  • Glow comes from distance: k / d has a long tail; exp(-d * k) makes the size easy to control
  • Use abs(d) to make an outline glow. Lights combine by adding
  • Don't clip values above 1.0; tone map them (1.0 - exp(-x), x / (1.0 + x)), setting brightness with exposure beforehand
  • Screens emit light at the value to the power of 2.2, so gamma-correct once at the end with pow(col, vec3(1.0 / 2.2))
  • A vignette draws the eye in, grain adds texture, and chromatic aberration (three scene calls) adds a lens feel
  • The order: scene → exposure → tone mapping → vignette → gamma → grain

Next time we make works that respond to the viewer. Using the cursor position mouse and attention, which rises when someone hovers a work in the Gallery, we'll build images that react when touched.

Finished lessons get a mark in the course contents