LearnInto 3D

Lesson 25 of 30 · 60 min

Materials and atmosphere

Finish a scene with per-material color, reflections, fog and sky

In this lesson you will

  • Return a distance and a material number together from vec2 map(vec3 p), and give each object its own color and shine
  • Put a checkerboard on the floor and add reflections with a single bounce
  • Use distance fog (exp) to give the scene a sense of air and depth
  • Paint a sky gradient and a sun, and tie the fog color to the sky
  • Finish a complete scene with tone mapping, color grading and gamma, in that order

01Numbering your objects

So far map has returned only the distance to the nearest surface. When it is time to color a pixel, though, you also want to know what you hit: the floor or the sphere? Until now we have either checked the height of the hit point to spot the floor, or, as in Building 3D shapes, measured each part's distance again at the hit point and picked the one near 0. But re-measuring means writing each shape's formula twice, once in map and once where you color, and every time you move an object you have to fix both. The more objects you have, the easier it is to slip up.

So we change map to return a vec2. x holds the distance as before, and y holds a material number (1 for the floor, 2 for the sphere, and so on). You choose the numbers.

1

2

3

4

5

6

vec2 map(vec3 p) {

vec2 res = vec2(p.y, 1.0); // start with the floor (distance, number)

float d = sdSphere(p - vec3(0.0, 0.7, 1.0), 0.7);

if (d < res.x) res = vec2(d, 2.0); // if the sphere is closer, switch to the sphere

return res;

}

The key is how the union (pick the nearest) is written: if (d < res.x) res = vec2(d, number); instead of min. min can only choose a distance, but the if chooses the nearer distance and its number as a pair. To add an object, just repeat those two lines.

Since map now returns a vec2, every function that uses it needs a small fix: wherever you want the distance, write map(…).x. In the raymarch loop, step by the distance and remember the number h.y of the last point checked.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

// normal: map(…) becomes map(…).x

vec3 calcNormal(vec3 p) {

vec2 e = vec2(0.001, 0.0);

return normalize(vec3(

map(p + e.xyy).x - map(p - e.xyy).x,

map(p + e.yxy).x - map(p - e.yxy).x,

map(p + e.yyx).x - map(p - e.yyx).x));

}

// inside softShadow: float h = map(ro + rd * t).x;

// inside calcAO: occ += (h - map(p + n * h).x) * w;

 

// raymarch: step by the distance, remember the number

float t = 0.0;

float m = 0.0;

for (int i = 0; i < 64; i++) {

vec2 h = map(ro + rd * t);

m = h.y;

if (h.x < 0.001 || t > 40.0) break;

t += h.x;

}

The scene below has four materials: floor, sphere, box and an avenue of pillars. The pillars repeat endlessly into the distance using the repetition from Infinite space and deformation, and abs(q.x) mirrors them into two rows, just like the symmetry trick in Building 3D shapes. Each color is chosen by looking at the number m.

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

Starts when it scrolls into view
Open in /edit ↗
▸Give the sphere vec3(0.7, 0.08, 0.05) a color you like. Change the number 2.0 in map to 3.0 and the sphere takes on the box's color

Watch out

The number is a float, so compare it with a cut between numbers, as in m < 2.5, rather than m == 2.0. == would work for now, but if you later blend two objects with smin, the number can come out as something like 2.3 at the seam, and == would match nothing.

02Color and shine per material

Different materials differ not only in color but in how they shine. A polished sphere has a crisp white highlight; stone pillars barely shine at all. So we write a material function that returns color and shine strength together: a vec4 with the color in rgb and the shine in the fourth slot, a.

We also gather the lighting into a shade function. It is the same sun, sky and bounce lighting you built in Shadows and depth; the only new part is multiplying the shine (specular) by the material's a. Having it as a function lets us reuse it for reflections later.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

Starts when it scrolls into view
Open in /edit ↗
▸Set the sphere's shine 1.0 in material to 0.0 and it looks like rubber. What if the pillars' 0.05 becomes 1.0?

The sun is far ahead, up to the left, so the objects are lit from behind. The faces toward us get only the blue of the sky and the bounce from the floor, while sunlight wraps around the edges: an evening scene in backlight.

03A checkered floor

Let's add a pattern to the floor. A material's color can depend not only on the number but also on where it was hit, pos. It is the same idea as the checkerboard in Repetition and tiles: round x and z down with floor, add them, and mod(…, 2.0) alternates between 0 and 1.

For example, if pos is (2.3, 0, 5.8), floor gives 2 and 5, which add to 7, and mod(7.0, 2.0) is 1: a light tile. Move x up by one to (3.3, 0, 5.8) and you get 3 + 5 = 8, which gives 0: a dark tile.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

