LearnPatterns and noise

Lesson 13 of 30 · 55 min

Layering noise (fBm)

Stack octaves of noise for natural textures like clouds, mountains and fire

In this lesson you will

  • Understand fBm, which stacks noise at double the detail and half the strength each time, and what an octave is
  • Stack layers with a for loop, and tune the look with the number of octaves, lacunarity and gain
  • Know why each layer is rotated and shifted, and read this course's fbm function
  • Paint clouds, mountain ranges, fire and water with fBm

01Nature has big shapes and small ones

From a plane, a coastline shows big bays and headlands. Walk down to the beach and you see small coves; crouch down and you see the bumps on the rocks. Clouds and mountains are the same: smaller shapes are layered on top of bigger ones, level after level.

The noise from Smooth noise has swells of just one size. That's why the hills were always round and the clouds looked flat. So we take a big noise, add a smaller noise at lower strength, then an even smaller one at even lower strength, and so on. This is called fBm, short for fractional Brownian motion. The name comes from mathematics, but for our purposes "noise stacked at different sizes" is all you need to remember.

02Stacking octaves

The basic recipe is to make each layer twice as fine and half as strong. Doubling the coordinates you feed to noise halves the size of the pattern, making it twice as fine. That fineness is called frequency, and the strength (how far it swings) is the amplitude. In music, a note at twice the frequency is "one octave up", so each layer is called an octave.

The preview below draws a 1D fBm as a mountain ridge. Right now only the first layer is on. The lines for layer 2 onward are switched off with //, so remove them one at a time from the top.

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

Starts when it scrolls into view
Open in /edit ↗
▸Each line you switch on adds one more level of finer bumps, and the round hill slowly turns into a rocky peak. Once they're all on, try setting 0.5, 0.25 and the rest all to 0.2

Halving the strength each time keeps the big shape in charge. The first layer decides the overall form of the mountain, and the later layers only add detail on top. Give every layer the same strength and the fine layers bury the big shape, leaving something like gritty static. The strengths add up to 0.5 + 0.25 + 0.125 + 0.0625 + 0.03125 = 0.96875, so the result stays roughly within 0 to 1.

The second number in noise(vec2(x * 2.0, 10.0)) differs for each layer. As with the hills in Smooth noise, changing y means using a different row of the noise, and so different random values. If every layer used the same row, the same shapes would reappear at different sizes, and it would look artificial.

03Layering in a loop

Those five lines fold neatly into a for loop like the ones in Time and motion. For fBm, each pass does three things: add a noise, double the coordinates, and halve the strength. So the loop count is simply the number of layers.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 5 in i < 5 to 1, 2 and 8. With 1 it's a single layer of last lesson's noise (at half strength); with 8 the finest layers are too small to make out

Watch out

More layers means more work. One layer is one noise call, which is four hash21 calls, so five layers cost 20 per pixel. On top of that, layers that are too fine get smaller than a pixel: they add cost without changing the picture, and they flicker when things move. With fbm(p * 3.0) and 8 layers, the finest layer has 768 cells across the screen's height, finer than the pixels of a typical preview. Aim for 5 or 6 layers at most.

04Lacunarity and gain

The 2 and the 0.5 in "twice as fine, half as strong" have names. How much the frequency is multiplied per layer is the lacunarity (from the Latin for "gap"), and how much the amplitude is multiplied is the gain. These two change the texture of fBm dramatically.

  • A higher gain (0.6–0.7) keeps the fine layers strong, giving a gritty texture like rock
  • A lower gain (around 0.3) lets the fine layers fade out fast, giving a soft, hazy texture
  • A higher lacunarity widens the size gap between layers, so the big shapes and fine grain separate clearly

Changing the gain also changes the total strength. With a gain of 0.7, for example, the total approaches 1.4 and the result goes past 1. So we also keep a running total of the strengths and divide by it at the end, v / total, which keeps the result within 0 to 1 whatever the gain.

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

Starts when it scrolls into view
Open in /edit ↗
▸Set gain to 0.7 for a rocky look, or 0.3 for a soft one. What happens with lacunarity at 3.0?

05Rotate and shift each layer

The fBm so far has a subtle weakness. Since we only ever double the coordinates, every layer's grid faces the same way, and their lattice points coincide. A lattice point of the first layer (where x and y are whole numbers) is still a lattice point in the second layer after doubling, and in the third after doubling again.

The most obvious spot is the origin, (0, 0). It's a lattice point in every layer, and hash21(vec2(0.0)) happens to be exactly 0. So at the origin every layer is 0. What's more, around a lattice point every layer sits on the flat part of its smooth curve (where the slope is 0), so even the fine bumps vanish. The result is a dark, featureless pit in the middle of the screen. Elsewhere, the horizontal and vertical habits of the grid pile up layer on layer and tend to show as streaks.

The fix is to rotate and shift the coordinates a little for each layer. Rotating by 0.5 radians (about 29 degrees) each time with rot(a) from Moving space points every layer's grid in a different direction. Adding an arbitrary offset like vec2(17.0, 9.0) also stops their lattice points from lining up.

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

Starts when it scrolls into view
Open in /edit ↗
▸Find the smooth, dark patch with no fine detail in the middle of the screen. Then put // in front of p = p * 2.0; and remove the // from the line after it. The featureless patch is gone, and there's detail everywhere

Here is the fbm this course uses from now on. It has five layers and returns roughly 0 to 1. Whenever you use it, include rot, hash21 and noise in your code as well.

