Pixel invaders

glint · Examples · Tutorials · Gallery

A wave of 8-bit invaders marching a step on every beat, rows coloured by their slice of the spectrum, a cannon firing on each kick. Scanlines included.

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

Pixel invaders: 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 PS() { return max(1.0, floor(resolution.y / 180.0)); }
vec2 vpix() { return floor(gl_FragCoord.xy / PS()); }
vec2 vsize() { return floor(resolution / PS()); }
float scan() { return 0.82 + 0.18 * step(0.5, fract(gl_FragCoord.y / PS())); }

// The classic crab, 11 x 8, two frames (bit 10 = leftmost).
bool crab(ivec2 s, int f) {
    if (s.x < 0 || s.x > 10 || s.y < 0 || s.y > 7) return false;
    int a[8] = int[8](0x104, 0x088, 0x1FC, 0x376, 0x7FF, 0x5FD, 0x505, 0x0D8);
    int b[8] = int[8](0x104, 0x489, 0x5FD, 0x777, 0x7FF, 0x3FE, 0x104, 0x202);
    int row = f == 0 ? a[s.y] : b[s.y];
    return ((row >> (10 - s.x)) & 1) == 1;
}
void main() {
    vec2 p = vpix(), V = vsize();
    vec3 col = vec3(0.01, 0.0, 0.04);
    float st = hash(p);
    if (st > 0.993) col += vec3(0.5) * (0.5 + 0.5 * sin(time * 3.0 + st * 300.0));
    float steps = kickId();
    float lap = floor(steps / 8.0), off = mod(steps, 8.0);
    float xoff = mod(lap, 2.0) < 1.0 ? off * 3.0 : (8.0 - off) * 3.0;
    vec2 origin = vec2(floor(V.x * 0.5) - 76.0 + xoff, V.y - 22.0 - mod(lap, 6.0) * 3.0);
    vec2 q = vec2(p.x - origin.x, origin.y - p.y);
    ivec2 cell = ivec2(floor(q / vec2(18.0, 13.0)));
    ivec2 inC = ivec2(mod(q, vec2(18.0, 13.0)));
    if (q.x >= 0.0 && q.y >= 0.0 && cell.x < 8 && cell.y < 5 && crab(inC - ivec2(3, 2), int(mod(steps, 2.0)))) {
        float r = float(cell.y);
        col = pal(r * 0.17 + 0.55) * (0.55 + 1.2 * level(0.05 + r * 0.12));
    }
    // The cannon (sways), and its shot after each kick.
    float cx = floor(V.x * 0.5 + sin(time * 0.7) * V.x * 0.3);
    if (p.y >= 8.0 && p.y < 12.0 && abs(p.x - cx) <= 6.0) col = vec3(0.2, 1.0, 0.3);
    if (p.y >= 12.0 && p.y < 14.0 && abs(p.x - cx) <= 1.0) col = vec3(0.2, 1.0, 0.3);
    float shotY = 14.0 + sinceKick() * 260.0;
    if (abs(p.x - cx) < 0.5 && p.y > shotY - 6.0 && p.y < shotY) col = vec3(1.0);
    if (p.y == 5.0) col = vec3(0.2, 1.0, 0.3) * 0.7;
    fragColor = vec4(col * scan(), 1.0);
}

More DJ visuals examples

Pixel racer · Brick breaker · 8-bit equaliser · 10 PRINT maze