Laser robots

glint · Examples · Tutorials · Gallery

A big-beat live show: two giant tin robots on stage, one red and one blue, firing fans of lasers from their eyes across a hazy hall. The beams sweep with the bar and flare on every kick, the robots sway and swing their arms to the beat, their chest screens show the music, and a crowd in silhouette holds up phones below.

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

Laser robots: 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;
uniform float bpm;
uniform float beatPhase;
uniform float barPhase;

in vec2 v_texcoord;
out vec4 fragColor;

#define PI 3.14159265
// 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); }
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
float bin(float x) { return texture(fft, vec2(clamp(x, 0.0, 1.0), 0.25)).r; }
// With no sound in, gentle stand-ins so a visual is never empty: a moving spectrum, a bass that follows the pulse.
float silent() { return step(spectrum.x + spectrum.y + spectrum.z, 0.02); }
float level(float x) { return mix(bin(x), 0.3 + 0.25 * sin(time * 2.3 + x * 17.0) * sin(time * 1.1 + x * 5.0) + 0.15 * kick(), silent()); }
float bass() { return mix(spectrum.x, 0.15 + 0.25 * kick(), silent()); }
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(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 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))); }
float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) { v += a * noise(p); p = rot(0.5) * p * 2.02; a *= 0.5; }
    return v;
}

