Sky Voyage

by MusicBar, progress bar

A real 3D sky over a living ocean: the song is one day, dawn to a starry night, a paper plane flies ahead with its contrail as the progress bar, the waves swell with the bass and lightning flickers in the night clouds on the beat. Needs Direct3D 11 (MusicBar 1.8.3 or later).

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 96f7a907979828b02ea30803ab7d54f8a8bb94409bb5b3578df0676f61d7cc0e

//! name: Sky Voyage
//! author: MusicBar
//! kind: progress
//! description: A real 3D sky over a living ocean: the song is one day, dawn to a starry night, the sun arcing across the taskbar while a paper plane flies ahead, its contrail the progress bar; the waves swell with the bass and lightning flickers in the night clouds on the beat. Needs Direct3D 11.

// A panorama: every column of the bar is a camera at its own place along the flight, looking out to sea, so
// the whole bar is one continuous view. Raymarched volumetric clouds and an ocean that reflects the sky:
// far too heavy for ps_3_0, made for Direct3D 11 (integer hashing, loops of dozens of steps per pixel).

static const float PI = 3.14159265;
static const float WorldPerBar = 14.0;     // world units across one bar height of screen
static const float CamHeight = 2.4;
static const float CloudBase = 9.0;
static const float CloudTop = 16.0;

// ------------------------------------------------------------------ noise on integers (stable on every card)
uint pcg(uint v)
{
    uint state = v * 747796405u + 2891336453u;
    uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
    return (word >> 22u) ^ word;
}

float hash3(int3 p) { return pcg(asuint(p.x) + pcg(asuint(p.y) + pcg(asuint(p.z)))) * (1.0 / 4294967295.0); }
float hash2(int2 p) { return pcg(asuint(p.x) + pcg(asuint(p.y))) * (1.0 / 4294967295.0); }

float noise3(float3 x)
{
    int3 i = (int3)floor(x);
    float3 f = frac(x);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash3(i), b = hash3(i + int3(1, 0, 0)), c = hash3(i + int3(0, 1, 0)), d = hash3(i + int3(1, 1, 0));
    float e = hash3(i + int3(0, 0, 1)), g = hash3(i + int3(1, 0, 1)), h = hash3(i + int3(0, 1, 1)), k = hash3(i + int3(1, 1, 1));
    return lerp(lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y), lerp(lerp(e, g, f.x), lerp(h, k, f.x), f.y), f.z);
}

float noise2(float2 x)
{
    int2 i = (int2)floor(x);
    float2 f = frac(x);
    f = f * f * (3.0 - 2.0 * f);
    return lerp(lerp(hash2(i), hash2(i + int2(1, 0)), f.x), lerp(hash2(i + int2(0, 1)), hash2(i + int2(1, 1)), f.x), f.y);
}

float fbm3(float3 p, int octaves)
{
    float sum = 0.0, amp = 0.5;
    [loop] for (int i = 0; i < octaves; i++)
    {
        sum += amp * noise3(p);
        p = p * 2.03 + float3(17.1, 3.7, 9.2);
        amp *= 0.5;
    }
    return sum;
}

// ------------------------------------------------------------------ the day: dawn, noon, sunset, night along the song
struct Sky { float3 sunDir; float3 sunColor; float3 zenith; float3 horizon; float night; float3 dirs[2]; };

// Where a body hangs, seen from this column: set at a distance along the flight, not infinitely far away, so in a
// panorama of cameras each looking straight out to sea it is a disc where it hangs rather than a band across the bar.
float3 bodyDir(float u, float at, float height, float aspect)
{
    return normalize(float3((at - u) * aspect * WorldPerBar / 22.0, height, 1.0));
}