Starts when it scrolls into view
Open in /edit ↗
▸Change floor(pos.x) to floor(pos.x * 2.0) and floor(pos.z) to floor(pos.z * 2.0) to halve the tile size

Tip

Far-away tiles shrink below a pixel and start to shimmer or form moiré stripes. The fog and reflections we add next soften the distant pattern. If it still bothers you, an easy fix is to shrink the difference between the two colors with distance.

04One bounce of reflection

Polished floors and spheres reflect their surroundings. The reflected color is "the color you would see looking from that point in the mirrored direction". GLSL gives you that direction with reflect(rd, n): the view direction rd flipped around the normal n.

Then raymarch once more in that direction and light whatever you hit with shade. If you hit nothing, use the sky color. So that we don't write the same loop twice, the raymarch now lives in a march function that returns the distance traveled and the material number as a vec2. Starting slightly off the surface (pos + n * 0.01) is for the same reason as with shadow rays.

The reflection strength refl is set per material: the polished floor and sphere reflect, the box and the stone pillars do not. Materials that don't reflect don't need the second raymarch at all, so it lives inside if (refl > 0.0) and gets skipped.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

Starts when it scrolls into view
Open in /edit ↗
▸Change the floor's 0.25 to 0.6 for a wet-looking floor. Change the sphere's 0.3 to 0.9 and it becomes a mirror ball

fre is the Fresnel effect. Look straight down at water and you see the bottom, but water in the distance mirrors the sky and shines. Everything reflects more strongly the more obliquely you look at it. 1.0 - dot(n, -rd) is 0 when you look head-on and approaches 1 at a grazing angle, so we raise it to the fifth power and add it to the reflection strength. Head-on (dot = 1) adds 0; at a slant where dot is 0.2, it adds 0.8 to the fifth, about 0.33.

Watch out

You may want reflections inside reflections, but GLSL functions cannot call themselves (no recursion). For a second bounce you would have to write it with a loop, and each bounce adds a whole raymarch to the cost. Here we also skip shadows for points seen in the reflection (passing 1.0 as sha) to keep it lighter.

05Fog

Distant scenery looks faded and bluish. As light travels through air it scatters and weakens little by little, and the color of the air mixes in instead. This fog is, alongside size and overlap, a key cue for distance.

For every unit light travels through air, it loses the same fraction. If 97% survives each unit, then 97% of 97% survives two units, and 97% to the power t survives t units. Written with the exponential function, that is exp(-k * t): the fraction of light that remains. The fog amount is the rest, 1.0 - exp(-k * t).

With k = 0.03, the box at distance 4 gets 11% fog, a pillar at 20 gets 45%, and 40 gets 70%. Near things stay crisp, and things fade smoothly toward the fog color with distance. For comparison, the picture below fogs only its right half. On the left half you can see the floor stop abruptly at 40, leaving a hard edge against the sky.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

Starts when it scrolls into view
Open in /edit ↗
▸In the float fog = … line, change 0.03 to 0.1 for thick fog or 0.01 for a light haze. Then delete if (p.x > 0.0) to fog the whole picture

Tip

We also fog the reflected color (rcol), because objects seen in a reflection are another hit2.x away from the reflecting point. Forget it and the distant pillars in the reflection stand out unnaturally sharp.

In the finish of Normals and lighting we used t squared: 1.0 - exp(-t * t * 0.006). The plain exp(-k * t) hazes things by the same fraction step by step, starting right in front of you. Squaring keeps nearby things almost clear, then hazes quickly past a certain distance. The squared version with 0.006 gives 9% at t = 4 and 91% at 20. Compared with 11% and 45% for the plain version with k = 0.03, the two are about the same up close, but the squared one swallows the distance in fog. Use the plain version when the whole landscape should feel hazy, and the squared one to keep the foreground clear and hide only what is far away. In the preview above, change exp(-0.03 * hit.x) to exp(-0.002 * hit.x * hit.x) to compare.

06Sky and sun

Let's paint the sky behind the scene. sky(rd) picks a sky color from the viewing direction rd. The horizon (rd.y = 0) is orange, straight up (rd.y = 1) is deep blue, and mix joins them. We pass it through sqrt because a real sky changes color faster near the horizon.

The sun comes from how closely the view lines up with the sun direction, dot(rd, SUN): 1 when looking right at the sun, smaller as you look away. Raise it to a high power and only the sun's direction lights up. To the 512th power, it is 0.73 at 2 degrees from the sun, 0.14 at 5 degrees and almost 0 at 10 degrees: a small disc. To the 6th power, 0.42 remains even at 30 degrees, giving a wide glow.

