Polygon dancer

glint · Examples · Tutorials · Gallery

A low-poly figure in glowing magenta wireframe, dancing to the tempo it hears (new moves each bar), in front of cyan wireframe box scaffolding that lights up box by box on the beat. Hazy stage, light bars, crowd below.

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

Polygon dancer: 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 float bpm;
uniform float beatPhase;
uniform float barPhase;

in vec2 v_texcoord;
out vec4 fragColor;

#define PI 3.14159265
// 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); }
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
float bin(float x) { return texture(fft, vec2(clamp(x, 0.0, 1.0), 0.25)).r; }
// With no sound in, gentle stand-ins so a visual is never empty: a moving spectrum, a bass that follows the pulse.
float silent() { return step(spectrum.x + spectrum.y + spectrum.z, 0.02); }
float level(float x) { return mix(bin(x), 0.3 + 0.25 * sin(time * 2.3 + x * 17.0) * sin(time * 1.1 + x * 5.0) + 0.15 * kick(), silent()); }
float bass() { return mix(spectrum.x, 0.15 + 0.25 * kick(), silent()); }
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(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 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))); }
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) { v += a * noise(p); p = rot(0.5) * p * 2.02; a *= 0.5; }
    return v;
}

float sdSeg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a, ba = b - a; float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0); return length(pa - ba * h); }
vec2 vtx(vec2 c, float seed) { return c + 0.42 * (hash2(c + seed) - 0.5); }
// Distance to the nearest mesh edge, in lattice units.
float mesh(vec2 q, float seed) {
    vec2 i = floor(q);
    float d = 1e9;
    for (int y = -1; y <= 1; y++)
        for (int x = -1; x <= 1; x++) {
            vec2 c = i + vec2(float(x), float(y));
            vec2 a = vtx(c, seed), b = vtx(c + vec2(1.0, 0.0), seed), e = vtx(c + vec2(0.0, 1.0), seed), f = vtx(c + vec2(1.0, 1.0), seed);
            d = min(d, sdSeg(q, a, b));
            d = min(d, sdSeg(q, a, e));
            d = min(d, hash(c + seed * 3.0) > 0.5 ? sdSeg(q, a, f) : sdSeg(q, b, e));
        }
    return d;
}
vec3 neon(float d, vec3 c, float w) { return c * (smoothstep(w, 0.0, d) * 1.2 + exp(-d * 160.0) * 0.35); }
// Stage haze in magenta, truss lights above, light bars and a crowd below.
vec3 stage(vec2 uv, float haze) {
    vec3 col = vec3(0.16, 0.0, 0.14) * haze + vec3(0.5, 0.05, 0.45) * exp(-abs(uv.y + 0.25) * 3.0) * 0.35 * haze;
    for (int i = 0; i < 12; i++) {
        vec2 lp = uv - vec2(-0.85 + float(i) * 0.155, 0.47);
        col += vec3(1.0, 0.5, 0.95) * exp(-dot(lp, lp) * 5000.0) * (0.5 + 0.8 * step(0.6, hash(float(i) + kickId())));
    }
    return col;
}
float crowd(vec2 uv) {
    float x = uv.x * 16.0, id = floor(x);
    float head = -0.43 + 0.05 * hash(id) + 0.012 * sin(time * 2.0 + id) * kick();
    float cx = fract(x) - 0.5;
    return max(step(uv.y, -0.47 + 0.02 * hash(id + 3.0)), step(length(vec2(cx / 16.0, uv.y - head) * vec2(16.0, 14.0)), 0.42));
}