// The whole bar is one moment of one day, and the day goes by with the song: dawn as it starts, noon in the middle,
// sunset near the end and night for the last bars. The sun arcs across the taskbar meanwhile, rising from the sea
// on the left and setting on the right; then the moon comes up.
Sky skyAt(float u, float aspect)
{
    Sky s;
    float day = saturate(progress / 0.84);
    float sunHeight = sin(day * PI) * 0.32 + 0.025;            // kept low enough to stay in view at noon
    float sunAt = lerp(0.1, 0.9, day);
    s.night = smoothstep(0.8, 0.92, progress);
    float moonRise = saturate((progress - 0.82) / 0.18);
    s.dirs[0] = bodyDir(u, sunAt, sunHeight, aspect);
    s.dirs[1] = bodyDir(u, lerp(0.25, 0.45, moonRise), lerp(0.04, 0.24, moonRise), aspect);
    s.sunDir = normalize(lerp(s.dirs[0], s.dirs[1], s.night));
    // The light's color and the sky's go with how high the sun is, for every column alike.
    float lift = saturate(sunHeight * 4.0 - 0.1);
    float3 low = float3(1.0, 0.42, 0.16), high = float3(1.0, 0.93, 0.82);
    s.sunColor = lerp(low, high, lift) * (1.0 - s.night) * 1.9 + float3(0.55, 0.65, 0.9) * 0.45 * s.night;
    s.zenith = lerp(lerp(float3(0.1, 0.12, 0.34), float3(0.07, 0.2, 0.55), lift), float3(0.006, 0.01, 0.035), s.night);
    s.horizon = lerp(lerp(float3(0.95, 0.48, 0.32), float3(0.5, 0.66, 0.85), lift), float3(0.025, 0.035, 0.08), s.night);
    // Dawn and dusk warm the horizon most on the sun's side.
    float side = exp(-abs(u - sunAt) * 2.5) * (1.0 - lift) * (1.0 - s.night);
    s.horizon = lerp(s.horizon, float3(1.0, 0.45, 0.22), side * 0.6);
    return s;
}

float3 skyColor(float3 rd, Sky s, float2 starCell)
{
    float up = saturate(rd.y);
    float3 c = lerp(s.horizon, s.zenith, pow(up, 0.5));
    // The sun and the moon: a glow round each and its disc, big as a long lens would show it.
    float disc = 0.0;
    [unroll] for (int i = 0; i < 2; i++)
    {
        float sd = saturate(dot(rd, s.dirs[i]));
        float w = i == 1 ? s.night : 1.0 - s.night;
        float3 tint = i == 1 ? float3(0.75, 0.82, 1.0) * 0.6 : s.sunColor;
        float d = smoothstep(0.9955, 0.9962, sd);
        c += tint * (pow(sd, 12.0) * 0.16 + pow(sd, 120.0) * 0.5) * w;
        c += (i == 1 ? float3(0.85, 0.88, 0.95) * 1.4 : s.sunColor * 2.2 + 0.4) * d * w;
        disc = max(disc, d * w);
    }
    // Stars at night, twinkling.
    if (s.night > 0.0)
    {
        float star = step(0.997, hash2((int2)starCell)) * smoothstep(0.02, 0.15, rd.y) * (1.0 - disc);
        c += float3(0.9, 0.95, 1.0) * star * s.night * (0.6 + 0.4 * sin(time * 3.0 + starCell.x));
    }
    return c;
}

// ------------------------------------------------------------------ clouds: a slab of density, raymarched with light
float cloudDensity(float3 p, float coverage, int octaves)
{
    float3 q = p * 0.09 + float3(time * 0.025, 0.0, time * 0.04);
    float shape = fbm3(q, octaves);
    // Flat bottoms, rounded tops.
    float h = saturate((p.y - CloudBase) / (CloudTop - CloudBase));
    float profile = smoothstep(0.0, 0.15, h) * smoothstep(1.0, 0.45, h);
    return saturate((shape - (1.0 - coverage)) * 2.6) * profile;
}

