Lion Dance

by MusicBar, progress bar

A red and gold festival lion prances to the playhead through a shower of confetti, nodding and snapping its jaw on every drum beat while the mirror on its forehead flashes.

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 2b15488b8181a55acd2e9c45f52c9ac8807062489689b6c37871f26ccfd771eb

//! name: Lion Dance
//! author: MusicBar
//! kind: progress
//! description: A festival lion prances to the playhead through a shower of red and gold confetti, bobbing its head and snapping its jaw on every drum beat.

float hash1(float n) { return frac(sin(n * 127.1 + 311.7) * 43758.5453); }
float hash2(float2 p) { return frac(sin(dot(p, float2(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 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))); }
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); }

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

// A shape with a dark rim round it, so it holds on a light taskbar too.
void ink(inout float3 acc, inout float cover, float3 rgb, float d, float aa)
{
    layer(acc, cover, colorA.rgb * 0.1, smoothstep(aa, -aa, d - aa * 1.3));
    layer(acc, cover, rgb, smoothstep(aa, -aa, d));
}

// Round shading: a sphere normal from an ellipse, a key light from the upper front, a gloss spot.
float3 shade(float2 q, float2 c, float2 r, float3 base)
{
    float2 s = (q - c) / r;
    float3 n = normalize(float3(s, sqrt(saturate(1.0 - dot(s, s))) + 0.05));
    return base * (0.38 + 0.8 * saturate(dot(n, float3(0.47, 0.62, 0.62)))) + pow(saturate(dot(n, float3(0.26, 0.44, 0.86))), 24.0) * 0.45;
}