// A tapered limb from a (radius ra) to b (radius rb): its distance, and coordinates along and across it.
float part(vec2 p, vec2 a, vec2 b, float ra, float rb, out vec2 local) {
    vec2 pa = p - a, ba = b - a;
    float len = length(ba);
    float h = clamp(dot(pa, ba) / (len * len), 0.0, 1.0);
    vec2 n = vec2(-ba.y, ba.x) / len;
    local = vec2(dot(pa, n), dot(pa, ba) / len);
    return length(pa - ba * h) - mix(ra, rb, h);
}
vec2 dirv(float a, float side) { return vec2(sin(a) * side, -cos(a)); }
// One wireframe box, drawn straight on with its back face offset (like the stage scaffolding).
float boxLines(vec2 p, vec2 c, vec2 hs, vec2 depth) {
    vec2 q = abs(p - c) - hs;
    float front = abs(max(q.x, q.y));
    vec2 q2 = abs(p - c - depth) - hs;
    float back = abs(max(q2.x, q2.y));
    float d = min(front, back);
    for (int k = 0; k < 4; k++) {
        vec2 corner = c + hs * vec2(k == 0 || k == 3 ? -1.0 : 1.0, k < 2 ? -1.0 : 1.0);
        d = min(d, sdSeg(p, corner, corner + depth));
    }
    return d;
}
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float haze = 0.45 + 0.55 * fbm(uv * 2.5 + vec2(time * 0.04, 0.0));
    vec3 col = stage(uv, haze);
    vec3 pink = vec3(1.0, 0.42, 0.95), cyan = vec3(0.2, 0.95, 0.95);
    // Cyan scaffolding: boxes in a cluster behind the dancer, long rails out to the sides.
    float k = kickId();
    for (int i = 0; i < 11; i++) {
        float fi = float(i);
        vec2 c = vec2(-0.55 + mod(fi * 0.37, 1.1), -0.12 + mod(fi * 0.23, 0.5));
        vec2 hs = vec2(0.05 + 0.06 * hash(fi), 0.04 + 0.06 * hash(fi + 2.0));
        vec2 depth = vec2(0.025, 0.02) * (hash(fi + 4.0) > 0.5 ? 1.0 : -1.0);
        float lit = 0.35 + 1.2 * step(0.5, hash(fi + floor(k)));
        col += neon(boxLines(uv, c, hs, depth), cyan, 0.0016) * lit * 0.6;
    }
    for (int i = 0; i < 4; i++) {
        float y = -0.2 + float(i) * 0.15;
        float rail = min(sdSeg(uv, vec2(-0.95, y), vec2(-0.55, y)), sdSeg(uv, vec2(0.55, y + 0.05), vec2(0.95, y + 0.05)));
        col += neon(rail, cyan, 0.0016) * 0.5;
    }
    col += vec3(1.0, 0.7, 1.0) * exp(-abs(uv.y + 0.08) * 60.0) * step(abs(uv.x - 0.25), 0.18) * 0.25 * (0.5 + kick());
    // The skeleton: moves change every bar (wave, pump, point).
    float b = beatPhase * 2.0 * PI;
    float move = mod(floor(time * max(bpm, 60.0) / 240.0), 3.0);
    vec2 hip = vec2(0.03 * sin(b * 0.5), -0.1 - 0.025 * abs(sin(beatPhase * PI)));
    float lean = 0.1 * sin(b * 0.5);
    vec2 chest = hip + rot(lean) * vec2(0.0, 0.25);
    vec2 neck = chest + rot(lean) * vec2(0.0, 0.05);
    vec2 headC = neck + rot(lean) * vec2(0.0, 0.07);
    vec2 best = vec2(1e9, 0.0);
    vec2 bestLocal = vec2(0.0);
    float outline = 1e9;
    vec2 L;
    // (Each part keeps its own lattice, so its triangles move with it.)
    float dd = part(uv, hip, chest, 0.06, 0.085, L);
    if (dd < best.x) { best = vec2(dd, 1.0); bestLocal = L; }
    outline = min(outline, dd);
    dd = part(uv, neck, headC + rot(lean) * vec2(0.0, 0.02), 0.025, 0.055, L);
    if (dd < best.x) { best = vec2(dd, 2.0); bestLocal = L; }
    outline = min(outline, dd);
    for (int s = 0; s < 2; s++) {
        float side = s == 0 ? -1.0 : 1.0;
        float ph = b + (s == 0 ? 0.0 : PI);
        float aU, aL;
        if (move < 1.0) { aU = s == 1 ? 2.1 + 0.3 * sin(b) : 0.5 + 0.25 * sin(ph); aL = s == 1 ? 1.2 : 0.3; }
        else if (move < 2.0) { aU = mix(0.5, 2.6, 0.5 + 0.5 * sin(ph)); aL = 0.4 + 0.4 * (0.5 + 0.5 * sin(ph)); }
        else { aU = s == 0 ? 2.8 : 0.9 + 0.2 * sin(b); aL = s == 0 ? 0.15 : 1.9; }
        vec2 sh = chest + rot(lean) * vec2(side * 0.095, -0.015);
        vec2 el = sh + dirv(aU, side) * 0.13;
        vec2 hd = el + dirv(aU + aL, side) * 0.12;
        float th = 0.12 * sin(ph) + 0.08;
        float lift = max(0.0, sin(ph));
        vec2 hp = hip + vec2(side * 0.058, -0.02);
        vec2 kn = hp + dirv(th, side) * 0.17;
        vec2 an = kn + dirv(th - 0.6 * lift, side) * 0.17;
        vec2 toe = an + vec2(side * 0.045, -0.01);
        float parts[5];
        vec2 locals[5];
        parts[0] = part(uv, sh, el, 0.03, 0.024, locals[0]);
        parts[1] = part(uv, el, hd, 0.024, 0.016, locals[1]);
        parts[2] = part(uv, hp, kn, 0.038, 0.03, locals[2]);
        parts[3] = part(uv, kn, an, 0.034, 0.022, locals[3]);
        parts[4] = part(uv, an, toe, 0.016, 0.012, locals[4]);
        for (int j = 0; j < 5; j++) {
            if (parts[j] < best.x) { best = vec2(parts[j], 10.0 + float(s * 5 + j)); bestLocal = locals[j]; }
            outline = min(outline, parts[j]);
        }
    }
    vec3 fig = vec3(0.0);
    if (best.x < 0.0) fig += neon(mesh(bestLocal * 24.0, best.y) / 24.0, pink, 0.0016);
    fig += neon(abs(outline), pink, 0.0018);
    col = mix(col, col * 0.4, step(outline, 0.0) * 0.6);
    col += fig * (1.0 + 0.6 * kick());
    col = mix(col, vec3(0.0), crowd(uv));
    fragColor = vec4(col, 1.0);
}

More DJ visuals examples

Deep sea · Laser robots · Neon wireframe head · Polygon face · Falling blocks · Pong · Snake · Vector rocks