float4 clouds(float3 ro, float3 rd, Sky s, float coverage, float flash)
{
    if (rd.y <= 0.01) return float4(0, 0, 0, 1);
    float t0 = (CloudBase - ro.y) / rd.y;
    float t1 = min((CloudTop - ro.y) / rd.y, t0 + 90.0);
    const int Steps = 40;
    float dt = (t1 - t0) / Steps;
    float t = t0 + dt * hash2((int2)(rd.xy * 9000.0)) ;  // jittered start: no banding
    float transmittance = 1.0;
    float3 light = 0.0;
    float mu = dot(rd, s.sunDir);
    // Henyey-Greenstein, forward and back: silver linings toward the sun.
    float hg = lerp(0.7 * (1.0 - 0.64) / pow(1.0 + 0.64 - 1.6 * mu, 1.5), 0.3 * (1.0 - 0.09) / pow(1.0 + 0.09 + 0.6 * mu, 1.5), 0.3) / (4.0 * PI);
    [loop] for (int i = 0; i < Steps; i++)
    {
        float3 p = ro + rd * t;
        float d = cloudDensity(p, coverage, 4);
        if (d > 0.01)
        {
            // How much sunlight reaches this point: two steps toward the sun.
            float shade = cloudDensity(p + s.sunDir * 1.2, coverage, 2) + cloudDensity(p + s.sunDir * 3.0, coverage, 2);
            float beer = exp(-shade * 1.8);
            float powder = 1.0 - exp(-d * 4.0);
            float3 ambient = lerp(s.horizon, s.zenith, 0.6) * 0.4;
            float3 lit = s.sunColor * beer * powder * (hg * 9.0 + 0.25) + ambient;
            lit += float3(0.75, 0.8, 1.0) * flash * (0.5 + d);  // lightning, inside
            float absorb = exp(-d * dt * 0.9);
            light += transmittance * (1.0 - absorb) * lit;
            transmittance *= absorb;
            if (transmittance < 0.02) break;
        }
        t += dt;
    }
    // Far clouds fade into the haze.
    float haze = exp(-t0 * 0.012);
    return float4(light * haze, lerp(1.0, transmittance, haze));
}

// ------------------------------------------------------------------ the ocean
float waveHeight(float2 p, float swell)
{
    float h = 0.0, amp = 0.32 * swell, freq = 0.18;
    float2 dir = float2(0.8, 0.6);
    [loop] for (int i = 0; i < 6; i++)
    {
        float phase = dot(p, dir) * freq + time * (0.9 + i * 0.35);
        h += amp * (1.0 - abs(sin(phase)));  // sharp crests
        dir = float2(dir.x * 0.6 - dir.y * 0.8, dir.x * 0.8 + dir.y * 0.6);
        freq *= 1.9;
        amp *= 0.5;
    }
    return h;
}

float3 oceanNormal(float2 p, float swell, float eps)
{
    float h = waveHeight(p, swell);
    return normalize(float3(h - waveHeight(p + float2(eps, 0), swell), eps, h - waveHeight(p + float2(0, eps), swell)));
}

