Shaders · Chapter 5 of 11

Shaders: Shapes

Written for version 2.7.0

Draw bars, rectangles, circles, rings, squares, slices and flowers from nothing but position, then combine and colour them.

The last chapter turned values into colour: three numbers for red, green and blue, Mix (Lerp) and Color Ramp to blend them, and a set of nodes to adjust them. This chapter draws shapes: bars, rectangles, circles, rings, squares, slices and flowers. It draws them from nothing but position, with the Math nodes you already know. This path builds its shapes from maths nodes so that you can see how each one works and bend it into a shape of your own.

A shape is a question

A shader runs once for every pixel, and no pixel can see its neighbours. So you cannot tell a shader to draw a circle in the middle. What you can do is ask every pixel the same question, such as "how far am I from the centre?". Each pixel answers with a number of its own. Then you cut that number with an edge, using Step for a hard edge or Smoothstep for a soft one: the pixels on one side are in the shape, and the rest are out. The shape that comes out is a value too, 1 inside and 0 outside, so shapes can be multiplied, subtracted and mixed like any other value.

A bar: two edges

Start with the simplest question: how far across am I? That is U. The UV Coord node, in the Input folder, gives each pixel its position, and Split Vector, in the Channels folder, takes it apart: X is U and Y is V. One Step turns on at the left side of the bar. A second Step stays on until the right side. Multiply the two and only the pixels between the sides are left.

The top Step turns on where U reaches its A. The bottom Step stays on until U passes its B. Multiply keeps the pixels where both are 1: a bar. Move both wires from X to Y and the same questions are asked of V, so the bar lies down.
  • Step answers 1 where B is at least A. In the top Step the edge is typed into A and U is wired into B, so it is 1 from that edge onwards. In the bottom Step it is the other way round: U goes into A and the edge is typed into B, so it is 1 up to that edge.
  • Multiplying two shapes means "both". 1 times 1 is 1, and anything times 0 is 0.
  • The plot under the square shows the value across the middle: 0, then 1 for the width of the bar, then 0 again. For the lying bar it is a flat line at 1, because the middle row is inside the bar all the way across.
  • Wire Y in place of X and the bar is measured along V. On a face of a cube V runs upwards, as it does in these pictures. On a sphere and on a layer V runs downwards, so the same bar is measured from the top.

A rectangle: two bars

A rectangle is the place where an upright bar and a lying bar cross. Build one bar from X and one from Y, then multiply the two bars together. That is nine nodes, which is a lot, but every side is a number you can see and move. Further down there is a shorter way to make a square, and it starts from the circle.

The top two Steps make an upright bar from U, the bottom two a lying bar from V. Multiply joins each pair, and a last Multiply keeps only the pixels inside both bars. Each of the four Steps sets one side.

A circle: distance from the centre

The question for a circle is "how far am I from the centre?". It takes two moves to ask it. UV counts from a corner, so first move zero to the middle: take 0.5 away from both U and V, and the centre of the square becomes 0, 0. Then the Length node, under Math in Geometric, measures how far each pixel is from 0, 0.

Subtract moves zero to the middle and Length measures how far each pixel is from it. The plot shows that distance across the square: 0 at the centre, 0.5 at the edges. Step is 1 where the distance is no more than B, the radius.
  • The distance is 0 at the centre and 0.5 at the middle of each edge. The corners are further away, at about 0.71.
  • The distance goes into A of Step and the radius is typed into B. Step is 1 where B is at least A, which here means "where the radius is at least my distance": inside the circle.
  • Swap the two, with the radius in A and the distance in B, and you get the opposite: a black disc in a white square.
  • The Distance node, in the same folder as Length, does both moves in one. Wire UV Coord into A, and into B a Construct Vector2 with X and Y set to 0.5. Change those two numbers and the centre moves anywhere you like.
Wire the 0.5, do not type it

Subtract works on U and V together, but a number typed into B only reaches the first of them. Type 0.5 there and only U is moved: the circle ends up centred on an edge of the square, and you see half of it. Add a Float node from the Parameters folder, set it to 0.5 and wire it into B. A wired Float fills every lane.

A soft circle and a glow

For a soft edge, put Smoothstep where Step was, with the distance in Value. Smoothstep is 0 below Edge 0 and 1 above Edge 1, so it is 0 near the centre and 1 far away: a black disc. One Minus after it makes the disc white.

