LearnFirst steps

Lesson 3 of 30 · 55 min

Shaping functions

Bend numbers with step, smoothstep and abs, and draw lines and graphs

In this lesson you will

  • Understand how bending a 0–1 number with a function shapes brightness and form
  • Plot y = f(x) as a thin line to see the shape of a function
  • Choose between step and smoothstep, and follow smoothstep's rule (a < b; for a falling shape write 1.0 - smoothstep)
  • Clip, fold, bend and wave numbers with min, max, clamp, abs, pow, sqrt and sin
  • Combine functions into stripes and wobbling patterns

01Changing the shape of a number

Many of the numbers in a shader live between 0 and 1: uv.x does, and so does every color component. Run such a number through a function that turns it into another 0–1 number, and the same screen can produce all sorts of brightness changes. Functions used this way are called shaping functions.

1

2

3

4

5

Starts when it scrolls into view
Open in /edit ↗
▸Try y = x * x, y = 1.0 - x or y = x * x * x instead of y = x. How does the brightness change across the screen?

With y = x * x, x = 0.5 gives y = 0.25: still dark in the middle, then brightening quickly near the right edge. y = 1.0 - x flips the direction. In every case the ends stay at 0 or 1; only the path in between changes. Learning to shape that path freely is the goal of this lesson.

02Plotting a graph

Brightness alone can be hard to read, so let's build a tool that draws the graph of y = f(x) as a line. A point is on the line when its height equals f(x). Almost no point is exactly equal, so we treat any point within 0.01 as part of the line.

To measure how big the difference is, we use abs, which drops the sign of a number: abs(-0.3) and abs(0.3) are both 0.3. So abs(q.y - y) is how far the point's height is from the graph's height. When every point answers "am I near the graph?", the line appears.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change y = x * x to y = x, y = 0.5 or y = 1.0 - x. What does the line do?

This code defines your own function, plot, before render. It has the same shape as render: returnType name(argType arg, …) { … return value; }. A function must be written before the place that uses it. q = uv * 1.2 - 0.1 is the scaling and shifting from Knowing where you are: it gives -0.1 where uv is 0 and 1.1 where uv is 1, leaving a small margin around the 0–1 square. Everything outside the square is darkened.

The background brightness is y as well, so notice that the higher the graph, the brighter the background. The brightness in the first playground and this graph are the same thing shown two ways. From here on, every new function gets a first look on this graph.

03Cutting with step

step(edge, x) returns 0.0 when x is less than edge, and 1.0 otherwise. It does in a single function what you would write with last lesson's ternary operator as x < 0.5 ? 0.0 : 1.0. For example, step(0.5, 0.3) is 0.0 and step(0.5, 0.7) is 1.0.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.5 to 0.2: where does the step move? What about 1.0 - step(0.5, x)?

The graph splits into a line at height 0 and a line at height 1, broken at 0.5. There is no vertical line joining them because step jumps from 0 to 1 instantly: no point takes a value in between.

Watch out

Mind the argument order: the edge comes first, the value being tested second. Written the other way round, step(x, 0.5) means the opposite: 1 when x is at most 0.5.

Two steps together make a band. step(0.3, x) - step(0.7, x) is 1 - 0 = 1 at x = 0.5, 1 - 1 = 0 at x = 0.8, and 0 - 0 = 0 at x = 0.2: it is 1 only between 0.3 and 0.7. And multiplying two such 0-or-1 values gives 1 only where both are 1.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
▸Remove the // on line 3. What happens to the band? Change 0.3 and 0.7 to resize it

04Smoothing with smoothstep

step's edge is so sharp that slanted or curved edges look jagged. That is where smoothstep(a, b, x) comes in: it is 0 when x is at or below a, 1 when x is at or above b, and joins them with a smooth S-curve in between.

1

2

3

// what smoothstep(a, b, x) does inside (it is built in, so you never write this yourself)

float t = clamp((x - a) / (b - a), 0.0, 1.0);

return t * t * (3.0 - 2.0 * t);

