Spin Cat

by MusicBar, visualizer

A few round ginger cats spin like tops along the bar inside neon speed rings, whirling and smearing harder as the song gets louder and popping up into the air 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 6b8990c8f7cb0e3578fc2e2f51d84c845daaf8c9e4e01e1354d70d3601fe450c

//! name: Spin Cat
//! author: MusicBar
//! kind: visualizer
//! description: A few round ginger cats spin like tops along the bar inside neon speed rings, whirling and smearing harder as the song gets louder and popping up into the air on every beat.

// A steady random number for a whole number.
float hash1(float n) { n = frac(n * 0.1031); n *= n + 33.33; n *= n + n; return frac(n); }

// Turns a point around the origin by a (radians, counterclockwise).
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); }

// Signed distance to an ellipse with radii r (close enough near the edge), and to a segment.
float ellipse(float2 p, float2 r) { float k0 = length(p / r); return k0 * (k0 - 1.0) / max(length(p / (r * r)), 0.0001); }
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))); }

// Anti-aliased fill of a distance d, where pw is one pixel in the same units.
float fill(float d, float pw) { return smoothstep(0.6 * pw, -0.6 * pw, d); }

// 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); }

// Signed distance to the triangle a, b, c.
float tri(float2 p, float2 a, float2 b, float2 c)
{
    float2 e0 = b - a, e1 = c - b, e2 = a - c, v0 = p - a, v1 = p - b, v2 = p - c;
    float2 k0 = v0 - e0 * saturate(dot(v0, e0) / dot(e0, e0)), k1 = v1 - e1 * saturate(dot(v1, e1) / dot(e1, e1)), k2 = v2 - e2 * saturate(dot(v2, e2) / dot(e2, e2));
    float s = sign(e0.x * e2.y - e0.y * e2.x);
    float2 d = min(min(float2(dot(k0, k0), s * (v0.x * e0.y - v0.y * e0.x)), float2(dot(k1, k1), s * (v1.x * e1.y - v1.y * e1.x))), float2(dot(k2, k2), s * (v2.x * e2.y - v2.y * e2.x)));
    return -sqrt(d.x) * sign(d.y);
}

// An ear seen at screen longitude lam: a rounded triangle on the crown, thin when it turns edge-on.
float ear(float2 q, float lam)
{
    float sx = sin(lam), w = 0.09 * max(abs(cos(lam)), 0.28);
    return tri(q, float2(0.2 * sx - w, 0.57), float2(0.2 * sx + w, 0.57), float2(0.27 * sx, 0.75)) - 0.016;
}

// The tail seen at screen longitude lam: it leaves the back of the body and curls up.
float tail(float2 q, float lam, float wag)
{
    float sx = sin(lam);
    float2 a = float2(0.12 * sx, 0.1), b = float2(0.3 * sx, 0.07), c = float2(0.42 * sx, 0.2), d = float2((0.4 + 0.04 * wag) * sx, 0.36 + 0.03 * wag);
    return min(min(sdSeg(q, a, b), sdSeg(q, b, c)), sdSeg(q, c, d)) - 0.043;
}

