Paper Boat

by MusicBar, progress bar

An origami paper boat sails to the playhead on three layers of paper-cut waves under a paper sun, rocking with the music and lifted by a bigger swell on every beat.

4 downloads Download file, 7.9 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 cb8d0df3148668215c98ada771f7d0e7e138be1f4d5ddc86c4768588f2c76b71

//! name: Paper Boat
//! author: MusicBar
//! kind: progress
//! description: An origami paper boat sails to the playhead on three layers of paper-cut waves under a paper sun, rocking with the music and riding a bigger swell on every beat.

float hash1(float n) { return frac(sin(n * 127.1 + 311.7) * 43758.5453); }

float vnoise(float2 p)
{
    float2 i = floor(p), f = frac(p);
    f = f * f * (3.0 - 2.0 * f);
    float n = i.x + i.y * 57.0;
    return lerp(lerp(hash1(n), hash1(n + 1.0), f.x), lerp(hash1(n + 57.0), hash1(n + 58.0), f.x), f.y);
}

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

// Distance outside one edge of a counterclockwise polygon (negative inside).
float edge(float2 p, float2 a, float2 b) { float2 e = normalize(b - a); return e.y * (p.x - a.x) - e.x * (p.y - a.y); }

// 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 cut edge of wave sheet i (0 back, 2 front): rolling swells with rounded crests, taller when the song is loud,
// and a swell that lifts around the boat on the beat.
float waveH(float x, float i, float u, float head)
{
    float fx = x / u, t = time;
    float h = 0.6 * sin(fx * (1.25 - 0.2 * i) + t * (0.55 + 0.3 * i) + i * 2.1) + 0.4 * sin(fx * (2.7 - 0.5 * i) - t * (0.8 + 0.25 * i) + i * 5.3);
    h = (0.52 - 0.15 * i) * u + (0.065 + 0.012 * i) * u * h * (0.7 + 0.6 * level);
    h += u * (0.055 * abs(sin(fx * (2.0 + 0.4 * i) + t * (0.9 + 0.4 * i) + i * 1.7)) - 0.035);   // round crests, scissor-sharp troughs
    return h + 0.14 * u * beat * exp(-pow((x - head) / (1.4 * u), 2.0)) * (0.4 + 0.3 * i);
}

