LearnPatterns and noise

Lesson 14 of 30 · 55 min

Domain warping

Warp the coordinates themselves with noise to make smoke, marble and flowing liquid

In this lesson you will

  • Understand domain warping: what happens to a picture when you add noise to its coordinates
  • Write the fbm(p + fbm(p)) pattern and Inigo Quilez's two-level warp
  • Use the in-between values q and r to color the result richly
  • Control how the pattern flows by choosing where to add time
  • Create patterns like marble, smoke, lava lamps and ink marbling

01Warp the coordinates, not the colors

In the lessons so far, noise decided colors: brightness, cloud density, mountain height. In this lesson, noise decides where to look. Before passing p to the formula that draws the picture, we nudge p by adding noise to it. The picture bends and warps, like a view through rippled glass or the bottom of a swimming pool.

In mathematics, the set of values a function accepts is called its domain. Because we bend the input side, the technique is called domain warping. The formula is very simple: instead of picture(p), you write picture(p + offset).

The offset needs both an x and a y part, so we call noise twice and build a vec2. The second call adds vec2(5.2, 1.3) to read a different part of the noise, so the x and y offsets move independently. Noise runs from 0 to 1, so - 0.5 centers it on -0.5 to 0.5; otherwise the whole picture would shift 0.5 up and to the right.

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

Starts when it scrolls into view
Open in /edit ↗
▸Set amount to 0.0 and the stripes go straight again. What about 1.0, then 3.0? Change both p * 2.0 to p * 6.0 for fine, crinkly ripples

With amount at 0.3, no point moves more than 0.15 in any direction. But that's about half the stripe spacing of 0.31 (one cycle of sin(q.x * 20.0)), so the stripes clearly ripple. How strong a warp looks depends on how big it is compared to the features of the original picture.

02Any picture can be warped

If you wrap the unwarped picture in a function like scene(p), you can warp any picture just by writing scene(p + offset). The example below warps the checkerboard from Repetition and tiles. Scrolling the two offset noises in opposite directions over time keeps it rippling, like tiles at the bottom of a pool.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.25 to 0.05 for a gentle shimmer. Try replacing the inside of scene with any picture you made in earlier lessons

Tip

Since only the coordinates are warped, you don't have to change anything inside scene. There's no need to bend each shape's formula one by one: shift p once at the entrance and the whole picture warps together.

03Marble

Marble is a classic use of domain warping. Start with diagonal stripes, sin((p.x + p.y * 0.6) * 5.0). Add fBm inside that sin, and each stripe gets pushed back and forth from place to place into a meandering pattern. Adding inside the stripe formula is the same as shifting the coordinates across the stripes, so this is domain warping too.

Marble veins are thin lines, so instead of using the stripes as they are, we take 1.0 - abs(v). That's 1 where v is 0 (the boundary between stripes) and falls off quickly away from it. Raising it to the 6th power with pow(…, 6.0) keeps only the values close to 1, which leaves thin veins.

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 ↗
▸With warp at 0.0 it's just diagonal stripes. What about 2.0 or 12.0? Replace (p.x + p.y * 0.6) * 5.0 with length(p) * 20.0 for tree rings, like wood grain

04Feeding fBm into fBm

Now let's warp fBm itself, with an offset that's also made from fBm. As a formula it's fbm(p + 4.0 * q), where q is a vec2 from two fBm calls. The cloudy pattern from Layering noise (fBm) gets stretched and swirled into something like smoke or churning liquid.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 4.0 in 4.0 * q to 0.0 and you're back to plain fBm clouds. Try 1.0 and 8.0 to change the warp strength

Here we don't subtract 0.5 from q. Skipping it shifts everything a little, but the pattern's exact position never meant anything, so it doesn't affect the warp. What matters is that q changes smoothly from place to place. Neighboring points move by almost the same amount, so the pattern never tears; points a little farther apart move in different directions, so the pattern gets stretched and curled.

05Two levels of warping

You can also warp the warp. This is the two-level warp described by Inigo Quilez, well known in the shader world, in his article. The first-level offset q shifts where we compute the second-level offset r, and r shifts where we read the final fBm.

1

2

3

vec2 q = vec2(fbm(s), fbm(s + vec2(5.2, 1.3)));

vec2 r = vec2(fbm(s + 4.0 * q + vec2(1.7, 9.2)), fbm(s + 4.0 * q + vec2(8.3, 2.8)));

float f = fbm(s + 4.0 * r);

There's nothing special about vec2(5.2, 1.3), vec2(1.7, 9.2) or vec2(8.3, 2.8). They're just arbitrary numbers that make each of the four fBm calls read a different place, so they vary independently.

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 fbm(s + 4.0 * r) to fbm(s + 4.0 * q) to go back to a single warp. Side by side, the two-level version has finer swirls wound over and over

