Snowy Pines

by MusicBar, visualizer

A cozy winter night of snow-laden pines and a little log cabin with warm windows, the snow falling thicker and faster as the song gets louder and a soft gust blowing through on every beat, made for December.

19 downloads Download file, 9.2 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 f8752fa9cb8f917533fe52ffb6cba9f8a916589b7a3b4575ea364f68ae0d5434

//! name: Snowy Pines
//! author: MusicBar
//! kind: visualizer
//! description: A cozy winter night of snow-laden pines and a little log cabin with warm windows, where the snow falls heavier and faster as the song gets louder and a soft gust blows through on every 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); }

// One tier of a pine: a cone from base b up to top with half width w at its base.
// Returns (distance to the outline, distance to the snowy upper slope only).
float2 tier(float2 q, float b, float top, float w) { float k = w / (top - b), s = (abs(q.x) - k * (top - q.y)) / sqrt(1.0 + k * k); return float2(max(s, b - q.y), s); }

// A snowy pine of height H: three tiers on a short trunk. x: outline distance, y: snow cover (0 to 1), z: trunk.
float3 pine(float2 q, float H, float aa)
{
    float2 t0 = tier(q, 0.1 * H, 0.55 * H, 0.38 * H);
    float2 t1 = tier(q, 0.38 * H, 0.8 * H, 0.29 * H);
    float2 t2 = tier(q, 0.62 * H, H, 0.2 * H);
    float d = min(min(t0.x, t1.x), t2.x);
    float lump = 0.012 * sin(q.y * 70.0 + q.x * 20.0);
    float th = 0.035 + 0.04 * saturate(1.0 - q.y / H);
    float snow = max(max(smoothstep(-th - aa, -th + aa, t0.y + lump) * step(0.0, t1.x), smoothstep(-th - aa, -th + aa, t1.y + lump) * step(0.0, t2.x)),
                     smoothstep(-th - aa, -th + aa, t2.y + lump));
    float trunk = max(abs(q.x) - 0.045 * H, q.y - 0.12 * H);
    return float3(min(d, trunk), snow * step(d, 0.0), trunk);
}

// Falling snow: one flake per cell, drifting and wobbling; dens is the share of cells that have one.
float flakes(float2 p, float cell, float speed, float r, float dens, float seed, float gust)
{
    float2 q = p + float2(time * 0.08 + gust, time * speed);
    float2 c = floor(q / cell);
    float h = hash2(c + seed);
    float2 o = (float2(hash2(c + seed + 3.1), hash2(c + seed + 7.7)) - 0.5) * (cell - 2.0 * r) + float2(0.25 * cell * sin(time * 1.5 + h * 30.0), 0.0);
    float2 d = frac(q / cell) * cell - 0.5 * cell - o;
    d.x /= 1.0 + 3.0 * saturate(gust);                         // the gust smears flakes sideways
    return smoothstep(r, r * 0.3, length(d)) * step(h, dens);
}

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

float pineH(float k) { return 0.4 + 0.57 * frac(hash1(k * 2.9 + 7.0) * 7.3); }

