Shaders · Chapter 6 of 11
Shaders: Moving, Turning and Repeating
Written for version 2.7.0
Change where a shader looks before it draws: move, scale and turn the coordinates, then repeat them into grids, checkerboards and bricks.
The last chapter drew shapes from nothing but position: bars, rectangles, circles, rings, pie slices and flowers, and ways to combine them. This chapter leaves the shape alone and changes the position it is handed. Move, stretch, turn or repeat the coordinates, and whatever is drawn from them moves, stretches, turns or repeats.
Draw bars, circles, rings and stars from position alone.
Change where it looks
Every shape so far began by asking where the pixel is: its U and V, the two numbers called its coordinates. A shape is a rule about those numbers. A disc in the middle of the square is the rule "white if I am close to the spot where U is 0.5 and V is 0.5". The nodes in the Coordinates folder sit in front of that question. Each one takes the coordinates in on its UV input, changes them, and hands the changed coordinates out. The shape never knows. It applies its rule to the numbers it is given.
- Leave a Coordinates node's UV input unwired and it reads the pixel's own UV, so it can start a chain on its own.
- Wire its UV output into the UV input of whatever should be changed: a Gradient, a Checker, a Wave, a Sample Texture, or the maths that draws a shape.
- Coordinates nodes can be chained, one into the next. The order matters, as you will see.
- The pictures in the first half of this chapter use two stand-ins that read a UV input: a white disc (a Gradient with its Type set to Spherical, cut with Step) and a Checker with its Scale at 2, which gives four big squares.
Moving: Offset Coords
Offset Coords adds a number to U and another to V. Its Offset input holds the two numbers. Type 0.25 into the first and watch which way the disc goes.
It moves the wrong way, and this is the one surprise of the chapter. The disc is drawn where the coordinates read 0.5 and 0.5. Add 0.25 to every U, and the pixels that now read 0.5 are the ones that used to read 0.25, a quarter of the way further left. The rule did not change. The numbers slid along underneath it.
You are not moving the picture. You are changing the question each pixel asks, and the picture answers by doing the opposite. Add to the coordinates and the picture moves back. Multiply them and it shrinks. Turn them one way and it turns the other. When a result goes the wrong way, flip the sign or swap big for small.
- To move a picture right by 0.25, type minus 0.25 into the first number of Offset.
- On the square shown here V runs up, so adding to V moves the picture down. Where V runs down, as on a sphere or a layer, adding to V moves it up the screen. Chapter 1 has more on which way V runs.
- Scroll Coords is Offset Coords with a clock in it: it adds a little more every second, so the picture slides on its own. The chapter on time comes back to it.
Growing and shrinking: Scale Coords
- Scale Coords multiplies the coordinates. Scale holds two numbers, one for U and one for V, both 1 to begin with.
- Bigger scale, smaller picture. With Scale at 2 the coordinates change twice as fast as you cross the square, so the disc's rule is met and then broken again in half the distance.
- Think of Scale as how much of the picture's world fits in the square. At 2 you see twice as much, so each thing looks half the size. To make a picture twice as big, set Scale to 0.5 and 0.5.
- Scale Coords multiplies outward from Center, which starts at 0.5 and 0.5, the middle of the square. The middle stays where it is and everything else moves towards it or away.
- Set Center to 0 and 0 and it scales from the corner where U and V are both 0. That is the same as a plain Multiply, and it matters for repeating, further down.
Turning: Rotate Coords
Rotate Coords turns the coordinates about a point. Its angle input is labelled Angle (rad), and the rad is a warning: the angle is in radians, not degrees. A radian is another unit for angles, the one shader maths uses everywhere. A full turn is 2 times pi radians, about 6.28. Half a turn is pi, about 3.14. A quarter turn is half of pi, about 1.57. You do not need to do the sums, because two nodes do them for you.
- Radians, under Math in Trigonometry, turns degrees into radians. Put a Float in front of it, type degrees into the Float, and wire Radians into Angle (rad). Degrees, next to it, goes the other way.
- The PI node, under Math in Common, has three outputs: pi (half a turn), 2 pi (a full turn) and pi over 2 (a quarter turn). Wire one straight into the angle.
- On the square here, a positive angle turns the picture clockwise. The coordinates turn anticlockwise, and the picture answers by going the other way. Where V runs down, as on a layer, the same angle shows as anticlockwise.
- If a rotation looks wrong, check the unit first. An angle of 90 typed straight into Angle (rad) is 90 radians, more than fourteen full turns, and the picture ends up at an angle that looks random.
A turn needs a point to turn about. Rotate Coords has a Center input for it, set to 0.5 and 0.5, the middle of the square. This is why rotation needs a centre and moving does not: without one, the turn would be about the point where U and V are both 0, a corner of the square, and your picture would swing out of view like a door on a hinge.
All three in one: Mapping
- Mapping does the work of the three nodes above in one. It has Location, Rotation (rad) and Scale, and it always works in the same order: scale first, then rotate, then move.
- It has no Center input. Scaling and turning are always about the middle of the square, 0.5 and 0.5.
- Reach for Mapping when one picture needs placing. Use the separate nodes when you need a different order or a different centre.
- Order changes the answer. Offset Coords wired into Rotate Coords gives a different picture from Rotate Coords wired into Offset Coords. If a chain does not do what you expect, swap two of its nodes before changing any numbers.
Repeating: multiply, then Fract
Chapter 3 repeated a ramp by multiplying it and taking Fract, which keeps only the part of a number after the decimal point. Do the same to the coordinates and everything drawn from them repeats, across and up.
Follow one row. Multiplied by 3, U runs from 0 to 3 across the square. Fract turns 0.9 into 0.9, 1.2 into 0.2 and 2.5 into 0.5, so the result climbs from 0 to 1 three times. Each climb is a whole set of coordinates for the disc, squeezed into a third of the width. Three across and three up makes nine tiles. The count along each side is the number you multiplied by.
UV is two numbers and Multiply's B, typed in the inspector, only reaches the first of them. Type 3 and you get three columns and one row. To multiply both, add a Float node from the Parameters folder, set it to 3 and wire it into B. A wired Float fills every lane. For a different count across and up, use Scale Coords in place of Multiply, with Center at 0 and 0: its Scale holds one number for each direction.
Numbering the tiles: Floor
Fract throws away the whole part of the number. Floor keeps it and throws away the rest: 2.5 becomes 2. On multiplied coordinates, Floor of U is the column a pixel is in and Floor of V is its row, counting from 0. Every pixel in a tile gets the same pair of numbers, and that pair is the tile's address. Once tiles have addresses they can differ from each other. The classic case: add the column number to the row number and ask whether the total is odd or even. Mod with B at 2 gives the remainder after dividing by 2, which is 0 for even and 1 for odd.
- Split Vector, from the Channels folder, takes the pair apart so the two numbers can be added.
- Use only the row number, with Mod by 2, and you get stripes of alternating rows. The brick pattern at the end of this chapter grows from that idea.
- The Checker node in the Procedural folder is those five nodes in one: it reads UV, has a Scale for the number of squares along a side, and gives 0 or 1. Use it when a checkerboard is all you want.
- Build it by hand when you want the tile addresses for something else: a different shade for each tile, every third tile lit, a shape that turns a little more in each column.
Big pixels: Pixelize Coords
Pixelize Coords uses the same Floor idea for a different end. It snaps the coordinates to the middle of a cell, so every pixel in a cell asks about the same spot and gets the same answer. The picture turns into flat blocks.
- Cells starts at 32 and 32. Lower numbers give bigger blocks.
- It must go in front of the picture, on its UV input. It changes where the picture is read, so it cannot be added after the colour has been worked out.
- It is at its best on something smooth or detailed, such as a gradient, a noise or a texture. On flat colour there is nothing to see.
A twist: Swirl Coords
Swirl Coords is a rotation that fades with distance. It turns the coordinates most at Center, less and less further out, and not at all beyond Radius. Radius is in the same units as UV, so 0.5 reaches the middle of each edge of the square.
Printed dots: the Halftone folder
The Halftone folder turns a picture into the dots or lines of a printed page: CMYK Halftone, Circular Screen, Dot Screen, Hatched Screen and Line Screen. They belong in this chapter because each one is a repeating pattern laid over a picture, with marks that grow where the picture is dark and shrink where it is light.
- The picture goes into Color. What comes out is black and white.
- Left unwired, Coord is the pixel's place on the screen, so the dots stay the same size on screen and do not stick to the model as it turns. Wire a UV in, multiplied up to a few hundred, to fix the dots to the surface.
- Width and Center are measured in screen pixels. Sharpness hardens the edge of each dot.
- Line Screen draws lines, Hatched Screen crossed lines, Circular Screen rings about Center, and CMYK Halftone prints in four coloured inks.
A worked pattern: bricks
Here is everything from this chapter in one graph: a brick wall. Bricks are a grid in which every other row is pushed along by half a brick. Build it in this order, looking at the result after each step.
- Add Scale Coords. Set Scale to 3 and 6 and Center to 0 and 0. The coordinates now run from 0 to 3 across and 0 to 6 up: three bricks wide, six rows high.
- Wire it into Split Vector. X is how far across, Y is how far up.
- Wire Y into Floor. That is the row number: 0 for the bottom row, then 1, 2 and so on.
- Wire Floor into Multiply and type 0.5 into B. That is half a brick for every row.
- Add that to X with Add. Each row is now pushed half a brick further than the one below.
- Wire Add into one Fract and Y into another. Each brick now has its own X and Y running from 0 to 1.
- Wire both Fracts into Min. Min is small wherever the pixel is close to the left edge or the bottom edge of its brick.
- Wire Min into B of Step and type 0.08 into A. Step gives 0 close to those edges, which is the mortar, and 1 everywhere else, which is the brick.
Why does pushing every row work, when only every other row should move? Row 2 is pushed by a whole brick, and after Fract a whole brick is no push at all. Row 3 is pushed by one and a half, which looks the same as a half. So the rows alternate without the graph ever asking which are odd.
Now you try
- Six columns and three rows. Start from the tiling graph and swap Multiply and its Float for Scale Coords, with Scale at 6 and 3 and Center at 0 and 0. The discs come out as ovals, because each tile is now twice as tall as it is wide.
- Diamonds. Put Rotate Coords in front of a Checker. Feed the angle from a Float set to 45, through Radians.
- A turned tile in every cell. Tile the coordinates with Multiply and Fract, wire Fract into Rotate Coords with 0.4 typed into Angle (rad), and wire that into a Checker with Scale at 2. Every tile turns about its own middle, because inside each tile the coordinates run from 0 to 1 again.
- Stripes of alternating rows. Take UV Coord through Split Vector, multiply Y by 6, then Floor, then Mod with B at 2, and wire that into Color.
- Coloured bricks. Change the Scale in the brick graph to change how many fit, then colour the wall with Mix (Lerp) as in the last chapter: a mortar colour in A, a brick colour in B, and the Step in T.
Random values, smooth noise and the patterns they make.
Tell us which node or picture confused you, what you built, and what the square showed instead of what you expected.