LearnMaking demos

Lesson 27 of 30 · 55 min

Tunnels and flight

Build endless flights with 2D and 3D tunnels

In this lesson you will

  • Draw a 2D tunnel from the angle and the inverse of the distance to the center
  • Add time to the pattern coordinates to fly forward and to spin
  • Build a 3D tunnel from the inside of a tube, bent along a path made with sin
  • Move the camera along the path, aim it a little ahead and bank it into turns
  • Use light streaks, passing markers, field of view and fog to make the flight feel fast

01What a tunnel looks like

Picture a photo of a tunnel or a long corridor. The pattern on the walls is large up close and shrinks with distance, gathering at one point in the middle of the picture (the vanishing point). Run that idea backwards and you can fake depth on a flat 2D screen.

Distant things look small because apparent size is inversely proportional to distance: twice as far, half as big. A ring of tunnel wall at depth z shows up on screen as a circle with a radius of roughly 1 / z. Turn that around, and a pixel at distance r from the center is looking at the wall at a depth of roughly 1 / r.

For the position around the tunnel, the angle atan(p.y, p.x) from Polar coordinates works as is. These two values, the position around and the depth, become the coordinates of the pattern on the tunnel wall. The example below paints a checkerboard with them.

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.4 to 1.0: the tunnel seems wider and the tiles look smaller. Change 12.0 to 6.0 to halve the number of tiles around

Where r is 1.0 (the top and bottom edges of the screen), z is 0.4; where r is 0.1, it is 4.0. Depth grows faster and faster toward the center, reaching 40 at r = 0.01. That is why the center looks noisy: dozens of tiles are squeezed into each pixel. That spot is "very far away", so in the next example we hide it in darkness.

02Making it flow

To travel down the tunnel, add time to the depth z. With z + time, the pattern slides one unit toward you every second, so it feels like you are moving forward. Add time to the position around, u, and the tunnel spins.

We also darken the distance. Just like the fog in Materials and atmosphere, multiply by exp(-distance * density) so that farther means darker. The noisy center sinks into the dark and the depth suddenly reads.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change time * 1.5 to time * 5.0 to go faster. Change the 0.6 in exp(-depth * 0.6) to 0.2 for thinner fog

We keep depth and z apart because they do different jobs. depth is how far away the visible bit of wall is, and it drives the fog. z is which part of the pattern you see, and it slides over time. If the fog used z, the whole screen would get darker and darker as time went on.

03Better patterns

Instead of checkers, you can put any 2D pattern on the wall. Treat (u, z) as ordinary coordinates and reach for fract from Repetition and tiles or a palette from Mixing colors. Below, the tile edges glow and each ring of tiles gets its own color.

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 u * 16.0 to u * 15.5 and a seam appears on the left. Change the 0.3 in smoothstep(0.3, 0.5, …) to 0.45 for thinner edge lines

Watch out

Always repeat a whole number of times around. The atan angle jumps from π to -π when pointing straight left, so u jumps from 1 to 0 there. With a whole number of repeats, the pattern at u = 1 and at u = 0 is the same, so it lines up; with something like 15.5 you get a broken seam on the left (the same ±π seam you met in Polar coordinates).

Notice also that lines near you look thick while distant ones look thin. In pattern coordinates they are all the same width; this is perspective again, making nearby things look bigger.

04Changing the shape, bending the view

The cross-section doesn't have to be a circle. r = length(p) measures distance from the center in circles. Switch it to max(abs(p.x), abs(p.y)) and it measures in squares, giving a square tunnel. It is the same idea as in Circles and distance: change how you measure distance and the shape changes.

Move the vanishing point (the center at the far end) and the tunnel seems to bend. Shifting the whole screen by the same amount just looks like a sideways slide, though. So shift the area near the center (far away) a lot, and the edges of the screen (close to you) hardly at all. Straight up close, veering off in the distance: that is what a curving road looks like.

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 ↗
▸Put // in front of the max(abs(q.x), abs(q.y)) line and remove the // from one of the two lines below it to get a round or rounded-square tunnel. Change the 0.4 in 0.4 * vec2( to 0.0 to straighten it again

Tip

pow(pow(abs(q.x), 4.0) + pow(abs(q.y), 4.0), 0.25) sits between a circle (power 2) and a square (a very large power). Change 4.0 and 0.25 to 8.0 and 0.125 and it gets even squarer. pow is undefined for a negative first argument, which is why abs comes first.

05A 3D tunnel

2D tunnels are cheap and fun, but nothing is lit, nothing truly bends, and you can't put objects inside. So let's use Raymarching and fly through a real tube.

Take a tube of radius 1 along the z axis. The distance from a point p to the tube's axis is length(p.xy) (z doesn't matter). Our distance functions have been positive outside an object and negative inside, but this time the camera lives in the hollow inside the tube. We want positive in the hollow and negative beyond the wall, so we flip the subtraction: 1.0 - length(p.xy). On the axis (length is 0) the distance is 1; at 0.3 from the wall it is 0.3. It really is the distance to the wall.

For light we use a lamp attached to the camera, like a headlight. The dot product dot(n, -rd) of the normal and the view direction makes walls that face us brighter. Then we multiply by 1.0 / (1.0 + t * t * 0.05) so that far walls, which the light barely reaches, get darker. Light weakens with the square of distance, hence t * t.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 1.0 in 1.0 - length(p.xy) to 0.5 for a narrow tube. Change the 1.5 in vec3(p, 1.5) to 0.8 for a wide-angle lens

