Spectrum landscape

glint · Examples · Tutorials · Gallery

The spectrum's history as terrain: one pass keeps a scrolling spectrogram (a row per frame), the next raymarches it as a height field, so the music you just heard stretches away as mountains.

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

Spectrum landscape: 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

2 passes, drawn in this order each frame, in GLSL.

History: 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; }

void main() {
    vec2 p = gl_FragCoord.xy;
    // Rows move up a pixel a frame; the bottom row is now.
    if (p.y > 1.0) { fragColor = texture(feedback, (p - vec2(0.0, 1.0)) / resolution); return; }
    float x = p.x / resolution.x;
    float v = bin(abs(x - 0.5) * 2.0 * 0.5 + 0.01);          // mirrored: bass down the middle
    if (silent()) v = 0.4 * exp(-pow((abs(x - 0.5) - 0.2 - 0.1 * sin(time)) * 10.0, 2.0)) + 0.5 * kick() * exp(-abs(x - 0.5) * 6.0);
    fragColor = vec4(v, 0.0, 0.0, 1.0);
}

Terrain: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform vec2 resolution;
uniform float time;

in vec2 v_texcoord;
out vec4 fragColor;

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 cameraRay(vec3 ro, vec3 target, vec2 uv, float zoom) {
    vec3 f = normalize(target - ro);
    vec3 r = normalize(cross(vec3(0.0, 1.0, 0.0), f));
    vec3 u = cross(f, r);
    return normalize(uv.x * r + uv.y * u + zoom * f);
}

// x across the spectrum, z back in time (0 = now).
float height(vec2 xz) {
    if (xz.y < 0.0 || xz.y > 1.0 || abs(xz.x) > 1.0) return 0.0;
    return texture(prevPass, vec2(xz.x * 0.5 + 0.5, xz.y)).r * 0.35;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.45, -0.35);
    vec3 rd = cameraRay(ro, vec3(0.0, 0.0, 0.5), uv, 1.4);
    vec3 col = mix(vec3(0.05, 0.02, 0.1), vec3(0.4, 0.1, 0.35), exp(-max(rd.y, 0.0) * 8.0));
    float t = 0.0;
    for (int i = 0; i < 160; i++) {
        vec3 p = ro + rd * t;
        float h = height(p.xz);
        if (p.y < h) {
            float fog = smoothstep(0.2, 1.5, t);
            vec3 c = pal(0.6 + h * 1.6) * (0.3 + 2.0 * h);
            // Grid lines on the ground, like old vector graphics.
            vec2 g = abs(fract(p.xz * vec2(20.0, 30.0)) - 0.5);
            c += vec3(0.2, 0.6, 1.0) * smoothstep(0.05, 0.0, min(g.x, g.y)) * 0.5;
            col = mix(c, col, fog);
            break;
        }
        t += 0.012;
        if (t > 1.8) break;
    }
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Halftone spectrum · Orbitals · Bar clock · Bass snake · Rising bubbles · Op art · Square tunnel · Waveform ring