Nightless Metropolis

by Zu Phạm, progress bar

Một đêm dài không hồi kết giữa đô thị nhộn nhịp, từng giai điệu đánh thức những ánh đèn, mỗi ô cửa sổ sáng lên theo nhịp đập của bài hát, và phổ âm nhuộm màu đô thị thành một bức tranh đầy màu sắc 🌃🌈

41 downloads Download file, 44.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 7bc39e9d2843a23a414510fc787684b1f6cac387fa74ba61dd05cb374508a63e

//! name: Nightless Metropolis
//! author: Zu Phạm
//! kind: progress
//! description: Neon city at night with a flying butterfly and a spectrum line that turns the city below it into a moving rainbow.

// Original code by Inigo Quilez (2021), licensed under the MIT License.
// Use, modification, and redistribution are permitted under its terms.
// When redistributing, include the original copyright notice and full MIT License.
// Provided "AS IS", without warranty or liability.

#define CAM_DIR      float3(-0.666667, -0.333333, -0.666667)
#define CAM_DIST     9.0
#define CITY_SPEED   0.4
#define WINDOW_A     float3(0.0, 0.0, 1.7)
#define WINDOW_B     float3(0.6, 1.8, 2.4)
#define FOG_OFFSET   7.0
#define FOG_DENSITY  0.7
#define FOG_COLOR    float3(0.25, 0.0, 0.3)
#define FOG_HIGH     float3(0.12, 0.18, 0.35)
#define FOG_V_LOW    float3(0.16, 0.06, 0.24)
#define FOG_V_HIGH   float3(0.06, 0.12, 0.28)
#define LIGHT_HEIGHT 0.0
#define LIGHT_SPEED  0.15
#define LIGHT_HIDDEN_GLOW 0.25
#define LIGHT_A      float3(0.7, 0.35, 0.15)
#define LIGHT_B      float3(0.95, 0.7, 0.5)
#define SIGN_A       float3(0.0, 0.0, 1.8)
#define SIGN_B       float3(3.4, 3.4, 3.4)
#define TAU          6.283185
#define VIEW_TAN     0.7
#define VIEW_SHIFT   0.0
#define SPEC_Z0      -0.5
#define SPEC_Z1      6.0
#define SPEC_JITTER  0.08
#define SPEC_MIN     0.9
#define SPEC_GAIN    2.5
#define FLY_D0        10.2
#define FLY_AZ        2.2
#define FLY_AX        4.4
#define FLY_SPEED     0.55
#define FLY_OMEGA     0.30
#define FLY_SIZE_VAR  0.15
#define FLY_DNEAR     8.0
#define FLY_K         0.95
#define FLY_FORESHORT (-CAM_DIR.z)
#define FLY_RISE      0.55
#define FLY_LAG       0.35
#define FLY_EPOCH     30.0
#define FLY_SIGHTING_PERIOD 45.0
#define FLY_SIGHTING_LENGTH_MIN 18.0
#define FLY_SIGHTING_LENGTH_MAX 32.0
#define FLY_SIGHTING_FADE 0.8
#define FLY_EXIT      2.4
#define FLY_VK        3.0
#define FLAP_RATE     1.25
#define FLAP_LIFT     0.09
#define FLAP_SURGE    0.9
#define FLY_B2_PROB   0.55
#define FLY_B2_OFFSET 37.3
#define FLY_B2_SIZE   0.85
#define BF_S          3.0
#define DUST_N        12
#define DUST_DT       0.18
#define DUST_LIFE     1.6
#define TEXT_CELL      float2(24.0, 9.0)
#define TEXT_SIGN_HASH float4(0.5, 0.0, 0.75, 0.9)
#define RIM_GAIN      0.8
#define WAVE_SPEED    0.25
#define WAVE_Z0       -0.8
#define WAVE_Z1       6.8
#define WAVE_WIDTH    0.9
#define WAVE_BRIGHT   0.8
#define HEAD_DT       0.6
#define HEAD_E        0.45
#define FOG_BREATH    0.25
#define NEON_CYAN     float3(0.00, 0.85, 1.00)
#define NEON_BLUE     float3(0.15, 0.35, 1.00)
#define NEON_VIOLET   float3(0.55, 0.15, 1.00)
#define NEON_MAGENTA  float3(1.00, 0.10, 0.85)
#define NEON_PINK     float3(1.00, 0.20, 0.45)
#define NEON_AMBER    float3(1.00, 0.55, 0.10)
#define CITY_GAIN     1.15
#define CITY_HUE_SPREAD 0.25
#define LIGHT_GAIN    1.35
#define SIGN_GAIN     2.1
#define SPEC_TINT     0.0
#define EV_EVERY      3.0
#define EV_COVER      12.0
#define EV_HOLD       6.0
#define EV_RELEASE    2.0
#define EV_CALM_T     1.2
#define EV_EDGE       0.35
#define EV_TOP        1.4
#define RIM_GAIN_V15  0.9
#define RAINBOW_SPAN  0.85
#define RAINBOW_SPEED 0.046875
#define RAINBOW_RISE  0.10
#define RAINBOW_SAT   0.85
#define RB_JITTER     0.03
#define RB_PULSE_BEAT 0.35
#define RB_PULSE_LVL  0.25
#define RB_FOG_TINT   0.85
#define RB_FOG_GAIN   0.30
#define VIB_SAT       1.4
#define VIB_GAIN      1.1
#define RB_SPREAD     0.10
#define VIB_SAT_RB    1.05
#define VIB_GAIN_RB   1.0
#define RB_FLOOR      0.08
#define RB_HOT_WHITE  0.50
#define CITY_GAIN_V15 1.8
#define LIGHT_GAIN_V15 1.3
#define SIGN_GAIN_V15 2.0
#define SPEC_TINT_V15 0.25
#define FOG_V_LOW_V15  float3(0.35, 0.02, 0.42)
#define FOG_V_HIGH_V15 float3(0.03, 0.16, 0.48)
float2 wrapCell(float2 b)
{
    return b - 512.0 * floor(b / 512.0);
}
float3 neonPal(float t)
{
    t = saturate(t) * 5.0;
    float3 c = lerp(NEON_CYAN, NEON_BLUE, saturate(t));
    c = lerp(c, NEON_VIOLET, saturate(t - 1.0));
    c = lerp(c, NEON_MAGENTA, saturate(t - 2.0));
    c = lerp(c, NEON_PINK, saturate(t - 3.0));
    return lerp(c, NEON_AMBER, saturate(t - 4.0));
}
float3 cityNeon(float h)
{
    h = saturate(h);
    if (h < 0.55) return lerp(NEON_CYAN, NEON_BLUE, h / 0.55);
    if (h < 0.80) return lerp(NEON_VIOLET, NEON_MAGENTA, (h - 0.55) / 0.25);
    if (h < 0.93) return lerp(NEON_MAGENTA, NEON_PINK, (h - 0.80) / 0.13);
    return lerp(NEON_PINK, NEON_AMBER, (h - 0.93) / 0.07);
}
float3 cityWindow(float h)
{
    h = saturate(h);
    if (h < 0.42) return lerp(float3(1.00, 0.85, 0.60), float3(1.00, 0.72, 0.42), h / 0.42);
    if (h < 0.68) return lerp(float3(0.88, 0.93, 1.00), float3(0.72, 0.82, 1.00), (h - 0.42) / 0.26);
    if (h < 0.80) return lerp(float3(1.00, 0.62, 0.25), float3(1.00, 0.50, 0.18), (h - 0.68) / 0.12);
    if (h < 0.90) return lerp(float3(0.35, 0.90, 0.65), float3(0.20, 0.75, 0.70), (h - 0.80) / 0.10);
    if (h < 0.96) return lerp(float3(1.00, 0.35, 0.40), float3(0.95, 0.25, 0.55), (h - 0.90) / 0.06);
    return lerp(float3(0.40, 0.60, 1.00), float3(0.55, 0.40, 1.00), (h - 0.96) / 0.04);
}
float hash1(float p)
{
    float3 p3 = frac(p * float3(5.3983, 5.4427, 6.9371));
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.x + p3.y) * p3.z);
}
float hash1(float2 p2)
{
    p2 = frac(p2 * float2(5.3983, 5.4427));
    p2 += dot(p2.yx, p2.xy + float2(21.5351, 14.3137));
    return frac(p2.x * p2.y * 95.4337);
}
float hash1(float2 p2, float p)
{
    float3 p3 = frac(float3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.x + p3.y) * p3.z);
}
float2 hash2(float2 p2, float p)
{
    float3 p3 = frac(float3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));
    p3 += dot(p3, p3.yzx + 19.19);
    return frac((p3.xx + p3.yz) * p3.zy);
}
float3 hash3(float2 p2)
{
    float3 p3 = frac(float3(p2.xyx) * float3(5.3983, 5.4427, 6.9371));
    p3 += dot(p3, p3.yxz + 19.19);
    return frac((p3.xxy + p3.yzz) * p3.zyx);
}
float4 hash4(float2 p2)
{
    float4 p4 = frac(p2.xyxy * float4(5.3983, 5.4427, 6.9371, 7.1283));
    p4 += dot(p4, p4.yxwz + 19.19);
    return frac((p4.xxxy + p4.yyzz + p4.zwww) * p4.wzyx);
}
float4 signParams(float2 hb)
{
    float isCell = step(abs(hb.x - TEXT_CELL.x) + abs(hb.y - TEXT_CELL.y), 0.5);
    return lerp(hash4(hb), TEXT_SIGN_HASH, isCell);
}
float vnoise(float2 p)
{
    float2 i = floor(p);
    float2 f = frac(p);
    float2 u = f * f * (3.0 - 2.0 * f);
    return lerp(lerp(hash1(i + float2(0.0, 0.0)),
                     hash1(i + float2(1.0, 0.0)), u.x),
                lerp(hash1(i + float2(0.0, 1.0)),
                     hash1(i + float2(1.0, 1.0)), u.x), u.y);
}
float3 boxHit(float3 eye, float3 ri, float3 ray, float3 side, float3 lo, float3 hi)
{
    float3 wall = lerp(hi, lo, side);
    float3 t = (wall - eye) * ri;
    float3 dm = step(t.zxy, t) * step(t.yzx, t);
    float maxT = dot(dm, t);
    float maxFace = dm.x - dm.y;
    float3 p = eye + maxT * ray;
    dm += step(lo, p) * step(p, hi);
    float hit = step(0.5, dm.x * dm.y * dm.z);
    return float3(maxT, maxFace, hit);
}
float4 castRay(float3 eye, float3 ray, float2 center, out float edgeOut)
{
    float2 block = floor(eye.xy);
    float3 ri = 1.0 / ray;
    float3 rs = sign(ray);
    float3 side = 0.5 + 0.5 * rs;
    float2 ris = ri.xy * rs.xy;
    float2 dis = (block - eye.xy + 0.5 + rs.xy * 0.5) * ri.xy;
    float4 result = float4(100.0, 0.0, 0.0, 1.0);
    edgeOut = 1.0;
    [loop]
    for (int i = 0; i < 16; ++i)
    {
        float2 hb = wrapCell(block);
        float d = dot(block - center, CAM_DIR.xy);
        float height = 3.0 * hash1(hb) - 1.0 + 1.5 * d - 0.1 * d * d;
        float2 lo0 = block;
        float2 loX = float2(0.45, 0.45);
        float2 hi0 = block + 0.55;
        float2 hiX = float2(0.45, 0.45);
        float dist = 500.0;
        float face = 0.0;
        float edgeV = 1.0;
        {
            float4 signHash = signParams(hb);
            float2 sc = float2(0.2, -0.4) + float2(0.6, -0.8) * signHash.xy;
            float width = 0.06 + 0.1 * signHash.w;
            float3 lo = float3(sc.x - width, 0.55, -100.0);
            float3 hi = float3(sc.x + width, 0.99, sc.y + width + height);
            float s = step(0.5, signHash.z);
            lo = float3(block, 0.0) + lerp(lo, lo.yxz, s);
            hi = float3(block, 0.0) + lerp(hi, hi.yxz, s);
            float3 h = boxHit(eye, ri, ray, side, lo, hi);
            if (h.z > 0.5 && h.x < dist)
            {
                dist = h.x;
                face = h.y;
            }
        }
        [unroll]
        for (int j = 0; j < 5; ++j)
        {
            float top = height - 0.4 * float(j);
            float3 lo = float3(lo0 + loX * hash2(hb, float(j)), -100.0);
            float3 hi = float3(hi0 + hiX * hash2(hb, float(j) + 0.5), top);
            float3 h = boxHit(eye, ri, ray, side, lo, hi);
            if (h.z > 0.5 && h.x < dist)
            {
                dist = h.x;
                face = h.y;
                float3 q = eye + h.x * ray;
                float exd = min(q.x - lo.x, hi.x - q.x);
                float eyd = min(q.y - lo.y, hi.y - q.y);
                float sideM = step(0.5, abs(h.y));
                float tang = lerp(exd, eyd, step(0.5, h.y));
                edgeV = lerp(min(exd, eyd), min(tang, hi.z - q.z), sideM);
            }
        }
        if (dist < 400.0)
        {
            result = float4(dist, height, face, 1.0);
            edgeOut = edgeV;
            break;
        }
        float2 stepDim = step(dis.xy, dis.yx);
        dis += stepDim * ris;
        block += stepDim * rs.xy;
    }
    return result;
}
float3 hueNeon(float h)
{
    float3 c = saturate(abs(frac(h + float3(0.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 1.0);
    return lerp(float3(1.0, 1.0, 1.0), c, RAINBOW_SAT);
}
float3 hueCity(float h)
{
    return hueNeon(h);
}
float3 rimColor(float2 id, float vv, float leg, float rb)
{
    float3 a = lerp(WINDOW_A, WINDOW_B, hash3(id));
    float3 b = lerp(WINDOW_A, WINDOW_B, hash3(id + 0.1));
    float3 c = lerp(a, b, 0.5);
    c = c / max(max(c.x, max(c.y, c.z)), 0.001);
    return lerp(c, lerp(cityWindow(hash1(id + 0.3)) * 0.9, hueCity(rb + RB_JITTER * (hash1(id + 0.3) - 0.5)), leg), vv);
}
float3 window(float z, float2 pos, float2 id, float px, float waveLit, float vv, float leg, float rb)
{
    float windowSize = 0.03 + 0.12 * hash1(id + 0.1);
    float windowProb = lerp(lerp(0.45, 0.3, leg), 0.15, vv * leg) + 0.8 * hash1(id + 0.2);
    float depth = z / windowSize;
    float lv = floor(depth);
    float district = hash1(floor(id / 8.0) * 0.618);
    float baseT = saturate(district + 0.20 * (hash1(id + 0.3) - 0.5));
    float baseT15 = hash1(id + 0.3);
    float spread = CITY_HUE_SPREAD * (2.0 * hash1(id + 0.1) - 1.0);
    float hj = RB_JITTER * (baseT15 - 0.5);
    float3 palA = lerp(cityWindow(baseT), hueCity(rb + hj), leg);
    float3 palB = lerp(cityWindow(baseT + spread), hueCity(rb + hj + RB_SPREAD * spread), leg);
    float3 colorA = lerp(lerp(WINDOW_A, WINDOW_B, hash3(id)), palA, vv);
    float3 colorB = lerp(lerp(WINDOW_A, WINDOW_B, hash3(id + 0.1)), palB, vv);
    float3 color = lerp(colorA, colorB, hash1(id, lv)) * lerp(1.0, lerp(CITY_GAIN, CITY_GAIN_V15, leg), vv);
    float noise = vnoise(20.0 * pos + 100.0 * hash1(lv));
    float ne = clamp(10.0 * px, 0.015, 0.08);
    color *= 0.15 + 0.95 * smoothstep(0.33 - ne, 0.33 + ne, noise);
    float tt = time * 0.15;
    float te = floor(tt);
    float onA = step(0.08, hash1(id, lv + 0.3 + te));
    float onB = step(0.08, hash1(id, lv + 0.3 + te + 1.0));
    float onOff = lerp(onA, onB, smoothstep(0.7, 1.0, tt - te));
    float on = step(windowProb, hash1(id, lv + 0.1)) * onOff;
    on = max(on, waveLit * step(0.25, hash1(id, lv + 0.9)));
    color *= on * (1.0 + WAVE_BRIGHT * waveLit);
    color *= lerp(1.0, 1.0 + RB_PULSE_BEAT * beat + RB_PULSE_LVL * level, vv * leg);
    float f = depth - lv;
    float wa = clamp(0.7 * px / windowSize, 0.02, 0.25);
    float windowGlow = smoothstep(0.10, 0.10 + 2.0 * wa, f)
                     * (1.0 - smoothstep(0.90 - 2.0 * wa, 0.90, f));
    return color * (lerp(1.0, 0.85, leg) * windowGlow);
}
float3 addLight(float3 eye, float3 ray, float res, float tm, float height, float vv, float leg, float rb)
{
    float2 q = eye.xy + ((height - eye.z) / ray.z) * ray.xy;
    float row = floor(q.x + 0.5);
    float rowH = row - 512.0 * floor(row / 512.0);
    tm += hash1(rowH);
    float col = floor(0.125 * q.y - tm);
    float colH = col - 64.0 * floor(col / 64.0);
    float pos = 0.4 + 0.4 * cos(tm + TAU * hash1(float2(rowH, colH)));
    float3 lightPos = float3(row, 8.0 * (col + tm + pos), height);
    float3 lightDir = float3(0.0, 1.0, 0.0);
    float3 w = eye - lightPos;
    float a = dot(ray, ray);
    float b = dot(ray, lightDir);
    float c = dot(lightDir, lightDir);
    float d = dot(ray, w);
    float e = dot(lightDir, w);
    float D = a * c - b * b;
    float s = (b * e - c * d) / D;
    float t = (a * e - b * d) / D;
    t = max(t, 0.0001);
    float dist = distance(eye + s * ray, lightPos + t * lightDir);
    float mask = 1.0 - smoothstep(res, res + 0.12, s);
    float light = min(1.0 / pow(160.0 * dist * dist / t + 14.0 * t * t, 0.75), 2.6);
    float shaft = 0.35 * exp(-1.8 * dist) * saturate(1.0 - t * 0.15);
    float fog = exp(-FOG_DENSITY * max(s - FOG_OFFSET, 0.0));
    float3 colO = lerp(LIGHT_A, LIGHT_B, hash3(float2(rowH, colH)));
    float lampWarm = step(0.45, hash1(float2(rowH, colH)));
    float3 colV16 = LIGHT_GAIN * lerp(lerp(float3(1.0, 0.62, 0.28), float3(0.85, 0.92, 1.0), lampWarm), float3(1.0, 1.0, 1.0), 0.1);
    float3 colV15 = LIGHT_GAIN_V15 * lerp(hueCity(rb + RB_JITTER * (hash1(float2(rowH, colH)) - 0.5)), float3(1.0, 1.0, 1.0), 0.15);
    float3 colV = lerp(colV16, colV15, leg);
    float3 color = lerp(colO, colV, vv);
    return (mask * light + lerp(LIGHT_HIDDEN_GLOW, 1.0, mask) * shaft) * fog * color;
}
#define SEG(x0, y0, x1, y1) gd = min(gd, segDist(gq, float2(x0, y0), float2(x1, y1)))
float segDist(float2 p, float2 a, float2 b)
{
    float2 pa = p - a;
    float2 ba = b - a;
    float h = saturate(dot(pa, ba) / max(dot(ba, ba), 0.0001));
    return length(pa - ba * h);
}
float hanGlyph(float gi, float2 gq, float gpx)
{
    float gd = 100.0;
    if (abs(gi - 4.0) < 0.5)
        gi = 1.0;
    if (gi < 0.5)
    {
        SEG(2.5, 3.6, 13.5, 3.6);
        SEG(5.8, 1.5, 5.8, 5.4);
        SEG(10.2, 1.5, 10.2, 5.4);
        SEG(3.0, 6.6, 13.0, 6.6);
        SEG(4.5, 6.6, 4.5, 11.4);
        SEG(11.5, 6.6, 11.5, 11.4);
        SEG(8.0, 6.6, 8.0, 11.4);
        SEG(4.5, 9.0, 11.5, 9.0);
        SEG(4.5, 11.4, 11.5, 11.4);
        SEG(6.5, 11.4, 3.0, 14.5);
        SEG(9.5, 11.4, 13.0, 14.5);
    }
    else if (gi < 1.5)
    {
        SEG(3.2, 2.6, 5.0, 4.0);
        SEG(2.4, 6.6, 4.4, 8.0);
        SEG(2.2, 13.4, 5.4, 9.6);
        SEG(10.5, 1.8, 10.5, 10.5);
        SEG(8.0, 4.4, 6.8, 6.8);
        SEG(13.0, 4.4, 14.0, 6.8);
        SEG(13.5, 8.0, 7.5, 14.5);
    }
    else if (gi < 2.5)
    {
        SEG(9.0, 3.0, 12.0, 6.5);
    }
    else if (gi < 3.5)
    {
        SEG(4.5, 1.8, 12.5, 1.8);
        SEG(4.5, 1.8, 4.5, 7.4);
        SEG(12.5, 1.8, 12.5, 7.4);
        SEG(4.5, 4.6, 12.5, 4.6);
        SEG(4.5, 7.4, 12.5, 7.4);
        SEG(2.5, 9.6, 13.5, 9.6);
        SEG(8.2, 7.4, 8.2, 12.4);
        SEG(8.2, 11.6, 3.0, 14.6);
        SEG(8.2, 11.6, 13.5, 14.6);
    }
    else if (gi < 5.5)
    {
        SEG(2.5, 1.8, 13.5, 1.8);
        SEG(2.5, 1.8, 2.5, 5.2);
        SEG(13.5, 1.8, 13.5, 5.2);
        SEG(2.5, 5.2, 13.5, 5.2);
        SEG(6.2, 1.8, 6.2, 5.2);
        SEG(9.8, 1.8, 9.8, 5.2);
        SEG(5.0, 6.8, 2.8, 9.0);
        SEG(2.8, 9.0, 5.6, 9.0);
        SEG(5.6, 9.0, 3.2, 11.2);
        SEG(4.2, 11.0, 4.2, 14.6);
        SEG(2.6, 12.4, 3.4, 13.4);
        SEG(5.8, 12.4, 5.0, 13.4);
        SEG(11.8, 6.6, 9.4, 8.0);
        SEG(11.2, 6.8, 11.2, 14.6);
        SEG(9.2, 9.4, 13.8, 9.4);
        SEG(9.2, 11.2, 13.8, 11.2);
        SEG(9.2, 13.0, 13.8, 13.0);
        SEG(8.0, 8.4, 7.2, 10.6);
        SEG(7.7, 9.6, 7.7, 14.6);
    }
    else if (gi < 6.5)
    {
        SEG(3.0, 3.2, 13.0, 3.2);
        SEG(8.0, 1.5, 8.0, 5.0);
        SEG(4.8, 5.0, 11.2, 5.0);
        SEG(4.8, 5.0, 4.8, 7.3);
        SEG(11.2, 5.0, 11.2, 7.3);
        SEG(4.8, 7.3, 11.2, 7.3);
        SEG(4.5, 8.8, 11.5, 8.8);
        SEG(4.5, 8.8, 4.5, 12.4);
        SEG(11.5, 8.8, 11.5, 12.4);
        SEG(4.5, 12.4, 11.5, 12.4);
        SEG(4.5, 10.6, 11.5, 10.6);
        SEG(6.5, 12.4, 3.8, 14.6);
        SEG(9.5, 12.4, 12.2, 14.6);
    }
    else if (gi < 7.5)
    {
        SEG(3.6, 3.6, 7.0, 3.6);
        SEG(5.3, 2.0, 5.3, 6.8);
        SEG(2.8, 6.8, 7.8, 6.8);
        SEG(5.3, 6.8, 5.3, 10.0);
        SEG(5.3, 10.0, 2.8, 12.4);
        SEG(3.2, 13.4, 13.8, 13.4);
        SEG(8.5, 4.6, 13.8, 4.6);
        SEG(8.8, 7.2, 13.8, 7.2);
        SEG(12.0, 2.2, 12.0, 11.0);
        SEG(12.0, 11.0, 10.6, 12.0);
        SEG(10.2, 2.8, 8.8, 10.8);
        SEG(13.0, 3.0, 13.8, 3.8);
    }
    else
    {
        SEG(2.5, 3.0, 13.5, 3.0);
        SEG(8.0, 1.5, 8.0, 6.0);
        SEG(3.5, 6.0, 12.5, 6.0);
        SEG(3.5, 6.0, 3.5, 14.5);
        SEG(12.5, 6.0, 12.5, 14.5);
        SEG(5.5, 8.2, 10.5, 8.2);
        SEG(8.0, 6.0, 8.0, 8.2);
        SEG(5.5, 10.4, 10.5, 10.4);
        SEG(5.5, 10.4, 5.5, 13.2);
        SEG(10.5, 10.4, 10.5, 13.2);
        SEG(5.5, 13.2, 10.5, 13.2);
        SEG(8.0, 8.2, 8.0, 13.2);
    }
    float r = max(0.85, 0.8 * gpx);
    float e = max(0.4 * gpx, 0.05);
    return 1.0 - smoothstep(r - e, r + e, gd);
}
float3 addSign(float3 color, float3 pos, float side, float2 id, float tw, float px, float vv, float leg, float rb)
{
    float4 signHash = signParams(id);
    float s = step(0.5, signHash.z);
    if ((s - 0.5) * side < 0.1)
        return color;
    float2 center = float2(0.2, -0.4) + float2(0.6, -0.8) * signHash.xy;
    float2 p = lerp(pos.xz, pos.yz, s);
    float isText = step(abs(id.x - TEXT_CELL.x) + abs(id.y - TEXT_CELL.y), 0.5);
    float halfWidth = lerp(0.04 + 0.06 * signHash.w, 0.85 * (0.06 + 0.1 * signHash.w), isText);
    float charCount = lerp(floor(1.0 + 8.0 * hash1(id + 0.5)), 9.0, isText);
    float second = 0.0;
    if (center.y - p.y > 2.0 * halfWidth * (charCount + 1.0))
    {
        second = 1.0;
        center.y -= 2.0 * halfWidth * (charCount + 1.5 + 5.0 * hash1(id + 0.6));
        charCount = floor(2.0 + 12.0 * hash1(id + 0.7));
        id += 0.05;
    }
    float3 signV = lerp(SIGN_GAIN * lerp(cityNeon(hash1(id + 0.5)), float3(1.0, 1.0, 1.0), 0.2), SIGN_GAIN_V15 * lerp(hueCity(rb + RB_JITTER * (hash1(id + 0.5) - 0.5)), float3(1.0, 1.0, 1.0), 0.2), leg);
    float3 outlineV = lerp(SIGN_GAIN * lerp(cityNeon(hash1(id + 0.6)), float3(1.0, 1.0, 1.0), 0.2), SIGN_GAIN_V15 * lerp(hueCity(rb + 0.08 + RB_JITTER * (hash1(id + 0.6) - 0.5)), float3(1.0, 1.0, 1.0), 0.2), leg);
    float3 signColor = lerp(lerp(SIGN_A, SIGN_B, hash3(id + 0.5)), signV, vv) * (1.0 + 0.6 * beat);
    float3 outlineColor = lerp(lerp(SIGN_A, SIGN_B, hash3(id + 0.6)), outlineV, vv);
    float flash = 6.0 - 24.0 * hash1(id + 0.8);
    flash *= step(3.0, flash);
    float beatFlick = 1.0 + 2.5 * beat;
    flash = smoothstep(0.1, 0.5, 0.5 + 0.5 * cos(flash * tw * beatFlick));
    float useText = isText * (1.0 - second);
    flash = lerp(flash, 1.0, useText);
    float2 halfSize = float2(halfWidth, halfWidth * charCount);
    float tpx = px / halfWidth;
    center.y -= halfSize.y;
    float outline = length(max(abs(p - center) - halfSize, 0.0)) / halfWidth;
    color *= smoothstep(0.1, 0.1 + max(0.1, 2.0 * tpx), outline);
    float2 charPos = 0.5 * (p - center + halfSize) / halfWidth;
    float2 cell = floor(charPos);
    float2 charId = id + 0.05 + 0.1 * floor(charPos);
    float flicker = hash1(charId);
    flicker = step(0.93, flicker);
    flicker = 1.0 - flicker * step(0.96, hash1(charId, tw));
    flicker = lerp(flicker, 1.0, useText);
    float charBright = 0.7 + 0.3 * sin(tw * (1.5 + 3.0 * hash1(charId + 0.2)) + hash1(charId) * TAU);
    float glyph = -3.5 + 8.0 * vnoise(id + 6.0 * charPos);
    charPos = frac(charPos);
    float ca = max(0.04, 0.6 * tpx);
    glyph *= smoothstep(0.15, 0.15 + 2.0 * ca, charPos.x) * (1.0 - smoothstep(0.85 - 2.0 * ca, 0.85, charPos.x));
    glyph *= smoothstep(0.15, 0.15 + 2.0 * ca, charPos.y) * (1.0 - smoothstep(0.85 - 2.0 * ca, 0.85, charPos.y));
    if (useText > 0.5)
    {
        float gx = lerp(1.0 - charPos.x, charPos.x, s);
        glyph = hanGlyph(charCount - 1.0 - cell.y, float2(gx, 1.0 - charPos.y) * 16.0, 8.0 * tpx);
    }
    color = lerp(color, signColor * charBright, flash * flicker * step(outline, 0.01) * clamp(glyph, 0.0, 1.0));
    float fa0 = 0.08;
    float fa1 = max(0.46, fa0 + 2.2 * tpx);
    float fe = max(0.03, 0.5 * tpx);
    outline = smoothstep(fa0 - fe, fa0 + fe, outline) * (1.0 - smoothstep(fa1 - fe, fa1 + fe, outline));
    return lerp(color, outlineColor, flash * outline);
}
float gmod(float x, float y)
{
    return x - y * floor(x / y);
}
float3 trackMin(float3 v, float d)
{
    if (d < v.x)      v = float3(d, v.x, v.y);
    else if (d < v.y) v = float3(v.x, d, v.y);
    else if (d < v.z) v = float3(v.x, v.y, d);
    return v;
}
float4 butterfly(float2 p, float aa)
{
    p.x = abs(p.x) + 0.00001;
    p.y *= 0.9;
    float4 col = float4(0.0, 0.0, 0.0, 0.0);
    float a = atan2(p.x, p.y);
    float r = length(p);
    if (p.y < 0.0)
    {
        float f = 0.6 + 0.01 * sin(24.0 * a);
        float w = 1.1 * a - 0.8;
        f *= sin(w) * sin(w);
        float th = f + 0.001;
        float th2 = th;
        float3 wcol = lerp(float3(210.0, 119.0, 40.0) / 255.0,
                           float3(232.0, 79.0, 12.0) / 255.0, smoothstep(0.0, 0.7, r));
        wcol *= 1.5;
        wcol *= 1.0 + 0.1 * sin(17.0 * p.x + float3(0.0, 0.0, 4.0))
                          * sin(23.0 * p.y + float3(0.0, 0.0, 4.0));
        float2 q = p;
        q.xy += 0.02 * sin(q.yx * 12.0);
        q.y = min(q.y, 0.0);
        float3 v = float3(10.0, 10.0, 10.0);
        v = trackMin(v, length(q - float2(0.29, -0.20)));
        v = trackMin(v, length(q - float2(0.10, -0.30)));
        v = trackMin(v, length(q - float2(0.20, -0.26)));
        v = trackMin(v, length(q - float2(0.28, -0.29)));
        v = trackMin(v, length(q - float2(0.34, -0.27)));
        v = trackMin(v, length(q - float2(0.38, -0.24)));
        v = trackMin(v, length(q - float2(0.39, -0.20)));
        v = trackMin(v, length(q - float2(0.38, -0.15)));
        v = trackMin(v, length(q - float2(0.35, -0.08)));
        v.y -= v.x;
        v.z -= v.x;
        float g = 1.25 * v.y * v.z / max(v.y + v.z, 0.001);
        wcol *= smoothstep(0.0, max(0.01, aa), g);
        th -= 0.05 * (1.0 - smoothstep(0.0, 0.05, g)) - 0.02;
        wcol *= smoothstep(max(0.02, aa * 0.8), max(0.03, aa * 1.6), (th - r) * th);
        q = float2(gmod(a, 0.2) - 0.1, (r - th + 0.025) * 3.1415 * 0.5);
        float d = length(q) - max(0.015, aa * 1.1);
        wcol = lerp(wcol, float3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.0, max(0.005, aa), d));
        wcol *= smoothstep(0.01, max(0.03, aa * 1.2), length(p - float2(0.235, -0.2)));
        d = r - (th + th2) * 0.5;
        col = float4(wcol, smoothstep(0.0, 2.0 * aa, -d));
    }
    if (a < 2.2)
    {
        float f = 0.65 + 0.015 * sin(24.0 * a);
        float w = a * (3.1416 / 2.356);
        float th = f * sin(w) * sin(w) + 0.001;
        float th2 = th;
        th += 0.25 * exp2(-50.0 * (w - 1.4) * (w - 1.4));
        float3 wcol = lerp(float3(0.7, 0.5, 0.2),
                           float3(0.8, 0.2, 0.0), smoothstep(0.0, 1.0, r));
        wcol *= 1.4;
        wcol *= 1.0 + 0.1 * sin(13.0 * p.x + float3(0.0, 0.0, 4.0))
                          * sin(19.0 * p.y + float3(0.0, 0.0, 4.0));
        float3 v = float3(10.0, 10.0, 10.0);
        v = trackMin(v, length(p - float2(0.25, 0.2)));
        v = trackMin(v, length(p - float2(0.35, 0.0)));
        v = trackMin(v, length(p - float2(0.4, 0.1)));
        v = trackMin(v, length(p - float2(0.45, 0.2)));
        v = trackMin(v, length(p - float2(0.45, 0.3)));
        v.y -= v.x;
        v.z -= v.x;
        float g = 2.0 * v.y * v.z / max(v.y + v.z, 0.001);
        wcol *= smoothstep(0.0, max(0.02, aa * 1.5), g);
        th2 -= 0.05 * (1.0 - smoothstep(0.0, 0.05, g));
        float isblack = smoothstep(max(0.02, aa * 0.8), max(0.03, aa * 1.6), (th2 - r) * th2);
        float2 q = float2(gmod(a, 0.2) - 0.1, (r - th + 0.025) * 3.1415 * 0.5);
        float d = length(q) - max(0.015, aa * 1.1);
        float ww = 1.0 - smoothstep(0.0, max(0.01, aa), d);
        if (r > th2)
        {
            float cells = 9.0;
            float2 cp = p * cells;
            float2 qq = frac(cp) - 0.5;
            float2 iq = floor(cp);
            float cid = iq.x * 111.0 + iq.y * 13.0;
            qq += 0.25 * sin(cid * float2(15.0, 17.0) + float2(0.0, 2.0));
            float rr = 1.0 + 0.75 * sin(cid * 431.0);
            ww = max(ww, 1.0 - smoothstep(0.0, max(0.01, aa * cells), length(qq) - 0.25 * rr));
        }
        wcol = lerp(wcol, float3(ww, ww, ww), 1.0 - isblack);
        d = r - th;
        float al = smoothstep(0.0, 2.0 * aa, -d);
        col.xyz = lerp(col.xyz, wcol, al);
        col.w = 1.0 - (1.0 - col.w) * (1.0 - al);
    }
    return col;
}
float2 wingPl(float2 p, float wing)
{
    float an = 1.55 * (0.5 - 0.5 * cos(12.566371 * wing));
    return float2(sign(p.x) * sin(an), -cos(an));
}
float2 proj(float2 p, float wing)
{
    float2 pl = wingPl(p, wing);
    float3 ro = float3(0.0, 0.085, 2.1);
    float3 rd = float3(p.x, p.y, -3.0);
    float den = dot(rd.xz, pl);
    den = (abs(den) < 0.001) ? 0.001 : den;
    float3 pos = ro - rd * (dot(ro.xz, pl) / den);
    return float2(length(pos.xz), pos.y);
}
float3 flyPathT(float t)
{
    float th = FLY_OMEGA * t + 0.9 * sin(0.21 * t + 1.0) + 0.4 * sin(0.53 * t);
    float fx = FLY_AX * (1.0 + 0.25 * sin(0.17 * t + 2.0)) * (sin(th) + 0.12 * sin(1.3 * t + 0.7));
    float fn = FLY_AZ * (cos(th) + 0.18 * sin(0.8 * t + 2.0) + 0.5 * sin(0.29 * t + 4.0));
    float rise = 0.8 * sin(0.31 * t + 0.8 * sin(0.13 * t)) + 0.3 * sin(0.77 * t + 2.0) + 0.2 * sin(1.37 * t + 5.0);
    fx += 0.30 * sin(2.3 * t + 1.0);
    fn += 0.25 * sin(1.9 * t + 3.0);
    rise += 0.05 * sin(2.7 * t + 0.5);
    return float3(fx, fn, FLY_RISE * rise);
}
float flapPhase(float t)
{
    float ft = gmod(t, 4.0);
    return FLAP_RATE * ft + 0.15 * sin(TAU * ft / 4.0) + 0.06 * sin(3.0 * TAU * ft / 4.0 + 1.0);
}
float flapAmp(float t)
{
    float g = sin(0.37 * t) * sin(0.23 * t + 1.0) + 0.35 * sin(0.83 * t + 2.0);
    return lerp(0.2, 1.0, smoothstep(-0.3, 0.5, g));
}
float flapPose(float t)
{
    return lerp(0.35, cos(12.566371 * flapPhase(t)), flapAmp(t));
}
float3 flyPath(float t)
{
    float phi = 12.566371 * flapPhase(t);
    float surge = flapAmp(t) * FLAP_SURGE * cos(phi) / (12.566371 * FLAP_RATE);
    return flyPathT(FLY_SPEED * t + surge);
}
float3 flyDrift(float t)
{
    float3 fwd = normalize(CAM_DIR);
    float3 rgt = normalize(cross(fwd, float3(0.0, 0.0, 1.0)));
    float3 upv = cross(rgt, fwd);
    float3 vrel = float3(CITY_SPEED * CAM_DIR.x, CITY_SPEED * CAM_DIR.y, 0.0);
    float tau = gmod(t, FLY_EPOCH) - 0.5 * FLY_EPOCH;
    return FLY_LAG * tau * float3(dot(vrel, rgt), dot(vrel, upv), dot(vrel, fwd));
}
float flyExit(float t)
{
    float tau = gmod(t, FLY_EPOCH) - 0.5 * FLY_EPOCH;
    return FLY_EXIT * sign(tau) * smoothstep(0.25 * FLY_EPOCH, 0.5 * FLY_EPOCH, abs(tau));
}
float4 flyScreen(float t, float prob)
{
    float3 f = flyPath(t);
    float3 dr = flyDrift(t);
    float depth = max(FLY_D0 - f.y + dr.z, 3.0);
    float xf = 0.5 + 0.5 * (f.x + dr.x) / (depth * VIEW_TAN);
    float barH = 2.0 * VIEW_TAN * depth * size.y / size.x;
    float lift = FLAP_LIFT * (1.0 + 0.5 * beat) * flapAmp(t) * cos(12.566371 * flapPhase(t));
    float yb = f.z + lift - 0.10 * f.y / FLY_AZ + dr.y / barH;
    float sizeVar = 1.0 + FLY_SIZE_VAR * (0.6 * sin(0.53 * t + 1.0) + 0.4 * sin(1.31 * t + 2.0)) + 0.04 * beat;
    float k = FLY_K * FLY_DNEAR / depth * sizeVar;
    return float4(xf, yb, depth, k);
}
float2 flyHeadVec(float t, float depth)
{
    float3 a = flyPathT(FLY_SPEED * (t - HEAD_E));
    float3 b = flyPathT(FLY_SPEED * (t + HEAD_E));
    float3 fwd = normalize(CAM_DIR);
    float3 rgt = normalize(cross(fwd, float3(0.0, 0.0, 1.0)));
    float3 upv = cross(rgt, fwd);
    float3 vrel = float3(CITY_SPEED * CAM_DIR.x, CITY_SPEED * CAM_DIR.y, 0.0);
    float3 dv = (1.0 - FLY_LAG) * 2.0 * HEAD_E * float3(dot(vrel, rgt), dot(vrel, upv), dot(vrel, fwd));
    float barH = 2.0 * VIEW_TAN * max(depth, 3.0) * size.y / size.x;
    float farV = -(b.y - a.y - dv.z)
              + (b.z - a.z + dv.y / barH) * FLY_VK / FLY_FORESHORT;
    float2 v = float2(b.x - a.x + dv.x, farV);
    return v / max(length(v), 0.0001);
}
float4 drawButterfly(float2 lp, float dp, float psi, float wing)
{
    float cs = cos(psi);
    float sn = sin(psi);
    float2 pw = float2(lp.x, lp.y / FLY_FORESHORT);
    float u = pw.x * cs + pw.y * sn;
    float v = -pw.x * sn + pw.y * cs;
    float2 p = float2(-v, u - 0.22);
    float2 pl = wingPl(p, wing);
    float2 q  = proj(p, wing);
    float2 qx = proj(p + float2(sn, cs) * dp, wing);
    float2 qy = proj(p + float2(-cs, sn) * (dp / FLY_FORESHORT), wing);
    float aa = clamp(length(qx - q) + length(qy - q), 0.004, 0.25);
    q = float2(min(q.x, 4.0), clamp(q.y, -4.0, 4.0));
    float4 tmp = saturate(butterfly(q, aa));
    tmp.xyz *= 0.1 + 0.9 * lerp(1.0, abs(q.y) * 0.5 + min(q.x * 2.0, 1.0), pl.x * pl.x);
    tmp.xyz *= saturate(0.25 + 0.75 * (pl.x - pl.y + 1.0));
    return tmp;
}
float isEventEpoch(float epoch)
{
    return step(abs(gmod(epoch, EV_EVERY) - (EV_EVERY - 1.0)), 0.5);
}
float calcSight(float tt, float prob, float hueShift)
{
    float sightEpoch = floor(tt / FLY_SIGHTING_PERIOD);
    float sightPhase = gmod(tt, FLY_SIGHTING_PERIOD);
    float sightRoll  = hash1(gmod(sightEpoch, 512.0) + 19.73 + hueShift);
    float maxStart   = max(FLY_SIGHTING_PERIOD - FLY_SIGHTING_LENGTH_MAX - 1.0, 1.0);
    float sightStart = hash1(gmod(sightEpoch, 512.0) + 31.17 + hueShift) * maxStart;
    float sightLen   = lerp(FLY_SIGHTING_LENGTH_MIN, FLY_SIGHTING_LENGTH_MAX,
                           hash1(gmod(sightEpoch, 512.0) + 43.91 + hueShift));
    return max(step(sightRoll, prob), step(hueShift, 0.5) * isEventEpoch(sightEpoch))
         * smoothstep(sightStart, sightStart + FLY_SIGHTING_FADE, sightPhase)
         * (1.0 - smoothstep(sightStart + sightLen - FLY_SIGHTING_FADE,
                            sightStart + sightLen, sightPhase));
}
float4 butterflyLayer(float2 uv, float tt, float prob, float sizeMul, float hueShift)
{
    float bh = size.y;
    float4 fl = flyScreen(tt, prob);
    float sight = calcSight(tt, prob, hueShift);
    float dp = BF_S / (fl.w * sizeMul) / bh;
    float2 lp = float2((uv.x - fl.x) * size.x, (0.5 - uv.y - fl.y) * bh) * dp;
    float rl = length(lp);
    if (rl >= 1.85 || sight < 0.001)
        return float4(0.0, 0.0, 0.0, 0.0);

    float amp = flapAmp(tt);
    float phase = flapPhase(tt);
    float2 hv0 = flyHeadVec(tt - HEAD_DT, fl.z);
    float2 hv1 = flyHeadVec(tt, fl.z);
    float2 hv2 = flyHeadVec(tt + HEAD_DT, fl.z);
    float2 hs = hv0 + hv1 + hv2;
    float hdg = atan2(hs.y, hs.x);
    float turn = atan2(hv0.x * hv2.y - hv0.y * hv2.x, dot(hv0, hv2));
    float bank = 0.3 * tanh(turn * 2.0);
    float psi = hdg + bank
              + 0.08 * amp * sin(12.566371 * phase)
              + 0.12 * sin(1.1 * tt + 0.5);
    float wing = acos(clamp(flapPose(tt), -1.0, 1.0)) / 12.566371;

    float4 bw = drawButterfly(lp, dp, psi, wing);

    float nearness = saturate((12.0 - fl.z) / 6.0);
    float edgeBoost = lerp(1.0, 1.3, nearness);
    bw.xyz = pow(max(bw.xyz, 0.0), 1.0 / edgeBoost) * edgeBoost;

    float blum = dot(bw.xyz, float3(0.3, 0.59, 0.11));
    float hueMix = 0.5 + 0.5 * sin(0.7 * tt + hueShift + 2.0 * lp.y + 1.2 * lp.x);
    float3 neonCol = lerp(float3(0.10, 0.90, 1.00), float3(1.00, 0.15, 0.85), hueMix);
    float3 glow = neonCol * (0.35 + 1.2 * blum);
    glow += float3(1.0, 1.0, 1.0) * smoothstep(0.8, 1.0, blum) * 0.35;
    bw.xyz = saturate(glow * (1.0 + 0.25 * level + 0.4 * beat));

    float fogB = exp(-0.15 * max(fl.z - FOG_OFFSET, 0.0));
    bw.xyz = lerp(FOG_COLOR * 1.5, bw.xyz, 0.5 + 0.5 * fogB);
    bw.w *= 1.0 - smoothstep(1.55, 1.8, rl);
    bw.w *= sight;

    if (bw.w > 0.05)
    {
        float4 flTrail = flyScreen(tt - 0.06, prob);
        float2 lpT = float2((uv.x - flTrail.x) * size.x, (0.5 - uv.y - flTrail.y) * bh) * dp;
        float rlT = length(lpT);
        float trailA = (1.0 - smoothstep(0.5, 1.1, rlT)) * 0.14 * sight * bw.w;
        bw.xyz = lerp(neonCol * 0.55, bw.xyz, saturate(bw.w / max(bw.w + trailA, 0.001)));
        bw.w = 1.0 - (1.0 - bw.w) * (1.0 - trailA);
    }

    return bw;
}
float4 dustLayer(float2 uv, float tt, float prob, float sizeMul, float hueShift)
{
    float bh = size.y;
    float4 fl = flyScreen(tt, prob);
    float sight = calcSight(tt, prob, hueShift);
    if (sight < 0.001)
        return float4(0.0, 0.0, 0.0, 0.0);
    float dyu = uv.y - (0.5 - fl.y);
    if (abs(fl.y) > 3.3 || abs(uv.x - fl.x) > 0.3 || dyu < -0.55 || dyu > 1.0)
        return float4(0.0, 0.0, 0.0, 0.0);

    float dp = BF_S / (fl.w * sizeMul) / bh;
    float2 lp = float2((uv.x - fl.x) * size.x, (0.5 - uv.y - fl.y) * bh) * dp;

    float tw = gmod(tt, DUST_DT * 2048.0);
    float id0 = floor(tw / DUST_DT);
    float ph0 = tw / DUST_DT - id0;

    float3 pm = float3(0.0, 0.0, 0.0);
    float aw = 0.0;
    float beatBoost = 1.0 + 0.25 * level + 0.35 * beat;

    [loop]
    for (int k = 0; k < DUST_N; ++k)
    {
        float a = (ph0 + float(k)) * DUST_DT;
        float pid = gmod(id0 - float(k), 2048.0);
        float tk = tt - a;
        float2 hA = hash2(float2(pid, 3.7), 0.9);
        float2 hB = hash2(float2(pid, 8.1), 2.3);

        float emit = step(hB.y, 0.28 + 0.55 * flapAmp(tk));
        if (emit < 0.5)
            continue;

        float4 se = flyScreen(tk, prob);
        float ang = hA.x * TAU;
        float2 eo = float2(cos(ang), sin(ang)) * (sqrt(hA.y) * 0.7);
        float2 pp = float2((se.x - fl.x) * size.x, (se.y - fl.y) * bh) * dp + eo;

        float upChance = step(0.88, hB.y);
        float fall = (0.55 + 0.55 * hB.x) * a * a / (1.0 + 1.1 * a);
        pp.y -= fall * (1.0 - 1.7 * upChance);
        pp.x += (hB.y - 0.5) * 0.5 * (1.0 - exp(-1.2 * a));
        pp.x += 0.025 * sin(1.7 * a + ang * 4.0 + pid) * min(a, 0.7);
        pp.y += 0.01 * sin(2.1 * a + pid * 0.7) * min(a, 0.5);

        float sizeVar = 0.45 + 1.1 * hA.y;
        float rad = sizeVar * dp;
        float2 dlt = lp - pp;
        float d2 = dot(dlt, dlt);
        if (d2 > rad * rad * 20.0)
            continue;

        float d = sqrt(d2);
        float core = 1.0 - smoothstep(rad * 0.4, rad * 1.3, d);
        float halo = (0.18 + 0.14 * hA.y) * (1.0 - smoothstep(0.0, rad * (3.0 + 2.0 * hA.y), d));
        float life = saturate(1.0 - a / DUST_LIFE);
        float fade = life * life * smoothstep(0.0, 0.12, a);
        float twk = 0.78 + 0.22 * sin(9.0 * a + 40.0 * hA.x + hB.y * 6.0);
        float w = saturate((core + halo) * fade * twk);

        float hueMix = 0.5 + 0.5 * sin(0.7 * tk + hueShift + 6.0 * hB.x);
        float3 dcol = lerp(float3(0.10, 0.90, 1.00), float3(1.00, 0.15, 0.85), hueMix);
        float3 deathCol = float3(0.75, 0.15, 0.85);
        dcol = lerp(deathCol, dcol, life * 0.85 + 0.15);
        dcol = lerp(dcol, float3(1.0, 1.0, 1.0), 0.40 * core) * beatBoost;

        pm += dcol * w;
        aw = 1.0 - (1.0 - aw) * (1.0 - w);
    }

    float alpha = aw * sight;
    return float4(saturate(pm / max(aw, 0.001)), alpha);
}
#define BAR_WIDTH_PX      3.5
#define BAR_GAP_PX        2.0
#define BAR_MIN           0.03
#define BAR_GAIN          0.72
#define BAR_TOP_MAX       0.95
#define BAR_BEAT_PUMP     0.10
#define BAR_ALPHA         0.55
#define LINE_PX          1.4
#define CURVE_STEP       3.0
#define UNDER_GAIN       1.6
#define UNDER_MIX        0.70
#define UNDER_WARM       0.45
#define UNDER_WARM_REF   float3(1.00, 0.50, 0.20)
#define UNDER_LIGHT      float3(1.00, 0.85, 0.55)
#define SPLINE_SMOOTH    1.0
#define NEON_CORE_WHITE   0.20
#define BAR_SHADOW        0.25
#define UNDER_CITY_HUE   0.35
#define UNDER_WAVE       0.06
#define UNDER_SPARK      0.45
#define UNDER_PULSE_BEAT 0.35
#define UNDER_PULSE_LVL  0.25

float3 songWarm(float3 c)
{
    c = c / max(max(c.x, max(c.y, c.z)), 0.05);
    return saturate(lerp(c, UNDER_WARM_REF, UNDER_WARM));
}
float3 warmRamp(float l)
{
    float3 wB = songWarm(colorB.rgb) * 0.85;
    float3 wA = songWarm(colorA.rgb);
    float3 c = lerp(float3(0.0, 0.0, 0.0), wB, smoothstep(0.0, 0.35, l));
    c = lerp(c, wA, smoothstep(0.30, 0.70, l));
    return lerp(c, lerp(wA, UNDER_LIGHT, 0.6), smoothstep(0.65, 1.0, l));
}
float3 underRainbow(float l, float hue)
{
    float3 rb = hueNeon(hue);
    float3 c = rb * lerp(0.12, 1.0, smoothstep(0.0, 0.65, l));
    return lerp(c, lerp(rb, float3(1.0, 1.0, 1.0), 0.55), smoothstep(0.65, 1.0, l));
}
float specAt(float c)
{
    return spectrum(saturate(c));
}
float barFrac(float v)
{
    return min(BAR_TOP_MAX, (BAR_MIN + BAR_GAIN * v) * (1.0 + BAR_BEAT_PUMP * beat));
}
float2 peakCurve(float h0, float h1, float h2, float h3, float t)
{
    float t2 = t * t;
    float t3 = t2 * t;
    float cr = 0.5 * (2.0 * h1 + (h2 - h0) * t + (2.0 * h0 - 5.0 * h1 + 4.0 * h2 - h3) * t2 + (3.0 * h1 - h0 - 3.0 * h2 + h3) * t3);
    float dcr = 0.5 * ((h2 - h0) + 2.0 * (2.0 * h0 - 5.0 * h1 + 4.0 * h2 - h3) * t + 3.0 * (3.0 * h1 - h0 - 3.0 * h2 + h3) * t2);
    float bs = (h0 + 4.0 * h1 + h2 + 3.0 * (h2 - h0) * t + 3.0 * (h0 - 2.0 * h1 + h2) * t2 + (3.0 * h1 - h0 - 3.0 * h2 + h3) * t3) / 6.0;
    float dbs = (3.0 * (h2 - h0) + 6.0 * (h0 - 2.0 * h1 + h2) * t + 3.0 * (3.0 * h1 - h0 - 3.0 * h2 + h3) * t2) / 6.0;
    return float2(lerp(cr, bs, SPLINE_SMOOTH), lerp(dcr, dbs, SPLINE_SMOOTH));
}
float4 coverEvent()
{
    float epoch = floor(time / FLY_SIGHTING_PERIOD);
    float phase = gmod(time, FLY_SIGHTING_PERIOD);
    float eh = gmod(epoch, 512.0);
    float maxStart = max(FLY_SIGHTING_PERIOD - FLY_SIGHTING_LENGTH_MAX - 1.0, 1.0);
    float start = hash1(eh + 31.17) * maxStart + FLY_SIGHTING_FADE;
    float on = isEventEpoch(epoch);
    float tev = phase - start;
    float tEnd = EV_COVER + EV_HOLD;
    float act = on * step(0.0, tev) * step(tev, tEnd + EV_RELEASE);
    float rel = smoothstep(tEnd, tEnd + EV_RELEASE, tev);
    float calm = smoothstep(0.0, EV_CALM_T, tev) * (1.0 - rel);
    float prog = saturate(tev / EV_COVER);
    float done = on * step(EV_COVER, tev);
    float styleB = gmod(floor(epoch / EV_EVERY) + done, 2.0);
    return float4(float3(prog, calm, rel) * act, styleB);
}
void curveEval(float2 uv, float4 ev, out float u, out float py, out float yCurve, out float slope)
{
    u = abs(2.0 * uv.x - 1.0);
    float columns = max(floor(0.5 * size.x / ((BAR_WIDTH_PX + BAR_GAP_PX) * CURVE_STEP)), 1.0);
    float pitch = 0.5 * size.x / columns;
    py = rise(uv) * size.y;

    float k = floor(u * columns - 0.5);
    float t = u * columns - (k + 0.5);
    float h0 = barFrac(specAt(abs((k - 0.5) / columns))) * size.y;
    float h1 = barFrac(specAt(abs((k + 0.5) / columns))) * size.y;
    float h2 = barFrac(specAt(abs((k + 1.5) / columns))) * size.y;
    float h3 = barFrac(specAt(abs((k + 2.5) / columns))) * size.y;
    float2 cv = peakCurve(h0, h1, h2, h3, t);
    yCurve = max(cv.x, 0.0);
    slope = cv.y / pitch;

    if (ev.y > 0.0001)
    {
        float topY = size.y * EV_TOP;
        float s = saturate((ev.x * (1.0 + EV_EDGE) - u) / EV_EDGE);
        float c = s * s * (3.0 - 2.0 * s);
        float dc = -6.0 * s * (1.0 - s) / EV_EDGE / (0.5 * size.x);
        c *= 1.0 - ev.z;
        dc *= 1.0 - ev.z;
        slope = (1.0 - c) * slope + (topY - yCurve) * dc;
        yCurve = lerp(yCurve, topY, c);
    }
}
float4 seekSpectrum(float u, float py, float yCurve, float slope, float3 pre, float a)
{
    float dLine = abs(py - yCurve) * rsqrt(1.0 + slope * slope) - 0.5 * LINE_PX;

    float ty = saturate(py / (size.y * 0.75));
    float th = time - 4096.0 * floor(time / 4096.0);
    float hue = RAINBOW_SPAN * u + RAINBOW_SPEED * th + RAINBOW_RISE * ty;
    float3 col = hueNeon(hue) * (1.0 + 0.25 * beat);

    float lineA = (1.0 - smoothstep(-0.5, 0.5, dLine)) * BAR_ALPHA;

    float halo = exp(-max(dLine, 0.0) / 2.5) * smoothstep(-0.5, 0.5, dLine);
    pre *= 1.0 - BAR_SHADOW * halo * (1.0 - lineA);

    float3 lineCol = lerp(col, float3(1.0, 1.0, 1.0), NEON_CORE_WHITE);
    pre = lineCol * lineA + pre * (1.0 - lineA);
    a = lineA + a * (1.0 - lineA);
    return float4(pre, a);
}
float4 main(float2 uv : TEXCOORD) : COLOR
{
    float tw = time - 4096.0 * floor(time / 4096.0);
    float cu, cpy, cyc, cslope;
    float4 ev = coverEvent();
    curveEval(uv, ev, cu, cpy, cyc, cslope);
    float vvG = smoothstep(-0.5, 0.5, cyc - cpy);
    float vv = lerp(vvG, 1.0, ev.w);
    float leg = lerp(1.0, vvG, ev.w);
    float2 center = -CITY_SPEED * time * CAM_DIR.xy;
    center -= 512.0 * floor(center / 512.0);
    float3 eye = float3(center, 0.0) - CAM_DIST * CAM_DIR;
    float3 forward = normalize(CAM_DIR);
    float3 right = normalize(cross(forward, float3(0.0, 0.0, 1.0)));
    float3 up = cross(right, forward);
    float2 xy = float2(2.0 * uv.x - 1.0, (1.0 - 2.0 * uv.y) * size.y / size.x) * VIEW_TAN
              + float2(0.0, VIEW_SHIFT);
    float3 ray = normalize(xy.x * right + xy.y * up + forward);
    float edgeV;
    float4 res = castRay(eye, ray, center, edgeV);
    float3 p = eye + res.x * ray;
    float2 block = floor(p.xy);
    float2 hb = wrapCell(block);
    float2 pw = hb + (p.xy - block);
    float cid = hash1(hb + 0.37);
    float rbH = RAINBOW_SPAN * cu + RAINBOW_SPEED * tw + RAINBOW_RISE * saturate(cpy / (size.y * 0.75))
              + UNDER_CITY_HUE * (cid - 0.5)
              + UNDER_WAVE * sin(0.1 * cpy - 1.570796 * tw + 6.0 * cu);
    float px = 1.4 * res.x * (2.0 * VIEW_TAN / size.x);
    float waveZ = lerp(WAVE_Z0, WAVE_Z1, frac(tw * WAVE_SPEED));
    float wd = (p.z - waveZ) / WAVE_WIDTH;
    float waveLit = exp(-wd * wd) * (0.35 + 0.65 * saturate(beat + 0.5 * level));
    float3 color = window(p.z - res.y, pw, hb, px, waveLit, vv, leg, rbH);
    float specX = saturate((p.z - SPEC_Z0) / (SPEC_Z1 - SPEC_Z0) + SPEC_JITTER * (hash1(hb + 0.9) - 0.5));
    float3 specCol = spectrum(specX);
    float satBoost = lerp(1.2, 1.0 + 0.45 * length(specCol), leg);
    float gray = dot(color, float3(0.33, 0.33, 0.33));
    color = lerp(float3(gray, gray, gray), color, satBoost);
    float specLum = dot(specCol, float3(0.3, 0.59, 0.11));
    float specMin = lerp(SPEC_MIN, 1.3, vv * leg);
    float3 specMul = specMin + SPEC_GAIN * specCol;
    float3 specMulL = float3(1.0, 1.0, 1.0) * (specMin + SPEC_GAIN * specLum);
    color *= lerp(specMul, lerp(specMul, specMulL, 1.0 - lerp(SPEC_TINT, SPEC_TINT_V15, leg)), vv);
    color = addSign(color, float3(p.xy - block, p.z - res.y), res.z, hb, tw, px, vv, leg, rbH);
    color = lerp(float3(0.0, 0.0, 0.0), color, abs(res.z));
    color += hueCity(rbH) * (RB_FLOOR * vv * leg);
    float heightFog = saturate((p.z + 2.0) / 8.0);
    float3 fogCol = lerp(lerp(FOG_COLOR, FOG_HIGH, heightFog), lerp(lerp(FOG_V_LOW, FOG_V_HIGH, heightFog), lerp(lerp(FOG_V_LOW_V15, FOG_V_HIGH_V15, heightFog), hueCity(rbH + 0.15) * RB_FOG_GAIN, RB_FOG_TINT), leg), vv);
    float fog = exp(-FOG_DENSITY * (1.0 + FOG_BREATH * level) * max(res.x - FOG_OFFSET, 0.0));
    color = lerp(fogCol, color, fog);
    float rimW = max(0.015, 2.0 * px);
    float rim = 1.0 - smoothstep(0.0, rimW, edgeV);
    color += rimColor(hb, vv, leg, rbH) * (lerp(RIM_GAIN, RIM_GAIN_V15, leg) * lerp(1.0, 1.4, vv * leg) * (0.5 + 0.9 * beat + 0.5 * level) * rim * fog);
    float tl = LIGHT_SPEED * time;
    float lightGain = 1.05 + 0.55 * level;
    color += lightGain * addLight(eye.xyz, ray.xyz, res.x, tl, LIGHT_HEIGHT - 0.6, vv, leg, rbH);
    color += lightGain * addLight(eye.yxz, ray.yxz, res.x, tl, LIGHT_HEIGHT - 0.4, vv, leg, rbH);
    color += lightGain * addLight(float3(-eye.xy, eye.z), float3(-ray.xy, ray.z), res.x, tl, LIGHT_HEIGHT - 0.2, vv, leg, rbH);
    color += lightGain * addLight(float3(-eye.yx, eye.z), float3(-ray.yx, ray.z), res.x, tl, LIGHT_HEIGHT, vv, leg, rbH);
    float vl = dot(color, float3(0.3, 0.59, 0.11));
    float3 vib = max(lerp(float3(vl, vl, vl), color, lerp(1.0, lerp(VIB_SAT, VIB_SAT_RB, leg), vv)) * lerp(1.0, lerp(VIB_GAIN, VIB_GAIN_RB, leg), vv), 0.0);
    float vmx = max(vib.x, max(vib.y, vib.z));
    float3 soft = lerp(vib / max(vmx, 1.0), float3(1.0, 1.0, 1.0), RB_HOT_WHITE * saturate(vmx - 1.0));
    color = saturate(lerp(vib, soft, vv * leg));
    float lum = dot(color, float3(0.3, 0.59, 0.11));
    float alpha = saturate(0.3 + 2.0 * lum);
    float4 b2 = butterflyLayer(uv, time + FLY_B2_OFFSET, FLY_B2_PROB, FLY_B2_SIZE, 2.5);
    float4 b1 = butterflyLayer(uv, time, 0.92, 1.0, 0.0);
    float4 d2 = dustLayer(uv, time + FLY_B2_OFFSET, FLY_B2_PROB, FLY_B2_SIZE, 2.5);
    float4 d1 = dustLayer(uv, time, 0.92, 1.0, 0.0);
    float3 pre = d2.xyz * d2.w + color * alpha * (1.0 - d2.w);
    alpha = d2.w + alpha * (1.0 - d2.w);
    pre = d1.xyz * d1.w + pre * (1.0 - d1.w);
    alpha = d1.w + alpha * (1.0 - d1.w);
    pre = b2.xyz * b2.w + pre * (1.0 - b2.w);
    alpha = b2.w + alpha * (1.0 - b2.w);
    pre = b1.xyz * b1.w + pre * (1.0 - b1.w);
    alpha = b1.w + alpha * (1.0 - b1.w);
    color = pre / max(alpha, 0.001);
    float bloomLum = dot(color, float3(0.3, 0.59, 0.11));
    float3 bloom = color * smoothstep(0.55, 1.1, bloomLum) * 0.45;
    color = saturate(color + bloom);
    float4 outPre = seekSpectrum(cu, cpy, cyc, cslope, color * alpha, alpha);
    return paint(outPre.xyz / max(outPre.w, 0.001), outPre.w);
}