With the distance in Value, Smoothstep is 0 inside Edge 0 and 1 outside Edge 1, and One Minus makes that a white disc. Pull the edges apart and the rim softens. From 0 to 0.5 it is a glow, and a higher B on Pow tightens it.
  • Edges close together, such as 0.29 and 0.31, give a crisp rim that is never jagged. Prefer that to Step for any circle that will be seen.
  • Edges far apart give a soft disc. With Edge 0 at 0 and Edge 1 at 0.5 the whole disc is a glow that fades from the centre to the sides of the square.
  • Pow after One Minus bends the fade. A power above 1 pulls the glow in to a tight, bright core. A power below 1 makes it fuller. For a fade with a shape of your own, use Float Curve in place of Pow.

A ring: two circles

Both Smoothsteps read the same distance. The top one turns on at the inner rim, the bottom one at the outer rim. Subtract takes the bottom from the top, so only the distances between the two rims are left at 1.

A ring is a big disc with a smaller disc taken out of it. It is also the band from the chapter on shaping values, asked of the distance and not of U: on after one edge, off again after a second. The gap between the two rims is the thickness of the ring. Bring them close together for a thin outline, which is how you outline any shape that starts from a distance.

A square: a different distance

A circle is every pixel within a certain distance of the centre, measured in a straight line. Measure the distance another way and the same cut gives another shape. For a square the question is "how far across am I, or how far up, whichever is more?".

Abs folds the centred position so every quarter reads like the top right one. Split Vector gives how far across and how far up, and Max keeps the larger. Step cuts that at B, which is half the side of the square.
  • Abs, under Math in Common, makes a negative number positive. After the Subtract, the left half of the square has a negative X and the bottom half a negative Y. Abs folds them over, so all four quarters read alike.
  • Max gives the larger of its two inputs. Along the sides of a square, one of the two numbers is always the same, which is why the cut comes out square.
  • B on Step is half the side. At 0.5 the square fills the whole picture.
  • For a rectangle, put a Multiply on X or on Y before Max. Multiplying X by 2 makes the shape half as wide.

Four more nodes round the corners. The idea is to make a smaller square inside, then keep every pixel within a short distance of it. Growing a square by the same amount in every direction rounds its corners. Half the side of the finished shape is the size plus the radius.

The Float is the size of an inner square. Subtract and Max leave only how far a pixel is outside it, and Length makes that one distance. Step keeps everything within B. A size of 0 gives a circle, a very small B sharp corners.
  1. After Abs, add a Subtract. Wire a Float node into its B, as you did for the 0.5. This Float is the size of the inner square: half its side.
  2. Add Max and wire the Subtract into A. Leave B alone: it is 0. Everything inside the inner square is now 0, and outside it only the part that sticks out is left.
  3. Add Length. It turns what is left into one number: the distance from the inner square.
  4. Add Step, with the distance in A and a radius typed into B. The radius is how round the corners are.

Shortcuts: the Gradient node

The Gradient node, in the Procedural folder, has a Type menu, and its types are the questions on this page, ready made. Use them whenever the centre of the square is where you want the shape. Spherical into Step is a circle in two nodes, but its distance is doubled, so a radius of 0.6 there is a radius of 0.3 from Length. The Wave node with its Type set to Rings draws ring after ring from the centre.

TypeWhat it givesThe same as
Linear0 at the left edge, 1 at the rightX from Split Vector
SphericalThe distance from the centre, doubled: 0 at the centre, 1 at the middle of each edgeSubtract, Length, then Multiply by 2 and Saturate
RadialThe direction from the centre as a value from 0 to 1, once roundATan2 divided by 2 PI, then Fract
Diagonal0 in the corner where U and V are 0, 1 in the opposite cornerU and V added and halved

Angles: which way am I facing?

The second question a pixel can answer about the centre is "in which direction am I?". ATan2, under Math in Trigonometry, answers it. Split the centred position, wire Y into A and X into B, and out comes an angle.

