Bass blob

glint · Examples · Tutorials · Gallery

A raymarched ball of jelly that wobbles with the bass and ripples with the rest of the spectrum, lit with a soft glow from inside.

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

Bass blob: 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) {
    vec3 dir = normalize(p);
    // Wobble: slow bass waves plus spectrum ripples, by direction.
    float wob = 0.18 * sin(3.0 * p.x + time) * sin(3.0 * p.y + time * 1.3) * sin(3.0 * p.z + time * 0.7) * (0.6 + spectrum.x * 3.0);
    float ripple = 0.25 * bin(0.02 + 0.3 * (0.5 + 0.5 * dir.y)) * sin(20.0 * dir.x + time * 4.0);
    return length(p) - 1.0 - wob - ripple - 0.15 * kick();
}

vec3 normal(vec3 p) {
    vec2 e = vec2(0.002, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.0, -3.6);
    ro.xz = rot(time * 0.2) * ro.xz;
    vec3 f = normalize(-ro), r = normalize(cross(f, vec3(0.0, 1.0, 0.0))), u = cross(r, f);
    vec3 rd = normalize(mat3(r, u, f) * vec3(uv, 1.6));
    vec3 col = mix(vec3(0.03, 0.02, 0.06), vec3(0.12, 0.05, 0.2), uv.y + 0.5);
    float t = 0.0;
    for (int i = 0; i < 80; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001) break;
        t += d * 0.7;
        if (t > 8.0) break;
    }
    if (t < 8.0) {
        vec3 p = ro + rd * t;
        vec3 n = normal(p);
        vec3 l = normalize(vec3(0.6, 0.8, -0.4));
        float fres = pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
        vec3 body = pal(0.8 + spectrum.y * 0.4 + p.y * 0.1);
        col = body * (0.25 + 0.6 * max(dot(n, l), 0.0));
        col += body * 0.5 * (0.5 + 0.5 * dot(n, -l)) * (0.4 + kick());   // glow through the back
        col += fres * vec3(0.9, 0.8, 1.0) * 0.8;
        col += pow(max(dot(reflect(-l, n), -rd), 0.0), 40.0);
    }
    fragColor = vec4(pow(col, vec3(0.9)), 1.0);
}

More Audio-reactive examples

Beat kaleidoscope · Lissajous scope · Strobe shapes · LED meter · Vinyl · Sonar · Neon runner · Hex pulse