Pinball Night
by MusicBar, visualizer
A pinball table strip after dark: a chrome ball ricochets down a row of pop bumpers that pulse with the spectrum and flash when it passes, chase lights run along the rails, and the flippers at both ends flick on every beat.
How it installs, its SHA-256 and its code
Click Apply: MusicBar downloads it, checks its SHA-256 and puts it on the taskbar, or just puts it on if it already has it. Needs MusicBar 1.8 or newer.
SHA-256 31e80c17dadfcf35e6095a3699c0796f329309c2599da425b4a593df52efcbf7
//! name: Pinball Night
//! author: MusicBar
//! kind: visualizer
//! description: A pinball table strip after dark: a chrome ball ricochets down a row of pop bumpers that pulse with the spectrum and flash when it passes, chase lights run along the rails, and the flippers at both ends flick on every beat.
float hash1(float n) { return frac(sin(n * 127.1 + 311.7) * 43758.5453); }
float sdSeg(float2 p, float2 a, float2 b) { float2 pa = p - a, ba = b - a; return length(pa - ba * saturate(dot(pa, ba) / dot(ba, ba))); }
// Lays a color over what is there (premultiplied light, and how much of the pixel it covers).
void layer(inout float3 acc, inout float cover, float3 rgb, float a)
{
a = saturate(a);
acc = rgb * a + acc * (1.0 - a);
cover = a + cover * (1.0 - a);
}
// The ball at time tt (bar heights): it runs flipper to flipper and back, banking off the rails
// above and below each bumper; on the way back it takes the other side of every bumper.
float2 ballAt(float tt, float x0, float x1, float S, float Tc)
{
float f = frac(tt / (2.0 * Tc));
float x = f < 0.5 ? lerp(x0, x1, f * 2.0) : lerp(x1, x0, f * 2.0 - 1.0);
float g = 4.0 * abs(frac(((x - x0) / S + 0.5) * 0.5) - 0.5) - 1.0;
return float2(x, 0.5 + 0.32 * g * (f < 0.5 ? 1.0 : -1.0));
}
float4 main(float2 uv : TEXCOORD) : COLOR
{
float u = size.y;
float2 p = float2(uv.x * size.x, (1.0 - uv.y) * size.y) / u; // bar heights, screen-up
float aa = 1.0 / u; // one pixel
float Lx = size.x / u;
float3 white = float3(1, 1, 1);
float3 acc = float3(0, 0, 0);
float cover = 0;
// The lane between the flippers holds M bumpers; the ball crosses it in about two seconds.
float x0 = 0.9, x1 = Lx - 0.9;
float M = max(2.0, floor((x1 - x0) / 1.5 + 0.5));
float S = (x1 - x0) / M;
float Tc = 1.4 + (x1 - x0) * 0.035;
float2 ball = ballAt(time, x0, x1, S, Tc);
float fdir = frac(time / (2.0 * Tc)) < 0.5 ? 1.0 : -1.0;
// Rails along both edges: chrome lips with a strip of chase lights running inside, faster when loud.
float ry = abs(p.y - 0.5);
float lip = smoothstep(aa * 1.2, 0.0, abs(ry - 0.415)) * 0.8 + smoothstep(aa * 1.2, 0.0, abs(ry - 0.49)) * 0.5;
layer(acc, cover, float3(0.75, 0.78, 0.86), lip);
float ci = floor(p.x / 0.26);
float cx = abs(frac(p.x / 0.26) - 0.5) * 0.26;
float chase = frac(ci / 9.0 - time * 1.5 * (p.y > 0.5 ? 1.0 : -1.0));
float lamp = smoothstep(0.35, 0.0, chase) + 0.25 * beat;
float3 lampCol = frac(ci / 3.0) < 0.3 ? colorA.rgb : (frac(ci / 3.0) < 0.6 ? colorB.rgb : colorC.rgb);
float seg = smoothstep(aa, -aa, max(cx - 0.085, abs(ry - 0.452) - 0.022));
layer(acc, cover, lampCol, exp(-max(max(cx - 0.085, abs(ry - 0.452) - 0.022), 0.0) / 0.05) * lamp * 0.45);
layer(acc, cover, lerp(lampCol * 0.15, lerp(lampCol, white, 0.45), saturate(lamp)), seg * 0.8);
// Arrow inserts in the playfield between the bumpers, pointing the way the ball runs and lighting as it passes.
float gi = clamp(floor((p.x - x0) / S + 0.5), 1.0, M - 1.0);
float2 aq = p - float2(x0 + gi * S, 0.5);
aq.x *= fdir;
float vv = aq.x + abs(aq.y);
float chev = max(min(abs(vv - 0.03), abs(vv + 0.09)) - 0.032, abs(aq.y) - 0.1);
float nearBall = exp(-abs(ball.x - x0 - gi * S) / 0.9);
layer(acc, cover, colorC.rgb, exp(-max(chev, 0.0) / 0.04) * (0.15 + 0.5 * nearBall) * 0.6);
layer(acc, cover, lerp(colorC.rgb * 0.3, lerp(colorC.rgb, white, 0.4), saturate(0.2 + nearBall + 0.5 * beat)), smoothstep(aa, -aa, chev) * 0.9);
// Pop bumpers: the nearest one to this pixel, with its band of the spectrum, a flash when the ball goes by
// and a shock ring that leaves it right after.
float n = clamp(floor((p.x - x0) / S), 0.0, M - 1.0);
float2 bc = float2(x0 + (n + 0.5) * S, 0.5);
float spec = spectrum(abs(bc.x / Lx - 0.5) * 2.0);
float hit = smoothstep(0.65, 0.34, length(ball - bc));
float since = (ball.x - bc.x) * fdir / ((x1 - x0) / Tc);
float3 bcol = frac(n / 3.0) < 0.3 ? colorA.rgb : (frac(n / 3.0) < 0.6 ? colorB.rgb : colorC.rgb);
float lit = saturate(0.2 + 0.9 * spec + 0.25 * beat);
float2 q = p - bc;
float r = length(q);
layer(acc, cover, bcol, exp(-max(r - 0.2, 0.0) / 0.16) * (0.2 + 0.45 * lit + 0.5 * hit));
float sring = abs(r - 0.24 - since * 2.1);
layer(acc, cover, lerp(bcol, white, 0.5), step(0.0, since) * saturate(1.0 - since / 0.22) * smoothstep(aa * 1.5, 0.0, sring));
float sh = 0.5 + 0.5 * q.y / max(r, 0.001);
layer(acc, cover, bcol * 0.12, smoothstep(aa, -aa, r - 0.235));
layer(acc, cover, lerp(bcol * 0.35, lerp(bcol, white, 0.3), lit) * (0.7 + 0.4 * sh), smoothstep(aa, -aa, r - 0.225) * smoothstep(-aa, aa, r - 0.19));
layer(acc, cover, float3(0.82, 0.84, 0.9) * (0.45 + 0.55 * sh), smoothstep(aa, -aa, r - 0.19));
float ang = atan2(q.y, q.x);
float star = r - 0.075 * lerp(0.45, 1.0, pow(0.5 + 0.5 * cos(5.0 * ang + 1.57), 2.0));
float3 cap = lerp(bcol * 0.25, bcol * (1.0 + 0.2 * sh), lit) + white * (0.9 * hit);
cap = lerp(cap, lerp(white, bcol, 0.25) * (0.5 + 0.6 * lit), smoothstep(aa, -aa, star));
cap += white * 0.5 * smoothstep(0.05, 0.0, length(q - float2(-0.05, 0.07))) * (0.5 + 0.5 * lit);
layer(acc, cover, cap, smoothstep(aa, -aa, r - 0.155));
// Flippers: both ends, a pair each; folded so one shape draws all four. They flick on the beat and when the ball arrives.
float right = p.x > 0.5 * Lx ? 1.0 : 0.0;
float2 fp = float2(right > 0.5 ? Lx - p.x : p.x, 0.5 + abs(p.y - 0.5));
float fph = frac(time / (2.0 * Tc));
float arr = right > 0.5 ? abs(fph - 0.5) : min(fph, 1.0 - fph);
float flick = max(beat, exp(-pow(arr * 2.0 * Tc / 0.12, 2.0)));
float fa = lerp(-2.36, -1.05, flick);
float2 pv = float2(0.6, 0.86);
float2 tip = pv + 0.38 * float2(cos(fa), sin(fa));
float2 pa = fp - pv, ba = tip - pv;
float hh = saturate(dot(pa, ba) / dot(ba, ba));
float fd = length(pa - ba * hh) - lerp(0.085, 0.042, hh);
float side = dot(pa, float2(-ba.y, ba.x));
layer(acc, cover, float3(0.0, 0.0, 0.02), smoothstep(aa, -aa, fd - aa * 1.5) * 0.8);
float3 rubber = colorA.rgb * (0.6 + 0.5 * flick);
float3 plastic = float3(0.95, 0.94, 0.9) * (0.7 + 0.3 * smoothstep(-0.05, 0.05, side));
layer(acc, cover, lerp(plastic, rubber, smoothstep(-0.026, -0.014, fd)), smoothstep(aa, -aa, fd));
float pin = length(fp - pv);
layer(acc, cover, float3(0.75, 0.77, 0.82) * (0.6 + 0.6 * smoothstep(0.03, -0.03, pin - 0.02 - (fp.y - pv.y))), smoothstep(aa, -aa, pin - 0.03));
// The ball: a short motion streak, then polished chrome reflecting the table lights.
float2 b1 = ballAt(time - 0.035, x0, x1, S, Tc), b2 = ballAt(time - 0.07, x0, x1, S, Tc);
float tr = min(sdSeg(p, ball, b1), sdSeg(p, b1, b2) + 0.02);
layer(acc, cover, lerp(colorB.rgb, white, 0.5), smoothstep(0.095, 0.0, tr) * 0.4);
float2 bq = (p - ball) / 0.095;
float bd = length(bq);
layer(acc, cover, white, exp(-max(bd - 1.0, 0.0) / 1.2) * 0.25);
layer(acc, cover, float3(0.02, 0.02, 0.04), smoothstep(aa, -aa, (bd - 1.0) * 0.095 - aa));
float3 chrome = lerp(float3(0.3, 0.32, 0.38), float3(0.92, 0.94, 1.0), smoothstep(-0.2, 0.6, bq.y));
chrome = lerp(chrome, float3(0.1, 0.1, 0.13), smoothstep(0.12, 0.0, abs(bq.y + 0.05)) * 0.8); // the horizon line of the reflection
chrome += lerp(colorA.rgb, colorB.rgb, 0.5 + 0.5 * bq.x) * smoothstep(-0.3, -0.9, bq.y) * 0.6; // table lights in the lower half
chrome += white * smoothstep(0.35, 0.1, length(bq - float2(-0.35, 0.42)));
layer(acc, cover, chrome * (0.75 + 0.25 * sqrt(saturate(1.0 - bd * bd))), smoothstep(aa, -aa, (bd - 1.0) * 0.095));
return paint(acc / max(cover, 0.001), cover);
}