Shaders · Chapter 3 of 11
Shaders: Shaping Values
Written for version 2.7.0
Bend a value that runs from 0 to 1 into hard and soft edges, bands, curves, waves and repeats, using a handful of Math nodes.
The last chapter showed where the shader graph is and how to add, wire and set nodes. This one is about the raw material of every shader: a single number that changes from one pixel to the next, and the handful of nodes that bend it into edges, bands, curves, waves and repeats. Almost everything later in this path is built from the moves on this page.
Make a graph material, add nodes, wire ports and set values.
A number is a brightness
Every picture in this chapter starts from the same node: Gradient, in the Procedural folder. It gives each pixel one number, called a value, that is 0 at the left edge of the square and 1 at the right edge. Wire a value into Surface Output's Color and you see it as a brightness: 0 is black, 1 is white.
- The square is the result: what the graph makes, worked out for every pixel.
- The small plot under it shows the same value as a line, read from the left edge of the square to the right. Height is the value: the bottom of the plot is 0 and the top is 1.
- One Minus takes a value away from 1. It is the quickest way to flip anything that runs from 0 to 1.
A value of 0.5 shows as a light grey, not a middle grey, and the gradient looks brighter than you might expect. The numbers are spread evenly, but the screen shows the low end of the range much darker than the rest. A grey that looks halfway between black and white is a value of about 0.2. Trust the plot for the numbers and the square for the look.
Scale and offset
Two things you will do to a value all the time: multiply it, which makes it change faster or slower, and add to it, which shifts the whole thing up or down. The Scale And Offset node does both in one: it multiplies by Scale, then adds Offset.
| Node | What it does to the value |
|---|---|
| Multiply | Multiplies A by B. Above 1 the value grows faster, below 1 slower |
| Add | Adds B to A. The whole line moves up |
| Subtract | Takes B from A. The whole line moves down |
| Scale And Offset | Multiplies by Scale, then adds Offset |
| One Minus | Flips the value: 1 minus it |
| Negate | Changes its sign, so 0.3 becomes minus 0.3 |
A value can be 2.5, or minus 0.75. The screen cannot show brighter than white or darker than black, so everything above 1 looks white and everything below 0 looks black. The number itself is still there, though, and the next node works on the real number, not on what you see.
A hard edge: Step
Step asks a yes or no question of every pixel. It has two inputs, A and B, and it answers 1 where B is at least as big as A, and 0 everywhere else. Type a threshold into A, wire the value into B, and the square is cut in two.
On Step, A is the edge and B is the value being tested. Wire them the other way round and you get the opposite picture, white on the left. That is often useful: it is a flip for free. Do not reach for Compare when you want a threshold: Compare answers 1 only where A and B are equal. The Logic folder has Is Greater and Is Less, which ask the same kind of question as Step in plainer words.
A soft edge: Smoothstep
A hard edge is rarely what you want. Smoothstep makes an edge with a width. It gives 0 below Edge 0, 1 above Edge 1, and eases gently from one to the other in between.
- The value you are shaping goes into the third input, Value. Edge 0 and Edge 1 are usually typed in.
- The distance between the two edges is the softness. The point halfway between them is where the fade sits.
- Two edges a hair apart, such as 0.49 and 0.51, give a crisp line with a clean edge. Prefer that to Step when the edge will be seen.
Two edges make a band
One edge splits the square in two. Two edges give you a stripe: on after the first edge, off again after the second. The first Smoothstep is 1 everywhere to the right of the band's left side. The second is 1 everywhere to the right of its right side. Take the second away from the first and only the stretch between them is left. This is how lines, rings and outlines are made later in the path: a value, two edges, one subtraction.
Mix between two values
Mix (Lerp) blends from one thing to another. It has three inputs: A, B and T. Where T is 0 you get A, where T is 1 you get B, and in between you get a blend. Wire the ramp into T and the square fades from A at the left to B at the right.
- Mix is the usual way to fit a value into a new range: from 0.25 to 0.6, say, in place of 0 to 1.
- Swap A and B and the blend runs the other way.
- A and B do not have to be single numbers. Wire two colours into them and Mix blends the colours. That is the heart of the next chapter.
Clamp and remap
Sometimes a value wanders outside the range you want, and sometimes it only covers part of it. These nodes put it back where it belongs.
| Node | What it does |
|---|---|
| Saturate | Holds the value between 0 and 1. Anything lower becomes 0, anything higher becomes 1 |
| Clamp | The same, between a Min and a Max that you set |
| Remap | Stretches one range onto another. A value running from In Min to In Max comes out running from Out Min to Out Max |
| Min | The smaller of A and B at each pixel |
| Max | The larger of A and B at each pixel |
A value below In Min comes out below Out Min, and one above In Max comes out above Out Max. Follow Remap with Saturate or Clamp when that matters.
Powers and curves
Scale and Offset keep a line straight. To bend it, raise the value to a power. Pow takes the value in A and the power in B.
Because the ends do not move, Pow is a safe way to push the middle of a value darker or lighter without losing black or white. For a bend that Pow cannot make, draw the curve yourself with Float Curve, in the Color folder.
- Each point is an X and a Y, both from 0 to 1. Add Point adds one, up to twelve, and the red minus removes one.
- The curve eases from each point to the next, so even the two points it starts with, 0 to 0 and 1 to 1, give a soft S and not a straight line.
- The value coming in is held between 0 and 1 first.
Waves
A wave is a value that rises and falls again and again. The quickest way to get one is the Wave node, in the Procedural folder, which reads the position directly and gives a value from 0 to 1.
- Type chooses Bands, which run across the square, or Rings, which spread out from the middle.
- Distortion bends the waves so they stop looking ruled.
- The Sin node, under Math in Trigonometry, is the wave in its raw form. It swings between minus 1 and 1, so follow it with Scale And Offset, with Scale 0.5 and Offset 0.5, to bring it into the range 0 to 1. One full wave takes an input change of about 6.28, and the PI node has that number ready as its second output.
Repeating
To repeat something, make its value run through the range 0 to 1 several times over. Multiply the ramp so it reaches a bigger number, then use Fract, which throws away the whole part of a number and keeps what comes after the decimal point: 2.3 becomes 0.3.
| Node | What it does |
|---|---|
| Fract | Keeps the part after the decimal point, so the value wraps from 1 back to 0 |
| Mod | The remainder after dividing A by B. With B at 1 it does what Fract does |
| Wrap | Wraps the value round between a Min and a Max that you set |
| Ping-Pong | Climbs to Scale and comes back down, again and again, with no sudden drop |
| Snap | Rounds the value to the nearest multiple of Increment, turning a smooth ramp into flat steps |
| Floor, Ceil, Round | Round down, round up, or round to the nearest whole number |
Now you try
- Each of these three uses only nodes from this page, starts from a Gradient and ends at Surface Output's Color. First, a soft dark stripe on white. Build the band from two Smoothsteps and a Subtract, then put One Minus after it.
- Three hard stripes. Multiply the ramp by 3, follow it with Fract, then with Step, typing 0.5 into A and wiring the value into B.
- A soft spotlight. Set the Gradient's Type to Spherical, so the value is the distance from the middle. Follow it with Smoothstep, with Edge 0 at 0.3 and Edge 1 at 0.8, then One Minus.
Turn values into colour: mix, ramp and adjust.
Tell us which node or picture confused you, what you built, and what the square showed instead of what you expected.