Spectrum city

glint · Examples · Tutorials · Gallery

A night skyline where every building is an FFT bin: three layers scrolling at different speeds, windows flickering with the treble.

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

Spectrum city: 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; }

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 uv = gl_FragCoord.xy / resolution;
    float aspect = resolution.x / resolution.y;
    // Sky: deep blue to a purple glow on the horizon, a moon, stars.
    vec3 col = mix(vec3(0.25, 0.08, 0.3), vec3(0.01, 0.02, 0.08), pow(uv.y, 0.6));
    vec2 mp = vec2(uv.x * aspect - aspect * 0.78, uv.y - 0.78);
    col += vec3(1.0, 0.9, 0.7) * (smoothstep(0.07, 0.065, length(mp)) + 0.15 * exp(-length(mp) * 8.0));
    vec2 sp = floor(gl_FragCoord.xy / 2.0);
    col += step(0.997, hash(sp)) * (0.5 + 0.5 * sin(time * 3.0 + hash(sp.yx) * 20.0)) * step(0.45, uv.y);
    // Three layers of buildings, far to near.
    for (int l = 0; l < 3; l++) {
        float fl = float(l);
        float cells = 22.0 - fl * 6.0;
        float x = uv.x * cells + time * (0.3 + fl * 0.5);
        float id = floor(x), f = fract(x);
        float level = bin(0.02 + fract(id * 0.137 + fl * 0.31) * 0.45);
        float h = (0.12 + fl * 0.07) + hash(id + fl * 50.0) * 0.18 + level * (0.35 - fl * 0.05);
        if (uv.y < h && f > 0.08 && f < 0.92) {
            vec3 wall = vec3(0.02, 0.02, 0.05) * (1.0 + fl);
            // Windows: a grid, some lit; the lit ones flicker with the treble.
            vec2 w = vec2(f * 5.0, uv.y * (60.0 + fl * 20.0));
            vec2 wf = fract(w), wi = floor(w);
            float lit = step(0.55, hash(wi + id * 7.0 + fl)) * step(0.2, wf.x) * step(wf.x, 0.8) * step(0.25, wf.y) * step(wf.y, 0.75);
            float flick = 0.6 + 0.4 * step(0.5, hash(wi + floor(time * 4.0 + spectrum.z * 10.0)));
            col = wall + lit * flick * pal(hash(id) * 0.3 + 0.1 + fl * 0.2) * (0.25 + 0.35 * fl) * (1.0 + kick());
            // A neon edge along the roof.
            col += pal(fl * 0.3 + time * 0.05) * smoothstep(0.004, 0.0, abs(uv.y - h + 0.003)) * (0.6 + level * 2.0);
        }
    }
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Sub-bass tunnel · Kick ripples · Beat kaleidoscope · Lissajous scope · Strobe shapes