LearnInto 3D
Lesson 24 of 30 · 55 min
Shadows and depth
Ground your shapes with soft shadows and ambient occlusion
In this lesson you will
- Add shadows by casting a second ray from the hit point toward the light
- Understand why the shadow ray starts a little away from the surface
- Understand how
softShadowworks and tune how soft the shadows are - Use
calcAOto darken gaps and crevices for a sense of depth - Build lighting from sun, sky and bounce light, and balance loop counts against cost
01Without shadows, things float
In Normals and lighting you learned to shade a surface by the way it faces. Something is still missing, though. The picture below has two spheres. One rests on the floor; the other floats a little above it. Can you tell which one floats just by looking?
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
44
45
46
47
48
49
50
51
map gives away the answer. Change the right sphere's height 0.85 to 0.5 and the picture barely changesThe right sphere floats 0.35 above the floor, but in the picture you cannot tell whether it is floating or just farther back. We read how objects relate to the ground from their shadows: a shadow touching the object means it rests there, a shadow set apart means it floats. Shadows are the strongest cue for depth and weight.
In this lesson the floor stretches far away, so rays give up at 50.0 instead of 20.0. The distant floor is blended into the haze color haze so it melts into the sky.
02Casting a second ray toward the light
Whether a point is in shadow is the same question as "can this point see the light?". And you already know how to check whether something can be seen: raymarch again, from the point toward the light direction l. If the ray hits something on the way, the light is blocked and the point is in shadow. If it travels far without hitting anything, the point is in the sun.
1
2
3
4
5
6
7
8
9
10
float hardShadow(vec3 ro, vec3 rd) {
float t = 0.02; // start a little away from the surface
for (int i = 0; i < 32; i++) {
float h = map(ro + rd * t);
if (h < 0.001) return 0.0; // hit something → shadow
t += h;
if (t > 10.0) break; // got far enough
}
return 1.0; // nothing in the way → lit
}
It is almost the same as the main raymarch. The only difference is that it returns just whether it hit: 0.0 for shadow, 1.0 for light. To use it, cast from the hit point pos toward the light l and multiply the diffuse term dif by the result. In shadow only the sunlight disappears; the light from the sky remains.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
float t = 0.02; inside hardShadow becomes 0.0?Watch out
The shadow ray starts a little away from the surface (t = 0.02). The hit point pos is where map dropped below 0.001, so starting at t = 0.0 makes the very first step report "hit something", and every surface — even ones facing the light — turns to shadow. Sometimes it shows up as blotchy speckles instead; this is called shadow acne.
03Soft shadows
We have shadows now, but their edges are razor sharp, like paper cutouts. The real sun is not a point; it has a small size, so the edge of a shadow has a penumbra where only part of the sun is hidden. The penumbra is thin right next to the object and gets wider and blurrier farther away.
With distance functions you get this penumbra almost for free. As the shadow ray travels, the value h that map returns at each point is "the distance to the nearest object from here". If the ray grazes an object's edge, h gets small there. So we track the smallest value of h / t — the clearance h relative to the distance traveled t — along the whole ray. h / t is roughly the angle of the gap as seen from the starting point; the smaller it is, the more of the sun is hidden.
1
2
3
4
5
6
7
8
9
10
11
float softShadow(vec3 ro, vec3 rd) {
float res = 1.0;
float t = 0.02;
for (int i = 0; i < 32; i++) {
float h = map(ro + rd * t);
res = min(res, 8.0 * h / t); // the closest graze so far
t += clamp(h, 0.02, 0.2); // step between 0.02 and 0.2
if (res < 0.001 || t > 10.0) break;
}
return clamp(res, 0.0, 1.0);
}
res… 1.0 is full light, 0.0 is full shadow, values in between are penumbra8.0 * h / t…8.0sets the hardness: larger is sharper, smaller is softerclamp(h, 0.02, 0.2)… jumping too far could skip the moment of grazing, so at most 0.2; and at least 0.02 so the ray keeps moving while it grazesres < 0.001… once it is fully dark, stop early instead of checking the rest
Let's check with numbers why it blurs with distance. 8.0 * h / t drops below 1 — the point darkens at all — when h is less than t / 8. On the floor right next to an object (the object is t = 0.4 away), only the region with h below 0.05 is penumbra, so the edge is crisp. On the floor far from the object (t = 3.2), everything with h below 0.4 is penumbra, so the edge spreads wide. The same behavior as real shadows falls out of this one line.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
8.0 in softShadow to 2.0 for very soft shadows, or 32.0 for crisp ones. Set the largest step 0.2 to 0.03 and the shadows get cut shortThe shadows get cut in that last experiment because a shadow ray has a limited reach. With a largest step of 0.2 and at most 32 steps, it can never get past 32 × 0.2 = 6.4. With 0.03 the reach is 32 × 0.03 ≈ 1.0, so floor more than 1.0 away from an object gets no shadow. Choose these two numbers to suit the size of your scene.
Tip
The standing ring, sdTorus(q.xzy, …), is the trick from Building 3D shapes: swapping y and z stands the lying doughnut up.
04Darkness in the gaps (ambient occlusion)
Shadows were about sunlight. Light from the sky, the ambient light, arrives a little from every direction. In places like the corner of a room or the gap under an object, most of the sky is hidden by nearby things, so less sky light gets in and they turn a little darker. This is called ambient occlusion (AO).
Doing it for real would mean casting rays in every direction, which is far too expensive. Distance functions offer a shortcut: step away from the surface along the normal and check map at five points. If nothing is around, a point h away from the surface is exactly h from the nearest surface. If a wall or object is nearby, map comes out smaller than h. Adding up those differences tells you how enclosed the point is.
1
2
3
4
5
6
7
8
9
10
float calcAO(vec3 p, vec3 n) {
float occ = 0.0;
float w = 1.0;
for (int i = 0; i < 5; i++) {
float h = 0.01 + 0.12 * float(i); // 0.01, 0.13, 0.25, 0.37, 0.49
occ += (h - map(p + n * h)) * w; // add how much closer than expected
w *= 0.9; // farther samples count less
}
return clamp(1.0 - 3.0 * occ, 0.0, 1.0);
}
Let's check with numbers. In the middle of an open floor, map equals h at all five points, so the differences are 0, occ is 0, and AO is 1.0 — no darkening at all. On the floor at the foot of a sphere, the sphere overhangs, so at h = 0.37 map might be only 0.2, a difference of 0.17. If the weighted differences add up to an occ of 0.25, AO is 1.0 - 3.0 × 0.25 = 0.25: quite dark. It is only five calls to map, much cheaper than a shadow.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
3.0 in calcAO to 6.0 for stronger darkening. Change 0.12 to 0.3 to pick up enclosure over a wider areaLooking at AO alone in black and white, you can see soft darkening at the base of each object, inside the ring, and where the box meets the floor. Unlike shadows, it does not depend on the light direction; it darkens wherever things are enclosed. Multiply it into the ambient light and the objects settle firmly onto the floor.
Tip
This AO only samples along one line, the normal, so it is a plausible approximation rather than the real thing. It is not accurate, but it is cheap and looks great, which is why raymarching demos use it everywhere.
05Building up the lighting
With shadows and AO in hand, let's rebuild the lighting. The trick is to split the light reaching a point by where it comes from and add the parts together. Each part has its own multiplier.
- Sun (direct light) …
dif × shadow. The shadow decides whether the sun is visible. Warm orange - Sky (ambient light) …
(0.5 + 0.5 * n.y) × AO. Faces pointing up see more sky, and AO decides how much of it is visible. Blue - Bounce …
(0.5 - 0.5 * n.y) × AO. The sunlit floor lights things from below, so faces pointing down receive more. A warm color close to the floor's - Shine (specular) …
spe × shadow. It is a reflection of the sun, so it vanishes in shadow
Shadow means "can I see the sun?" and AO means "can I see the sky?", so the shadow multiplies only sunlight and AO only the sky and bounce light. If you multiply everything by the shadow, shadowed areas turn pitch black and look wrong. It is the faint blue sky light filling the shadows that makes them believable.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
lin += lines one at a time with // to see what each light does. Remove occ from the sky line and the darkness in the gaps disappearsNote the dif > 0.0 ? softShadow(pos, l) : 0.0 part as well. A face turned away from the light (dif is 0) gets no sunlight whether or not something blocks it. For those points we skip softShadow entirely and save the work.
06Counting the cost
Shadows and AO make the picture much better, but they add work. Let's count, in the worst case, how many times one pixel calls map: 64 in the main raymarch, 6 for the normal, 32 for the shadow, 5 for AO — 107 in all. A Full HD screen has about two million pixels, so that is over 200 million calls per frame, repeated 60 times a second.
This site's publishing check requires each frame to render within 8 ms. The loop limits (64 for the raymarch, 32 for shadows, 5 for AO) exist to stay inside that budget. In practice most pixels leave their loops much earlier. The picture below colors each pixel by how many steps the main raymarch and the shadow loop took together: blue is cheap, yellow and red are expensive.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
dif > 0.0 ? shadowSteps(pos, l) : 0.0 with just shadowSteps(pos, l) and faces in the dark get expensive too. What happens if the largest step 0.2 becomes 0.1?The expensive places are where rays graze an object's edge: just outside an object's outline for the main raymarch, and along shadow edges for the shadow ray, because both creep forward in tiny steps. The sky (rays fly off into the distance) and the middle of a shadow (it quickly turns fully dark and exits) stay cheap. Here are ways to keep the cost down:
- Skip the shadow on faces turned away from the light (
dif > 0.0 ? … : 0.0) - Shrink the shadow ray's reach (
t > 10.0) and largest step to fit the size of your scene - Make
mapitself cheaper; for a complex shape, answer with a simple bounding box while the point is far away - Every extra light adds another shadow loop, so give shadows to only one or two lights
Tip
You can check the real cost with the frame time shown in the editor (/edit). Fast and small goes deeper into what to trim to make things faster.
07Moving the sun
Finally, let's turn the sun slowly so the shadows sweep across the floor. The lighting is built the same way as before; only the light direction l rotates with time. Watch how the shadow shapes, the penumbra and the darkness in the gaps change with the light.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
0.45 to 0.15 for long evening shadows, or raise it to 1.5 for short midday ones08Exercises
Use shadows and AO to build scenes where light is the star. softShadow and calcAO are already in every starter.
Sundial
There is a thin pole standing on the floor with a ring around it. Make the sun go around once every 12 seconds so the pole's shadow sweeps over the ring like a clock hand. Keep the sun low enough that the tip of the shadow reaches the ring.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Two colored lights
The scene is lit by one warm orange light. Add a second, blue light from the other side. Give each light its own shadow: where only one light is blocked, the shadow takes on the other light's color. Aim for two-colored shadows, like stage lighting.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Light through a lattice window
A wall stands at the back, and the sun is on the far side of it, so the floor in front is entirely in the wall's shadow. Cut small square windows in the wall, 7 across and 3 high, so the light coming through lays a row of square sunny patches on the floor.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
09Recap
- A shadow is decided by raymarching again from the hit point toward the light and seeing whether anything gets in the way
- Start the shadow ray a little off the surface (
t = 0.02), or everything turns to shadow (shadow acne) softShadowbuilds a penumbra from how closely the ray grazes,h / t.8.0sets the hardness; step size × step count sets the reachcalcAOadds up "how much closer than expected" at five points along the normal to darken the gaps- Shadows multiply the sun; AO multiplies the sky and bounce. Skip shadows on faces turned away, and let loop limits cap the cost
With light and shadow in place, your shapes now have weight. Next time we give each object its own color and material, add reflections on the floor, and paint the air with fog and sky to finish a complete scene.
Finished lessons get a mark in the course contents