The first line turns x into a fraction (0 to 1) of the way from a to b (clamp cuts off anything outside 0–1; it shows up later in this lesson). The second line bends that fraction into an S. For smoothstep(0.2, 0.8, 0.5), the fraction t is 0.5 and so is the answer. For smoothstep(0.2, 0.8, 0.35), t is 0.25 and the answer is 0.25 × 0.25 × 2.5 ≈ 0.16. The slope is zero at both ends, so it blends into 0 and 1 without a visible seam.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Bring 0.2 and 0.8 closer, to 0.45 and 0.55. How is it different from step(0.5, x)?

The closer a and b are, the more it looks like step; the further apart, the gentler the slope. The beauty of smoothstep is that two numbers say exactly where the change from 0 to 1 begins and ends.

05smoothstep's one rule

smoothstep has one rule you must always keep: the first number, a, must be smaller than the second, b. The GLSL specification says the result is undefined when a >= b. Look back at the formula: if a equals b you divide by zero, and if a is larger the fraction runs backwards.

Undefined means different GPUs and drivers are allowed to give different answers. Many computers happen to produce a reversed S-curve, so if you accidentally write smoothstep(0.8, 0.2, x) it may look fine on your screen. Someone else's phone, though, might draw something different. Your work runs on many people's many GPUs.

When you want a shape that falls from 1 to 0, build it with a < b and then subtract it from 1.0 to flip it.

1

2

float bad = smoothstep(0.8, 0.2, x); // × a > b: the result is undefined

float good = 1.0 - smoothstep(0.2, 0.8, x); // ○ the same shape, written safely

The plot function can be smoothed the same way. abs(q.y - y) is 0 on the line and grows as you move away. Pass it through 1.0 - smoothstep(0.0, 0.012, …) and you get 1 on the line, 0 at a distance of 0.012, and a smooth fade in between. The playgrounds from here on use this smoother plot.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change the 0.012 inside plot to 0.05. What happens to the line? Then try smoothstep(0.2, 0.5, x) - smoothstep(0.5, 0.8, x) for y

Watch out

This course always writes smoothstep with a < b. From the next lesson on you will see shapes like 1.0 - smoothstep(-px, px, d), which is 1 inside a shape, over and over. When you want a falling shape, don't swap the arguments — put 1.0 - in front.

06Clipping and folding (min, max, clamp, abs)

min(a, b) returns the smaller value and max(a, b) the larger. On the graph, min(x, 0.6) flattens out at 0.6, and max(x, 0.3) never drops below 0.3. clamp(x, lo, hi) does both at once, keeping x inside lo to hi (the same as min(max(x, lo), hi)).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Try min(x, 0.6) or max(x, 0.3). What shape does min(x, 1.0 - x) * 2.0 make?

abs(x) turns negatives into positives. On a graph, everything below 0 gets folded upward. x - 0.5 is a straight line rising from -0.5 to 0.5, but abs(x - 0.5) is a V bent at 0.5. It turns "how far from 0.5" directly into a number.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Try 1.0 - abs(x - 0.5) * 2.0 (a peak). How many bends does abs(abs(x - 0.5) - 0.25) * 4.0 have?

abs works on coordinates too. With abs(p), the left half becomes a mirror image of the right half, and the bottom of the top. Whatever you draw in the top-right quarter gets folded into all four. Draw one slanted line, y = 0.8 - x, in the top-right quarter and you get a diamond.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
▸Change abs(p) back to p and you get a single line again. What about 0.8 - q.x * q.x or 0.5 - q.x * 0.3?

07Bending (pow, sqrt)

pow(x, n) is x to the power of n. On x between 0 and 1, the ends 0 and 1 stay put and only the middle bends. With n above 1 the curve sags (slow start, sharp rise at the end); with n below 1 it bulges (quick rise, then easing into 1). At x = 0.5, pow(x, 2.0) is 0.25 and pow(x, 0.5) is about 0.71. sqrt(x) is the square root, the same as pow(x, 0.5).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change 2.0 to 5.0, 0.5 and 0.2. Also check that sqrt(x) and pow(x, 0.5) draw the same line

Watch out

pow(x, n) is also undefined when x is negative (and so is sqrt). In this playground q.x goes negative in the left margin, so max(q.x, 0.0) keeps it from dropping below 0 before pow sees it. Whenever a value might be negative, add that extra step: pow(max(x, 0.0), n) or pow(abs(x), n).

