LearnInto 3D

Lesson 23 of 30 · 55 min

Infinite space and deformation

Build endless scenes with domain repetition, twists and bends

In this lesson you will

  • Repeat 3D space with p - s * round(p / s) and line up one shape forever
  • Choose which axes repeat, and use clamp to stop after a set number of copies
  • Give each copy its own height or color from its cell number and hash21
  • Twist, bend and ripple shapes by rotating and shifting the coordinates
  • Understand why deformations make the distance inaccurate, and prevent holes by shortening the step

01One shape, forever

In Repetition and tiles we cut the plane into cells with fract and filled it with one pattern. The same idea works in 3D, and there it pays off even more.

Remember that in raymarching, map(p) answers "how far is the nearest surface from point p?". If, inside map, we first turn p into a position inside one cell and then measure, every point answers with the distance to "the shape in the middle of its own cell". We wrote the shape once, yet every cell in space seems to hold a copy — and the cost is still that of a single shape.

1

vec3 q = p - s * round(p / s);

s is the spacing; q is the position seen from the center of the nearest cell

Let's read it piece by piece. p / s measures the position in units of cells. round snaps to the nearest whole number, so round(p / s) is the number of the nearest cell. Multiplying by s gives that cell's center, and subtracting it from p gives the position as seen from the center. Each component of q always lands between -s / 2 and s / 2.

Let's check with numbers. With s = 2.0 and p.x = 4.7, p.x / s is 2.35, round gives 2, the center is 4.0, so q.x is 0.7. With p.x = 5.2 we get 2.6 → 3 → center 6.0, so q.x is -0.8. The two points are close together, but they sit on either side of the border at 5.0, so they are measured from different centers.

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

Starts when it scrolls into view
Open in /edit ↗
▸What if the spacing 2.0 becomes 3.0? What if the radius 0.4 becomes 0.8?

We wrote a single sphere, yet copies fill every direction and the camera never reaches the end. The camera sits at x = 1.0 and y = 1.0, right in the gap between spheres. Distant spheres are blended into the fog color by distance, as with the floor in Rays and cameras (we look at the fog formula closely in Materials and atmosphere).

Watch out

Keep the shape small enough to fit in its own cell. q only ranges from -s / 2 to s / 2, so a sphere with a radius bigger than s / 2 gets sliced flat at the cell walls — each point only ever looks at its own cell's shape. Stay a little under half the spacing.

Tip

mod(p + 0.5 * s, s) - 0.5 * s and (fract(p / s + 0.5) - 0.5) * s give exactly the same q. The round version keeps the cell number, round(p / s), right there in the formula, which is handy when we vary each cell later.

02Choosing which axes repeat

You do not have to repeat all three axes. To stand pillars on a floor, repeat only across (x) and into the distance (z), and leave height (y) alone. A swizzle applies the formula to q.xz only.

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 ↗
▸Change the q.xz = … line to q.z = p.z - s * round(p.z / s); and only one row of pillars remains. Change 3.0 to 2.0 to pack the pillars closer

The floor distance p.y uses the original p, not the repeated q. The floor is already one endless sheet, so there is nothing to repeat. Inside map you can freely mix repeated things and one-off things: measure repeated objects with q and single objects with p.

We pick each object's color from the height of the hit point (pos.y < 0.01 means floor). That trick stops working as scenes grow; the proper way, giving each object a number, comes in Materials and atmosphere.

03Limiting the count

Sometimes you want a fixed number, say a 5×5 grid, instead of an endless field. Then clamp the cell number round(p / s) into a range.

1

q.xz = p.xz - s * clamp(round(p.xz / s), -n, n);

With n = 2.0 the cell numbers run from -2 to 2 — five in all. Points outside the range use the number of the last cell, so they get the correct distance to the end box. For example, with s = 1.2 and p.x = 5.0, p.x / s is 4.17 and round gives 4, but clamp holds it at 2. The center is 2.4 and q.x is 2.6. If the box is 0.4 wide on each side, the distance comes out at about 2.2 — exactly the distance to the right-hand box.

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