// ------------------------------------------------------------------ the frame
float4 main(float2 uv : TEXCOORD) : COLOR
{
    float aspect = size.x / size.y;
    float u = uv.x;
    float v = rise(uv);
    Sky s = skyAt(u, aspect);
    float bass = spectrum(0.05);
    float swell = 0.7 + 0.9 * bass + 0.3 * level;
    // Lightning in the night clouds on a strong beat: one soft burst at a place that changes with each strike.
    int strike = (int)(time * 3.0);
    float strikeAt = hash2(int2(strike, 7));
    float flash = s.night * step(0.55, beat) * beat * exp(-pow((u - strikeAt) * aspect / 2.5, 2.0));

    // This column's camera: at its place along the flight, looking out to sea, a little up.
    float3 ro = float3(u * aspect * WorldPerBar, CamHeight, 0.0);
    float3 rd = normalize(float3(0.0, (v - 0.36) * 0.62, 1.0));
    float2 starCell = float2(uv.x * size.x * 0.5, v * size.y * 0.5);

    float3 col;
    if (rd.y > 0.0)
    {
        col = skyColor(rd, s, starCell);
        float coverage = 0.3 + 0.2 * noise2(float2(u * 6.0, 1.3)) + 0.1 * s.night;
        float4 cl = clouds(ro, rd, s, coverage, flash);
        col = col * cl.a + cl.rgb;
    }
    else
    {
        // The sea: where the ray meets it, the waves' normal (finer up close), the sky it reflects.
        float t = -ro.y / rd.y;
        float3 p = ro + rd * t;
        float3 n = oceanNormal(p.xz, swell, 0.18 + t * 0.012);
        n = normalize(lerp(n, float3(0, 1, 0), saturate(t / 220.0)));  // far away the waves blur to a mirror
        float3 rr = reflect(rd, n);
        rr.y = abs(rr.y);
        float fresnel = 0.02 + 0.98 * pow(1.0 - saturate(dot(-rd, n)), 5.0);
        float3 reflected = skyColor(rr, s, starCell * 1.3);
        // The clouds above, roughly, in the reflection.
        reflected *= 1.0 - 0.35 * smoothstep(0.45, 0.75, noise2(rr.xz / max(rr.y, 0.05) * 0.05 + time * 0.01));
        float3 deep = lerp(float3(0.0, 0.05, 0.08), float3(0.0, 0.12, 0.16), saturate(s.sunColor.g * 0.4)) * (1.0 - 0.8 * s.night);
        // Light through the crests.
        float crest = saturate(waveHeight(p.xz, swell) / (0.6 * swell));
        deep += float3(0.0, 0.25, 0.22) * pow(crest, 3.0) * saturate(s.sunColor.g) * 0.4;
        col = lerp(deep, reflected, fresnel);
        // Glitter of the sun on the water, more on the beat.
        float spec = pow(saturate(dot(reflect(rd, n), s.sunDir)), 90.0);
        col += s.sunColor * spec * (0.8 + 1.6 * beat);
        // Distance: the sea fades into the horizon's haze.
        col = lerp(col, s.horizon, 1.0 - exp(-t * 0.008));
    }

    // ---------------------------------------------------------------- the contrail: the song so far
    float head = clamp(progress, 0.45 / aspect, 1.0 - 0.45 / aspect);  // never half off the bar
    float hx = (u - head) * aspect;          // in bar heights, negative behind the plane
    float trailY = 0.6 + 0.04 * sin(u * 23.0 + time * 0.4);
    float age = saturate(-hx / (aspect * 0.9));
    float width = 0.025 + 0.05 * age;
    float dy = v - trailY;
    float trail = exp(-dy * dy / (width * width)) * step(hx, 0.0) * (1.0 - age * 0.55);
    // Older vapour spreads and breaks up in the wind.
    trail *= lerp(1.0, smoothstep(0.25, 0.75, noise2(float2(u * aspect * 2.5 - time * 0.05, v * 18.0))), saturate(age * 2.0));
    // It shimmers with the music along its length.
    float shimmer = 0.75 + 0.6 * spectrum(saturate(u));
    float3 trailColor = lerp(s.sunColor * 0.35 + 0.35, colorA.rgb * 1.4, 0.45) * shimmer;
    col = lerp(col, trailColor, trail * 0.75);

    // ---------------------------------------------------------------- the paper plane
    float2 pp = float2(hx * 1.0, (v - trailY) * 1.0) * float2(1.0, 1.0);
    pp.y -= 0.025 * sin(time * 2.0);
    // A dart pointing ahead, edges smoothed over a pixel: the upper wing catching the light, the lower in
    // shade, a fold down the middle, and a soft shadow under it to keep it clear against a bright sky.
    float px = 1.0 / size.y;
    float nose = 0.34, tail = -0.32;
    float along = saturate((pp.x - tail) / (nose - tail));
    float edge = max(max(pp.x - nose, tail - pp.x), abs(pp.y) - 0.15 * (1.0 - along));
    float plane = smoothstep(px, -px, edge);
    float2 sp = pp - float2(-0.03, 0.04);
    float alongS = saturate((sp.x - tail) / (nose - tail));
    float shadowEdge = max(max(sp.x - nose, tail - sp.x), abs(sp.y) - 0.15 * (1.0 - alongS));
    col = lerp(col, col * 0.3, smoothstep(px * 3.0, -px, shadowEdge) * (1.0 - plane) * 0.6);
    float lit = saturate(s.sunColor.r * 0.35 + 0.35);
    float3 upper = float3(0.97, 0.96, 0.93) * (0.45 + 0.55 * lit) + s.sunColor * 0.06;
    float3 lower = float3(0.62, 0.66, 0.76) * (0.4 + 0.5 * lit);
    float3 paper = lerp(lower, upper, smoothstep(-px, px, pp.y));
    paper = lerp(paper, paper * 0.7, smoothstep(px * 1.5, 0.0, abs(pp.y)) * step(-0.2, pp.x));  // the fold
    col = lerp(col, paper, plane);

    // Exposure, filmic tone, then to the screen's sRGB.
    col *= 0.8;
    col = col * (2.51 * col + 0.03) / (col * (2.43 * col + 0.59) + 0.14);
    col = pow(saturate(col), 1.0 / 2.2);
    return paint(col, 0.97);
}