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.

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.

Elapsed Time straight into Color passes 1 in its first second, and the square is white from then on. Fract, wired in between, keeps only the part after the decimal point: the value climbs from 0 to 1 and starts again every second.
  • 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.
Do not count on what the clock reads

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 turns the climbing clock into a smooth swing between minus 1 and 1. Half of that is below 0, so the square sits black half the time. Scale And Offset fits it: Scale 0.5 halves the swing, Offset 0.5 lifts it, and the value now runs from 0 to 1.
  • 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.

Multiply the clock before you shape it. Ping-Pong climbs to 1 and falls back, and with B at 1 the round trip takes two seconds. With B at 3 the clock runs three times as fast and so does the bounce. With B at 0.4 it slows right down.
ShaperWhat the value doesOne cycle takes
FractClimbs from 0 to 1, then drops back at once1 second
Ping-PongClimbs from 0 to Scale, then falls back at the same rateTwice Scale, in seconds
SinSwings smoothly between minus 1 and 1About 6.28 seconds
WrapClimbs from Min to Max, then drops back at onceMax minus Min, in seconds
Multiply before, not after

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.

The fitted sine, running from 0 to 1, goes into T of Mix (Lerp), with a Color node in A and another in B. The square breathes from one colour to the other and back. Change either colour and the pulse carries on between the new pair.
  • 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.

Scroll Coords moves the coordinates it passes on by Speed every second, and the Checker that reads them slides. Speed X is across and Speed Y is up and down. No Elapsed Time node is needed: Scroll Coords reads the clock itself.
It slides the other way

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:

  1. Add Elapsed Time and a Multiply. Wire Seconds into A and type a speed, such as 0.2, into B.
  2. Add Construct Vector2, from the Channels folder. Wire Multiply's Out into X and leave Y at 0.
  3. Add Offset Coords, from the Coordinates folder. Wire Construct Vector2's Out into Offset.
  4. 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.

Rotate Coords turns the coordinates by Angle about Center. With the clock wired into Angle, the angle keeps growing and the checker keeps turning. Multiply sets how fast, and a negative B turns it the other way.
  • 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.

Simplex Noise 3D reads a position of three numbers. X and Y come from the UV, split apart and rebuilt by Construct Vector3. Z is the clock, slowed by Multiply. The pattern churns in place without sliding, and a Color Ramp makes it fire, water or cloud.
  • 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.

Step answers 1 where B is at least A. A, the edge, is the clock slowed down and bounced by Ping-Pong, so it glides from 0 to 1 and back. With Gradient in B a straight line wipes across. With Noise in B the same edge eats the square away in patches.
  • 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.

With only the clock going into Ping-Pong, the whole square blinks as one. Random Noise holds a different number for every tile. Add it to the clock and each tile is at a different point of the same bounce, so they twinkle.
  • 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

WhereWhat Elapsed Time reads
The Rig Editor viewportSeconds 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 EditorThe clock at the moment you take it. Two snapshots of a moving material will differ
A SceneThe 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 SceneEach 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.
Under the Ray Traced engine

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.

  1. 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.
  2. 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.
  3. 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.
  4. Add Simplex Noise 3D from the Procedural folder. Wire the Add's Out into Position and set Scale to 2.5.
  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.
  6. Wire the Color Ramp's Color into Emissive on PBR. The cracks light up and start to crawl.
The finished material. World Position plus the slowed clock goes into Simplex Noise 3D. A Color Ramp turns the noise into black crust and bright cracks and feeds PBR's Emissive, so the cracks give off light. A near black Color is the rock.
  • 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

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

Tell us which picture or step lost you, what you built, and what moved, or did not, compared with what you expected.

Send Feedback