Kick ripples

glint · Examples · Tutorials · Gallery

A pool where every kick drops a stone: a wave simulation (two buffers, the wave equation) rendered with refraction and moving caustics. Click to make your own ripples.

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

Kick ripples: 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.

Waves: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform int frame;
uniform float time;
uniform float timeDelta;
uniform float beatTime;
uniform int beatCount;
uniform vec3 spectrum;

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))); }

// r = height now, g = height a frame ago.
vec2 S(vec2 p) { return texture(feedback, p / resolution).rg; }

void main() {
    vec2 p = gl_FragCoord.xy;
    if (frame < 2) { fragColor = vec4(0.0); return; }
    vec2 c = S(p);
    float lap = S(p + vec2(1, 0)).x + S(p - vec2(1, 0)).x + S(p + vec2(0, 1)).x + S(p - vec2(0, 1)).x - 4.0 * c.x;
    // The wave equation, one step: next = 2 now - before + speed * curvature. Slightly damped.
    float h = (2.0 * c.x - c.y + 0.45 * lap) * 0.994;
    // Stones: one per kick (or one every so often without audio), and the mouse.
    vec2 at = vec2(-1000.0);
    float size = 0.0;
    if (beatCount > 0 && beatTime < timeDelta * 1.2) { at = resolution * (0.15 + 0.7 * hash2(float(beatCount))); size = 1.5 + spectrum.x * 2.0; }
    if (beatCount == 0 && fract(time * 1.3) < timeDelta * 1.3) { at = resolution * (0.1 + 0.8 * hash2(floor(time * 1.3))); size = 1.0; }
    if (mouseButtons.x > 0.5) { at = mouse * resolution; size = 0.6; }
    vec2 d = p - at;
    h += size * exp(-dot(d, d) / (resolution.y * resolution.y * 0.0005));
    fragColor = vec4(h, c.x, 0.0, 1.0);
}

Water: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

float H(vec2 uv) { return texture(prevPass, uv).r; }

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec2 e = 1.0 / vec2(textureSize(prevPass, 0));
    float hx = H(uv + vec2(e.x, 0.0)) - H(uv - vec2(e.x, 0.0));
    float hy = H(uv + vec2(0.0, e.y)) - H(uv - vec2(0.0, e.y));
    vec3 n = normalize(vec3(-hx, -hy, 0.15));
    // The pool floor seen through the water: tiles, bent by the surface.
    vec2 f = (uv + n.xy * 0.05) * vec2(resolution.x / resolution.y, 1.0) * 7.0;
    vec2 g = abs(fract(f) - 0.5);
    vec3 floorCol = mix(vec3(0.15, 0.55, 0.7), vec3(0.06, 0.25, 0.35), smoothstep(0.44, 0.49, max(g.x, g.y)));
    // Caustics: light gathers where the surface curves like a lens.
    float curve = H(uv + vec2(e.x, 0.0)) + H(uv - vec2(e.x, 0.0)) + H(uv + vec2(0.0, e.y)) + H(uv - vec2(0.0, e.y)) - 4.0 * H(uv);
    floorCol *= 1.0 + clamp(-curve * 40.0, -0.5, 2.0);
    vec3 l = normalize(vec3(0.4, 0.6, 1.0));
    float spec = pow(max(dot(reflect(-l, n), vec3(0.0, 0.0, 1.0)), 0.0), 80.0);
    fragColor = vec4(floorCol + spec, 1.0);
}

More Audio-reactive examples

Sub-bass tunnel · Spectrum city · Beat kaleidoscope · Lissajous scope · Strobe shapes · LED meter