// One dancer's leg under the cloth: gold trousers bent at the knee, a fluffy fur cuff and a black shoe.
void leg(inout float3 acc, inout float cover, float2 q, float hx, float ph, float lift, float dim, float aa)
{
    float2 hip = float2(hx, 0.34 + lift), foot = float2(hx + 0.09 * cos(ph), 0.07 + 0.11 * max(sin(ph), 0.0));
    float2 knee = (hip + foot) * 0.5 + float2(0.07, 0.0), c = q - foot - float2(0.0, 0.04);
    ink(acc, cover, colorB.rgb * (0.5 + 0.5 * saturate((q.x - hx + 0.03) / 0.1)) * dim, min(sdSeg(q, hip, knee), sdSeg(q, knee, foot)) - 0.06, aa);
    float cuff = length(c / float2(1.0, 0.75)) - 0.07 - 0.012 * sin(atan2(c.y, c.x + 0.0001) * 9.0);
    layer(acc, cover, float3(0.97, 0.94, 0.86) * dim * (0.75 + 0.25 * saturate(c.y / 0.05 + 0.5)), smoothstep(aa, -aa, cuff));
    layer(acc, cover, float3(0.07, 0.06, 0.06) + 0.25 * smoothstep(-0.01, 0.02, q.y - foot.y), smoothstep(aa, -aa, ellipse(q - foot - float2(0.04, -0.02), float2(0.085, 0.035))));
}

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 lion always stands
    float head = progress * size.x, k = 0.92 * u;             // the lion's unit: a little under the bar height
    float2 q = (px - float2(head, 0.0)) / k;                   // the lion's frame, head at x = 0, feet at y = 0
    float aa = 1.0 / k, s = -q.x;                              // one pixel; how far back from the head
    float3 acc = float3(0, 0, 0);
    float cover = 0;
    float g = time * 8.0;                      // the dancers' stepping
    float hop = 0.03 * abs(sin(g));

    // Confetti drifting down over the played part, spinning so each paper flashes as it turns; densest near the lion.
    float2 cg = px / (0.2 * u) + float2(0.35 * time, 1.2 * time);
    float2 cell = floor(cg);
    float ch = hash2(cell), spin = time * (3.0 + 4.0 * hash2(cell)) + ch * 30.0;
    float2 cr = rot((frac(cg) - 0.25 - 0.5 * float2(hash2(cell + 7.1), hash2(cell + 3.7))) * 0.2 * u, ch * 6.28 + 0.5 * sin(time * 2.0 + ch * 9.0));
    float2 bd = abs(cr) - float2(0.012 + 0.04 * abs(cos(spin)), 0.022) * u;
    float paper = smoothstep(0.5, -0.5, length(max(bd, 0.0)) + min(max(bd.x, bd.y), 0.0));
    float many = 0.05 + 0.5 * exp(-max(s - 0.8, 0.0) / 3.5) + 0.12 * level + 0.25 * beat;
    float3 paperC = hash1(ch * 91.0) < 0.5 ? lerp(colorA.rgb, float3(0.9, 0.08, 0.1), 0.35) : lerp(colorB.rgb, float3(1.0, 0.78, 0.2), 0.35);
    layer(acc, cover, paperC * (0.55 + 0.7 * abs(sin(spin))), paper * step(ch, many) * smoothstep(0.7, 1.6, s));

    // Spent papers carpeting the ground behind it.
    float gi = floor(px.x / (0.05 * u)), gh = hash1(gi * 3.1);
    layer(acc, cover, gh < 0.72 ? colorA.rgb * 0.8 : colorB.rgb * 0.85, smoothstep(0.012 * u + 0.5, 0.012 * u - 0.5, abs(px.y - (0.015 + 0.035 * hash1(gi + 0.7)) * u)) * step(0.45, gh) * smoothstep(0.4, 1.2, s) * 0.8);

    // A warm self-lit glow round the whole lion, swelling on the beat.
    layer(acc, cover, colorA.rgb, exp(-max(min(sdSeg(q, float2(-2.1, 0.42), float2(-0.2, 0.48)) - 0.2, length(q - float2(0.0, 0.55)) - 0.36), 0.0) / 0.12) * (0.14 + 0.16 * level + 0.25 * beat));

    // The far legs of both dancers (darker), then the near ones, stepping out of phase.
    leg(acc, cover, q, -0.36, g + 3.14, hop, 0.6, aa);
    leg(acc, cover, q, -1.78, g + 1.57, hop, 0.6, aa);
    leg(acc, cover, q, -0.1, g, hop, 1.0, aa);
    leg(acc, cover, q, -1.52, g + 4.71, hop, 1.0, aa);

    // The cloth body: high at the neck, a dip, a hump over the back dancer, then the rump; it ripples backward.
    float top = 0.6 + 0.03 * sin(s * 2.6 - time * 4.0) + hop + 0.06 * exp(-pow((s - 0.3) / 0.3, 2.0)) - 0.07 * exp(-pow((s - 0.95) / 0.3, 2.0))
              + 0.03 * exp(-pow((s - 1.6) / 0.3, 2.0)) - 0.34 * smoothstep(1.75, 2.4, s);
    float hem = 0.33 + hop + 0.012 * sin(s * 11.0 - time * 7.0);
    float fl = (hem - q.y) / (0.06 + 0.025 * sin(s * 23.0));
    float strand = abs(frac((s + 0.015 * sin(time * 6.0 + s * 4.0) * fl) * 14.0) - 0.5) - 0.32 + 0.3 * fl;
    layer(acc, cover, lerp(float3(1.0, 0.95, 0.85), colorB.rgb, 0.45), smoothstep(0.08, -0.08, strand) * step(0.0, fl) * step(fl, 1.0) * step(0.1, s) * step(s, 2.3));
    float v = saturate((q.y - hem) / max(top - hem, 0.01));
    float2 sc = float2(s / 0.17, (q.y - hem) / 0.1 + 0.3);
    sc.x += 0.5 * floor(sc.y);
    float scl = length((frac(sc) - float2(0.5, 0.0)) * float2(1.0, 1.3));
    float3 cloth = colorA.rgb * (0.35 + 0.75 * v) * (0.75 + 0.45 * smoothstep(0.5, 0.1, scl));
    cloth = lerp(cloth, lerp(colorB.rgb, float3(1, 0.9, 0.5), 0.3) * (0.6 + 0.5 * v), smoothstep(0.09, 0.02, abs(scl - 0.5)) * 0.85);
    ink(acc, cover, lerp(cloth, colorB.rgb * 0.9, smoothstep(0.022, 0.008, abs(q.y - hem - 0.03))), max(max(q.y - top, hem - q.y), max(s - 2.45, 0.05 - s)), aa);
    float ridge = smoothstep(aa, -aa, q.y - top - 0.03 - 0.022 * abs(sin(s * 15.0 - time * 3.0))) * smoothstep(-aa, aa, q.y - top + 0.035) * smoothstep(0.1, 0.2, s) * smoothstep(2.25, 2.0, s);
    layer(acc, cover, lerp(float3(1, 0.96, 0.88), colorB.rgb, 0.3 + 0.3 * sin(s * 40.0)), ridge);

    // The tail: a fluffy plume at the rump, flicking up with the steps and on the beat.
    float2 tq = rot(q - float2(-2.2, 0.4 + hop), -(0.55 + 0.25 * sin(time * 5.0) + 0.5 * beat));
    ink(acc, cover, lerp(colorA.rgb * 0.9, lerp(colorB.rgb, float3(1, 0.97, 0.9), 0.5), smoothstep(-0.05, -0.2, tq.x)) * (0.85 + 0.15 * sin(tq.y * 160.0)),
        length(tq / float2(1.0, 0.45) - float2(-0.15, 0.0)) - 0.15 - 0.012 * sin(atan2(tq.y, tq.x + 0.0001) * 10.0 + time * 4.0), aa);

    // The head rides on the front dancer: it dips and nods on the beat and looks around a little.
    float2 hq = rot(q - float2(0.04, 0.56 + hop - 0.08 * beat + 0.02 * sin(time * 2.1)), 0.24 * beat - 0.07 * sin(time * 1.6));
    float jaw = 0.06 + 0.45 * beat + 0.05 * level * (0.5 + 0.5 * sin(time * 11.0));
    float2 J = float2(-0.12, -0.07);
    float2 jq = rot(hq - J, jaw) + J;                          // the pixel seen in the jaw's closed pose

    // Mane: a ruff of fur round the back of the face, strands fluttering; a white beard under the jaw.
    float2 mq = hq - float2(-0.08, 0.02);
    float ma = atan2(mq.y, mq.x + 0.0001);
    float mr = 0.35 + 0.03 * cos(ma * 12.0 + 0.5 * sin(time * 5.0)) + 0.02 * sin(ma * 5.0 - time * 6.0);
    float2 bq = jq - float2(0.0, -0.24);
    float dBeard = length(bq / float2(1.0, 0.65)) - 0.15 - 0.015 * cos(atan2(bq.y, bq.x + 0.0001) * 10.0 + time * 3.0);
    layer(acc, cover, colorA.rgb * 0.1, smoothstep(aa, -aa, min(length(mq) - mr, dBeard) - aa * 1.3));
    layer(acc, cover, lerp(colorB.rgb, float3(1, 0.97, 0.9), smoothstep(mr - 0.1, mr, length(mq)) * 0.8) * (0.8 + 0.2 * sin(ma * 45.0)), smoothstep(aa, -aa, length(mq) - mr));
    layer(acc, cover, lerp(float3(0.97, 0.95, 0.9), colorB.rgb, 0.2) * (0.8 + 0.2 * sin(bq.x * 90.0)), smoothstep(aa, -aa, dBeard));

    // Mouth cavity, then the lower jaw with its teeth.
    float2 kq = rot(hq - J, 0.5 * jaw) - float2(0.22, -0.04);
    ink(acc, cover, lerp(float3(0.25, 0.02, 0.04), float3(0.8, 0.15, 0.2), saturate(kq.y / 0.12 + 0.5)), ellipse(kq, float2(0.25, 0.03 + 0.22 * jaw)), aa);
    float3 jawC = lerp(shade(jq, float2(0.1, -0.15), float2(0.25, 0.12), colorA.rgb * 0.85), float3(1, 0.98, 0.92), step(-0.135 + 0.08 * abs(frac(jq.x / 0.06) - 0.5), jq.y) * step(0.03, jq.x));
    ink(acc, cover, jawC, ellipse(jq - float2(0.1, -0.15), float2(0.25, 0.075)), aa);

    // The upper head: round and glossy with a gold rim, a ball nose, and teeth hanging from the upper lip.
    float dHead = smin(ellipse(hq - float2(-0.02, 0.06), float2(0.28, 0.26)), ellipse(hq - float2(0.17, -0.02), float2(0.18, 0.08)), 0.06);
    float3 headC = shade(hq, float2(-0.02, 0.06), float2(0.33, 0.32), colorA.rgb);
    ink(acc, cover, lerp(headC, colorB.rgb * 1.1, smoothstep(0.022, 0.008, abs(dHead + 0.035)) * step(hq.y, 0.2)), dHead, aa);
    float tux = abs(frac((hq.x - 0.03) / 0.06) - 0.5) * 2.0;
    layer(acc, cover, float3(1, 0.98, 0.92) * (0.8 + 0.2 * tux), step(hq.y, -0.07) * step(-0.095 - 0.045 * tux, hq.y) * step(0.05, hq.x) * step(hq.x, 0.3));
    layer(acc, cover, shade(hq, float2(0.34, 0.0), float2(0.055, 0.055), colorC.rgb), smoothstep(aa, -aa, length(hq - float2(0.34, 0.0)) - 0.055));

    // The horn on top, ringed in gold, curving forward.
    float2 ha = float2(-0.1, 0.25), hb = float2(-0.02, 0.41);
    float hh = saturate(dot(hq - ha, hb - ha) / dot(hb - ha, hb - ha));
    ink(acc, cover, lerp(colorC.rgb, colorB.rgb, step(0.5, frac(hh * 3.5))) * (0.7 + 0.5 * saturate((hq.x - ha.x) / 0.08)), length(hq - ha - (hb - ha) * hh) - lerp(0.045, 0.014, hh), aa);

    // The mirror on the forehead in a gold frame, flashing on the beat.
    float2 M = float2(0.06, 0.245), sq = abs(hq - M);
    float dMir = length(hq - M) - 0.042;
    ink(acc, cover, colorB.rgb, dMir - 0.016, aa);
    layer(acc, cover, lerp(float3(0.62, 0.72, 0.85), float3(1, 1, 1), smoothstep(0.025, 0.0, abs(hq.x - M.x + (hq.y - M.y) * 0.8))) + 0.4 * beat, smoothstep(aa, -aa, dMir));
    layer(acc, cover, float3(1, 1, 0.92), beat * (exp(-sq.x / 0.01 - sq.y / 0.14) + exp(-sq.y / 0.01 - sq.x / 0.14)));

    // The big round eye with a colored lid, a glint, and long lashes; it blinks now and then.
    float2 E = float2(0.16, 0.06);
    float dEye = length(hq - E) - 0.095;
    float3 eyeC = lerp(float3(1, 1, 1), float3(0.75, 0.8, 0.9), saturate((E.y - hq.y) / 0.2 + 0.3));
    eyeC = lerp(eyeC, float3(0.05, 0.04, 0.08), smoothstep(aa, -aa, length(hq - E - float2(0.035, -0.005)) - 0.052));
    eyeC = lerp(eyeC, float3(1, 1, 1), smoothstep(aa, -aa, length(hq - E - float2(0.055, 0.02)) - 0.018));
    eyeC = lerp(eyeC, colorC.rgb * 0.9, step(hq.y, E.y + 0.1) * step(E.y + 0.065 - 0.2 * step(frac(time / 3.7), 0.04), hq.y));
    float la = atan2(hq.y - E.y, hq.x - E.x + 0.0001);         // lashes fan out over the upper lid
    layer(acc, cover, float3(0.06, 0.04, 0.05), smoothstep(0.3, 0.12, abs(frac(la / 0.45) - 0.5)) * step(0.5, la) * step(la, 2.7) * step(dEye, 0.07) * step(0.0, dEye));
    layer(acc, cover, float3(0.05, 0.03, 0.04), smoothstep(aa, -aa, dEye - 0.022));
    layer(acc, cover, eyeC, smoothstep(aa, -aa, dEye));

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