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.

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.

Gradient's value runs from 0 at the left to 1 at the right, shown as black to white. The plot draws the same value as a line. One Minus, wired in between, turns it round: 1 at the left and 0 at the right.
  • 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.
Half is not half as bright

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.

A Scale above 1 makes the line steeper, so white arrives sooner. A negative Offset slides the line down, so black lasts longer. Where the line leaves the plot the square has run out of range: it is pure white or pure black.
NodeWhat it does to the value
MultiplyMultiplies A by B. Above 1 the value grows faster, below 1 slower
AddAdds B to A. The whole line moves up
SubtractTakes B from A. The whole line moves down
Scale And OffsetMultiplies by Scale, then adds Offset
One MinusFlips the value: 1 minus it
NegateChanges its sign, so 0.3 becomes minus 0.3
Numbers do not stop at 0 and 1

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.

Step answers 1 where B is at least A and 0 where it is not. A is typed in as the threshold and the ramp goes into B. Drag A and the edge moves: at 0.2 most of the square is white, at 0.8 very little.
Which one is the edge?

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.

Smoothstep is 0 below Edge 0 and 1 above Edge 1, and eases between them. Pull the edges apart for a wide, soft fade. Bring them close together for a line that is nearly hard but never jagged.
  • 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 Smoothstep switches on at the left side of the band, the other at its right side. Subtract takes the second from the first, which leaves 1 only between them. Slide the edges to move the band or widen it.

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 (Lerp) gives A where T is 0 and B where T is 1. With the ramp in T, the left edge shows A and the right edge shows B. Whatever A and B are set to, the line runs straight from one to the other.
  • 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.

NodeWhat it does
SaturateHolds the value between 0 and 1. Anything lower becomes 0, anything higher becomes 1
ClampThe same, between a Min and a Max that you set
RemapStretches one range onto another. A value running from In Min to In Max comes out running from Out Min to Out Max
MinThe smaller of A and B at each pixel
MaxThe larger of A and B at each pixel
Remap does not hold the value in

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.

Pow raises the ramp to the power B. Above 1 the line sags and the square stays dark for longer. Below 1 it bulges and the square brightens early. The two ends stay put: 0 stays 0 and 1 stays 1.

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.

Float Curve reads the value through a curve set by points: X is the value coming in, Y the value going out. Two points give a gentle S. Add Point puts a third in the middle, and its Y pulls the curve up or down there.
  • 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.

Wave rises and falls across the square. Scale sets how many waves fit. Profile picks the shape of each one: Sine swells smoothly, Saw climbs and drops, Triangle climbs and falls in straight lines.
  • 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.

Multiply makes the ramp run from 0 to 4. Fract keeps only the part after the decimal point, so the ramp starts again each time it passes a whole number: one ramp becomes four. Raise B for more.
NodeWhat it does
FractKeeps the part after the decimal point, so the value wraps from 1 back to 0
ModThe remainder after dividing A by B. With B at 1 it does what Fract does
WrapWraps the value round between a Min and a Max that you set
Ping-PongClimbs to Scale and comes back down, again and again, with no sudden drop
SnapRounds the value to the nearest multiple of Increment, turning a smooth ramp into flat steps
Floor, Ceil, RoundRound down, round up, or round to the nearest whole number

Now you try

  1. 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.
  2. 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.
  3. 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.
Something unclear?

Tell us which node or picture confused you, what you built, and what the square showed instead of what you expected.

Send Feedback