3D bar ring

glint · Examples · Tutorials · Gallery

A ring of 48 instanced cubes, each stretched up by one band of the spectrum and turned to face outwards: the classic analyser, in 3D.

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

3D bar ring: 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.

vertex shader

#version 300 es
in vec4 a_position;
in vec3 a_normal;

uniform mat4 mvp;
uniform mat3 normalMatrix;
uniform int instanceCount;

uniform float time;
uniform vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }
float bin(float x) { return silent() ? 0.35 + 0.3 * sin(x * 40.0 - time * 3.0) : texture(fft, vec2(x, 0.25)).r; }
float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }

out vec3 v_normal;
out vec3 v_color;

void main() {
    float i = float(gl_InstanceID), n = float(instanceCount);
    float a = i / n * 6.28318;
    // Mirrored round the ring so it's symmetric: bass at the front and back.
    float h = 0.05 + 1.2 * bin(0.01 + abs(i / n - 0.5) * 2.0 * 0.4);
    vec3 p = a_position.xyz * vec3(0.08, h, 0.08);
    p.y += h * 0.5 - 0.4;
    p.x += 1.0;
    float c = cos(a), s = sin(a);
    p.xz = mat2(c, s, -s, c) * p.xz;
    vec3 nrm = a_normal;
    nrm.xz = mat2(c, s, -s, c) * nrm.xz;
    v_normal = normalMatrix * nrm;
    v_color = 0.5 + 0.5 * cos(6.28318 * (i / n + vec3(0.0, 0.33, 0.67))) * (0.7 + h);
    gl_Position = mvp * vec4(p, 1.0);
}

fragment shader

#version 300 es
precision highp float;

in vec3 v_normal;
in vec3 v_color;
out vec4 fragColor;

void main() {
    vec3 n = normalize(v_normal);
    vec3 l = normalize(vec3(0.4, 0.9, 0.5));
    float spec = pow(max(dot(reflect(-l, n), vec3(0.0, 0.0, 1.0)), 0.0), 24.0);
    fragColor = vec4(v_color * (0.25 + 0.75 * max(dot(n, l), 0.0)) + spec * 0.4, 1.0);
}

More Audio-reactive examples

Heartbeat · Disco ball · Grid pulses · Feedback mandala · Swarm · Neon sign · Ribbons · Bead knot