// The near pine of slot k: some are strung with lights that glow with their band of the music.
void nearPine(inout float3 acc, inout float cover, float2 p, float k, float cabX, float3 snowC, float aa)
{
    float h = hash1(k * 2.9 + 7.0);
    float H = pineH(k);
    float x = (k + 0.5 + 0.45 * (frac(h * 11.0) - 0.5)) * 0.62;
    float2 q = p - float2(x, 0.05);
    q.x += (0.03 * sin(time * 0.9 + h * 20.0) + 0.22 * sqrt(beat)) * q.y * q.y;   // the tips sway, and bend with the gust
    float3 tr = pine(q, H, aa);
    float has = step(0.1, h) * step(1.05, abs(x - cabX));
    float band = spectrum(frac(x * 0.37) * 0.8);
    float3 needle = lerp(float3(0.05, 0.16, 0.1), float3(0.12, 0.3, 0.2), saturate(q.y / H)) * (0.85 + 0.3 * smoothstep(0.0, -0.1, q.x));
    needle = lerp(needle, float3(0.22, 0.13, 0.08), smoothstep(aa, -aa, tr.z));
    layer(acc, cover, snowC * 0.6, smoothstep(aa, -aa, tr.x - 1.2 * aa) * has * 0.6);
    layer(acc, cover, lerp(needle, snowC * (0.8 + 0.25 * saturate(q.y / H)), tr.y), smoothstep(aa, -aa, tr.x) * has);
    float2 lc = floor(q / 0.085);
    float lh = hash2(lc + k * 13.0);
    float bulb = smoothstep(0.024, 0.008, length(frac(q / 0.085) * 0.085 - 0.0425)) * step(0.6, lh) * step(tr.x, -0.03) * step(0.45, frac(h * 3.1));
    float3 bulbC = frac(lh * 7.0) < 0.5 ? colorA.rgb : colorC.rgb;
    float glowB = 0.35 + 0.9 * band * (0.6 + 0.4 * sin(time * 3.0 + lh * 20.0));
    layer(acc, cover, lerp(bulbC, float3(1, 1, 1), 0.3 * band), bulb * has * saturate(glowB));
}

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 trees stand and the snow falls down
    float aa = 1.0 / u;
    float gust = 0.35 * sqrt(beat);
    float3 snowC = lerp(float3(0.92, 0.96, 1.0), colorB.rgb, 0.15);
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // A few stars twinkling high up.
    float2 sc = floor(p / 0.45);
    float sh = hash2(sc + 21.0);
    float2 sd = p - (sc + 0.2 + 0.6 * float2(hash2(sc + 1.0), hash2(sc + 2.0))) * 0.45;
    float tw = 0.5 + 0.5 * sin(time * (1.5 + 2.0 * sh) + sh * 50.0);
    layer(acc, cover, lerp(colorB.rgb, float3(1, 1, 1), 0.6), smoothstep(0.018, 0.0, length(sd)) * step(0.9, sh) * step(0.55, p.y) * tw * 0.8);

    // Far pines, small and blue in the dark.
    float fk = floor(p.x / 0.5);
    float fh = hash1(fk * 5.7 + 2.0);
    float3 far = pine(p - float2((fk + 0.5 + 0.1 * (fh - 0.5)) * 0.5, 0.04), 0.35 + 0.3 * fh, aa);
    float3 farC = lerp(float3(0.1, 0.16, 0.24), colorB.rgb * 0.4, 0.4);
    layer(acc, cover, lerp(farC, snowC * 0.7, far.y), smoothstep(aa, -aa, far.x) * step(0.25, fh) * 0.75);

    // A cabin every so often (the near trees keep clear of it): log walls, a door, a warm window,
    // a roof under deep snow and a smoking chimney.
    float cn = floor(p.x / 16.0);
    float cabX = (cn + 0.2 + 0.1 * hash1(cn * 3.3)) * 16.0;
    float2 c = p - float2(cabX, 0.06);
    float walls = max(abs(c.x) - 0.36, abs(c.y - 0.17) - 0.17);
    float roof = max(max((abs(c.x) * 0.79 + (c.y - 0.68) * 0.62), 0.29 - c.y), abs(c.x) - 0.5);
    float chim = max(abs(c.x - 0.2) - 0.045, abs(c.y - 0.6) - 0.1);
    float warm = 0.8 + 0.25 * level + 0.08 * sin(time * 9.0) * sin(time * 5.3);
    float3 lightC = lerp(colorA.rgb, float3(1.0, 0.85, 0.5), 0.35);
    float win = max(abs(c.x - 0.16) - 0.075, abs(c.y - 0.18) - 0.065);
    float door = max(abs(c.x + 0.15) - 0.06, abs(c.y - 0.11) - 0.11);
    layer(acc, cover, lightC, exp(-max(win, 0.0) / 0.12) * 0.45 * warm);
    layer(acc, cover, lightC, exp(-length((c - float2(0.16, -0.02)) / float2(0.5, 0.06))) * 0.3 * warm);
    float3 logs = float3(0.32, 0.18, 0.1) * (0.75 + 0.35 * smoothstep(0.0, 0.03, abs(frac(c.y / 0.057) - 0.5) * 0.057));
    logs = lerp(logs, float3(0.18, 0.1, 0.06), smoothstep(aa, -aa, door) * 0.8);
    layer(acc, cover, float3(0.12, 0.07, 0.05), smoothstep(aa, -aa, min(min(walls, chim), roof) - 1.2 * aa));
    layer(acc, cover, float3(0.28, 0.18, 0.15), smoothstep(aa, -aa, chim));
    layer(acc, cover, logs, smoothstep(aa, -aa, walls));
    float3 glass = lightC * (0.9 + 0.3 * smoothstep(0.07, 0.0, abs(c.y - 0.16))) * warm;
    glass = lerp(glass, float3(0.25, 0.14, 0.08), smoothstep(0.012, 0.004, min(abs(c.x - 0.16), abs(c.y - 0.18))));
    layer(acc, cover, glass, smoothstep(aa, -aa, win));
    layer(acc, cover, lightC, smoothstep(aa, -aa, length(c - float2(-0.11, 0.11)) - 0.012) * warm);
    float3 roofC = lerp(snowC * 0.75, snowC, smoothstep(0.3, 0.6, c.y));
    roofC = lerp(roofC, float3(0.2, 0.12, 0.08), smoothstep(0.32, 0.3, c.y));
    layer(acc, cover, roofC, smoothstep(aa, -aa, roof));
    layer(acc, cover, snowC, smoothstep(aa, -aa, length((c - float2(0.2, 0.705)) / float2(1.0, 0.45)) - 0.055));

    // Smoke curling from the chimney, drifting off with the breeze.
    float sy = c.y - 0.72;
    float sx = c.x - 0.2 - sy * sy * 0.9 - 0.04 * sin(sy * 14.0 - time * 2.0) * saturate(sy * 5.0);
    float smoke = exp(-abs(sx) / (0.03 + 0.07 * sy)) * smoothstep(0.0, 0.05, sy) * smoothstep(0.32, 0.05, sy) * (0.65 + 0.35 * sin(sy * 30.0 - time * 4.0));
    layer(acc, cover, float3(0.78, 0.78, 0.84), smoothstep(0.0, 1.0, smoke) * 0.5 * step(0.0, sy));

    // Near pines along the edge, uneven in height and spacing: each spot checks its own slot and the closer
    // neighbor, drawing the taller tree behind.
    float nk = floor(p.x / 0.62);
    float nj = nk + (frac(p.x / 0.62) > 0.5 ? 1.0 : -1.0);
    float tallJ = pineH(nj), tallK = pineH(nk);
    nearPine(acc, cover, p, tallJ > tallK ? nj : nk, cabX, snowC, aa);
    nearPine(acc, cover, p, tallJ > tallK ? nk : nj, cabX, snowC, aa);

    // The snowy ground in soft drifts.
    float g = 0.065 + 0.025 * sin(p.x * 1.3) + 0.015 * sin(p.x * 3.7 + 1.0);
    float3 groundC = lerp(snowC * 0.6, snowC, smoothstep(0.0, g, p.y));
    groundC += lightC * 0.35 * exp(-length((p - float2(cabX + 0.16, 0.05)) / float2(0.6, 0.08))) * warm;
    layer(acc, cover, groundC, smoothstep(aa, -aa, p.y - g) * 0.92);

    // Falling snow: a light fall always, a thick fast one when the song is loud; on the beat a gust sweeps it sideways.
    float g1 = gust * (0.4 + p.y);
    float f1 = flakes(p, 0.2, 0.22, 0.02, 0.3, 1.0, g1);
    float f2 = flakes(p, 0.15, 0.55, 0.016, 0.15 + 0.45 * level, 5.0, g1 * 1.5);
    float f3 = flakes(p, 0.34, 0.9, 0.032, 0.25 * level, 9.0, g1 * 2.0);
    layer(acc, cover, snowC, f1 * 0.6 + f2 * (0.3 + 0.5 * level) + f3 * 0.8);

    // The gust itself: a faint veil of blown snow rolling low along the drifts.
    float veil = sin(p.x * 2.1 - time * 3.0 + sin(p.x * 0.7) * 2.0) * 0.5 + 0.5;
    layer(acc, cover, snowC, veil * exp(-p.y / 0.2) * gust * 0.8);

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