Swarm

glint · Examples · Tutorials · Gallery

Twelve thousand points in three swarms, each circling its own attractor; on every kick the attractors jump somewhere new and the swarms stream after them.

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

Swarm: 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.

vertex shader

#version 300 es
uniform mat4 mvp;

uniform float time;
uniform vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }
float bin(float x) { return silent() ? 0.35 + 0.3 * sin(x * 40.0 - time * 3.0) : texture(fft, vec2(x, 0.25)).r; }
float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }

out vec3 v_color;

vec3 attractor(float group, float k) {
    return (vec3(hash(group + k * 3.1), hash(group + k * 5.7), hash(group + k * 7.3)) - 0.5) * vec3(2.4, 1.4, 1.4);
}

void main() {
    float id = float(gl_VertexID);
    float group = mod(id, 3.0);
    float k = kickId();
    // Stream from last kick's attractor to this one's; the far ones lag behind.
    float lag = hash(id) * 0.6;
    float e = smoothstep(0.0, 0.5 + lag, sinceKick());
    vec3 centre = mix(attractor(group, k - 1.0), attractor(group, k), e);
    // Each point circles it on its own tilted orbit.
    float r = 0.1 + 0.35 * hash(id + 1.0) * (1.0 + spectrum.x);
    float a = time * (1.0 + 2.0 * hash(id + 2.0)) + hash(id + 3.0) * 6.28;
    vec3 axis = normalize(vec3(hash(id + 4.0), hash(id + 5.0), hash(id + 6.0)) - 0.5);
    vec3 u = normalize(cross(axis, vec3(0.0, 1.0, 0.1)));
    vec3 v = cross(axis, u);
    vec3 p = centre + r * (cos(a) * u + sin(a) * v);
    gl_Position = mvp * vec4(p, 1.0);
    gl_PointSize = max(1.0, 5.0 / gl_Position.w);
    v_color = (0.5 + 0.5 * cos(6.28318 * (group / 3.0 + vec3(0.0, 0.33, 0.67)))) * 0.35;
}

fragment shader

#version 300 es
precision highp float;

in vec3 v_color;
out vec4 fragColor;

void main() {
    vec2 c = gl_PointCoord - 0.5;
    fragColor = vec4(v_color * smoothstep(0.25, 0.0, dot(c, c)), 1.0);
}

More Audio-reactive examples

Disco ball · Grid pulses · Feedback mandala · 3D bar ring · Neon sign · Ribbons · Bead knot