Starts when it scrolls into view
Open in /edit ↗
▸Set n to vec2(3.0, 1.0) for a 7×3 grid. What does vec2(0.0, 2.0) give?

04Making each copy different

A row of perfectly identical things always looks artificial. This is where the cell number comes in. round(p.xz / s) is a pair of whole numbers unique to each cell, so passing it to hash21 from Making randomness gives each cell its own fixed random value from 0 to 1. Feed that into height, color, rotation or speed, and every copy becomes different.

In the city below, each cell's random value sets its building height. The same value picks a color through palette from Mixing colors. Since map only returns a distance, we work the cell number out again from the hit point, round(pos.xz / s), when coloring. The hit point is in the same cell as the building, so it gets the same number.

There is a trap, though. Each point only looks at the building in its own cell. Suppose your cell's building is short and the neighbor's is tall. A point at height 1.5 near the cell wall might be 1.2 away from its own short building, while the neighbor's tall wall is only 0.2 away sideways. map answers 1.2, the ray jumps 1.2, lands inside the neighbor's building, and passes straight through it. That is why buildings look sliced at an angle.

The fix is one line. Every building stays at least 0.2 inside its cell walls, so a neighbor's building is at least "the distance to my cell wall + 0.2" away. The distance to the nearest wall of a square cell is 0.5 * s - max(abs(q.x), abs(q.z)). Take the smaller of that and the distance to your own building, and the ray can never jump over a neighbor.

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

Starts when it scrolls into view
Open in /edit ↗
▸Put // in front of the d = min(d, 0.5 * s … line and the buildings get sliced. What if the height range 1.2 becomes 3.0?

Tip

If the per-cell change is only color or rotation and the size stays the same, you do not need to worry about this. When sizes vary, remember: keep shapes away from the cell walls, and cap the distance with "distance to the wall + the gap".

05Twisting

Repetition multiplied shapes by folding the coordinates. Now we move the coordinates a little differently at every point to deform the shape itself. First up: twisting.

In Building 3D shapes we saw that q.xz = rot(a) * q.xz turns a shape around the y axis. Now, instead of a fixed angle a, make it proportional to height: q.y * k. Every horizontal slice of the shape turns by a slightly different angle, more and more as you go up, and the shape twists. k is the angle in radians per unit of height; with k = 3.0, one unit of height turns about 172 degrees.

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

Starts when it scrolls into view
Open in /edit ↗
▸Look at the torn, jagged edges. Set k to 1.0: the twist relaxes and the tears disappear

We get a nicely twisted ribbon, but at k = 3.0 the edges are torn and in places you can see right through it. The formula is not wrong — so what is going on?

06Deformation breaks the distance

Raymarching trusts what map says. It can leap forward by d because of a promise: "there is no surface within d" (Raymarching). sdBox and sdSphere keep this promise exactly. Put another way, when a point moves by 1, the distance value changes by at most 1.

Twisting breaks the promise. map measures the distance in the straight, untwisted box. But in real space, the ribbon's edge, 0.7 from the axis, swings sideways by 0.7 × 3.0 = 2.1 for every 1 it rises. The shape leans far more than map thinks, so the real surface is closer than map says — sometimes only about half as far. If map says 1.0 when the surface is really 0.5 away, the ray steps past the surface into the shape. With a thin ribbon it can come out the other side, and you see a hole. This is exactly the "overestimated distance makes holes" problem from Raymarching.

The fix is to take map's answer at a discount. Change t += d; to t += d * 0.6; and each step covers only 60% of the distance. Moving more carefully makes overshooting much less likely, and near the surface the steps shrink on their own, so the ray still arrives.

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

Starts when it scrolls into view
Open in /edit ↗
▸Put 0.6 back to 1.0 and tears appear whenever the twist is strong. Try 0.2: dark smudges appear along the edges, because 64 steps are no longer enough to reach the surface

Discounting too much is a problem too. Smaller steps mean more steps to cover the same distance, and the loop stops at 64. With something like 0.2, the ray runs out of steps before reaching the surface or the far parts of the scene. A good range is 0.5–0.8. Start at 1.0, lower it bit by bit, and stop as soon as the tears are gone.

