Voronoi crystals

glint · Examples · Tutorials · Gallery

Animated Voronoi cells shaded as raised crystal facets: exact cell borders (two-pass Voronoi), bump lighting and per-cell pulses from the spectrum.

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

Voronoi crystals: 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 vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform sampler2D fft;

in vec2 v_texcoord;
out vec4 fragColor;

vec2 hash2(vec2 p) { p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3))); return fract(sin(p) * 43758.5453); }

// Voronoi with exact distance to the border (Inigo Quilez): returns (border distance, cell id hash, dist to point).
vec3 voronoi(vec2 x, float t) {
    vec2 n = floor(x), f = fract(x);
    vec2 mg, mr;
    float md = 8.0;
    for (int j = -1; j <= 1; j++)
        for (int i = -1; i <= 1; i++) {
            vec2 g = vec2(float(i), float(j));
            vec2 o = hash2(n + g);
            o = 0.5 + 0.45 * sin(t + 6.2831 * o);
            vec2 r = g + o - f;
            float d = dot(r, r);
            if (d < md) { md = d; mr = r; mg = g; }
        }
    md = 8.0;
    for (int j = -2; j <= 2; j++)
        for (int i = -2; i <= 2; i++) {
            vec2 g = mg + vec2(float(i), float(j));
            vec2 o = hash2(n + g);
            o = 0.5 + 0.45 * sin(t + 6.2831 * o);
            vec2 r = g + o - f;
            if (dot(mr - r, mr - r) > 0.00001) md = min(md, dot(0.5 * (mr + r), normalize(r - mr)));
        }
    return vec3(md, hash2(n + mg).x, length(mr));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float t = time * 0.4 + spectrumIntegrated.y * 0.5;
    vec2 p = uv * 6.0;
    vec3 v = voronoi(p, t);
    // Height = distance to the border, so each cell is a raised facet; light it via finite differences.
    vec2 e = vec2(0.01, 0.0);
    float h = v.x;
    float hx = voronoi(p + e.xy, t).x, hy = voronoi(p + e.yx, t).x;
    vec3 n = normalize(vec3(h - hx, h - hy, 0.02));
    vec3 l = normalize(vec3(-0.5, 0.6, 0.6));
    float level = texture(fft, vec2(0.02 + v.y * 0.4, 0.25)).r;
    vec3 base = 0.5 + 0.5 * cos(6.2831 * (v.y + vec3(0.0, 0.33, 0.67) + time * 0.03));
    base *= 0.35 + 0.9 * level + 0.1;
    float dif = max(dot(n, l), 0.0);
    float spec = pow(max(dot(reflect(-l, n), vec3(0, 0, 1)), 0.0), 30.0);
    vec3 col = base * (0.25 + dif) + spec * 0.6;
    col = mix(vec3(0.02), col, smoothstep(0.0, 0.04, v.x));        // dark grout
    col += vec3(1.0, 0.8, 0.5) * smoothstep(0.03, 0.0, v.x) * beat;  // grout flashes on the kick
    fragColor = vec4(pow(col, vec3(0.9)), 1.0);
}

More Showpieces examples

Volumetric clouds · Ocean · KIFS tunnel · Truchet weave · Domain warping · Neon city · Galaxy · Fluid