// One paper sheet: its color, a fibrous texture, a lit cut edge, and the soft shadow the next sheet casts on it.
void sheet(inout float3 acc, inout float cover, float2 px, float h, float hNext, float3 col, float played, float fiber, float u)
{
    float inside = smoothstep(0.6, -0.6, px.y - h);
    float depth = saturate((h - px.y) / (0.3 * u));
    float3 c = col * (1.0 - 0.18 * depth) * (0.95 + 0.08 * fiber);
    c += smoothstep(max(1.5, 0.03 * u), 0.0, h - px.y) * 0.18;                          // light on the cut edge
    c *= 1.0 - 0.45 * exp(-max(px.y - hNext - 0.01 * u, 0.0) / (0.07 * u)) * step(hNext - 0.02 * u, px.y);
    layer(acc, cover, c, inside * lerp(0.05, 0.96, played));
    layer(acc, cover, float3(1, 1, 1), smoothstep(1.2, 0.0, abs(px.y - h)) * (1.0 - played) * 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
    float head = progress * size.x;
    float played = smoothstep(head + 0.6 * u, head + 0.25 * u, px.x);
    float fiber = vnoise(px * float2(0.3, 0.8)) * 0.6 + vnoise(px * 1.7) * 0.4;
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // The paper sun hanging behind the played part: a disc on a zigzag ray star that turns slowly and flares on the beat.
    float2 sq = px - float2(head * 0.62, 0.74 * u);
    float sa = atan2(sq.y, sq.x) / 6.2832 * 12.0 + time * 0.25;
    float rayR = (0.19 + (0.08 + 0.05 * beat) * (1.0 - 2.0 * abs(frac(sa) - 0.5))) * u;
    float dStar = length(sq) - rayR, dDisc = length(sq) - 0.15 * u;
    layer(acc, cover, float3(0.1, 0.05, 0.0), smoothstep(0.03 * u, -0.02 * u, length(sq + float2(-0.025, 0.03) * u) - rayR) * 0.3);
    layer(acc, cover, lerp(colorC.rgb, float3(1.0, 0.5, 0.2), 0.3) * (0.92 + 0.1 * fiber), smoothstep(0.5, -0.5, dStar));
    layer(acc, cover, float3(0.25, 0.1, 0.0), smoothstep(0.03 * u, -0.01 * u, length(sq + float2(-0.012, 0.016) * u) - 0.15 * u) * 0.35);
    layer(acc, cover, lerp(colorC.rgb, float3(1, 1, 0.9), 0.35) * (0.93 + 0.1 * fiber), smoothstep(0.5, -0.5, dDisc));

    // The three wave sheets, back to front, with the boat between the middle and the front one.
    float h0 = waveH(px.x, 0.0, u, head), h1 = waveH(px.x, 1.0, u, head), h2 = waveH(px.x, 2.0, u, head);
    sheet(acc, cover, px, h0, h1, lerp(colorB.rgb, float3(1, 1, 1), 0.35), played, fiber, u);
    sheet(acc, cover, px, h1, h2, lerp(colorA.rgb, colorB.rgb, 0.45), played, fiber, u);

    // The boat rides the front sheet at the playhead, rocking with the slope of the swell and the music.
    float S = 0.84 * u;
    float hb = waveH(head, 2.0, u, head);
    float slope = (waveH(head + 0.3 * u, 2.0, u, head) - waveH(head - 0.3 * u, 2.0, u, head)) / (0.6 * u);
    float rock = 0.7 * atan(slope) + (0.04 + 0.06 * level) * sin(time * 1.9) + 0.1 * beat * sin(time * 9.0);
    float2 bq = rot(px - float2(head, hb - 0.07 * u), -rock) / S;
    float dHull = max(max(edge(bq, float2(-0.3, 0.0), float2(0.3, 0.0)), edge(bq, float2(0.3, 0.0), float2(0.52, 0.2))), max(edge(bq, float2(0.52, 0.2), float2(-0.52, 0.2)), edge(bq, float2(-0.52, 0.2), float2(-0.3, 0.0))));
    float dSail = max(max(edge(bq, float2(-0.22, 0.2), float2(0.22, 0.2)), edge(bq, float2(0.22, 0.2), float2(0.0, 0.58))), edge(bq, float2(0.0, 0.58), float2(-0.22, 0.2)));
    float dBoat = min(dHull, dSail) * S;
    float2 shq = bq + float2(-0.05, 0.05);
    float dShadow = min(max(max(edge(shq, float2(-0.3, 0.0), float2(0.3, 0.0)), edge(shq, float2(0.3, 0.0), float2(0.52, 0.2))), max(edge(shq, float2(0.52, 0.2), float2(-0.52, 0.2)), edge(shq, float2(-0.52, 0.2), float2(-0.3, 0.0)))),
                        max(max(edge(shq, float2(-0.22, 0.2), float2(0.22, 0.2)), edge(shq, float2(0.22, 0.2), float2(0.0, 0.58))), edge(shq, float2(0.0, 0.58), float2(-0.22, 0.2)))) * S;
    layer(acc, cover, float3(0.02, 0.04, 0.08), smoothstep(0.04 * u, -0.02 * u, dShadow) * 0.3);

    // Folded paper: the sail lit on one side and shaded on the other, the hull's pocket fold, crisp creases.
    float3 boatPaper = lerp(float3(0.99, 0.98, 0.94), colorC.rgb, 0.12) * (0.95 + 0.07 * fiber);
    float3 bc = boatPaper * lerp(1.05, 0.82, step(0.0, bq.x)) * (1.0 - 0.1 * saturate((0.58 - bq.y) / 0.4));
    float pocket = max(edge(bq, float2(-0.22, 0.2), float2(0.0, 0.03)), edge(bq, float2(0.0, 0.03), float2(0.22, 0.2)));
    bc = lerp(bc, boatPaper * lerp(0.9, 0.72, step(0.0, bq.x)), step(dHull, 0.0) * step(0.0, pocket));
    float crease = min(lerp(9.0, abs(bq.x), step(0.2, bq.y)), lerp(9.0, abs(pocket), step(dHull, 0.0))) * S;
    bc *= 1.0 - 0.3 * smoothstep(max(0.8, 0.012 * u), 0.0, crease);
    layer(acc, cover, lerp(colorA.rgb, float3(0, 0, 0), 0.6), smoothstep(0.5, -0.5, dBoat - 1.0) * 0.8);
    layer(acc, cover, bc, smoothstep(0.5, -0.5, dBoat));

    // The front sheet hides the hull's waterline; then flecks of paper spray off the bow on the beat.
    sheet(acc, cover, px, h2, -9.0 * u, colorA.rgb * 0.85, played, fiber, u);

    // The wake: little paper foam chips bobbing on the front sheet behind the hull, smaller further back.
    float fc = floor((px.x + time * 0.4 * u) / (0.16 * u));
    float fx = (fc + 0.5) * 0.16 * u - time * 0.4 * u;
    float fb = head - 0.32 * u - fx;
    float fsz = 0.035 * u * exp(-max(fb, 0.0) / (0.9 * u)) * (0.7 + 0.5 * hash1(fc));
    float2 fo = px - float2(fx, waveH(fx, 2.0, u, head) - 0.015 * u + 0.012 * u * sin(time * 4.0 + fc));
    float chip = smoothstep(0.5, -0.5, length(fo / float2(1.6, 1.0)) - fsz) * step(0.0, fb) * step(0.3, hash1(fc + 4.0));
    layer(acc, cover, float3(1, 1, 1) * (0.92 + 0.08 * fiber), chip);
    for (int i = 0; i < 5; i++)
    {
        float fi = i, t = 1.0 - beat;
        float2 v = float2(0.4 + 0.5 * hash1(fi + 1.0), 0.6 + 0.6 * hash1(fi + 7.0)) * u;
        float2 fp = float2(head + 0.3 * u + v.x * t, hb + v.y * t - 1.6 * u * t * t);
        float2 fq = rot(px - fp, fi * 1.3 + t * 6.0);
        float fleck = smoothstep(0.5, -0.5, max(abs(fq.x), abs(fq.y)) - 0.025 * u);
        layer(acc, cover, float3(1, 1, 1), fleck * step(0.05, beat) * saturate(beat * 2.0));
    }

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