Spectrum tree

glint · Examples · Tutorials · Gallery

A fractal tree from one branch: fold space in half, move up, turn, shrink, repeat. Each level's angle follows its own band of the spectrum, so the music opens and closes the branches.

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

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

float segment(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a, ba = b - a;
    return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0));
}

void main() {
    vec2 p = (gl_FragCoord.xy - vec2(0.5 * resolution.x, 0.04 * resolution.y)) / resolution.y;
    float scale = 1.0, d = 1e9, level = 0.0, leaf = 1e9;
    float len = 0.26;
    for (int i = 0; i < 10; i++) {
        float fi = float(i);
        // The branch is always the same segment; the space around it is what moves. Divide by scale for true distance.
        float b = (segment(p, vec2(0.0), vec2(0.0, len)) - 0.02 * (1.0 - fi * 0.08)) / scale;
        if (b < d) { d = b; level = fi; }
        p.y -= len;
        p.x = abs(p.x);                                           // both sides at once
        float band = silent() ? 0.3 + 0.2 * sin(time + fi) : bin(0.02 + fi * 0.04);
        p = rot(0.35 + band * 0.7 + 0.05 * sin(time * 0.8 + fi)) * p;
        p *= 1.0 / 0.74;
        scale /= 0.74;
    }
    leaf = (length(p) - 0.12) / scale;
    float px = 1.5 / resolution.y;
    vec3 col = mix(vec3(0.02, 0.02, 0.05), vec3(0.1, 0.05, 0.12), gl_FragCoord.y / resolution.y);
    vec3 wood = mix(vec3(0.35, 0.2, 0.1), vec3(0.5, 0.8, 0.4), level / 9.0);
    col = mix(col, wood, smoothstep(px, 0.0, d));
    vec3 blossom = pal(0.9 + level * 0.02 + time * 0.02) * (1.0 + kick());
    col = mix(col, blossom, smoothstep(px, 0.0, leaf));
    col += blossom * 0.002 / (abs(leaf) + 0.004) * (0.3 + kick());
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

FFT sphere · Shockwave · Pendulum wave · Beat cells · Particle fountain · VU meters · Ring tunnel · Light curtain