Then make the fog color not a constant but the sky color in that direction, sky(rd). Fog looking toward the sun glows bright orange, fog on the other side is calmer, and distant objects melt naturally into the sky. Since the sun that lights the objects and the sun painted in the sky share the same SUN direction, the shadows and reflections all agree.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

Starts when it scrolls into view
Open in /edit ↗
▸Change the y of SUN from 0.25 to 0.05 to lower the sun toward the horizon and lengthen the shadows. Change 512.0 to 64.0 for a bigger sun

Tip

The sun's disc is boosted to 6.0, far brighter than 1.0. For now it is clipped at 1.0 and flattens into white, but the tone mapping in the next step squeezes it onto the screen while keeping the difference in brightness.

07Finishing the color

Finally, we turn the computed light into screen colors. We follow the order from Light and finishing — scene → exposure → tone mapping → vignette → gamma correction → grain — and add one step, color grading, right after tone mapping. Changing the order changes the result, so keeping the same order every time saves a lot of guesswork.

  • Scene … lighting and fog; values may exceed 1.0
  • Exposure and tone mapping … 1.0 - exp(-col * exposure) squeezes overly bright values below 1.0
  • Color grading … contrast, saturation and tint, done on values already in 0 to 1
  • Vignette … darken the corners a little
  • Gamma correction … pow(col, vec3(1.0 / 2.2)); if you add grain, it comes after this

The contrast step col * col * (3.0 - 2.0 * col) is the same S-curve as inside smoothstep: 0.25 drops to 0.16, 0.75 rises to 0.84, and 0.5 stays put. Darks get darker and lights get lighter. Saturation pushes the color 1.2 times farther from its gray (the brightness dot(col, vec3(0.299, 0.587, 0.114))). A factor above 1.0 can push values outside 0 to 1, so we clamp before gamma.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

Starts when it scrolls into view
Open in /edit ↗
▸Comment out the finishing lines one at a time with // and compare. What happens if the exposure 1.1 becomes 2.0?

Watch out

Adding or multiplying light after gamma makes the image disagree with your lighting math. Do all the light math before gamma, and apply pow(col, vec3(1.0 / 2.2)) exactly once. About the only thing that belongs after gamma is film grain, a texture on top of the screen.

08Putting it all together

Everything is in place. As a last touch, add a little motion: the sphere bobs slowly up and down and the camera drifts side to side. As the sphere lifts, its shadow separates and its reflection moves with it. Check that each technique is doing its job.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

Starts when it scrolls into view
Open in /edit ↗
▸What if the bobbing 0.15 becomes 0.4? Change the camera drift 0.8 to 2.0 for a better look at the avenue of pillars
  • vec2 map … distance plus material number; per-object color and shine come from material
  • softShadow and calcAO … the shadows on the floor and the darkness in the gaps
  • reflect and a second march … reflections on the floor and sphere, stronger at grazing angles thanks to Fresnel
  • sky(rd) … the background, the fog color and the sky in reflections, with the same SUN as the lighting
  • Tone mapping → color grading → gamma … the finish into screen colors

09Exercises

Try adding materials, changing how things shine, and changing the time of day. Every starter comes from this lesson's previews.

Exercise 1

A golden ring

Stand a shining golden ring at the back of the scene, behind the gap between the sphere and the box. Give it material number 5, and use material to make it gold and very shiny.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

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

A flooded temple

Turn the floor into rippling water. Keep the floor's shape in map flat; after the hit, tilt only its normal a little with waves that move over time. Make the floor reflect more strongly too, and the reflections of the sunset and the pillars will shimmer.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

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

From dusk to night

Make the sun sink slowly and then rise again. As it gets lower, the sky and the light darken, and once it sets the sky turns night-time navy. The fog and reflections should follow the sky.

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

Starts when it scrolls into view
Open in /edit ↗

10Recap

  • vec2 map returns a distance and a material number together. Union with if (d < res.x) res = vec2(d, number); and read map(…).x where you need the distance
  • Compare numbers with cuts between them, like m < 1.5. Color and shine come from material; patterns come from the hit position pos
  • Reflection is one more raymarch along reflect(rd, n), stronger at grazing angles with Fresnel. No recursion, and every bounce costs a full raymarch
  • Fog is mix(col, fogColor, 1.0 - exp(-k * t)). Use sky(rd) as the fog color and the distance melts into the sky
  • Finish in this order: scene → exposure and tone mapping → color grading → vignette → gamma (the order from Light and finishing, plus color grading)

You can now draw a 3D scene with light, shadow, materials and air. The next lesson begins Part 4, "Making demos", where we enter the world of fractals: endlessly detailed shapes grown from a simple formula repeated over and over.

Finished lessons get a mark in the course contents