Disco ball

glint · Examples · Tutorials · Gallery

A turning mirror ball: its normals are snapped to square facets (so each tile reflects one flat colour), throwing moving spots of light round the room. Kicks flash the lamps.

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

Disco ball: 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;

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))); }

vec3 lamps(vec3 r) {
    vec3 c = vec3(0.0);
    c += vec3(1.0, 0.2, 0.5) * pow(max(dot(r, normalize(vec3(1.0, 0.5, -1.0))), 0.0), 300.0);
    c += vec3(0.2, 0.6, 1.0) * pow(max(dot(r, normalize(vec3(-1.0, 0.7, -0.8))), 0.0), 300.0);
    c += vec3(1.0, 0.9, 0.5) * pow(max(dot(r, normalize(vec3(0.0, 1.0, -0.3))), 0.0), 300.0);
    return c * (8.0 + 20.0 * kick());
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.0, -3.0), rd = normalize(vec3(uv, 1.5));
    // The room: spots of light swept round by the ball's turning.
    float a = atan(uv.y, uv.x) + time * 0.4, r = length(uv);
    vec2 g = vec2(a * 6.0, r * 14.0);
    vec2 id = floor(g), f = fract(g) - 0.5;
    vec3 col = vec3(0.02, 0.015, 0.03);
    col += pal(hash(id)) * smoothstep(0.25, 0.1, length(f)) * step(0.7, hash(id + 5.0)) * (0.3 + kick()) * smoothstep(0.3, 0.6, r);
    // The ball.
    vec3 oc = ro;
    float b = dot(oc, rd), h = b * b - dot(oc, oc) + 0.64;
    if (h > 0.0) {
        vec3 n = normalize(ro + rd * (-b - sqrt(h)));
        // Turn it, then snap the normal to the middle of its tile (latitude/longitude grid).
        float c = cos(time * 0.4), s = sin(time * 0.4);
        vec3 m = vec3(c * n.x - s * n.z, n.y, s * n.x + c * n.z);
        float lat = acos(clamp(m.y, -1.0, 1.0)), lon = atan(m.z, m.x);
        float rows = 18.0;
        float cols = max(4.0, floor(rows * 2.0 * sin(floor(lat / 3.14159 * rows + 0.5) / rows * 3.14159)));
        float lat0 = (floor(lat / 3.14159 * rows) + 0.5) / rows * 3.14159;
        float lon0 = (floor(lon / 6.28318 * cols) + 0.5) / cols * 6.28318;
        vec3 t = vec3(sin(lat0) * cos(lon0), cos(lat0), sin(lat0) * sin(lon0));
        vec3 nt = vec3(c * t.x + s * t.z, t.y, -s * t.x + c * t.z);
        float edge = smoothstep(0.0, 0.08, min(fract(lat / 3.14159 * rows), 1.0 - fract(lat / 3.14159 * rows))) * smoothstep(0.0, 0.08, min(fract(lon / 6.28318 * cols), 1.0 - fract(lon / 6.28318 * cols)));
        col = (vec3(0.12) + lamps(reflect(rd, nt))) * edge + vec3(0.02);
    }
    fragColor = vec4(1.0 - exp(-col), 1.0);
}

More Audio-reactive examples

Square tunnel · Waveform ring · Cube burst · Heartbeat · Grid pulses · Feedback mandala · 3D bar ring · Swarm