ATan2 takes Y in A and X in B and gives the direction of each pixel from the centre as an angle. The top Step keeps the angles that are at least its A. The bottom Step is a disc. Multiplied, they make a pie with a slice missing.
  • The angle is in radians. It is 0 pointing right, about 1.57 pointing towards higher V, and 3.14, which is PI, pointing left. The other half of the circle is negative: minus 1.57 is straight down in these pictures.
  • Step keeps the angles that are at least A. On its own that is a wedge that runs to the corners of the square. Multiplying it by a disc trims it to a pie.
  • For a single slice between two angles, use two Steps and a Multiply, exactly like the bar.
  • If you think in degrees, the Radians node converts them: wire a Float of 90 through it and you get 1.57. The PI node has PI, 2 PI and half of PI ready as outputs.
A and B are Y and X, in that order

ATan2 wants Y first. Wire X into A and Y into B by mistake and the picture is mirrored across the diagonal: 0 points up, not right. There is also a seam: along the line from the centre to the left edge the angle jumps from 3.14 to minus 3.14. Gradient set to Radial has its seam on the right, where it jumps from 1 back to 0.

Flowers: a radius that changes with the angle

The second Multiply sets the number of petals. Scale And Offset turns the swing of Cos into a radius: Offset is the size of the flower and Scale the depth of the petals. Step compares that radius with the distance from the centre.
  • A circle has the same radius in every direction. Let the radius depend on the direction and the circle grows petals. This graph uses both shortcuts: Radial for the direction and Spherical for the distance.
  • Gradient, set to Radial, gives the direction as 0 to 1. Multiply it by the 2 PI output of the PI node to turn that into radians, 0 to about 6.28.
  • Multiply again by the number of petals, typed into B. Keep it whole: with 4.5 the last petal does not meet the first, and a cut shows on the right.
  • Cos swings between minus 1 and 1, once for every petal. Sin in its place gives the same flower turned a little.
  • Scale And Offset turns the swing into a radius. Offset is the size of the flower and Scale is how deep the petals are cut. A Scale of 0 gives a plain circle.
  • A second Gradient, set to Spherical, is the distance. Step is 1 where the radius is at least the distance.

Putting shapes together

Because a shape is a value, one node is enough to join two of them. Here are two discs, each made with Distance from its own centre, and three ways to combine them.

Two discs, each a Distance from its own centre cut by a Step. Max keeps a pixel that is in either disc. Min keeps one that is in both. Subtract takes the second disc out of the first, which leaves a crescent.
NodeWhat you getIn words
MaxEvery pixel that is in either shapeA or B
MinOnly the pixels that are in both shapesA and B
MultiplyThe same as Min for hard shapes. For soft shapes the overlap fades fasterA and B
SubtractThe first shape with the second cut out of it. Where a pixel is only in the second shape it gives minus 1, which looks black but upsets a Mix or a Multiply after it: follow it with SaturateA but not B
One MinusEverything outside the shapeNot A
AddBoth shapes, but twice as bright where they overlapUse Max for a clean join

Colouring a shape

The shape goes into T of Mix (Lerp): 0 inside the disc, 1 outside. Colour A shows where T is 0 and B where it is 1, so A paints the disc and B the ground. Widen the Smoothstep and the two colours blend across the rim.
  • A shape is a value from 0 to 1, and that is exactly what T on Mix (Lerp) wants. Wire two Color nodes into A and B and the shape into T: one colour fills the shape and the other fills the ground.
  • Mix gives A where T is 0 and B where T is 1. A shape that is 1 inside, like the ones made with Step on this page, takes colour B. Swap the two wires, or add One Minus, to turn it round.
  • A soft edge blends the two colours, so a glow in T gives a coloured glow.
  • For more than two colours, chain the Mixes: the output of the first goes into A of the second, with a second shape in its T. Each Mix paints one more shape on top.
  • A Color Ramp reading the distance colours rings with as many stops as you like.

Now you try

  1. A frame. Build the square, then add a second Step on the same Max with a smaller B. Subtract the small square from the large one.
  2. A crescent moon in colour. Build the two discs with Distance, set the second centre to 0.6 and 0.55, Subtract, then Saturate. Wire the result into T of a Mix with a dark blue in A and a pale yellow in B.
  3. A sun. Build the flower with 12 petals and a Scale of 0.08. Add a glow from the soft circle graph, and join the two with Max.
  4. One slice of a pie. Use two Steps on the angle, the first with 0.5 typed into A and the angle in B, the second with the angle in A and 1.5 typed into B. Multiply them together, then multiply by a disc.
Something unclear?

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

Send Feedback