pow is also handy for keeping only the bright parts. Raise a 0–1 value to a large power and almost everything except values near 1 drops to nearly 0: 0.9 to the 10th is about 0.35, while 0.5 to the 10th is about 0.001. We'll use this in the stripes and exercises below.

08Waves with sin

sin(x) is a wave that glides back and forth between -1 and 1. It repeats every time x advances by 2π (about 6.283); that repeat length is called the period. GLSL has no built-in constant for π, so you name it yourself, as in #define TAU 6.28318531 (TAU means 2π). #define name value tells the compiler to replace that name with the value everywhere; write it at the very top, outside any function.

sin(x * TAU * 2.0) repeats exactly twice as x goes from 0 to 1. On its own it spans -1 to 1, so * 0.4 + 0.5 maps it to 0.1–0.9 to fit the graph. Add - time inside and the wave flows to the right (motion gets its own lesson in Time and motion).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Starts when it scrolls into view
Open in /edit ↗
▸Change 2.0 to 5.0, and * 0.4 to * 0.1. If - time becomes + time, which way does it flow?

09Stripes

Use a function's answer as brightness across the whole screen instead of a graph line, and you get a pattern. sin(p.x * 20.0) returns to the same brightness every time p.x advances 2π ÷ 20 ≈ 0.31, giving vertical stripes. As-is they are soft, smoothly rising and falling. Pass them through step or smoothstep to sharpen them.

1

2

3

4

5

6

7

Starts when it scrolls into view
Open in /edit ↗
▸Remove the // on line 3, then on line 4, and compare. Change p.x to p.x + p.y for diagonal stripes

You can also put functions inside functions. Add a small sin that depends on p.y to the coordinate inside the main sin, and the stripes wiggle sideways. Put time in as well and they sway. A great many shader patterns are built from combinations like this.

1

2

3

4

5

6

7

Starts when it scrolls into view
Open in /edit ↗
▸What happens to the wiggle if 0.3 becomes 0.8? What about p.y * 10.0 instead of p.y * 4.0?

10A quick peek at fract and mod

To finish, two functions that make repetition. fract(x) returns only the fractional part of x: fract(2.7) is 0.7 and fract(0.3) is 0.3. As x grows, it climbs 0 to 1 again and again like the teeth of a saw. mod(x, m) is the remainder of x divided by m, repeating 0 to m (fract(x) is the same as mod(x, 1.0)).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Try abs(fract(x * 3.0) - 0.5) * 2.0 (a triangle wave). What about mod(x, 0.4)?

Repetition takes center stage in Repetition and tiles, for grids and tiles, and in Time and motion, for looping movement. For now, it is enough to remember that fract repeats 0 to 1.

11Exercises

Combine functions to make shapes and patterns. Checking shapes on the graph as you go makes it much easier.

Exercise 1

A soft band of light

Make a soft-edged band of light running across the middle of the screen. It should be brightest at the center (p.y = 0), fade as you move up or down, and vanish at p.y = ±0.3.

1

2

3

4

5

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

Mountain ridges

Make a bumpy mountain ridge about a third of the way up the screen, with mountain color below it and sky above. Then add a second, paler row of distant mountains behind it.

1

2

3

4

5

6

7

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

Shimmer on the water

On a dark blue water surface, make many thin horizontal lines of light that wobble gently. The lines should be thin and bright, with darkness between them.

1

2

3

4

5

Starts when it scrolls into view
Open in /edit ↗

12Recap

  • Shaping functions turn a 0–1 number into another 0–1 number to create brightness and form
  • Light up the points where abs(q.y - f(x)) is small and you have drawn a graph
  • step switches from 0 to 1 sharply; smoothstep does it smoothly
  • smoothstep always takes a < b; for a falling shape, write 1.0 - smoothstep(a, b, x)
  • Clip with min, max and clamp, fold with abs, bend with pow and wave with sin — and never feed pow a negative
  • Combining functions gives stripes, ridges and wobbling patterns; fract repeats 0 to 1

Next time we draw circles and rings from the distance to a center, and use the smoothstep you learned here to smooth their edges.

Finished lessons get a mark in the course contents