// A round fur ball: soft key light, neon rims in the two palette colors, a glint.
float3 shade(float2 s, float z, float3 base)
{
    float3 n = normalize(float3(s, z + 0.05));
    float dif = saturate(dot(n, normalize(float3(-0.4, 0.65, 0.65)))), spec = pow(saturate(dot(n, normalize(float3(-0.35, 0.5, 1.0)))), 30.0);
    return base * (0.38 + 0.75 * dif) + (colorA.rgb * saturate(-s.x * 1.6) + colorB.rgb * saturate(s.x * 1.6)) * pow(1.0 - z, 2.5) + spec * 0.35;
}

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float u = size.y;
    float2 px = float2(uv.x * size.x, (1.0 - uv.y) * u);     // pixels, y up the screen, so the cats stand upright
    float W = 3.0 * u, seats = max(floor(size.x / W), 1.0);  // one spot per cat, the row centered on the bar
    float x0 = (size.x - seats * W) * 0.5, i = clamp(floor((px.x - x0) / W), 0.0, seats - 1.0), cx = x0 + (i + 0.5) * W;
    float h = hash1(i * 2.31 + 0.7), dir = step(0.5, frac(i * 0.5)) * 2.0 - 1.0;   // neighbors spin opposite ways
    float pw = 1.0 / (1.1 * u);                               // one pixel, in cat units (a cat unit is 1.1 bar heights)

    // How hard this cat spins: its own band and the loudness; every beat throws in one extra whole turn.
    float band = spectrum(abs(cx / size.x - 0.5) * 1.6);
    float drive = saturate(0.4 * level + 0.75 * band);
    float theta = dir * (time * (7.0 + 3.0 * h) + h * 6.2832 - 6.2832 * beat);
    float smear = 0.04 + 0.3 * drive + 1.3 * beat;           // motion blur, in radians of turn

    // Pop up on the beat, stretched; squash on the landing; wobble like a top.
    float land = smoothstep(0.0, 0.1, beat) * smoothstep(0.4, 0.12, beat);
    float2 p = (px - float2(cx, 0.0)) * pw;
    float2 q = rot(p - float2(0.0, 0.09 * beat), 0.07 * sin(theta) * (0.4 + drive));
    q = float2(q.x / (1.0 - 0.08 * beat + 0.12 * land), q.y / (1.0 + 0.08 * beat - 0.14 * land));
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // A neon spot under each cat, its shadow (smaller while it is up), and a shock ring when it lands.
    layer(acc, cover, colorC.rgb, exp(-max(length(p / float2(0.5, 0.07)) - 0.5, 0.0) * 2.0) * (0.2 + 0.35 * drive));
    layer(acc, cover, float3(0, 0, 0), smoothstep(1.0, 0.3, length(p / float2(0.27 - 0.08 * beat, 0.045))) * 0.55);
    float shock = abs(ellipse(p - float2(0.0, 0.03), float2(0.3 + 0.75 * (1.0 - beat), 0.05 + 0.1 * (1.0 - beat))));
    layer(acc, cover, lerp(colorC.rgb, float3(1, 1, 1), 0.3), (exp(-shock / 0.025) + fill(shock - 0.006, pw)) * beat * 0.8);

    // Two neon speed rings with comet dashes racing round; the far halves sit behind the cat.
    float2 r1c = float2(0.0, 0.07), r1r = float2(0.4 + 0.08 * drive, 0.06), r2c = float2(0.0, 0.3), r2r = float2(0.5 + 0.1 * drive, 0.08);
    float2 s1 = (q - r1c) / r1r, s2 = (q - r2c) / r2r;
    float d1 = abs(ellipse(q - r1c, r1r)), d2 = abs(ellipse(q - r2c, r2r)), ringOn = 0.25 + 0.75 * drive + 0.5 * beat;
    float c1 = pow(frac(dir * (atan2(s1.y, s1.x) - 1.25 * theta) / 3.1416), 2.5), c2 = pow(frac(dir * (atan2(s2.y, s2.x) - 1.1 * theta + 1.3) / 3.1416), 2.5);
    float g1 = (exp(-d1 / 0.03) * 0.55 + fill(d1 - 0.012, pw)) * (0.12 + 0.88 * c1) * ringOn, g2 = (exp(-d2 / 0.03) * 0.55 + fill(d2 - 0.012, pw)) * (0.12 + 0.88 * c2) * ringOn;
    float3 n1 = lerp(colorA.rgb, float3(1, 1, 1), 0.5 * c1 * fill(d1 - 0.012, pw)), n2 = lerp(colorB.rgb, float3(1, 1, 1), 0.5 * c2 * fill(d2 - 0.012, pw));
    layer(acc, cover, n1, g1 * smoothstep(-0.2, 0.2, s1.y));
    layer(acc, cover, n2, g2 * smoothstep(-0.2, 0.2, s2.y));

    // The parts that turn: tail and ears follow the spin, each with a faint ghost trailing behind.
    float wag = sin(time * 6.0 + h * 4.0);
    float lamT = theta + 3.1416, lamL = theta - 0.62, lamR = theta + 0.62, lag = dir * smear * 0.45;
    float dT = tail(q, lamT, wag), dTg = tail(q, lamT - lag, wag);
    float dEL = ear(q, lamL), dER = ear(q, lamR), dEg = min(ear(q, lamL - lag), ear(q, lamR - lag));
    float2 hc = float2(0.0, 0.45), hr = float2(0.26, 0.21), bc = float2(0.0, 0.17), br = float2(0.205, 0.17);
    float dHead = ellipse(q - hc, hr), dBody = ellipse(q - bc, br);
    float tF = step(0.0, cos(lamT)), lF = step(0.0, cos(lamL)), rF = step(0.0, cos(lamR));
    float3 fur = float3(1.0, 0.6, 0.25), stripe = float3(0.64, 0.24, 0.07), cream = float3(1.0, 0.95, 0.88);
    float3 tailCol = lerp(fur, stripe, step(0.5, frac((q.y + abs(q.x)) * 9.0)) * 0.7) * 0.85;

    // The outline keeps them readable on a light taskbar; ghosts and the far tail and ears go behind the body.
    layer(acc, cover, fur * 0.45, fill(min(dEg, dTg), pw) * 0.3 * saturate(smear - 0.3));
    layer(acc, cover, float3(0.18, 0.06, 0.04), fill(min(min(dHead, dBody), min(min(dEL, dER), dT)) - 1.5 * pw, pw));
    layer(acc, cover, tailCol * 0.75, fill(dT, pw) * (1.0 - tF));
    layer(acc, cover, fur * 0.7, fill(min(dEL + 9.0 * lF, dER + 9.0 * rF), pw));

    // The body: a ball painted in longitude, so its cream belly and stripes turn with it.
    float2 bs = (q - bc) / br;
    float bz = sqrt(saturate(1.0 - dot(bs, bs))), bl = atan2(bs.x, bz) - theta, bb = asin(clamp(bs.y, -1.0, 1.0));
    bl -= 6.2832 * floor(bl / 6.2832 + 0.5);
    float blur = saturate(smear * 1.2);
    float3 bcol = lerp(fur, stripe, lerp(smoothstep(0.4, 0.8, cos(bl * 5.0)) * step(1.2, abs(bl)), 0.25, blur));
    bcol = lerp(bcol, cream, smoothstep(0.75 + smear * 0.5, 0.55, abs(bl)) * smoothstep(0.9, 0.2, bb));
    layer(acc, cover, shade(bs, bz, bcol) * (1.0 - 0.4 * smoothstep(0.08, 0.0, dHead)), fill(dBody, pw));

    // White front paws peeking out at the foot, turning with the body (hidden while they face away).
    float la = theta - 0.38, lb = theta + 0.38;
    float dPa = ellipse(q - float2(0.17 * sin(la), 0.035), float2(0.06 * max(abs(cos(la)), 0.45), 0.042)) + 9.0 * step(cos(la), 0.0);
    float dPb = ellipse(q - float2(0.17 * sin(lb), 0.035), float2(0.06 * max(abs(cos(lb)), 0.45), 0.042)) + 9.0 * step(cos(lb), 0.0);
    layer(acc, cover, float3(0.18, 0.06, 0.04), fill(min(dPa, dPb) - 1.2 * pw, pw));
    layer(acc, cover, cream * (0.8 + 0.25 * saturate(q.y / 0.07)), fill(min(dPa, dPb), pw));
    layer(acc, cover, tailCol, fill(dT, pw) * tF);

    // The head: a ball with the face painted on it in longitude hl (0 at the nose) and latitude hb.
    float2 hs = (q - hc) / hr;
    float hz = sqrt(saturate(1.0 - dot(hs, hs))), hl = atan2(hs.x, hz) - theta, hb = asin(clamp(hs.y, -1.0, 1.0));
    hl -= 6.2832 * floor(hl / 6.2832 + 0.5);
    float sm = smear * 0.35, soft = 9.0 / (u * max(hz, 0.25));
    float3 col = lerp(fur, stripe, lerp(smoothstep(0.55, 0.9, cos(hl * 6.0)), 0.15, blur) * smoothstep(0.3, 0.5, hb) * smoothstep(1.45, 1.1, hb));
    col = lerp(col, cream, smoothstep(1.0 + soft, 1.0 - soft, length(float2(max(abs(hl) - sm, 0.0) / 0.55, (hb + 0.4) / 0.33))));
    col = lerp(col, lerp(colorA.rgb, float3(1.0, 0.5, 0.6), 0.5), 0.45 * smoothstep(1.0, 0.6, length(float2(max(abs(abs(hl) - 0.66) - sm, 0.0) / 0.16, (hb + 0.14) / 0.1))));
    // Big eyes: dark, a hint of iris low down and two glints; they smear into streaks when it whirls.
    float2 de = float2(max(abs(abs(hl) - 0.5) - sm, 0.0), hb - 0.05);
    float eye = smoothstep(1.0 + soft, 1.0 - soft, length(de / float2(0.24, 0.33))) * 0.24 / (0.24 + sm);
    float3 eyeCol = lerp(float3(0.04, 0.03, 0.07), colorB.rgb * 0.6, smoothstep(-0.16, -0.32, hb - 0.05) * 0.35);
    eyeCol = lerp(eyeCol, float3(1, 1, 1), smoothstep(1.0 + soft * 2.0, 1.0 - soft * 2.0, length(float2(max(abs(abs(hl) - 0.44) - sm, 0.0), hb - 0.16) / 0.075)));
    eyeCol = lerp(eyeCol, float3(1, 1, 1), 0.7 * smoothstep(1.0 + soft * 3.0, 1.0 - soft * 3.0, length(float2(max(abs(abs(hl) - 0.56) - sm, 0.0), hb + 0.07) / 0.04)));
    col = lerp(col, eyeCol, eye);
    // Pink nose, a w-shaped mouth that opens into a yowl on the beat, faint whiskers.
    col = lerp(col, float3(1.0, 0.5, 0.58), smoothstep(1.0 + soft, 1.0 - soft, length(float2(max(abs(hl) - sm, 0.0), hb + 0.17) / float2(0.09, 0.06))) * 0.09 / (0.09 + sm));
    float yowl = smoothstep(0.15, 0.5, beat);
    float wm = abs(length(float2(max(abs(abs(hl) - 0.07) - sm, 0.0), hb + 0.27)) - 0.07) + step(-0.27, hb);
    col = lerp(col, float3(0.25, 0.08, 0.06), smoothstep(0.035 + soft * 0.1, 0.015, wm) * (1.0 - yowl) * 0.1 / (0.1 + sm));
    col = lerp(col, float3(0.4, 0.05, 0.1), smoothstep(1.0 + soft, 1.0 - soft, length(float2(max(abs(hl) - sm, 0.0), hb + 0.4) / float2(0.13, 0.04 + 0.13 * yowl))) * yowl);
    col = lerp(col, cream, smoothstep(0.03, 0.01, min(abs(hb + 0.22 - 0.15 * (abs(hl) - 0.6)), abs(hb + 0.3 + 0.05 * (abs(hl) - 0.6)))) * step(0.6, abs(hl)) * step(abs(hl), 1.15) * (1.0 - blur) * 0.7);
    layer(acc, cover, shade(hs, hz, col), fill(dHead, pw));

    // Near ears in front of the head, pink inside while they face us.
    layer(acc, cover, lerp(fur * 0.9, float3(1.0, 0.62, 0.66), 0.7 * smoothstep(0.1, 0.5, cos(lamL)) * step(dEL + 0.035, 0.0)), fill(dEL, pw) * lF);
    layer(acc, cover, lerp(fur * 0.9, float3(1.0, 0.62, 0.66), 0.7 * smoothstep(0.1, 0.5, cos(lamR)) * step(dER + 0.035, 0.0)), fill(dER, pw) * rF);

    // The near halves of the rings pass in front.
    layer(acc, cover, n1, g1 * smoothstep(0.2, -0.2, s1.y));
    layer(acc, cover, n2, g2 * smoothstep(0.2, -0.2, s2.y));

    return paint(acc / max(cover, 0.001), cover);
}