Watch out

The two-level warp calls fbm five times per pixel. With five-layer fBm that's 25 noise calls and 100 hash21 calls. That's about as far as you can comfortably go within the publishing check of 8 ms per frame. A third level costs a lot more without looking much different, so stop at two levels.

06Coloring with the in-between values

With the two-level warp, besides the final f, you still have the in-between values q and r. They vary from place to place too, in patterns shaped differently from f. Using each one as the mix amount for a different color gives far more depth than coloring by a single value.

  • f sets the base color, from dark navy to teal
  • Where r.x is large, blend in golden streaks
  • Darken where length(q) is large (where the first level shifted things a lot) to add shadows
  • Add white highlights where f is especially high

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

Starts when it scrolls into view
Open in /edit ↗
▸Switch off the four color lines one at a time with // to see where each one shows up. Change r.x to r.y and the golden streaks move

07Letting it flow

Put time into the warp and the pattern starts to flow like liquid. Where you add time changes how it moves.

  • Add it to the first level, q, and the whole offset slowly changes, creating large-scale currents
  • Add it to the second level, r, and the small swirls churn as they move
  • Add it to the final fbm(s + 4.0 * r), and the pattern itself drifts across the screen

The trick is to use very small speeds. The warp scales its offsets up by 4 before using them, which magnifies any movement too. Around time * 0.05 is where it finally settles into a calm flow.

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

Starts when it scrolls into view
Open in /edit ↗
▸Multiply 0.05 and 0.1 by 10 and it boils furiously. Change the last argument to palette, vec3(0.0, 0.1, 0.2), to vec3(0.5, 0.2, 0.25) for a completely different color scheme

08Smoke and lava lamps

Give a warped fBm a shape and it becomes a specific thing. For smoke, flow the pattern upward (subtract time from y), multiply by a column that's narrow at the bottom and spreads toward the top, and fade it out as it rises. The key detail is reusing the first-level offset q.x to sway the column from side to side. Since the sway comes from the same value as the warp, the bending of the column matches the swirls in the smoke.

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 ↗
▸Change the 1.5 in 1.5 * y to 0.0 for smoke rising straight up, or 3.0 for smoke tossed by the wind. Change the 0.25 in smoothstep(0.25, 0.65, d) to 0.1 for thicker smoke

Warping isn't just for fBm; it works on crisp shapes too. The wax blobs in a lava lamp are really just circles. Five circles from Circles and distance rise from bottom to top over and over using fract, and the wax pooled at the bottom is the horizontal line q.y + 0.72. Just nudging the coordinates with noise before measuring distances turns perfect circles into molten, shape-shifting blobs of wax.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change warp * 0.25 to warp * 0.0 and you're back to perfect circles. Try 0.5 for an even more molten look

09Exercises

Each exercise is finished by shifting p, without changing the picture itself. Think about how large, how fine and which way the offset should flow.

Exercise 1

Heat haze

On a hot day, the view above a road seems to shimmer: that's heat haze. The scene in the starter is a desert road at dusk. Shift the coordinates passed to scene with noise, so that fine ripples rise from the bottom up. Make the shimmer strongest around the horizon and weaker farther from it for a realistic effect.

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

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

Suminagashi

Suminagashi is a traditional Japanese technique: drops of ink spread into rings on water, a gentle breath sets them swirling, and the pattern is transferred to paper. The starter has concentric rings around the spot where the ink was dropped. Warp the coordinates with fBm before measuring the distance to get a suminagashi pattern. If you have time, alternate indigo and vermilion from ring to 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

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

Nebula

Let's float a cloud of gas, a nebula, in a starry sky. In the starter the gas is just plain fBm painted gray. Rewrite it as a two-level warp, mix colors like violet, teal and a reddish tint using q and r, and make it drift very slowly. Let the stars behind show through where the gas is thin.

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 ↗

10Recap

  • Domain warping shifts the coordinates with noise before they reach the picture's formula: picture(p) becomes picture(p + offset)
  • The offset needs an x and a y, so read noise twice, at different places, to build a vec2
  • fbm(p + 4.0 * q) gives smoky swirls, and a second level winds them finer. Stop at two levels (five fbm calls)
  • Using the in-between values q and r as color mix amounts gives rich, deep color
  • Add time to q or r in very small amounts. Warping crisp shapes makes them look molten

Next time we return to the one-point-per-cell idea from Making randomness and use the distances between points to build Voronoi patterns: cells, cracks and stained glass.

Finished lessons get a mark in the course contents