Claw Machine
by MusicBar, progress bar
An arcade claw crane rides the rail to the playhead over a pile of plush toys under the glass, dipping and wiggling on every beat, while the plushies already won sit in a row behind it and bounce along.
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 73db12f493e8102d2b317816bdb2fc0eca9b0a3b5f425a8241a9f64fb9d72773
//! name: Claw Machine
//! author: MusicBar
//! kind: progress
//! description: An arcade claw crane rides the rail to the playhead over a pile of plush toys under the glass, dipping and wiggling on every beat, while the plushies already won sit in a row behind it and bounce along.
float hash1(float n) { return frac(sin(n * 127.1 + 311.7) * 43758.5453); }
float2 rot(float2 p, float a) { float c = cos(a), s = sin(a); return float2(c * p.x - s * p.y, s * p.x + c * p.y); }
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))); }
float sdBox(float2 p, float2 b, float r) { float2 q = abs(p) - b + r; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r; }
// 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 plush colors: the palette softened, plus cream and caramel.
float3 plushColor(float h)
{
return lerp(h < 0.25 ? colorA.rgb : (h < 0.5 ? colorB.rgb : (h < 0.7 ? colorC.rgb : (h < 0.85 ? float3(0.96, 0.9, 0.8) : float3(0.82, 0.6, 0.42)))), float3(1, 1, 1), 0.22);
}
// A plush head at q (bar heights, from its center), radius r. kind: 0 bear, 1 bunny, 2 cat, 3 frog.
void plushHead(inout float3 acc, inout float cover, float2 q, float r, float kind, float3 col, float a, float aa)
{
float2 e = float2(abs(q.x), q.y);
float dHead = (length(q / float2(1.0 + 0.1 * step(1.5, kind) + 0.14 * step(2.5, kind), 1.0)) - r) * 0.94;
float dBear = length(e - float2(0.68, 0.7) * r) - 0.32 * r;
float dBun = (length((e - float2(0.34, 1.22) * r) / (float2(0.21, 0.55) * r)) - 1.0) * 0.21 * r;
float2 o = rot(e - float2(0.6, 0.6) * r, -0.35);
float dCat = max(-o.y, (abs(o.x) * 0.62 + o.y * 0.32 - 0.1984 * r) / 0.6977) - 0.03 * r;
float dFrog = length(e - float2(0.46, 0.74) * r) - 0.3 * r;
float dEar = kind < 0.5 ? dBear : (kind < 1.5 ? dBun : (kind < 2.5 ? dCat : dFrog));
float d = min(dHead, dEar);
layer(acc, cover, col * 0.22, smoothstep(aa, -aa, d - aa * 1.4) * a);
float3 fur = col * (0.7 + 0.42 * saturate(q.y / r * 0.5 + 0.5));
float inner = (kind < 0.5 ? dBear + 0.13 * r : (kind < 1.5 ? dBun + 0.08 * r : dCat + 0.11 * r)) + step(2.5, kind);
fur = lerp(fur, float3(1.0, 0.72, 0.78), smoothstep(aa, -aa, inner) * step(0.0, dHead));
float muzzle = (length((q - float2(0.0, -0.36 * r)) / (float2(0.42, 0.27) * r)) - 1.0) * 0.27 * r;
fur = lerp(fur, float3(0.98, 0.93, 0.85), smoothstep(aa, -aa, muzzle) * step(kind, 0.5));
fur = lerp(fur, float3(0.25, 0.14, 0.12), smoothstep(aa, -aa, length((q - float2(0.0, -0.24 * r)) / float2(1.4, 1.0)) - 0.09 * r) * step(kind, 0.5));
fur = lerp(fur, float3(1.0, 0.55, 0.62), smoothstep(aa, -aa, length(q - float2(0.0, -0.16 * r)) - 0.07 * r) * step(0.5, kind) * step(kind, 2.5));
float smile = abs(length(q - float2(0.0, 0.12 * r)) - 0.5 * r) - 0.035 * r;
fur = lerp(fur, col * 0.25, smoothstep(aa, -aa, smile) * step(q.y, -0.2 * r) * step(2.5, kind));
fur = lerp(fur, float3(1.0, 0.55, 0.6), smoothstep(aa, -aa, length((e - float2(0.6, -0.2) * r) / float2(1.0, 0.55)) - 0.16 * r) * 0.7);
float2 ec = kind > 2.5 ? float2(0.46, 0.74) * r : float2(0.36, 0.04) * r;
fur = lerp(fur, float3(1, 1, 1), smoothstep(aa, -aa, length(e - ec) - 0.21 * r) * step(2.5, kind));
fur = lerp(fur, float3(0.08, 0.06, 0.1), smoothstep(aa, -aa, length(e - ec) - 0.13 * r));
fur = lerp(fur, float3(1, 1, 1), smoothstep(aa, -aa, length(e - ec - float2(-0.04, 0.05) * r) - 0.045 * r));
layer(acc, cover, fur, smoothstep(aa, -aa, d) * a);
}
// A sitting plush body under a head centered at the origin: belly, stubby arms and two feet.
void plushBody(inout float3 acc, inout float cover, float2 q, float r, float3 col, float aa)
{
float2 e = float2(abs(q.x), q.y);
float dBody = (length((q - float2(0.0, -1.12 * r)) / (float2(0.82, 0.72) * r)) - 1.0) * 0.72 * r;
float dArm = (length((e - float2(0.74, -0.98) * r) / (float2(0.22, 0.3) * r)) - 1.0) * 0.22 * r;
float dFoot = (length((e - float2(0.42, -1.76) * r) / (float2(0.3, 0.2) * r)) - 1.0) * 0.2 * r;
float d = min(min(dBody, dArm), dFoot);
layer(acc, cover, col * 0.22, smoothstep(aa, -aa, d - aa * 1.4));
float3 fur = col * (0.62 + 0.3 * saturate((q.y + 1.8 * r) / (1.2 * r)));
fur = lerp(fur, lerp(col, float3(1, 1, 1), 0.55), smoothstep(aa, -aa, length((q - float2(0.0, -1.2 * r)) / float2(1.0, 0.85)) - 0.42 * r));
fur = lerp(fur, lerp(col, float3(1, 1, 1), 0.45), smoothstep(aa, -aa, dFoot + 0.08 * r));
layer(acc, cover, fur, smoothstep(aa, -aa, d));
}
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;
float head = progress * size.x / u;
float3 white = float3(1, 1, 1);
float3 acc = float3(0, 0, 0);
float cover = 0;
float ahead = smoothstep(head - 0.3, head + 0.15, p.x);
// The pile under the glass ahead of the claw: a back row and a front row of plush heads, softened by the glass.
float bi = floor(p.x / 0.55);
float bh = hash1(bi * 3.1 + 1.0);
float2 bq = p - float2((bi + 0.5 + 0.25 * (bh - 0.5)) * 0.55, 0.3 + 0.07 * hash1(bi + 7.0));
plushHead(acc, cover, bq, 0.15 + 0.02 * bh, floor(hash1(bi + 3.3) * 3.99), plushColor(hash1(bi + 5.1)) * 0.75, ahead * step(0.35, bh) * 0.42, aa);
float fi = floor(p.x / 0.6 + 0.5);
float fh = hash1(fi * 7.7 + 2.0);
float2 fq = p - float2((fi + 0.25 * (fh - 0.5)) * 0.6, 0.1 + 0.06 * hash1(fi + 9.0));
plushHead(acc, cover, rot(fq, 0.25 * (fh - 0.5)), 0.18, floor(hash1(fi + 4.4) * 3.99), plushColor(hash1(fi + 6.2)), ahead * step(0.1, fh) * 0.62, aa);
// Plushies already won sit in a row behind the claw, bouncing on the beat and swaying to the music.
float wi = floor(p.x / 1.25);
float wh = hash1(wi * 5.3 + 4.0);
float wr = 0.2 + 0.02 * wh;
float2 wc = float2((wi + 0.5) * 1.25, 1.98 * wr + 0.005 + 0.09 * beat * (0.6 + 0.4 * wh));
float won = step(wc.x, head - 0.75);
float drop = saturate((head - 0.75 - wc.x) / 0.6); // a fresh prize drops in from above
wc.y += 1.0 - drop * drop;
float2 wq = rot(p - wc - float2(0.0, -1.8 * wr), 0.08 * sin(time * 2.4 + wi * 1.7) * (0.5 + level)) + float2(0.0, -1.8 * wr);
float3 wcol = plushColor(hash1(wi + 8.8));
if (won > 0.5)
{
plushBody(acc, cover, wq, wr, wcol, aa);
plushHead(acc, cover, wq, wr, floor(hash1(wi + 2.2) * 3.99), wcol, 1.0, aa);
}
// The gantry rail along the top with a chase of cabinet bulbs that twinkle with the music.
float rail = sdBox(p - float2(p.x, 0.943), float2(1.0, 0.018), 0.0);
float3 railCol = float3(0.7, 0.72, 0.78) * (0.55 + 0.6 * saturate((p.y - 0.925) / 0.036));
layer(acc, cover, float3(0.05, 0.05, 0.07), smoothstep(aa, -aa, rail - aa));
layer(acc, cover, railCol, smoothstep(aa, -aa, rail));
float li = floor(p.x / 0.45);
float2 lq = p - float2((li + 0.5) * 0.45, 0.943);
float lit = smoothstep(0.45, 0.0, frac(li / 7.0 - time * 1.4)) + 0.6 * beat;
float3 lcol = frac(li / 3.0) < 0.3 ? colorA.rgb : (frac(li / 3.0) < 0.6 ? colorC.rgb : colorB.rgb);
float ld = length(lq);
layer(acc, cover, lcol, exp(-max(ld - 0.03, 0.0) / 0.05) * saturate(lit) * 0.6);
layer(acc, cover, lerp(lcol * 0.35, lerp(lcol, white, 0.55), saturate(lit)), smoothstep(aa, -aa, ld - 0.032));
// The claw: the cable pays out on the beat so it dips into the pile, the prongs close a little, and it wiggles.
float dip = 0.17 * beat + 0.015 * sin(time * 2.1);
float wig = 0.2 * beat * sin(time * 40.0) + 0.05 * sin(time * 1.7);
float2 cq = rot(p - float2(head, 0.83), wig);
float hubTop = -0.06 - dip;
float dCable = max(abs(cq.x) - 0.013, max(cq.y, hubTop - cq.y));
float dHub = sdBox(cq - float2(0.0, hubTop - 0.055), float2(0.1, 0.055), 0.032);
float2 k = cq - float2(0.0, hubTop - 0.1);
float2 kf = float2(abs(k.x), k.y);
float open = 1.0 - 0.55 * beat;
float2 P1 = float2(0.08 + 0.16 * open, -0.13), P2 = float2(0.05 + 0.1 * open, -0.29);
float dSide = min(sdSeg(kf, float2(0.055, 0.0), P1), sdSeg(kf, P1, P2)) - 0.028;
dSide = min(dSide, length(kf - P2 - float2(-0.016, 0.0)) - 0.036);
float dMid = min(sdSeg(k, float2(0.0, 0.0), float2(0.0, -0.23)), sdSeg(k, float2(0.0, -0.23), float2(0.04 * open, -0.27))) - 0.026;
float dClaw = min(min(dSide, dMid), min(dHub, dCable));
layer(acc, cover, float3(1, 1, 1), exp(-max(dClaw, 0.0) / 0.06) * 0.12);
layer(acc, cover, float3(0.03, 0.03, 0.05), smoothstep(aa, -aa, dClaw - aa * 1.5));
float3 chromeS = lerp(float3(0.32, 0.34, 0.4), float3(0.82, 0.85, 0.92), smoothstep(0.004, -0.016, dSide)) + 0.45 * smoothstep(0.012, 0.0, abs(dSide + 0.02)); // round chrome: dark rims, a bright core line
float3 chromeM = lerp(float3(0.36, 0.38, 0.44), float3(0.88, 0.9, 0.96), smoothstep(0.024, 0.004, abs(k.x - 0.004))) + 0.35 * smoothstep(0.008, 0.0, abs(k.x + 0.006));
layer(acc, cover, float3(0.55, 0.57, 0.63), smoothstep(aa, -aa, dCable));
layer(acc, cover, chromeS, smoothstep(aa, -aa, dSide));
float3 hubCol = lerp(colorA.rgb * 0.75, float3(0.88, 0.9, 0.95), smoothstep(-0.01, 0.01, abs(cq.y - hubTop + 0.055) - 0.016));
hubCol *= 0.65 + 0.55 * saturate((cq.y - hubTop + 0.11) / 0.11);
layer(acc, cover, hubCol, smoothstep(aa, -aa, dHub));
layer(acc, cover, chromeM, smoothstep(aa, -aa, dMid));
// The carriage riding the rail, with a lamp that blinks faster on the beat.
float2 gq = p - float2(head, 0.88);
float dCar = sdBox(gq, float2(0.21, 0.06), 0.035);
float wheel = length(float2(abs(gq.x) - 0.12, gq.y - 0.062)) - 0.038;
layer(acc, cover, float3(0.03, 0.03, 0.05), smoothstep(aa, -aa, min(dCar, wheel) - aa * 1.4));
layer(acc, cover, float3(0.2, 0.2, 0.24) + 0.3 * smoothstep(0.03, 0.0, abs(length(float2(abs(gq.x) - 0.12, gq.y - 0.062)) - 0.02)), smoothstep(aa, -aa, wheel));
float3 car = colorA.rgb * (0.55 + 0.7 * saturate((gq.y + 0.06) / 0.12)) + white * 0.35 * smoothstep(0.012, 0.0, abs(gq.y - 0.033)) * step(abs(gq.x), 0.15);
layer(acc, cover, car, smoothstep(aa, -aa, dCar));
float blink = step(0.5, frac(time * 1.8));
float ldd = length(gq - float2(0.13, -0.005));
layer(acc, cover, colorC.rgb, exp(-max(ldd - 0.025, 0.0) / 0.04) * (0.3 + 0.6 * blink));
layer(acc, cover, lerp(colorC.rgb, white, 0.5 * blink), smoothstep(aa, -aa, ldd - 0.025));
// The glass ahead: slow diagonal reflections slide over the pile.
float g = frac((p.x - p.y * 0.55) / 4.2 - time * 0.04);
float refl = smoothstep(0.012, 0.0, abs(g - 0.5)) * 0.32 + smoothstep(0.06, 0.0, abs(g - 0.535)) * 0.14 + smoothstep(0.006, 0.0, abs(g - 0.56)) * 0.2;
layer(acc, cover, white, refl * ahead * step(p.y, 0.9));
return paint(acc / max(cover, 0.001), cover);
}