Perfect loop

glint · Examples · Tutorials · Gallery

Made to loop: everything moves with loopPhase (0 to 1 over the 4 s loop, set by the loop button), so Render video gives a GIF or MP4 that repeats forever without a jump.

Perfect loop: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform float loopPhase;
uniform float rings;

in vec2 v_texcoord;
out vec4 fragColor;

const float TAU = 6.2831853;

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // Every motion is a whole number of turns per loop, so the last frame leads straight into the first.
    float a = loopPhase * TAU;
    vec3 col = vec3(0.02, 0.02, 0.05);
    for (float i = 1.0; i <= 12.0; i++) {
        if (i > rings) break;
        float r = i * 0.036;
        // Ring i moves on by a whole number of dots per loop (so it looks the same at the end as at the start),
        // clockwise or anticlockwise by turns.
        float n = 6.0 + i * 3.0;
        float rot = (mod(i, 2.0) < 1.0 ? 1.0 : -1.0) * a * (1.0 + mod(i, 3.0)) / n;
        float ang = atan(uv.y, uv.x) - rot;
        float ca = floor(ang / TAU * n + 0.5) / n * TAU + rot;
        float pulse = 0.5 + 0.5 * cos(a * 2.0 - i * 0.6);
        vec2 c = vec2(cos(ca), sin(ca)) * r * (1.0 + 0.12 * sin(a + i * 0.8));
        float d = length(uv - c);
        vec3 hue = 0.55 + 0.45 * cos(TAU * (i / 12.0 + vec3(0.0, 0.33, 0.67)) + a);
        col += hue * smoothstep(0.009 + 0.004 * pulse, 0.0, d) * 1.4;
        col += hue * 0.0009 / (d * d + 0.0004) * 0.02;
    }
    fragColor = vec4(pow(col, vec3(0.9)), 1.0);
}

More More GLSL examples

Jellyfish · Fireflies · Newton fractal · Burning ship · Piano overlay · Listening piano