LearnPatterns and noise

Lesson 12 of 30 · 50 min

Smooth noise

Interpolate random values into noise for wobbly shapes and terrain lines

In this lesson you will

  • Build 1D noise by placing random values at whole numbers and joining them with mix
  • See why straight-line joins leave corners, and how f * f * (3.0 - 2.0 * f) smooths them out
  • Explain how the 2D noise function blends the values at a cell's four corners
  • Scale noise, make it flow over time, and use it to wobble shapes
  • Draw hill silhouettes and map-like color bands with noise

01From jumpy to smooth

The per-cell random numbers from Making randomness have nothing to do with their neighbors. That suits mosaics and confetti, but not the thickness of a cloud or the height of a hill. Natural things behave like this: nearby places are similar, and faraway places are unrelated. Step one pace inside a cloud and its density hardly changes, but ten kilometers away the sky might be clear.

Noise is how we build numbers with that property. The idea is simple: put random values only at the whole numbers, and fill the gaps in between by joining them smoothly. In this lesson we first work it out along a single line (1D), then extend it to the plane (2D).

021D noise: connecting the dots

With x as the horizontal axis, there are three steps. floor(x) gives the whole number to the left, i, and fract(x) gives how far past it we are, f (0 to 1). Next, make random values a and b at the two whole numbers i and i + 1.0. Finally, mix(a, b, f) returns a when f is 0, b when it is 1, and a blend of the two in between. hash21 takes two numbers, so in 1D we call it as hash21(vec2(i, 0.0)), with y fixed at 0.

The preview below graphs the result. The orange dots are the random values at the whole numbers, and the white line is what fills the gaps. The drawing part uses the same approach as Shaping functions, so focus on the first seven lines of render.

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 float u = f; to float u = 0.0; and the line turns into a staircase. That's exactly what the per-cell random numbers from last lesson look like

Going from the u = 0.0 staircase back to u = f, the steps get joined by sloping lines. Joining points with straight lines like this is called linear interpolation (interpolating means filling in between). The values no longer jump, so we now have the "nearby places are similar" property.

03Rounding the corners

Look closely at the graph, though, and the line kinks at every whole number, because the slope on the left differs from the slope on the right. In clouds or terrain those kinks show up as creases. So instead of passing f straight to mix, we first run it through a curve that moves slowly near 0 and 1.

That curve is f * f * (3.0 - 2.0 * f). Plug in some numbers: at f = 0.1 it gives 0.028, at 0.25 it gives 0.156, at 0.5 it gives 0.5, and at 0.75 it gives 0.844. The middle stays put, while the ends hug 0 and 1. The curve has zero slope (perfectly flat) at both 0 and 1, so at every whole number the line coming in from the left and the line leaving to the right meet flat, and the kink disappears. In fact, this is the very formula inside smoothstep(0.0, 1.0, f).

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
▸The gray straight line kinks sharply at 0 and at 1, while the orange curve joins up flat there. Rewrite eased as smoothstep(0.0, 1.0, f) and you get the same curve

Now let's put this curve into the 1D noise. Only the u line has changed.

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 ↗
▸Now it's a row of gentle hills. Since time * 0.5 is added to x, the graph scrolls to the left. Change 2.5 to 8.0 for a finer wave

042D noise: blending four corners

To extend this to the plane, we put random values at the lattice points: the points where grid lines cross, where both x and y are whole numbers. The value at any point is a blend of the random values at the four corners of the cell around it. Here is the noise function we'll use throughout the course.

1

2

3

4

5

6

7

8

9

10

float noise(vec2 p) {

vec2 i = floor(p);

vec2 f = fract(p);

vec2 u = f * f * (3.0 - 2.0 * f);

float a = hash21(i);

float b = hash21(i + vec2(1.0, 0.0));

float c = hash21(i + vec2(0.0, 1.0));

float d = hash21(i + vec2(1.0, 1.0));

return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);

}

  • i = floor(p) is the cell's bottom-left corner, and f = fract(p) is the position inside the cell (both x and y from 0 to 1)
  • u applies the smooth curve from above to x and y separately
  • a, b, c and d are the random values at the bottom-left, bottom-right, top-left and top-right corners
  • The last line first blends a with b along the bottom edge and c with d along the top edge, then blends those two results from bottom to top

At the exact center of a cell, for example, u is (0.5, 0.5), so the answer is the average of the four corners. Right on a corner, it's that corner's random value. Because it's always a blend, the result stays within 0 to 1, but extreme values like 0 or 1 are rare, and values near 0.5 are common.

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 ↗
▸Random values sit where the lines cross, and everything in between is blended. Change grid * 0.5 to grid * 0.0 to hide the lines. Inside noise, change vec2 u = … to vec2 u = f; and star-shaped artifacts appear along the grid

Tip

Because it places values at the lattice points, this is called value noise. There's also Perlin noise, which places slopes at the lattice points, and simplex noise, which uses a triangular grid. They look a bit more natural but take more code. Value noise is plenty for the works in this course.

05Zoom and flow

What noise looks like depends on the coordinates you feed it. noise(p * 2.0) gives big swells, about 4 cells across the screen's height; noise(p * 10.0) gives fine mottling, 20 cells across. Add time to the coordinates and the pattern flows; scale x and y differently and the pattern stretches in one direction.