It looks a lot like the 2D tunnel. But now the pattern sits at real z positions on the wall, and the brightness comes from actual lighting. From here on, we do what 2D couldn't.

06Bending the path

To bend the tube, shift space rather than the shape, as in Infinite space and deformation. First write path(z), which returns where the tube's center is at each depth z. Two sin waves give a road that gently weaves left and right, up and down.

In map, subtract the center at that depth, path(p.z), from the point's xy before measuring the tube distance. At z = 10, say, the tube's center is at path(10.0), so measuring from there gives you the tube at that spot. The camera goes on the path too, with ro = vec3(path(z0), z0).

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 2.0 in sin(z * 0.15) * 2.0 to 4.0 for sharper bends. The camera is on the path, but it only looks straight ahead (along +z), so the walls hide where the road goes

Watch out

Shifting space makes map drift slightly from the true distance, more so the sharper the bend. If it drifts too far, rays punch through the wall and leave holes. When that happens, step more cautiously, e.g. t += d * 0.7 instead of t += d. Gentle bends like this one are fine as they are.

07Flying along the path

To aim the camera ahead, use camera(ro, ta) from Rays and cameras and put the target ta on the path a little ahead. If ro is at z0, ta is on the path at z0 + 3. It's like driving: you look a bit down the road, not right in front of the car.

As a finishing touch, lean into the turns (this is called banking). If the path a little ahead is further right, we're turning right. Rotate the screen coordinates p with rot from Moving space, by an angle proportional to that offset, path(z0 + ahead).x - path(z0).x. Rotating p tilts the whole view.

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

Starts when it scrolls into view
Open in /edit ↗
▸Set ahead to 0.5 and the camera stares at the road right in front of it and jitters around. Change * 0.4 to * 1.5 to bank hard

The camera's position, direction and tilt all come from the same path, so if you change the road, the flight follows automatically. Play with the number and speed of the sin waves in path to find a road you like.

Watch out

The camera's z keeps growing with time * 3.0. Run it for hours, as a stream background might, and z gets so large that floats run out of precision and the picture breaks up. Likewise, if you use randomness for the wall pattern or the placement of lights, feeding in the distance traveled directly, as in hash21(floor(pos.z)), makes the pattern fall apart over long runs (Making randomness). For works meant to run a long time, keep time and distance small by wrapping them with mod, as in "Seamless loops" in Directing with time.

08Light streaks and a sense of speed

Finally, let's make the flight feel fast. A few things sell speed:

  • Long lines along the direction of travel (light streaks), which make the direction of motion obvious
  • Repeating markers that whiz past (rings, tiles): the faster they go by, the faster you feel
  • A wide field of view: a shorter focal length makes nearby things large and quick
  • Fog: hiding the far end makes the tunnel feel endless

The streaks are built inside the raymarch loop. At every step, measure the distance from that point to a light rail (a thin line running along the wall) and collect more light the closer it is. Rays that pass right by a rail end up brightest, so thin glowing lines hang in the air. It is the 1 / distance glow from Light and finishing, added up in 3D.

Multiplying by min(d, 0.3) weights each step by its length: a long step skips a long stretch, so it should add more. In the middle of the tube, though, steps get very long, so we cap the weight at 0.3 to keep it from blowing out.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 1.2 in vec3(rot(bank) * p, 1.2) to 0.7 for a wider angle, and the flight looks faster. Change time * 4.0 to time * 8.0 to fly twice as fast

The rings come from slicing the wall's pos.z every 6 units and lighting up only a narrow band in the middle of each slice. The color comes from feeding the slice number floor(pos.z / 6.0) into a palette. The final 1.0 - exp(-col * 1.5) is the tone mapping from Light and finishing, which gently tames spots where too much light piled up.

Tip

Light gathered inside the loop depends on how many steps the ray takes. If you change the loop limit, the break condition or the step size, revisit the glow strength (0.0012 here) as well.

09Exercises

Start with a 2D tunnel, then put objects into the 3D one and reshape it. Each exercise builds on one of the examples in this lesson.

Exercise 1

A swirling tunnel

Turn the 2D tunnel pattern into a swirl: six stripes that twist as they head into the distance. The stripe color should change with depth, and the edges between stripes should glow as thin lines.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

Flying through gates

In the tunnel flight, place a square gate (a frame with a hole in the middle) every 10 units so you fly through them. Center each gate on the path, give each one a slightly different rotation, and paint the gates a different color from the walls.

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

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

A lava cave

Turn the tube into a cave whose width changes along the way and whose walls are bumpy. Light the rock with a lamp just above the camera, and let orange lava glow through cracks in the rock. Let the far end sink into a dark red gloom.

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

Starts when it scrolls into view
Open in /edit ↗

10Recap

  • The inverse of the distance r from the center is depth, and the angle is the position around: together they map a pattern onto the wall
  • Add time to the depth to move forward, or to the position around to spin; hide the distance by darkening it with exp
  • Repeat a whole number of times around the tunnel (the ±π seam)
  • In 3D, the inside of a tube is 1.0 - length(p.xy); subtract path(p.z) to bend it
  • Put the camera on the path, look a little ahead, and bank into the turns
  • Speed comes from light streaks, passing markers, a wide field of view and fog

Next time we split time into scenes and use cuts, transitions and beat-synced motion to shape the flow of a whole demo.

Finished lessons get a mark in the course contents