TExcelTAD
Home › Shaders & Graphics Programming › Animating with iTime

Animating with iTime

A shader has no memory between frames. It cannot store "the ball is at x = 0.4, moving right" and update it next frame. Every frame it starts from scratch, with only its uniforms. So shader animation is written as a function of time: given iTime, where is everything now? This is a different mindset from a game loop, and a powerful one: you can jump to any moment, play backwards, or loop perfectly.

Oscillation with sin

sin(t) swings between -1 and 1 every 2π seconds. Remap it to the range you need and scale t for speed:

You want Expression
0..1 pulse, once per second 0.5 + 0.5 * sin(iTime * 6.28318)
a value between a and b mix(a, b, 0.5 + 0.5 * sin(iTime * speed))
a phase shift per object sin(iTime + float(i) * 0.5)
a 0..1 saw that loops every T s fract(iTime / T)
a bounce (always positive) abs(sin(iTime * speed))

Below, nine dots use the same sin with a different phase each, which turns into a wave:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float px = 1.0 / iResolution.y;
    vec3 color = vec3(0.07, 0.08, 0.12);

    for (int i = 0; i < 9; i++) {
        float fi = float(i);
        vec2 c = vec2(-0.6 + fi * 0.15, 0.25 * sin(iTime * 3.0 + fi * 0.6));
        float d = length(p - c) - 0.05;
        vec3 dotColor = 0.5 + 0.5 * cos(vec3(0.0, 2.0, 4.0) + fi * 0.5);
        color = mix(color, dotColor, 1.0 - smoothstep(-px, px, d));
    }
    fragColor = vec4(color, 1.0);
}

int and float never mix implicitly in GLSL ES: the loop counter i must be converted with float(i) before it meets 0.15. The GLSL Basics lesson shows the error you get otherwise.

Moving, rotating and scaling space

To move a shape, move the coordinates the opposite way: drawing sdCircle(p - offset, r) puts the circle at offset. Rotation works the same way: rotate p by -angle and the shape appears rotated by +angle. A 2D rotation matrix in GLSL:

glsl
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }

GLSL matrices are column-major: mat2(c, s, -s, c) fills the first column with (c, s) and the second with (-s, c). Multiplied as rot(a) * p, this rotates p counter-clockwise by a. The Matrices in Shaders lesson goes deeper.

glsl · try it yourself
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }

float sdBox(vec2 p, vec2 b) {
    vec2 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float px = 1.0 / iResolution.y;

    vec2 orbit = 0.25 * vec2(cos(iTime), sin(iTime));         // position on a circle
    vec2 q = rot(-iTime * 2.0) * (p - orbit);                  // spin around its own center
    float scale = 1.0 + 0.3 * sin(iTime * 4.0);
    float d = sdBox(q / scale, vec2(0.08)) * scale;            // scale, then fix the distance

    vec3 color = mix(vec3(0.1), vec3(1.0, 0.6, 0.2), 1.0 - smoothstep(-px, px, d));
    fragColor = vec4(color, 1.0);
}

Order matters, exactly as with transform matrices: translate the coordinates first, then rotate, so the box spins around its own center. Scaling p by 1/s scales the SDF's space, so its distances must be multiplied back by s to stay correct.

Easing and looping

sin is smooth but always the same shape. For UI-style motion (pop in, overshoot, settle), build a looping timeline with fract or mod, and push the 0..1 progress through an easing curve:

glsl · try it yourself
float easeOutBack(float t) {
    float c1 = 1.70158, c3 = c1 + 1.0;
    float u = t - 1.0;                               // negative while animating
    return 1.0 + c3 * u * u * u + c1 * u * u;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float px = 1.0 / iResolution.y;

    float cycle = mod(iTime, 2.0);                    // 2-second loop
    float t = clamp(cycle / 0.6, 0.0, 1.0);           // first 0.6 s: animate, then hold
    float r = 0.25 * easeOutBack(t);                  // overshoots, then settles

    float linearR = 0.25 * t;                         // for comparison
    float a = length(p - vec2(-0.35, 0.0)) - linearR;
    float b = length(p - vec2(0.35, 0.0)) - r;

    vec3 color = vec3(0.1);
    color = mix(color, vec3(0.5), 1.0 - smoothstep(-px, px, a));
    color = mix(color, vec3(0.3, 0.8, 0.5), 1.0 - smoothstep(-px, px, b));
    fragColor = vec4(color, 1.0);
}

Easing formulas found online are often written with pow(t - 1.0, 3.0). In GLSL, pow(x, y) is undefined for negative x (GPUs compute it as exp2(y * log2(x))), and t - 1.0 is negative here. Some drivers return the "right" answer, others NaN, so the bug shows up as a black or flickering shape on someone else's device. That is why the code above multiplies u * u * u by hand.

Time-varying noise and precision

To animate noise, add time to its input: scrolling (noise(p + vec2(iTime, 0.0))) moves the pattern, while using time as a third dimension (3D noise at vec3(p, iTime)) makes it evolve in place. Both are common for fire and clouds.

iTime keeps growing. After hours, sin(iTime * 50.0) loses precision in a 32-bit float and animations start to stutter; on mobile mediump floats it happens within minutes. Engine time values also grow without limit by default (Unreal's material Time node has an optional Period setting to wrap it). When you control the time uniform yourself, wrap it to the loop length of the effect: for a loop of period T, mod(iTime, T) keeps the numbers small:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    float hoursLater = iTime + 36000.0;                       // pretend 10 hours have passed
    float broken = 0.5 + 0.5 * sin(hoursLater * 40.0 + uv.x * 30.0);
    float fixed_ = 0.5 + 0.5 * sin(mod(hoursLater, 6.28318 / 40.0) * 40.0 + uv.x * 30.0);
    float v = uv.y > 0.5 ? broken : fixed_;
    fragColor = vec4(vec3(v), 1.0);
}

In highp both halves may look identical on your GPU; the point is the habit. Wrap any time value that drives high frequencies.

Test yourself

Why is shader animation written as a function of iTime rather than updated each frame?

  1. sin is faster
  2. Fragment shaders keep no state between frames
  3. iTime is an integer
  4. GPUs cannot add numbers

To draw a shape rotated by +a, what do you do to the coordinates?

  1. Rotate them by +a
  2. Rotate them by -a (the inverse)
  3. Scale them by a
  4. Nothing; rotate the color

Which expression gives a 0..1 sawtooth that loops every 3 seconds?

  1. sin(iTime / 3.0)
  2. fract(iTime / 3.0)
  3. floor(iTime / 3.0)
  4. iTime * 3.0