Sub-bass tunnel

glint · Examples · Tutorials · Gallery

Flying down a hexagonal tunnel whose walls breathe with the bass, light rings flashing on every kick.

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

Sub-bass tunnel: 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))); }

float map(vec3 p) {
    vec2 q = rot(p.z * 0.12 + time * 0.1) * p.xy;
    float r = 1.5 + 0.2 * sin(p.z * 0.6 + time) + spectrum.x * 0.8;
    // Distance to the inside of a hexagon: the polar angle folded into one side.
    float seg = 6.28318 / 6.0;
    float a = mod(atan(q.y, q.x) + seg * 0.5, seg) - seg * 0.5;
    return r - length(q) * cos(a) + 0.06 * smoothstep(0.6, 1.0, sin(p.z * 4.0));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.0, time * (4.0 + spectrum.x * 6.0));
    vec3 rd = normalize(vec3(uv, 1.1));
    float t = 0.0, glow = 0.0;
    for (int i = 0; i < 90; i++) {
        float d = map(ro + rd * t);
        glow += 0.015 / (0.05 + abs(d));
        if (d < 0.001 || t > 40.0) break;
        t += d * 0.8;
    }
    vec3 p = ro + rd * t;
    vec3 base = pal(p.z * 0.03 + time * 0.05);
    float band = exp(-30.0 * abs(fract(p.z * 0.2) - 0.5));
    vec3 col = base * band * (0.5 + 3.0 * kick() + spectrum.y * 2.0);
    col += base * glow * 0.012 * (0.4 + spectrum.z * 3.0);
    col *= exp(-0.07 * t);
    fragColor = vec4(1.0 - exp(-col * 1.4), 1.0);
}

More Audio-reactive examples

Spectrum city · Kick ripples · Beat kaleidoscope · Lissajous scope