Feedback trails

glint · Examples · Tutorials · Gallery

Two passes: a pass that feeds on its own last frame, then a colour-grading output pass.

Feedback trails: 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

2 passes, drawn in this order each frame, in GLSL.

Trails: fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform sampler2D feedback;
uniform float decay;
uniform float swirl;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = v_texcoord;
    vec2 c = uv - 0.5;
    c.x *= resolution.x / resolution.y;
    // Sample last frame slightly zoomed and rotated: that's what makes the trails.
    float a = swirl * 0.02;
    vec2 q = mat2(cos(a), -sin(a), sin(a), cos(a)) * (uv - 0.5) * 0.985 + 0.5;
    vec3 prev = texture(feedback, q).rgb * decay;

    vec3 col = vec3(0.0);
    for (int i = 0; i < 5; i++) {
        float fi = float(i);
        vec2 p = 0.35 * vec2(sin(time * (0.7 + fi * 0.13) + fi), cos(time * (0.9 + fi * 0.11) + fi * 2.0));
        float d = length(c - p);
        col += 0.004 / (d * d + 0.001) * (0.5 + 0.5 * cos(vec3(0.0, 2.0, 4.0) + fi + time));
    }
    vec2 m = (mouse - 0.5) * vec2(resolution.x / resolution.y, 1.0);
    col += 0.003 / (dot(c - m, c - m) + 0.0005) * vec3(1.0);
    fragColor = vec4(max(prev, col * 0.08), 1.0);
}

Output: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform float time;
uniform float exposure;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec3 c = texture(prevPass, v_texcoord).rgb * exposure;
    c = c / (1.0 + c);                       // tone map
    c = pow(c, vec3(0.85));
    float vig = smoothstep(0.9, 0.3, length(v_texcoord - 0.5));
    fragColor = vec4(c * vig, 1.0);
}

More Starters examples

Raymarcher · Audio spectrum · Vertex art · Lit mesh · Webcam kaleidoscope