Watch the direction of the flow. With noise(p * 3.0 + vec2(time, 0.0)), the pattern moves left. The spot that each point p looks at slides to the right over time, so on screen the pattern appears to move left (just like in Moving space, where moving the coordinates moved the shape the opposite way). And adding a single number to a vec2, as in p * 3.0 + time, adds it to both x and y, so the pattern drifts diagonally.

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 ↗
▸Change p * 3.0 to p * vec2(1.0, 8.0) for a pattern stretched sideways, like a water surface. Change vec2(time * 0.4, 0.0) to vec2(0.0, -time * 0.4) and it flows upward

p * vec2(1.0, 8.0) stretches the pattern sideways because only the y direction gets 8 times finer. In x a cell is a full unit wide (half the screen's height), but in y it's only 1/8 tall, so you get lots of long horizontal streaks stacked up.

06Wobbling shapes with noise

Noise isn't just for textures; it's also great for making shapes wobble. The circle from Circles and distance was length(p) - r. Make the radius r vary a little with direction, and you get a squishy, shape-shifting blob that looks almost alive.

To vary noise by direction, you might reach for the angle atan(p.y, p.x) from Polar coordinates. But feed that straight into noise and the values don't match up on the left, where the angle jumps from -π to π, leaving a seam. Instead, feed noise normalize(p): an arrow pointing the same way as p but with length 1, in other words a point on the unit circle. Going all the way around brings you back to the same point, so there's no seam.

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 ↗
▸Change the 1.5 in dir * 1.5 to 3.0 for more bumps. Replace noise(dir * 1.5 + time * 0.5) with noise(vec2(atan(p.y, p.x) * 2.0, time * 0.5)) and you'll see a seam on the left

The 1.5 in dir * 1.5 is the radius of the circle we trace through the noise. A bigger circle passes through more cells on the way around, so you get more bumps. + time * 0.5 slides that whole circle through the noise, so the shape keeps slowly changing.

Tip

After the wobble, length(p) - r is no longer the exact distance to the edge. But as long as the wobble is modest it's close enough, and works fine for smooth edges and soft glows.

07Hill silhouettes

A 1D noise graph already looks like a ridgeline. If p.y is below the ridge height, it's mountain; above, it's sky. To get 1D noise out of the 2D noise, fix y at some number and use just that one row. A different y gives a different row, and so a differently shaped ridge, so we use one number for the far hills and another for the near ones.

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 ↗
▸Change p.x * 2.5 to p.x * 6.0 and the near hills get narrower. But however fine you make them, they stay round hills; they never turn into rugged mountains

The nearer hills scroll faster, which gives a sense of depth, like watching from the window of a moving train. (Far things appearing to move more slowly is called parallax.) The colors help too: the far hills are close to the sky color and the near ones are dark, because the farther away something is, the more air you see it through, so it fades into the sky.

Real mountains, though, have small ridges and rocky bumps layered on top of their big shapes. A single noise only has swells of one size, so it always gives round hills. Layering noise of different sizes solves this, and that's fBm, the topic of the next lesson.

08Choosing colors with noise

Think of the noise value as height, and coloring by height gives you a map. Below some level it's sea; a little higher, beach; higher still, grass, then rock, then snow. step(threshold, h) is 1 when h is at or above the threshold, so combined with mix, it lets you paint over the colors one level at a time, from the bottom 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

Starts when it scrolls into view
Open in /edit ↗
▸Set sea to 0.4 and the land spreads; set it to 0.6 and only small islands remain. Noise favors values near 0.5 over the extremes, so snowy peaks are rare

Another approach is the contour lines you used for distances in Circles and distance. The fractional part of h * 12.0 wraps back to 0 every time the height rises by 1/12. Draw a line wherever it wraps and you have the contours of a map. Color by steps with floor(h * 12.0) and you get terraced bands, like rice paddies on a hillside.

To keep the lines an even width, we use a function called fwidth. fwidth(k) tells you how much k changes when you step to the neighboring pixel. Dividing the distance to a contour by it turns that distance into pixels, so the lines come out the same width on steep and gentle slopes alike.

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 both 12.0s to 24.0 for denser contours. Replace band with h in the two smoothstep calls and the colors become smooth instead of stepped

09Exercises

The trick in this lesson is choosing where to read the noise: around a circle, along a single row, or at a shifted spot. Keep those in mind as you work through these.

Exercise 1

Wobbling rings

There's a thin ring in the middle of the screen. Make its radius wobble with noise depending on direction, so its shape keeps changing over time. If you can, layer three rings with different seeds and colors, so they look like glowing loops twisting around each other.

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

A single island

The starter is a map colored by noise height, and right now there's land all over the screen. Change it so there's one big island in the middle of the screen with sea all around it. Keep the coastline ragged from the noise.

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

Aurora

A straight band of green light hangs in the night sky. Make it undulate from side to side using noise, with its shape slowly changing over time. Give the band a crisp lower edge that fades out softly toward the top, and it will start to look like an aurora curtain.

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 ↗

10Recap

  • Noise puts random values only at whole numbers (lattice points in 2D) and joins them smoothly in between
  • Straight-line joins leave kinks. f * f * (3.0 - 2.0 * f) has zero slope at both ends, so the joins are flat
  • 2D noise blends the four corner values with mix, left to right and then bottom to top. The result is 0 to 1, mostly near 0.5
  • Multiplying the coordinates sets the size and adding time makes it flow. To wobble a shape without a seam, read the noise around a circle with normalize(p)
  • Treat noise as height: thresholds turn it into a map, and fract turns it into contour lines

Next time we stack layers of noise at different sizes, a technique called fBm, to turn round hills into rugged mountains and blotches into fluffy clouds.

Finished lessons get a mark in the course contents