Birds on Wires

by MusicBar, visualizer

Little round birds perch on five swaying power lines like notes on a staff, hopping between the wires with the melody and fluttering their wings on every beat.

4 downloads Download file, 6.4 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 aa884a975a092c6e0417c374ccfff2fd8a571ff2ac07b4e528eb54d0e040e794

//! name: Birds on Wires
//! author: MusicBar
//! kind: visualizer
//! description: Little round birds perch on five swaying power lines like notes on a music staff, hopping up and down between the wires with the melody and fluttering their wings on every beat.

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

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

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

// Height of wire i at x (bar heights): it hangs between poles in a gentle sag that sways in the breeze.
float wireY(float i, float x, float span)
{
    float f = frac(x / span);
    float n = floor(x / span);
    float sag = 0.035 + 0.02 * sin(time * 1.1 + n * 2.3 + i * 0.35) + 0.015 * level;
    return 0.07 + 0.12 * i - sag * 4.0 * f * (1.0 - f);
}

// 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 p = float2(uv.x * size.x, (1.0 - uv.y) * u) / u;   // bar heights, up the screen so the birds perch upright
    float aa = 1.0 / u;
    float span = 9.0;                                          // the distance between poles
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // The poles: thin wooden posts with a glass knob where each wire is tied on.
    float px0 = (floor(p.x / span + 0.5)) * span;
    float pole = max(abs(p.x - px0) - 0.035, p.y - 0.62);
    float knobs = length(float2(p.x - px0, (frac((p.y - 0.07) / 0.12 + 0.5) - 0.5) * 0.12)) - 0.038;
    knobs = max(knobs, max(p.y - 0.6, 0.02 - p.y));
    layer(acc, cover, float3(0.3, 0.24, 0.2), smoothstep(aa, -aa, pole) * 0.8);
    layer(acc, cover, lerp(colorB.rgb, float3(0.85, 0.95, 1.0), 0.5), smoothstep(aa, -aa, knobs) * 0.85);

    // The five wires, like a music staff.
    float wires = 0.0;
    for (int i = 0; i < 5; i++)
    {
        wires = max(wires, smoothstep(0.9 * aa, 0.1 * aa, abs(p.y - wireY(i, p.x, span))));
    }
    layer(acc, cover, lerp(colorB.rgb, float3(0.75, 0.78, 0.85), 0.5), wires * 0.7);

    // One bird per slot (some slots stay empty). Its band of the spectrum sets which wire it sits on;
    // between two wires it hops over with its wings open.
    float sw = 1.0;
    float k = floor(p.x / sw);
    float h = hash1(k * 2.7 + 1.0);
    float bx = (k + 0.35 + 0.3 * hash1(k * 5.3 + 2.0)) * sw;
    float s = spectrum(abs(bx * u / size.x - 0.5) * 2.0);
    float v = clamp(smoothstep(0.1, 0.8, s) * 4.2 + (h - 0.5) * 1.6 + 0.7 * sin(time * 0.5 + h * 40.0) - 0.1, 0.0, 4.0);
    float i0 = min(floor(v), 3.0);
    float t = smoothstep(0.42, 0.58, v - i0);
    float hopA = sin(t * 3.1416);
    float by = lerp(wireY(i0, bx, span), wireY(i0 + 1.0, bx, span), t) + 0.1 * hopA + 0.025 * beat;
    float dir = h < 0.5 ? 1.0 : -1.0;
    float S = 1.22;                                            // bird size
    float2 b = (p - float2(bx, by)) * float2(dir, 1.0) / S;
    float on = step(0.22, hash1(k * 9.1 + 4.0));
    float edge = smoothstep(0.0, 0.15, min(p.x - k * sw, (k + 1.0) * sw - p.x));   // keeps each glow inside its own slot

    // The bird: a round body and head, a tail tipping down, a beak, feet on the wire. It leans and bobs at rest.
    float lean = 0.12 * sin(time * 1.5 + h * 20.0) * (1.0 - hopA);
    b = rot(b, -lean);
    float body = smin(ellipse(b - float2(-0.01, 0.12), float2(0.125, 0.11)), length(b - float2(0.075, 0.2)) - 0.078, 0.05);
    float tail = ellipse(rot(b - float2(-0.16, 0.08), 0.5 + 0.15 * sin(time * 3.0 + h * 9.0)), float2(0.075, 0.025));
    float beak = ellipse(b - float2(0.16, 0.19), float2(0.032, 0.015));
    float legs = max(abs(abs(b.x) - 0.03) - 0.008, max(b.y - 0.04, -b.y - 0.01));
    float shape = min(min(body, tail), min(beak, legs));

    // Wings: folded at rest, flung up and beating on the beat and while hopping.
    float open = saturate(max(hopA, sqrt(beat) * 1.2));
    float wa = lerp(0.25, -1.45, open) + 0.35 * open * sin(time * 38.0);
    float2 wq = rot(b - float2(0.0, 0.15), -wa);
    float wing = ellipse(float2(wq.x + 0.08, wq.y / lerp(1.15, 0.55, saturate(-wq.x / 0.17))), float2(0.095, 0.042));

    float pick = frac(h * 13.0) * 4.0;
    float3 plume = pick < 1.0 ? colorA.rgb : (pick < 2.0 ? colorC.rgb : (pick < 3.0 ? colorB.rgb : lerp(colorA.rgb, colorC.rgb, 0.5)));
    layer(acc, cover, plume, exp(-max(min(shape, wing), 0.0) * S / 0.05) * (0.12 + 0.25 * s) * on * edge);
    layer(acc, cover, plume * 0.2, smoothstep(aa, -aa, min(shape, wing) * S - 1.2 * aa) * on);
    layer(acc, cover, float3(1.0, 0.7, 0.25), smoothstep(aa, -aa, min(beak, legs) * S) * on);
    layer(acc, cover, plume * 0.7, smoothstep(aa, -aa, tail * S) * on);
    float lit = saturate((b.y - 0.02) / 0.25);
    float3 bc = plume * (0.6 + 0.55 * lit);
    float belly = smoothstep(0.02, -0.02, ellipse(b - float2(0.04, 0.08), float2(0.09, 0.07)));
    bc = lerp(bc, lerp(plume, float3(1.0, 0.97, 0.9), 0.7), belly);
    bc += 0.3 * smoothstep(0.04, 0.0, length(b - float2(0.05, 0.24)) - 0.02);
    layer(acc, cover, bc, smoothstep(aa, -aa, body * S) * on);

    // Face: a bright eye that blinks, and a rosy cheek.
    float blink = step(0.96, frac(time * 0.31 + h * 7.0));
    layer(acc, cover, float3(0.04, 0.03, 0.05), smoothstep(aa, -aa, (length((b - float2(0.1, 0.21)) / float2(1.0, lerp(1.0, 0.2, blink))) - 0.022) * S) * on);
    layer(acc, cover, float3(1, 1, 1), smoothstep(aa, -aa, (length(b - float2(0.107, 0.219)) - 0.008) * S) * on * (1.0 - blink));
    layer(acc, cover, float3(1.0, 0.55, 0.6), smoothstep(0.03, 0.0, length(b - float2(0.09, 0.165))) * 0.5 * on);

    // The wing over the body, darker with light feather tips.
    float3 wc = lerp(plume * 0.45, plume * 0.85, saturate(wq.y / 0.04 + 0.5));
    wc = lerp(wc, lerp(plume, float3(1, 1, 1), 0.5), smoothstep(-0.11, -0.16, wq.x) * 0.6);
    layer(acc, cover, plume * 0.2, smoothstep(aa, -aa, wing * S - 0.8 * aa) * on);
    layer(acc, cover, wc, smoothstep(aa, -aa, wing * S) * on);

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