float sdBox(vec2 p, vec2 b) { vec2 d = abs(p) - b; return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0); }
// A thin beam from o at angle a: a bright core and a soft glow, nothing behind its source.
float beam(vec2 p, vec2 o, float a, float w) {
    vec2 d = vec2(cos(a), sin(a));
    vec2 v = p - o;
    float t = dot(v, d);
    if (t < 0.0) return 0.0;
    float dist = length(v - d * t);
    return smoothstep(w * (1.0 + t * 0.6), 0.0, dist) + exp(-dist * 70.0) * 0.12;
}
// One robot, side -1 (left) or 1 (right). Returns colour in rgb, coverage in a; eyes in eyeL / eyeR.
vec4 robot(vec2 p, float side, vec3 tint, out vec2 eyeL, out vec2 eyeR) {
    float bob = 0.012 * sin(beatPhase * 2.0 * PI + side);
    vec2 c = vec2(side * 0.47, -0.06 + bob);
    vec2 q = p - c;
    q = rot(0.03 * sin(beatPhase * 2.0 * PI) * side) * q;
    float d = 1e9;
    d = min(d, sdBox(q - vec2(0.0, 0.27), vec2(0.072, 0.058)) - 0.008);       // head
    d = min(d, sdBox(q - vec2(0.0, 0.345), vec2(0.005, 0.02)));               // antenna
    d = min(d, length(q - vec2(0.0, 0.372)) - 0.011);
    d = min(d, sdBox(q - vec2(0.0, 0.2), vec2(0.03, 0.012)));                 // neck
    d = min(d, sdBox(q - vec2(0.0, 0.07), vec2(0.105, 0.125)) - 0.01);        // body
    // Arms from the shoulders, swinging opposite to each other on the beat.
    for (int k = 0; k < 2; k++) {
        float s = k == 0 ? -1.0 : 1.0;
        float sw = 0.35 * sin(beatPhase * 2.0 * PI + s * 1.5 + side) + 0.25 * kick() * s;
        vec2 sh = vec2(s * 0.125, 0.17);
        vec2 a = rot(sw) * (q - sh);
        d = min(d, sdBox(a - vec2(s * 0.015, -0.07), vec2(0.022, 0.075)) - 0.006);
        d = min(d, sdBox(a - vec2(s * 0.015, -0.155), vec2(0.026, 0.014)));    // hands
    }
    d = min(d, sdBox(vec2(abs(q.x), q.y) - vec2(0.045, -0.13), vec2(0.03, 0.075)));  // legs
    d = min(d, sdBox(vec2(abs(q.x), q.y) - vec2(0.05, -0.215), vec2(0.042, 0.014))); // feet
    float cover = smoothstep(0.002, -0.002, d);
    // Tin: lit from the front and above, darker at the edges and seams.
    float shade = 0.55 + 0.45 * smoothstep(-0.05, 0.0, -abs(d + 0.015)) + 0.25 * smoothstep(-0.2, 0.3, q.y);
    vec3 col = tint * shade * (0.75 + 0.35 * kick());
    col *= 0.7 + 0.3 * smoothstep(0.0, -0.01, d);
    // Chest screen: a chomping glyph on the left robot, VU bars on the right.
    vec2 sc = q - vec2(0.0, 0.09);
    if (sdBox(sc, vec2(0.065, 0.06)) < 0.0) {
        col = vec3(0.02);
        if (side < 0.0) {
            float r = length(sc), ang = atan(sc.y, sc.x);
            float mouth = 0.35 + 0.35 * abs(sin(beatPhase * PI));
            float ring = smoothstep(0.006, 0.0, abs(r - 0.035)) * step(mouth, abs(ang));
            col += vec3(1.0) * ring;
        } else {
            for (int b = 0; b < 3; b++) {
                float fb = float(b);
                float h = 0.1 * level(0.04 + fb * 0.15);
                vec2 bp = sc - vec2(-0.035 + fb * 0.035, -0.05 + h * 0.5);
                if (sdBox(bp, vec2(0.012, h * 0.5)) < 0.0) col = vec3(0.9, 0.95, 1.0) * (0.6 + 0.4 * fract(sc.y * 60.0));
            }
        }
    }
    // Eyes: white discs, pupils looking about, a blink now and then.
    vec2 look = 0.008 * vec2(sin(time * 0.7 + side * 2.0), cos(time * 0.9 + side));
    float blink = step(0.97, fract(time * 0.23 + side * 0.4));
    for (int k = 0; k < 2; k++) {
        float s = k == 0 ? -1.0 : 1.0;
        vec2 e = q - vec2(s * 0.032, 0.278);
        float white = smoothstep(0.023, 0.020, length(e * vec2(1.0, 1.0 + blink * 6.0)));
        float pupil = smoothstep(0.011, 0.008, length(e - look));
        col = mix(col, vec3(1.0), white);
        col = mix(col, vec3(0.03), pupil * white);
    }
    // World positions of the eyes (for the lasers), undoing the sway.
    mat2 back = rot(-0.03 * sin(beatPhase * 2.0 * PI) * side);
    eyeL = c + back * vec2(-0.032, 0.278);
    eyeR = c + back * vec2(0.032, 0.278);
    return vec4(col, cover);
}
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float haze = 0.45 + 0.55 * fbm(uv * 3.0 + vec2(time * 0.05, time * 0.02));
    // The hall: red light on the left, blue on the right.
    vec3 col = mix(vec3(0.12, 0.0, 0.03), vec3(0.03, 0.0, 0.12), smoothstep(-0.6, 0.6, uv.x)) * haze;
    col += vec3(0.25, 0.25, 0.5) * exp(-abs(uv.x - 0.02) * 9.0) * exp(-max(uv.y + 0.1, 0.0) * 2.0) * 0.25;
    // Lasers from the stage floor, criss-crossing, swept by the bar.
    float sweep = sin(barPhase * 2.0 * PI);
    for (int i = 0; i < 8; i++) {
        float fi = float(i);
        vec2 o = vec2(-0.75 + fi * 0.214, -0.33);
        float a = PI * 0.5 + (hash(fi) - 0.5) * 1.6 + sweep * 0.35 * (mod(fi, 2.0) < 1.0 ? 1.0 : -1.0);
        vec3 lc = mod(fi, 3.0) < 1.0 ? vec3(0.5, 0.3, 1.0) : vec3(1.0, 0.08, 0.3);
        col += lc * beam(uv, o, a, 0.0025) * (0.35 + 0.9 * kick()) * haze;
    }
    // Stage lamps along the front.
    for (int i = 0; i < 9; i++) {
        float fi = float(i);
        vec2 lp = uv - vec2(-0.4 + fi * 0.1, -0.3);
        vec3 lc = fi < 4.5 ? vec3(1.0, 0.2, 0.3) : vec3(0.3, 0.6, 1.0);
        col += lc * exp(-dot(lp, lp) * 9000.0) * (0.6 + 1.2 * step(0.5, hash(fi + kickId())));
    }
    vec2 eL1, eR1, eL2, eR2;
    vec4 r1 = robot(uv, -1.0, vec3(1.0, 0.18, 0.2), eL1, eR1);
    vec4 r2 = robot(uv, 1.0, vec3(0.3, 0.7, 1.0), eL2, eR2);
    col = mix(col, r1.rgb, r1.a);
    col = mix(col, r2.rgb, r2.a);
    // Lasers from their eyes: a fan of three from each eye, sweeping out over the crowd, flaring on the kick.
    float power = 0.6 + 1.6 * kick();
    for (int k = 0; k < 3; k++) {
        float fk = float(k) - 1.0;
        // Up over the hall, then down across the crowd and back.
        float swing = 0.7 * sin(time * 0.6 + barPhase * 2.0 * PI);
        float aL = PI * 0.95 + fk * 0.22 + swing;
        float aR = PI * 0.05 + fk * 0.22 - swing;
        vec3 red = vec3(1.0, 0.1, 0.25), blue = vec3(0.45, 0.35, 1.0);
        col += red * power * (beam(uv, eL1, aL, 0.0022) + beam(uv, eR1, aL - 0.4, 0.0022)) * haze;
        col += red * power * (beam(uv, eL2, aR + 0.4, 0.0022) + beam(uv, eR2, aR, 0.0022)) * haze;
        col += blue * power * 0.5 * beam(uv, eR2, aR + 0.9 + fk * 0.1, 0.0018) * haze;
    }
    // Glowing eyes.
    for (int k = 0; k < 4; k++) {
        vec2 e = k == 0 ? eL1 : k == 1 ? eR1 : k == 2 ? eL2 : eR2;
        col += vec3(1.0, 0.85, 0.9) * exp(-length(uv - e) * 90.0) * power * 0.5;
    }
    // The crowd: heads and shoulders in silhouette, a few phones up.
    float x = uv.x * 14.0;
    float id = floor(x);
    float head = -0.4 + 0.06 * hash(id) + 0.012 * sin(time * 2.0 + id) * kick();
    float cx = fract(x) - 0.5;
    float crowd = max(step(uv.y, -0.43 + 0.02 * hash(id + 3.0)), step(length(vec2(cx / 14.0 * 1.0, (uv.y - head) * 0.9) * vec2(14.0, 14.0)), 0.42));
    col = mix(col, vec3(0.0), crowd);
    if (hash(id * 7.0) > 0.8) {
        // A phone held up just above the head: a small lit screen with a glow.
        vec2 ph = vec2((fract(x) - 0.5 - (hash(id) - 0.5) * 0.4) / 14.0, uv.y - head - 0.045);
        float phone = smoothstep(0.002, 0.0, sdBox(ph, vec2(0.009, 0.014)));
        vec3 screen = vec3(0.85, 0.9, 1.0) * (0.7 + 0.3 * hash(id + floor(time)));
        col = mix(col, screen, phone);
        col += screen * exp(-length(ph) * 120.0) * 0.25;
    }
    fragColor = vec4(col, 1.0);
}

More DJ visuals examples

Lo-fi rain · VHS sunset · Ink in water · Deep sea · Neon wireframe head · Polygon face · Polygon dancer · Falling blocks