Instanced cubes

glint · Examples · Tutorials · Gallery

One cube mesh drawn 900 times with gl_InstanceID: a rippling, audio-reactive grid.

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

Instanced cubes: 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 float time;
uniform vec3 spectrum;
uniform int instanceCount;
uniform float waveHeight;

out vec3 v_normal;
out float v_height;

void main() {
    float side = floor(sqrt(float(instanceCount)));
    float id = float(gl_InstanceID);
    vec2 cell = vec2(mod(id, side), floor(id / side)) - (side - 1.0) * 0.5;
    vec2 xz = cell * 0.1;
    float h = waveHeight * (0.5 + 0.5 * sin(length(xz) * 6.0 - time * 3.0)) + spectrum.x * 1.5;
    vec3 p = a_position.xyz * vec3(0.08, 0.08 + h, 0.08);
    p.y += (0.08 + h) * 0.5;
    p.xz += xz;
    v_normal = normalMatrix * a_normal;
    v_height = h;
    gl_Position = mvp * vec4(p, 1.0);
}

fragment shader

#version 300 es
precision highp float;

in vec3 v_normal;
in float v_height;
out vec4 fragColor;

void main() {
    vec3 n = normalize(v_normal);
    float light = 0.25 + 0.75 * max(dot(n, normalize(vec3(0.4, 1.0, 0.3))), 0.0);
    vec3 base = 0.5 + 0.5 * cos(6.28318 * (v_height * 1.5 + vec3(0.0, 0.33, 0.67)));
    fragColor = vec4(base * light, 1.0);
}

More Starters examples

Tunnel · Mandelbrot & Julia · Reaction–diffusion · Game of Life · Wireframe waves · Bloom · Terrain flyover · Dal Platinum