Feedback mandala

glint · Examples · Tutorials · Gallery

Video feedback through a kaleidoscope: each frame reads the last one folded into six mirrored slices, zoomed and turned, and a shape drawn on each kick seeds new patterns.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Feedback mandala: an audio-reactive 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 vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
uniform sampler2D feedback;

in vec2 v_texcoord;
out vec4 fragColor;

// 1 on each kick, fading; without audio, a steady pulse twice a second.
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
// Seconds since the last kick, and which kick it was (the same fallback).
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
// One FFT bin, 0..1 across the spectrum (bass on the left).
float bin(float x) { return texture(fft, vec2(x, 0.25)).r; }
// Is anything playing? (Examples fake their input when not.)
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

void main() {
    vec2 aspect = vec2(resolution.x / resolution.y, 1.0);
    vec2 st = gl_FragCoord.xy / resolution;
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // Fold the lookup into six mirrored slices.
    vec2 p = uv;
    float a = atan(p.y, p.x), r = length(p);
    float slice = 3.14159 / 3.0;
    a = abs(mod(a, slice) - slice * 0.5) + time * 0.05;
    p = r * vec2(cos(a), sin(a)) * (0.97 - kick() * 0.03);
    vec3 prev = texture(feedback, p / aspect + 0.5).rgb;
    prev = 0.955 * mix(prev, prev.gbr, 0.05);
    // New ink: a ring of dots, flaring on each kick and sized by the bass.
    float dots = 0.0;
    for (int i = 0; i < 6; i++) {
        float ang = float(i) * 1.0472 + kickId();
        vec2 q = vec2(cos(ang), sin(ang)) * (0.2 + 0.15 * hash(kickId()));
        dots += smoothstep(0.04 + spectrum.x * 0.03, 0.0, length(uv - q)) * 2.0;
    }
    // And a wobbling ring all the way round, so every slice of the fold gets some.
    float ring = smoothstep(0.012, 0.0, abs(length(uv) - 0.1 - 0.04 * sin(atan(uv.y, uv.x) * 5.0 + time * 2.0)));
    vec3 ink = pal(kickId() * 0.13 + time * 0.05) * (dots * (0.35 + kick()) + ring * 0.12);
    fragColor = vec4(min(prev + ink, vec3(1.2)), 1.0);
}

More Audio-reactive examples

Cube burst · Heartbeat · Disco ball · Grid pulses · 3D bar ring · Swarm · Neon sign · Ribbons