Shaders · Chapter 8 of 11
Shaders: Time and Motion
Written for version 2.7.0
Elapsed Time is a number that keeps growing. Shape it, scale it and feed it into what you already know to make shaders pulse, slide, spin, flow and dissolve.
The last chapter made randomness you can control: static, smooth noise, cells. Everything so far has held still. This chapter makes it move, and it needs only one new idea. Elapsed Time is a number that keeps growing, and everything that moves is that number fed into something you already know.
Controlled randomness: static, smooth noise, layers of detail and cells.
A number that keeps growing
Elapsed Time is in the Input folder. It has one output, Seconds, and no inputs. Every pixel gets the same number from it, and that number is the time on a clock that only counts up: 1 after one second, 60 after a minute. On its own that is close to useless. Chapter 3 said that anything above 1 shows as white, and the clock is above 1 for all but its first second. So the first job is always the same: fold the endless climb into a range you can use. Fract is the simplest fold.
- The plot in this chapter is different from the one in earlier chapters. It runs through time, not across the square: the right edge is now, the left edge is four seconds ago, and the yellow dot is the value this instant.
- Fract of the clock is called a sawtooth, after the shape of its plot. It rises steadily for one second, then drops straight back to 0.
- The whole square shows one grey, because nothing in this graph depends on position. Every pixel is told the same time.
You rarely know what number the clock holds at a given moment, and you should not need to. Build every animation so that it repeats, with Fract, Sin or Ping-Pong, and it will look right whenever it is seen.
A smooth back and forth: Sin
A sawtooth snaps back, which suits a loading bar but not a heartbeat. For something that swells and fades, use Sin, under Math in the Trigonometry subfolder. Chapter 3 mentioned it as the raw form of a wave. Feed it the clock and you get a wave through time.
- Sin swings between minus 1 and 1. Almost everything you will wire it into wants 0 to 1, so the Scale And Offset that follows, with 0.5 and 0.5, is nearly always there.
- To fit the swing into some other range, use Remap in place of Scale And Offset. Set In Min to minus 1 and In Max to 1, then type the range you want into Out Min and Out Max: 0.2 and 0.8, say, for a glow that never goes fully out.
- One full swing of Sin takes about 6.28 seconds. Cos is the same swing, a quarter of a cycle ahead.
Speed
To change how fast something moves, change how fast its clock runs. Put a Multiply between Elapsed Time and whatever shapes it. Above 1 speeds things up. Between 0 and 1 slows them down.
| Shaper | What the value does | One cycle takes |
|---|---|---|
| Fract | Climbs from 0 to 1, then drops back at once | 1 second |
| Ping-Pong | Climbs from 0 to Scale, then falls back at the same rate | Twice Scale, in seconds |
| Sin | Swings smoothly between minus 1 and 1 | About 6.28 seconds |
| Wrap | Climbs from Min to Max, then drops back at once | Max minus Min, in seconds |
A Multiply before the shaper changes the speed. A Multiply after it changes the height of the swing and leaves the speed alone. If your pulse got dimmer when you wanted it slower, the Multiply is on the wrong side.
Pulsing
A value that rises and falls between 0 and 1 can drive anything that takes a number. The plainest use is a brightness, which the last three pictures showed. The next plainest is the T of a Mix (Lerp): 0 gives A, 1 gives B, and a value that swings between them blends back and forth.
- A value: wire the pulse into any number input. A Smoothstep's edge grows and shrinks a shape. A Noise's Scale makes a pattern breathe.
- A colour: Mix (Lerp) between two Color nodes, as here, or wire the pulse into Fac of a Color Ramp to travel through several colours.
- A glow: on a material, wire the pulse into PBR's Emissive Strength and a colour into Emissive. The surface gives off that colour, brighter and dimmer in time. The Shader FX guide linked at the foot of this page builds exactly that.
Sliding a pattern
Chapter 1 showed that nothing in a shader really moves. Each pixel only gives a different answer as the clock runs. To slide a pattern, keep adding to the coordinates it reads. The Coordinates folder has a node that does this in one step: Scroll Coords.
A positive Speed X moves the pattern to the left, not the right. Scroll Coords adds to the coordinates, so each pixel reads the pattern from further along, and the pattern appears to come towards it. Use a negative number to go the other way.
Scroll Coords always moves at a steady speed. To slide in some other way, build the same thing yourself with Offset Coords, which adds whatever you wire into Offset:
- Add Elapsed Time and a Multiply. Wire Seconds into A and type a speed, such as 0.2, into B.
- Add Construct Vector2, from the Channels folder. Wire Multiply's Out into X and leave Y at 0.
- Add Offset Coords, from the Coordinates folder. Wire Construct Vector2's Out into Offset.
- Wire Offset Coords' UV into the UV input of your pattern.
That does what Scroll Coords does. The gain is the gap you now have between the clock and the offset. Put a Sin there and the pattern sways from side to side. Put a Ping-Pong there and it slides out and back.
Spinning
Rotate Coords, in the same folder, turns the coordinates by an angle. Wire a growing number into Angle and the pattern spins.
- Angle is in radians, where a full turn is about 6.28. With B at 1 the pattern takes about 6.28 seconds to go once round.
- Center is the point everything turns about. It starts at 0.5, 0.5, the middle of the square.
- Swirl Coords takes a clock the same way, into Strength, and winds the middle of the pattern up like a spring.
Flowing noise
Fire, water and cloud are noise that moves. There are two ways to move it, and they look different. Scroll it, with Scroll Coords into the Noise node's UV, and the whole pattern drifts across like cloud on the wind, keeping its shape. Or change the noise itself, so that it boils in place. Boiling needs the Simplex Noise 3D node from the last chapter. It reads a position of three numbers. Give it the UV for the first two and the clock for the third, and the square becomes a flat slice through a block of noise, with the slice travelling steadily through the block.
- Split Vector takes the UV apart into X and Y. Construct Vector3 builds the three number position: X and Y go straight across, and the clock goes into Z.
- Multiply's B is how fast the pattern boils. Fire is quick, around 0.5. Cloud is slow, around 0.05.
- Do both for the most convincing result. Add a slowly growing number to Y as well and flames climb while they flicker.
- On a 3D model, start from World Position in place of the UV. The worked material at the end of this page does that.
A moving edge
Step cuts a value in two at an edge. Chapter 3 moved the edge by hand. Wire a shaped clock into it and the edge moves by itself. What it sweeps across depends on the value on Step's other input.
- On Step, A is the edge and B is the value being tested. The animated number goes into A.
- The shape of the wipe is the shape of the value in B. Set Gradient's Type to Spherical and a circle opens from the middle. Set it to Radial and the edge sweeps round like a clock hand.
- Noise in B gives a dissolve. Noise sits in the middle of its range, so the patches come and go while the edge is passing through the middle and nothing changes at the two ends of the bounce.
- Use the result as a mask, as in the last chapter: into T of a Mix (Lerp) to change one colour for another, or into Surface Output's Opacity to make the surface itself come and go. The Shader FX guide has a recipe for cutting a surface away with Opacity.
Not all together
Feed every pixel the same clock and everything moves in step, which looks mechanical. To break it up, give each place a clock that is a little ahead or behind. That takes one Add: the clock, plus a number that differs from place to place.
- Multiply's B is how far apart the tiles are spread in time. At 0 they all blink together again. At 2, a full round trip of this Ping-Pong, they are spread across the whole cycle.
- A tile number does it in an orderly way. Multiply Gradient's Out by 5 and follow it with Floor: the five columns now hold 0, 1, 2, 3 and 4. Multiply that by 0.2 and add it to the clock, and each column runs a step behind the one before, so the blink travels along the row.
- A noise value does it softly. Add Noise's Out, multiplied up, to the clock and neighbouring spots are nearly in step while distant ones are not: a shimmer.
- A distance makes ripples. Multiply the Out of a Gradient set to Spherical by 6, subtract the clock from it, and send the result through Sin. Rings spread out from the middle.
Delta Time
Next to Elapsed Time in the Input folder is Delta Time. It is the time since the previous frame was drawn, a small number such as a sixtieth of a second. It is there for effects that need to know how long a frame lasts. A shader keeps no memory from one frame to the next, so it cannot add those small steps up into movement. Almost every effect you build wants Elapsed Time.
When the clock runs in Kilona
| Where | What Elapsed Time reads |
|---|---|
| The Rig Editor viewport | Seconds since the editor opened. It runs the whole time, whether or not you are touching anything. There is no play button: an animated material moves while you work |
| A snapshot from the Rig Editor | The clock at the moment you take it. Two snapshots of a moving material will differ |
| A Scene | The playhead's position in seconds. The material moves when the Scene plays or when you drag the playhead, and holds still when it is stopped |
| A render of a Scene | Each frame gets its own time, worked out from its frame number, so a render comes out the same every time |
- In a Scene, Delta Time is the length of one frame.
- Scroll Coords reads the same clock as Elapsed Time, so everything above applies to it too.
- Because the Rig Editor's clock starts when the editor opens, a material will not be at the same point of its cycle each time you come back to it.
Elapsed Time works under Ray Traced, but a ray traced picture is built up over a few seconds, and all of it is traced at one moment of the clock. In the Rig Editor's Ray Traced preview a moving material therefore holds still: the clock alone never starts the picture again. In a Scene, moving the playhead does. Delta Time is always 0 under Ray Traced.
A worked material: molten rock
One material that uses four ideas from this page and the last: a clock, slowed down; noise read from a position in space; a Color Ramp; and a glow. Build it in the Rig Editor on a sphere, with a new graph material, as chapter 2 showed. The material starts as a Color wired into PBR's Base Color and PBR wired into Surface Output. Keep all three.
- Tap the Color node and make it nearly black. This is the cold rock. Tap PBR and set Roughness to 0.9 so the rock is dull.
- Add Elapsed Time from the Input folder and a Multiply. Wire Seconds into A and type 0.15 into B. This is the slow clock.
- Add World Position, also from Input, and an Add. Wire Position into A and the Multiply's Out into B. The one number fills all three lanes, so every point's position drifts steadily through space.
- Add Simplex Noise 3D from the Procedural folder. Wire the Add's Out into Position and set Scale to 2.5.
- Add a Color Ramp from the Color folder. Wire Fac into Fac. Give it four stops: black at 0.4, deep red at 0.52, orange at 0.62, pale yellow at 0.75.
- Wire the Color Ramp's Color into Emissive on PBR. The cracks light up and start to crawl.
- Faster or slower lava is the Multiply's B. Bigger or smaller cracks is Scale on Simplex Noise 3D.
- More rock and less fire: move the ramp's stops to the right. The black stop is the level where the crust begins.
- A glow that swells: add a second Elapsed Time into Sin into Scale And Offset, with Scale 0.25 and Offset 0.75, and wire that into Emissive Strength on PBR.
- The lights in the scene still fall on the rock, because Base Color and Roughness are untouched. Only the cracks make their own light.
Now you try
- A radar sweep. Set a Gradient's Type to Radial. Add Elapsed Time through a Multiply with B at 0.2, add the two together with Add, and follow with Fract. A ramp sweeps round the square like a clock hand. Put a Pow after it, with B at 4, to turn it into a bright blade with a fading tail.
- Ripples. Set a Gradient's Type to Spherical and multiply it by 6. Subtract Elapsed Time from that, then Sin, then Scale And Offset with 0.5 and 0.5. Rings spread from the middle. Use Add in place of Subtract and they close in.
- A starry sky. Build the twinkling tiles, with Random Noise's Scale at 20. Follow Ping-Pong with a Pow, B at 8, so that only the very top of each bounce shows. Most tiles are dark at any moment, and each one flashes in its own time.
Shaders on a 3D model: light, roughness, metal and glow.
More recipes: a pulsing glow, a cut away surface and effects on a render layer.
Tell us which picture or step lost you, what you built, and what moved, or did not, compared with what you expected.