Rising bubbles

glint · Examples · Tutorials · Gallery

Bubbles wobbling up through the water, fattened by the bass, under shafts of light that sway with the mids.

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

Rising bubbles: 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))); }

void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    float aspect = resolution.x / resolution.y;
    vec3 col = mix(vec3(0.0, 0.05, 0.12), vec3(0.0, 0.35, 0.5), st.y);
    // Light shafts from above.
    float shafts = 0.5 + 0.5 * sin(st.x * 18.0 + sin(st.y * 3.0 + time * 0.5) * (1.0 + spectrum.y * 4.0));
    col += vec3(0.1, 0.25, 0.3) * pow(shafts, 6.0) * st.y;
    float bass = silent() ? 0.3 + 0.4 * kick() : spectrum.x * 1.5;
    for (int i = 0; i < 40; i++) {
        float fi = float(i);
        float speed = 0.08 + 0.12 * hash(fi);
        float y = fract(time * speed + hash(fi + 3.0)) * 1.3 - 0.15;
        float x = hash(fi + 7.0) + 0.02 * sin(time * 3.0 + fi);
        vec2 d = (st - vec2(x, y)) * vec2(aspect, 1.0);
        float r = (0.008 + 0.022 * hash(fi + 11.0)) * (1.0 + bass);
        float l = length(d);
        float rim = smoothstep(r, r * 0.8, l) * smoothstep(r * 0.55, r, l);
        float shine = smoothstep(r * 0.3, 0.0, length(d - vec2(-0.35, 0.35) * r));
        col += vec3(0.7, 0.9, 1.0) * (rim * 0.6 + shine);
    }
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Orbitals · Bar clock · Bass snake · Spectrum landscape · Op art · Square tunnel · Waveform ring · Cube burst