Capybara Spa

by MusicBar, visualizer

Capybaras soak in a steaming hot spring with a little citrus on their heads and their eyes half closed, bobbing gently while the steam curls higher as the song gets louder and ripples spread on every beat.

5 downloads Download file, 9.6 KB
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 b30451e6ab95afff681db90fc1361ce8a2c9607f0a4054c7126611301d318d79

//! name: Capybara Spa
//! author: MusicBar
//! kind: visualizer
//! description: Capybaras soak in a steaming hot spring with a little citrus on their heads and their eyes half closed, bobbing gently while the steam curls higher as the song gets louder and ripples spread 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).
float ellipse(float2 p, float2 r) { float k0 = length(p / r); return k0 * (k0 - 1.0) / max(length(p / (r * r)), 0.0001); }

// Signed distance to a rounded box (half size b, corner r).
float boxD(float2 p, float2 b, float r) { float2 d = abs(p) - b + r; return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r; }

// Smooth union, so head, neck and back melt into one body.
float smin(float a, float b, float k) { float h = saturate(0.5 + 0.5 * (b - a) / k); return lerp(b, a, h) - k * h * (1.0 - h); }

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

// A shaded citrus ball: dimpled peel, a soft key light and a glint.
float3 citrus(float2 p, float r)
{
    float2 s = p / r;
    float z = sqrt(saturate(1.0 - dot(s, s)));
    float dim = 0.94 + 0.06 * sin(s.x * 23.0) * sin(s.y * 21.0);
    float spec = pow(saturate(dot(normalize(float3(s, z)), normalize(float3(-0.4, 0.6, 0.7)))), 18.0);
    return colorC.rgb * (0.45 + 0.65 * saturate(z * 0.7 + s.y * 0.3 - s.x * 0.2)) * dim + spec * 0.6;
}

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

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 capybaras sit upright
    float wl = 0.34 * u, wy = 0.14 * u;                      // the far rim of the pool, and where each capybara meets the water
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // The pool: a translucent jade band with a slow shimmer on its surface.
    float wv = px.y / wl;
    float sheen = pow(saturate(sin(px.x / u * 3.1 + sin(px.x / u * 1.3 + time * 0.6) * 2.0 + time * 0.4 + wv * 4.0) * 0.5 + 0.5), 12.0);
    layer(acc, cover, lerp(colorA.rgb * 0.45, colorA.rgb * 0.8, wv), step(px.y, wl) * lerp(0.5, 0.32, wv));
    layer(acc, cover, lerp(colorA.rgb, float3(1, 1, 1), 0.6), sheen * step(px.y, wl) * 0.25);
    layer(acc, cover, lerp(colorA.rgb, float3(1, 1, 1), 0.5), smoothstep(1.2, 0.0, abs(px.y - wl)) * 0.5);

    // Smooth stones along the far rim, here and there.
    float rw = 0.55 * u, rc = floor(px.x / rw), rh = hash1(rc * 4.7 + 1.0);
    float2 rq = (px - float2((rc + 0.3 + 0.4 * hash1(rc * 2.3)) * rw, wl + 0.02 * u)) / u;
    float dRock = ellipse(rq, float2(0.16 + 0.1 * rh, 0.08 + 0.05 * rh));
    float3 rock = lerp(float3(0.2, 0.2, 0.22), float3(0.5, 0.5, 0.52), saturate(rq.y / 0.12 + 0.4));
    layer(acc, cover, rock, fill(dRock * u, 1.0) * step(0.62, rh));

    // Steam: wavy ribbons curling up off the water; louder music lets more of them rise, and brighter.
    float sw = 0.55 * u, sc = floor(px.x / sw), sh = hash1(sc * 3.1 + 2.0);
    float sy = px.y / u, sph = frac(time * (0.22 + 0.1 * sh) + sh * 7.0);
    float stop = 0.3 + sph * 1.1;
    float wig = 0.07 * sin(sy * 10.0 - time * 2.6 + sh * 6.28) * (0.3 + sy);
    float sdx = abs(px.x / u - (sc + 0.5) * 0.55 - wig + (sh - 0.5) * 0.2);
    float spec = spectrum(frac(sc * 0.137) * 0.8);
    float steamOn = step(sh, 0.3 + 0.6 * level) * (0.35 + 0.65 * spec);
    float seg = smoothstep(stop - 0.6, stop - 0.3, sy) * smoothstep(stop, stop - 0.15, sy) * smoothstep(0.3, 0.42, sy);
    float3 steamCol = lerp(float3(1, 1, 1), colorB.rgb, 0.35);
    float sw2 = 0.012 + 0.015 * sy + 0.6 / u;                 // ribbons thicken as they rise
    layer(acc, cover, colorA.rgb * 0.35, smoothstep(sw2 + 1.5 / u, sw2 - 0.006, sdx) * seg * steamOn * 0.3);   // a faint shade so they show on light taskbars
    layer(acc, cover, steamCol, (smoothstep(sw2, sw2 - 0.012, sdx) * 0.6 + exp(-sdx / 0.07) * 0.3) * seg * steamOn);
    layer(acc, cover, steamCol, (0.5 + 0.5 * sin(px.x / u * 1.7 + time * 0.3)) * smoothstep(0.95, 0.35, sy) * smoothstep(0.3, 0.45, sy) * (0.03 + 0.07 * level));

    // One capybara per spot, facing either way, a little different in size.
    float W = 2.7 * u;
    float seats = max(floor(size.x / W), 1.0);
    float x0 = (size.x - seats * W) * 0.5;
    float i = clamp(floor((px.x - x0) / W), 0.0, seats - 1.0);
    float cx = x0 + (i + 0.5) * W;
    float h = hash1(i * 1.93 + 0.4);
    float face = step(0.5, hash1(i * 7.1 + 3.0)) * 2.0 - 1.0;
    float k = 1.2 * (0.9 + 0.15 * h);                        // capybara unit, in bar heights
    float pw = 1.0 / (u * k);

    // Ripples on the water: slow rings now and then, and a ring bursting out on each beat.
    float2 wq = (px - float2(cx, wy)) / u;
    float rr1 = frac(time * 0.3 + h) * 1.25;
    float ring1 = abs(ellipse(wq, float2(rr1, rr1 * 0.2) + 0.001));
    float rr2 = 0.6 + 0.65 * (1.0 - beat);
    float ring2 = abs(ellipse(wq, float2(rr2, rr2 * 0.2)));
    float3 ripCol = lerp(colorA.rgb, float3(1, 1, 1), 0.65);
    float ripA = smoothstep(0.012 + 1.2 / u, 0.0, ring1) * pow(1.0 - rr1 / 1.25, 2.0) * 0.6 + smoothstep(0.016 + 1.2 / u, 0.0, ring2) * beat;
    layer(acc, cover, ripCol, ripA * step(0.0, wq.y) * step(px.y, wl));

    // The capybara's frame: facing +x, y up, origin at its waterline under the neck; it bobs gently.
    float bob = (0.018 + 0.02 * level) * sin(time * 1.3 + h * 6.0) - 0.02 * beat;
    float2 c = float2((px.x - cx - face * 0.25 * u) * face, px.y - wy) / (u * k) - float2(0.0, bob);
    float dHead = boxD(rot(c - float2(0.26, 0.29), 0.1), float2(0.26, 0.16), 0.13);
    dHead = smin(dHead, ellipse(c - float2(0.14, 0.18), float2(0.2, 0.15)), 0.05);
    float dCapy = smin(smin(dHead, ellipse(c - float2(-0.06, 0.12), float2(0.22, 0.2)), 0.08), ellipse(c - float2(-0.42, 0.03), float2(0.42, 0.2)), 0.12);
    float dEar = ellipse(rot(c - float2(0.04, 0.445), -0.4), float2(0.058, 0.045));
    float above = smoothstep(-0.02, 0.01, c.y);

    // Fur: warm brown, lit from above, a darker blunt muzzle, a soft rim in the steam color.
    float3 fur = lerp(float3(0.36, 0.21, 0.12), float3(0.76, 0.52, 0.33), saturate(c.y / 0.42 + 0.1));
    fur = lerp(fur, float3(0.34, 0.2, 0.13), smoothstep(0.4, 0.54, c.x) * 0.55);
    fur += steamCol * smoothstep(-0.04, 0.0, dCapy) * saturate(c.y * 4.0) * 0.35;
    layer(acc, cover, float3(0.16, 0.08, 0.05), fill(min(dCapy, dEar) - 1.5 * pw, pw) * above);
    layer(acc, cover, float3(0.42, 0.25, 0.15), fill(dEar, pw));
    layer(acc, cover, fur, fill(dCapy, pw));
    layer(acc, cover, float3(0.25, 0.13, 0.08), fill(ellipse(rot(c - float2(0.045, 0.445), -0.4), float2(0.026, 0.019)), pw));

    // A sleepy half-closed eye (a heavy lid over a dark sliver), a rosy cheek, the nostril and a calm mouth.
    float2 eq = c - float2(0.25, 0.36);
    float dEye = ellipse(eq, float2(0.05, 0.034));
    layer(acc, cover, float3(0.06, 0.03, 0.03), fill(max(dEye, eq.y + 0.002), pw));
    layer(acc, cover, float3(0.18, 0.09, 0.05), fill(abs(eq.y + 0.002 - 0.25 * eq.x * eq.x / 0.05) - 0.5 * pw - 0.007, pw) * step(abs(eq.x), 0.06));
    layer(acc, cover, float3(1.0, 0.45, 0.45), smoothstep(0.03, -0.02, ellipse(c - float2(0.31, 0.26), float2(0.065, 0.035))) * 0.45);
    layer(acc, cover, float3(0.1, 0.05, 0.04), fill(ellipse(rot(c - float2(0.495, 0.34), 0.5), float2(0.024, 0.014)), pw));
    layer(acc, cover, float3(0.16, 0.08, 0.05), fill(sdSeg(c, float2(0.38, 0.21), float2(0.47, 0.22)) - 0.4 * pw - 0.004, pw));

    // The citrus on its head: it hops on the beat and lands again; a leaf on top.
    float hop = 0.08 * beat;
    float2 yq = c - float2(0.16, 0.51 + hop);
    float dYuzu = length(yq) - 0.085;
    layer(acc, cover, float3(0, 0, 0), smoothstep(0.03, -0.01, ellipse(c - float2(0.16, 0.435), float2(0.07 - 0.03 * beat, 0.02))) * 0.3);
    layer(acc, cover, colorC.rgb * 0.3, fill(dYuzu - 1.5 * pw, pw));
    layer(acc, cover, citrus(yq, 0.085), fill(dYuzu, pw));
    float2 lq = rot(yq - float2(0.03, 0.09), -0.6 + 0.2 * sin(time * 2.0 + h * 5.0));
    layer(acc, cover, lerp(float3(0.2, 0.55, 0.2), float3(0.5, 0.85, 0.35), saturate(lq.y / 0.03 + 0.5)), fill(ellipse(lq - float2(0.04, 0.0), float2(0.045, 0.02)), pw));

    // A citrus adrift by its nose.
    float2 fq = c - float2(0.66, -0.01 + 0.012 * sin(time * 1.7 + h * 9.0));
    layer(acc, cover, colorC.rgb * 0.3, fill(length(fq) - 0.07 - 1.5 * pw, pw));
    layer(acc, cover, citrus(fq, 0.07), fill(length(fq) - 0.07, pw));

    // The near water over everything below the waterline, a bright line where it meets the fur, front halves of the rings.
    float under = smoothstep(0.01, -0.01, c.y + 0.008 * sin(c.x * 26.0 + time * 2.0));
    layer(acc, cover, colorA.rgb * 0.6, under * step(px.y, wl) * 0.6);
    layer(acc, cover, lerp(colorA.rgb, float3(1, 1, 1), 0.7), smoothstep(0.02, 0.0, abs(c.y + 0.008 * sin(c.x * 26.0 + time * 2.0))) * step(min(dCapy, length(fq) - 0.07), 0.015) * 0.75);
    layer(acc, cover, ripCol, ripA * step(wq.y, 0.0));

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