Mosh pit

glint · Examples · Tutorials · Gallery

Spinning bars and discs, datamoshed: the last frame is dragged along by the new one's motion until a keyframe (or a kick) cleans it up. Open the Glitch pass to change the stack.

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

Mosh pit: 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

2 passes, drawn in this order each frame, in GLSL.

Scene: fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform float beat;
uniform vec3 spectrum;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float t = time * 0.6 + beat * 0.3;
    float a = atan(uv.y, uv.x) + t;
    float r = length(uv);
    // Bold stripes turning one way, discs orbiting the other: plenty of motion to mosh.
    vec3 col = mix(vec3(0.05, 0.02, 0.15), vec3(1.0, 0.25, 0.45), step(0.5, fract(a * 1.2732 + r * 3.0 - t)));
    for (int i = 0; i < 4; i++) {
        float fi = float(i);
        vec2 c = 0.45 * vec2(cos(-t * 1.3 + fi * 1.571), sin(-t * 1.3 + fi * 1.571));
        col = mix(col, 0.5 + 0.5 * cos(6.283 * (fi * 0.25 + vec3(0.0, 0.33, 0.67))), step(length(uv - c), 0.14 + 0.05 * spectrum.x));
    }
    fragColor = vec4(col, 1.0);
}

Glitch: fragment shader (generated from a stack of glitch effects)

#version 300 es
precision highp float;

// Generated by glint's glitch stack (Pass settings: the effects are toggled there, the knobs are parameters).
uniform vec2 resolution;
uniform float time;
uniform int frame;
uniform float beat;
uniform int beatCount;
uniform sampler2D prevPass;
uniform sampler2D feedback;
uniform float glitchBeat;
uniform float glitchSpeed;
uniform float blockAmount;
uniform float blockSize;
uniform float rgbSplit;
uniform float rgbAngle;
uniform float moshAmount;
uniform float moshMotion;

out vec4 fragColor;

float seed;
float kick;

// An integer hash of the floats' bits. Not fract(sin(x) * 43758.5): seeds grow with time, and real GPUs' sin (Direct3D
// via ANGLE, phones) loses precision on big arguments, so it returned ~0 everywhere: every JPEG row "corrupt" the
// same way, and the whole picture went green.
float hash(vec2 p) {
    uvec2 q = floatBitsToUint(p);
    uint n = q.x * 0x27d4eb2du ^ (q.y + 0x9e3779b9u) * 0x165667b1u;
    n ^= n >> 15; n *= 0x2c1b3c6du; n ^= n >> 12; n *= 0x297a2d39u; n ^= n >> 15;
    return float(n >> 8) / 16777216.0;
}
float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
vec3 toYcc(vec3 c) { return vec3(luma(c), (c.b - luma(c)) * 0.564 + 0.5, (c.r - luma(c)) * 0.713 + 0.5); }
vec3 fromYcc(vec3 y) { return vec3(y.x + 1.403 * (y.z - 0.5), y.x - 0.344 * (y.y - 0.5) - 0.714 * (y.z - 0.5), y.x + 1.773 * (y.y - 0.5)); }
vec3 src(vec2 uv) { return texture(prevPass, uv).rgb; }

// Where each pixel reads from.
vec2 warp(vec2 uv) {
    // Broken blocks: some cells read from another cell, some repeat the row above.
    vec2 bs = vec2(blockSize) / resolution;
    vec2 cell = floor(uv / bs);
    float hb = hash(cell + seed * 0.37);
    if (hb < blockAmount * 0.25 * kick) uv = (floor(vec2(hash(cell + 1.3), hash(cell + 2.7)) / bs) + fract(uv / bs)) * bs;
    else if (hb > 1.0 - blockAmount * 0.12 * kick) uv.y = (cell.y + 0.999) * bs.y;
    return uv;
}

// The input through the warp and the channel split.
vec3 base(vec2 uv) {
    vec2 w = warp(uv);
    vec2 o = vec2(cos(rgbAngle), sin(rgbAngle)) * rgbSplit * kick;
    return vec3(src(w + o).r, src(w).g, src(w - o).b);
}

// Before the per-pixel effects: the base, moshed into the last frame.
vec3 pre(vec2 q) {
    vec3 c = base(q);
    // Datamosh: last frame, pushed along this frame's motion (brightness slope + a per-block drift).
    vec2 mb = 16.0 / resolution;
    vec2 mc = (floor(q / mb) + 0.5) * mb;
    float l0 = luma(src(mc)), lx = luma(src(mc + vec2(mb.x, 0.0))), ly = luma(src(mc + vec2(0.0, mb.y)));
    float hm = hash(floor(q / mb) + floor(seed / 4.0));
    vec2 mv = (vec2(lx - l0, ly - l0) * 6.0 + vec2(hm - 0.5, hash(vec2(hm, 7.0)) - 0.5)) * moshMotion * kick;
    // Now and then (and on a strong beat) a clean keyframe, as when a real stream gets its next I-frame.
    float keep = moshAmount * step(hm, 0.35 + 0.65 * moshAmount) * (1.0 - smoothstep(0.85, 1.0, beat) * glitchBeat) * step(0.04, hash(vec2(seed, 91.0)));
    c = mix(c, texture(feedback, q - mv).rgb, clamp(keep, 0.0, 0.985));
    return c;
}

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    kick = 1.0 + glitchBeat * beat * 2.0;
    seed = floor(time * glitchSpeed) + float(beatCount) * 17.0;
    vec3 col = pre(uv);

    fragColor = vec4(col, 1.0);
}

More Nodes & glitch examples

Nodes: Colour drift · Nodes: Crystal · Nodes: Spectrum bars · Nodes: Lava · Corrupt file