1

2

3

4

5

6

7

8

9

10

float fbm(vec2 p) {

float v = 0.0;

float a = 0.5;

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

v += a * noise(p);

p = rot(0.5) * p * 2.0 + vec2(17.0, 9.0);

a *= 0.5;

}

return v;

}

Tip

rot(a) * v rotates the vector v counterclockwise by a radians. For fBm all that matters is that the directions differ, so the exact angle and direction don't matter. Any angle well away from a multiple of 90 degrees, such as 0.5, will do.

06Clouds

Clouds are the classic use of fBm. Treat the fBm value as cloud density and use smoothstep to split it into clear sky and cloud. In the example below, anything below 0.42 stays blue sky, anything above 0.72 is solid cloud, and values in between blend smoothly. Adding time to x lets the wind carry the clouds along.

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 ↗
▸Change 0.42 to 0.3 for an overcast sky, or 0.55 for scattered clouds (keep it below 0.72). Change p * 1.2 to p * 3.0 for lots of little clouds

As it is, the clouds look flat. To give them volume, we fake the lighting. Assume the sun is up and to the left, and look up the density at a spot shifted slightly toward the sun. If that spot is thinner than the current point, the current point is on the sunlit side of the cloud, so brighten it; if it's thicker, the point is in the cloud's shadow, so darken it. It isn't real lighting, but one extra fbm call makes the clouds far more convincing.

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 ↗
▸Change vec2(-0.03, 0.06) to vec2(0.03, -0.06) and the light seems to come from the lower right. Change 8.0 to 20.0 for stronger shading

07Mountain ranges

In Smooth noise we could only make round hills. Use one row of fBm as the ridgeline and you get rugged mountains. Then loop to draw five ranges from back to front, letting each nearer range paint over the ones behind it, and you get a landscape of ridges layered one behind another.

  • Farther ranges sit higher on the screen, are broader, and scroll more slowly
  • Farther ranges are closer to the sky color, nearer ones darker. Distant things are seen through more air, so they fade into the sky
  • Each range reads a different row of the fBm via fi * 10.0, so their shapes don't match

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 0.55 in 0.55 * (…) to 1.0 for a jagged range. Change the 0.2 in fi * 0.2 to 0.1 to pack the ranges closer together

Each pixel calls fbm five times (25 noise calls), so this picture is heavier than the earlier ones. If you add more ranges, keep an eye on how that multiplies with the number of fBm layers.

08Fire and water

Fire starts by making the fBm pattern flow upward. Subtracting time from y, as in p.y * 2.0 - time * 1.5, moves the pattern up (adding would move it down). Next, build a base flame shape, body, that's wide at the bottom and narrow at the top, and add fBm to tear its edges into tongues. Finally, layer the colors black → red → orange → yellow → white, so that hotter parts are brighter and whiter.

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

Starts when it scrolls into view
Open in /edit ↗
▸Change the 1.4 in (n - 0.5) * 1.4 to 0.5 for a calm, candle-like flame, or 2.5 for a wild one. What about time * 4.0 instead of time * 1.5?

You can make a water surface by stretching fBm out sideways. To add depth, make the pattern finer near the horizon: water near the horizon is far away, so its waves should look small. z = 1.0 / (horizon - p.y + 0.03) is about 1 at the bottom of the screen and grows as you approach the horizon, so it works as a stand-in for how far away the water at that point is. We multiply x by z too, because distant things look narrower as well. (Rays and cameras explains this properly.)

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 vec2(0.3, 3.0) to vec2(1.0, 1.0) and the waves stop stretching, leaving blotches. Set horizon to -0.2 for more sky

09Exercises

Depending on whether you treat the fBm value as density, height or heat, the same function makes completely different pictures. In each exercise, decide what the value means first, then color it.

Exercise 1

Clouds on a moonlit night

The moon is out in the night sky. Use fBm to make clouds that drift slowly across the face of the moon. Make the clouds brighter near the moon, lit by moonlight, and darker farther away, for a proper nighttime mood.

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 ↗
Exercise 2

Lightning veins

The ridge function in the starter is currently plain fBm, so all you see is a hazy purple cloud. If you fold each layer's noise so that it's 1 at 0.5 and drops to 0 near 0 and 1 before adding it, the middle values of the noise become thin ridges, and glowing veins like lightning or plasma appear. Rewrite ridge to make the 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

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

A small planet

A blue planet covered entirely by ocean floats in space. The surface coordinates sp (longitude and latitude) and the lighting are already set up. Feed sp into fBm to color ocean, shallows, land and desert, and make the north and south poles white with ice. Time is already added to sp.x, so once there's land the planet will appear to spin. If you have time, add white clouds with a second fBm.

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

  • fBm stacks layers of noise (octaves), each twice as fine and half as strong, so detail sits on top of big shapes
  • Stack the layers with a for loop. The loop count is the number of layers; keep it to 5–6
  • Lacunarity (frequency multiplier) and gain (amplitude multiplier) change the texture. If you change the gain, divide by the total strength
  • Rotating each layer with rot(0.5) and shifting it by vec2(17.0, 9.0) removes grid artifacts and the pit at the origin
  • Read the value as density and you get clouds, as height and you get mountains and planets, as heat and you get fire

Next time we move on to domain warping, where fBm values bend the coordinates themselves. Just feeding fBm into fBm produces patterns like smoke, marble and flowing liquid.

Finished lessons get a mark in the course contents