Watch out

The stronger the deformation, the bigger the discount it needs. For a twist, watch out when "distance from the axis × k" gets large. Together with the neighbor-cell problem from earlier in this lesson, this gives you a rule of thumb: when a raymarched image has tears, first suspect that map is overstating the distance.

07Bending

A twist was "turn the xz plane by an angle set by y". Change the plane and you get a bend: "turn the xy plane by an angle set by x". The farther along x, the more it tilts, so anything lined up straight along x curves like a bow.

Below, we bend space first and then repeat 11 thin slabs along the bent coordinate. Before bending they would stand in a straight row; in bent space they fan out. The color comes from the position pos.x through a palette.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the bend strength 0.6 to 1.2 and the fan curls almost into a ring. What happens with rot(q.y * k) instead of rot(q.x * k)?

Bending distorts distances too, so the step is discounted to 0.7 here as well. When you bend harder, lower this number along with it.

Tip

Shifting instead of rotating, as in q.x += sin(q.z * 0.5), is another common deformation. It turns a straight corridor into a snaking path; in Tunnels and flight we fly the camera along one.

08Rippling the surface

You can also add a small value to the distance itself instead of moving the coordinates. Where you add a positive value, the surface sinks inward; where you add a negative value, it bulges outward. Add the product of three sin waves and the sphere's surface ripples.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change all three 8.0s to 16.0 for fine ripples. What about 0.05 → 0.15? (If it tears, lower the 0.7 too)

This also bends the truth about distance. The slope of the added wave is about size × frequency: 0.05 × 8.0 = 0.4. When a point moves by 1, the distance value can now change by up to about 1.4, so a discount of 1 ÷ 1.4 ≈ 0.7 is enough. Bigger or finer waves need a bigger discount.

09A forest of endless pillars

Let's combine everything into one scene. Pillars repeat in x and z, and each cell's random value sets its twist strength and direction, its spin speed and its color. The pillars rise forever (their height is simply 50). Every cell has the same size of shape, so the neighbor problem does not arise. The twist calls for a 0.7 step discount.

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

Starts when it scrolls into view
Open in /edit ↗
▸What if both 0.35s (the pillar thickness) become 0.6? What about changing the 3.0 in (h - 0.5) * 3.0 to 6.0? (If it tears, lower the 0.7)
  • round(p.xz / s) … the cell number, used for both repetition and randomness
  • rot(q.y * k + …) … an angle that changes with height makes a twist; adding time makes it spin
  • t += d * 0.7 … discounts the distance that the deformation overstated
  • mix(col, fog, …) … fades the distance into fog so the scene feels endless

10Exercises

Now combine repetition and deformation yourself. Each starter already runs. Check for yourself whether you need to discount the step.

Exercise 1

Colorful spheres

The endless spheres are all the same orange. Use the cell number to give each sphere its own hue. A sphere must keep its color as the camera moves past it.

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

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

A 3×3×3 cube of cubes

There is one small cube in the middle. Turn it into 27 cubes, three along each axis. The whole block turns slowly, and each cube also spins in its own direction at its own speed.

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

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

An endless forest

Turn the single tree into an endless forest. The trees should not sit exactly in the middle of their cells: shift each one a little and vary their sizes. Watch out for the same problem that sliced the buildings.

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

Starts when it scrolls into view
Open in /edit ↗

11Recap

  • q = p - s * round(p / s) repeats space: endless copies for the cost of one. Keep shapes under half the spacing
  • Pick the axes with a swizzle; limit the count with clamp(round(p / s), -n, n)
  • Pass the cell number to hash21 to make every copy different; mind the neighbors when sizes vary
  • Rotating by a position-dependent angle twists (xz by y) or bends (xy by x); adding a wave to the distance ripples the surface
  • Deformations make map overstate the distance; discount the step, as in t += d * 0.6, to prevent tears

You can now multiply shapes without limit, but they still seem to float above the ground. Next time we cast a second ray toward the light to drop shadows, and darken the crevices with ambient occlusion, giving the scene weight and depth.

Finished lessons get a mark in the course contents