Phosphorus Airburst Tendrils shader

phosphorus-airburst-tendrils.glsl · 925 lines · GLSL ES 3.00 · MIT

Phosphorus Airburst TendrilsGame VFX
Phosphorus Airburst Tendrils WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 925 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Physically-Based VFX
phosphorus-airburst-tendrils.glsl (925 lines)
#version 300 es
// Phosphorus Airburst Tendrils (phosphorus-airburst-tendrils.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_realistic/lib/10_phosphorus_airburst_tendrils.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

// ============================================================================
// 10_phosphorus_airburst_tendrils.glsl
// Photoreal night-time white-phosphorus airburst with 12 curved 3D ballistic
// incandescent fragments, crisp billowing cauliflower smoke tendrils, fine
// directional trailing spark streaks, and dark tumbling shrapnel fragments.
//
// Features:
//   - Adaptive 3D volumetric raymarch (fine dt=0.026 inside smoke, fast empty
//     space skipping) with subtle IGN jitter for zero edge banding
//   - 12 curated 3D ballistic trajectories with aerodynamic drag, gravity arc,
//     and sinuous helical curl; analytical quadratic-curve projection + culling
//   - High-frequency curled 3D billow fBM cauliflower smoke with crisp silhouette
//     carving, dense Beer-Lambert extinction, and 3-tap directional self-shadowing
//   - Tight, intense incandescent white-orange phosphorus tips with local warm
//     inverse-square scatter into adjacent smoke + tapered 3D spark streaks
//   - 16 analytical 3D tumbling dark metallic shell/shrapnel fragments with
//     warm burst rim lighting and exact depth compositing against smoke
//   - Deep midnight-navy/slate night sky with subtle dark storm clouds
//     (calibrated in linear space for lumMin ~8-14, %black < 2%, %clip < 1%)
//   - uLightMode: 0=hero night sky, 1=neutral grayscale study (R==G==B),
//                 2=grazing raking key light, 3=backlit rim light
//   - uCamMode:   0=hero full starburst, 1=~2.5x macro close-up on glowing tips
// ============================================================================

const float PI  = 3.14159265359;
const float TAU = 6.28318530718;

const int   NUM_TRAILS = 12;
const int   NUM_SHRAP  = 16;
const vec3  BURST_ORIGIN = vec3(0.0, 0.14, 0.0);
const vec3  GRAVITY      = vec3(0.0, -0.74, 0.0);

struct Trail {
    vec3  p0;
    vec3  p1;
    vec3  p2;
    vec3  a;
    vec3  b;
    vec3  bCenter;
    float bRad;
    float thick;
    float phase;
    float heat;
};

Trail gTrails[NUM_TRAILS];
float gAge;
float gTime;

// ----------------------------------------------------------------------------
// Hash & 3D Value / Cauliflower Billow Noise
// ----------------------------------------------------------------------------
float hash11(float p) {
    p = fract(p * 0.1031);
    p *= p + 33.33;
    p *= p + p;
    return fract(p);
}

float hash13(vec3 p3) {
    p3 = fract(p3 * 0.1031);
    p3 += dot(p3, p3.zyx + 31.32);
    return fract((p3.x + p3.y) * p3.z);
}

vec3 hash33(vec3 p3) {
    p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
    p3 += dot(p3, p3.yxz + 33.33);
    return fract((p3.xxy + p3.yxx) * p3.zyx);
}

float ign(vec2 fragCoord) {
    vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
    return fract(magic.z * fract(dot(fragCoord, magic.xy)));
}

float noise3D(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    vec3 u = f * f * (3.0 - 2.0 * f);

    float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
    float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
    float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
    float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
    float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
    float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
    float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
    float n111 = hash13(i + vec3(1.0, 1.0, 1.0));

    float nx00 = mix(n000, n100, u.x);
    float nx10 = mix(n010, n110, u.x);
    float nx01 = mix(n001, n101, u.x);
    float nx11 = mix(n011, n111, u.x);

    float nxy0 = mix(nx00, nx10, u.y);
    float nxy1 = mix(nx01, nx11, u.y);

    return mix(nxy0, nxy1, u.z);
}

float noise2D(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash13(vec3(i, 0.0));
    float b = hash13(vec3(i + vec2(1.0, 0.0), 0.0));
    float c = hash13(vec3(i + vec2(0.0, 1.0), 0.0));
    float d = hash13(vec3(i + vec2(1.0, 1.0), 0.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

const mat3 OCT_ROT = mat3(
     0.00,  0.80,  0.60,
    -0.80,  0.36, -0.48,
    -0.60, -0.48,  0.64
);

// Coarse 3-octave cauliflower billow fBM (for shadow taps)
float billowFbm3(vec3 p) {
    float v = 0.0;
    float a = 0.54;
    for (int i = 0; i < 3; i++) {
        float n = noise3D(p);
        float puff = 1.0 - abs(n * 2.0 - 1.0);
        puff = puff * puff;
        v += a * mix(n, puff, 0.68);
        p = OCT_ROT * p * 2.12 + vec3(13.7, -9.2, 5.4);
        a *= 0.48;
    }
    return v;
}

// High-frequency 5-octave cauliflower billow fBM for crisp turbulent smoke curls
float billowFbm5(vec3 p) {
    float v = 0.0;
    float a = 0.50;
    for (int i = 0; i < 5; i++) {
        float n = noise3D(p);
        float puff = 1.0 - abs(n * 2.0 - 1.0);
        puff = puff * puff;
        v += a * mix(n, puff, 0.72);
        p = OCT_ROT * p * 2.14 + vec3(11.3, -7.1, 19.4);
        a *= 0.50;
    }
    return v;
}

// 2-octave 3D pseudo-curl displacement vector for sinuous smoke eddies
vec3 curlWarp(vec3 p) {
    vec3 q1 = p + vec3(0.0, -gTime * 0.16, 0.0);
    vec3 w1 = vec3(
        noise3D(q1 + vec3( 0.0,  0.0,  0.0)) - 0.5,
        noise3D(q1 + vec3(19.3,  7.1, 11.7)) - 0.5,
        noise3D(q1 + vec3( 5.9, 23.4, 15.2)) - 0.5
    );
    vec3 q2 = OCT_ROT * q1 * 2.25 + vec3(4.2, -3.7, 8.1);
    vec3 w2 = vec3(
        noise3D(q2 + vec3( 0.0,  0.0,  0.0)) - 0.5,
        noise3D(q2 + vec3(13.1, 17.5,  3.9)) - 0.5,
        noise3D(q2 + vec3(29.2,  2.7, 21.8)) - 0.5
    );
    return w1 * 1.45 + w2 * 0.72;
}

// ----------------------------------------------------------------------------
// 12 Curated Ballistic Phosphorus Trajectories
// ----------------------------------------------------------------------------
vec3 evalRawBallistic(vec3 v0, float s, float phase) {
    float drag = (1.0 - exp(-0.46 * s)) / 0.46;
    vec3 p = BURST_ORIGIN + v0 * drag + 0.5 * GRAVITY * s * s;

    // Sinuous aerodynamic wobble along the trajectory
    vec3 dir = normalize(v0 + vec3(0.001));
    vec3 ref = abs(dir.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
    vec3 uAx = normalize(cross(dir, ref));
    vec3 vAx = cross(dir, uAx);
    float wAmp = 0.068 * s * smoothstep(0.0, 0.35, s);
    p += uAx * sin(s * 5.1 + phase) * wAmp + vAx * cos(s * 4.3 + phase * 1.6) * wAmp;
    return p;
}

void initTrails() {
    gTime = uTime;
    float tCycle = mod(uTime, 4.2);
    // Dramatic at t=1.2 (age=1.18) and smooth continuous burst across 0..3s
    gAge = 0.72 + tCycle * 0.38 + 0.15 * uOpen;

    vec3 vels[NUM_TRAILS];
    vels[0]  = vec3( 1.28,  1.44, -0.18); // Upper-right sweeping hero arc
    vels[1]  = vec3(-1.04,  1.36,  0.14); // Upper-left curling hero arc
    vels[2]  = vec3(-1.34,  0.74,  0.26); // Mid-left thick billowing plunge
    vels[3]  = vec3(-0.42,  0.54,  0.96); // Foreground lower-left plunging hero
    vels[4]  = vec3( 0.86,  0.94,  0.52); // Upper-mid-right bright thick curl
    vels[5]  = vec3( 1.48,  0.52, -0.06); // Far-right sweeping downward arc
    vels[6]  = vec3( 0.16,  1.28,  0.24); // Upper-center vertical curling plume
    vels[7]  = vec3( 0.44,  0.14,  0.60); // Lower-right steep plunging plume
    vels[8]  = vec3(-1.12,  0.02, -0.24); // Lower-left curling smoke ribbon
    vels[9]  = vec3( 1.08, -0.04,  0.34); // Mid-lower-right arcing tendril
    vels[10] = vec3(-0.20,  1.72, -0.34); // Thin fast high-altitude top-center spark trail
    vels[11] = vec3( 1.06,  1.14, -0.58); // Thin fast secondary upper-right spark trail

    // Narrower, crisper, more sinuous smoke tendrils matching 10.png
    float thicks[NUM_TRAILS];
    thicks[0]  = 0.068;
    thicks[1]  = 0.078;
    thicks[2]  = 0.084;
    thicks[3]  = 0.080;
    thicks[4]  = 0.082;
    thicks[5]  = 0.068;
    thicks[6]  = 0.076;
    thicks[7]  = 0.066;
    thicks[8]  = 0.086;
    thicks[9]  = 0.064;
    thicks[10] = 0.024;
    thicks[11] = 0.026;

    float speeds[NUM_TRAILS];
    speeds[0]  = 1.08;
    speeds[1]  = 0.98;
    speeds[2]  = 1.02;
    speeds[3]  = 1.06;
    speeds[4]  = 0.90;
    speeds[5]  = 1.04;
    speeds[6]  = 0.92;
    speeds[7]  = 1.12;
    speeds[8]  = 0.96;
    speeds[9]  = 1.00;
    speeds[10] = 1.16;
    speeds[11] = 1.08;

    float heats[NUM_TRAILS];
    heats[0]  = 1.15;
    heats[1]  = 1.10;
    heats[2]  = 1.22;
    heats[3]  = 1.28;
    heats[4]  = 1.32;
    heats[5]  = 1.12;
    heats[6]  = 1.15;
    heats[7]  = 1.18;
    heats[8]  = 0.55;
    heats[9]  = 1.08;
    heats[10] = 0.85;
    heats[11] = 0.78;

    for (int i = 0; i < NUM_TRAILS; i++) {
        float sTip = gAge * speeds[i];
        float ph = float(i) * 2.17 + 0.5;
        vec3 p0 = BURST_ORIGIN;
        vec3 p1 = evalRawBallistic(vels[i], 0.5 * sTip, ph);
        vec3 p2 = evalRawBallistic(vels[i], sTip, ph);

        vec3 a = 4.0 * p1 - 3.0 * p0 - p2;
        vec3 b = 2.0 * p0 - 4.0 * p1 + 2.0 * p2;

        vec3 bc = (p0 + p1 + p2) * 0.333333;
        float r0 = length(p0 - bc);
        float r1 = length(p1 - bc);
        float r2 = length(p2 - bc);
        float br = max(r0, max(r1, r2)) + thicks[i] * 4.2 + 0.24;

        gTrails[i].p0      = p0;
        gTrails[i].p1      = p1;
        gTrails[i].p2      = p2;
        gTrails[i].a       = a;
        gTrails[i].b       = b;
        gTrails[i].bCenter = bc;
        gTrails[i].bRad    = br;
        gTrails[i].thick   = thicks[i];
        gTrails[i].phase   = ph;
        gTrails[i].heat    = heats[i];
    }
}

vec2 closestOnTrail(Trail tr, vec3 pos) {
    vec3 e1 = tr.p1 - tr.p0;
    float t1 = clamp(dot(pos - tr.p0, e1) / max(dot(e1, e1), 1e-5), 0.0, 1.0);
    vec3 d1v = pos - (tr.p0 + e1 * t1);
    float d1sq = dot(d1v, d1v);

    vec3 e2 = tr.p2 - tr.p1;
    float t2 = clamp(dot(pos - tr.p1, e2) / max(dot(e2, e2), 1e-5), 0.0, 1.0);
    vec3 d2v = pos - (tr.p1 + e2 * t2);
    float d2sq = dot(d2v, d2v);

    float u = (d1sq < d2sq) ? (0.5 * t1) : (0.5 + 0.5 * t2);

    vec3 cu  = tr.p0 + u * (tr.a + u * tr.b);
    vec3 dcu = tr.a + 2.0 * u * tr.b;
    vec3 diff = cu - pos;
    float num = dot(diff, dcu);
    float den = dot(dcu, dcu) + dot(diff, 2.0 * tr.b);
    if (den > 1e-4) {
        u = clamp(u - num / den, 0.0, 1.0);
    }

    vec3 finalP = tr.p0 + u * (tr.a + u * tr.b);
    return vec2(length(pos - finalP), u);
}

// Fast cheap envelope check for empty-space skipping (no 3D fBM noise)
bool insideSmokeEnvelope(vec3 pos) {
    if (length(pos - BURST_ORIGIN) > 3.15) return false;
    if (length(pos - (BURST_ORIGIN + vec3(0.0, -0.05, 0.04))) < 0.72) return true;
    vec3 lp = pos - vec3(-0.04, -0.78, -0.08);
    lp.x *= 1.45; lp.z *= 1.35;
    if (length(lp) < 0.85) return true;

    for (int i = 0; i < NUM_TRAILS; i++) {
        vec3 toC = pos - gTrails[i].bCenter;
        float br = gTrails[i].bRad;
        if (dot(toC, toC) > br * br) continue;
        vec2 du = closestOnTrail(gTrails[i], pos);
        float maxRad = gTrails[i].thick * 3.4 + 0.16;
        if (du.x < maxRad) return true;
    }
    return false;
}

// ----------------------------------------------------------------------------
// Volumetric Smoke Density Field: vec2(density, tipHeatFactor)
// ----------------------------------------------------------------------------
vec2 sampleSmokeField(vec3 pos, bool highDetail) {
    if (length(pos - BURST_ORIGIN) > 3.15) return vec2(0.0);

    vec3 warp = curlWarp(pos * 1.95);

    float maxField = 0.0;
    float heatFactor = 0.0;
    float bestAgeWeight = 0.5;

    for (int i = 0; i < NUM_TRAILS; i++) {
        vec3 toCenter = pos - gTrails[i].bCenter;
        float br = gTrails[i].bRad;
        if (dot(toCenter, toCenter) > br * br) continue;

        vec2 duRaw = closestOnTrail(gTrails[i], pos);
        if (duRaw.x > gTrails[i].thick * 3.6 + 0.18) continue;

        float smokeAge = 1.0 - duRaw.y;

        // Sinuous curl displacement grows strongly with smoke age
        float curlAmp = gTrails[i].thick * (0.45 + 2.35 * pow(smokeAge, 0.82));
        vec3 q = pos + warp * curlAmp;

        vec2 du = closestOnTrail(gTrails[i], q);
        float d = du.x;
        float u = du.y;
        float ageU = 1.0 - u;

        // High-frequency beaded cauliflower puff modulation along trail arc
        float puffBeads = 1.0 + 0.38 * sin(u * 24.0 + gTrails[i].phase)
                              + 0.22 * cos(u * 41.0 - gTrails[i].phase * 1.7);

        // Narrow tight wake behind tip -> billowing puffy mid-plume -> wispy inner base
        float radiusProfile = mix(0.32, 1.85, smoothstep(0.0, 0.78, ageU)) * puffBeads;
        radiusProfile *= smoothstep(1.02, 0.93, u) * 0.80 + 0.20;

        float rad = gTrails[i].thick * radiusProfile;

        float tube = 1.0 - smoothstep(rad * 0.15, rad * 1.32, d);
        if (tube > 0.001) {
            float densityScale = mix(1.30, 0.78, ageU * ageU);
            float contrib = tube * densityScale;
            if (contrib > maxField) {
                maxField = contrib;
                bestAgeWeight = ageU;
            }
            float tipProx = smoothstep(0.76, 0.995, u) * tube * gTrails[i].heat;
            heatFactor = max(heatFactor, tipProx);
        }
    }

    // Central detonation smoke cloud + lower descending silver-blue plume
    vec3 qCore = pos + warp * 0.24;
    float dCore = length(qCore - (BURST_ORIGIN + vec3(0.0, -0.04, 0.04)));
    float coreCloud = (1.0 - smoothstep(0.10, 0.62, dCore)) * 0.98;

    vec3 lowerPos = qCore - vec3(-0.04, -0.78, -0.08);
    lowerPos.x *= 1.55;
    lowerPos.z *= 1.45;
    float dLower = length(lowerPos);
    float lowerPlume = (1.0 - smoothstep(0.10, 0.78, dLower)) * 0.64;

    float baseField = max(maxField, max(coreCloud, lowerPlume));
    if (baseField <= 0.002) {
        return vec2(0.0, heatFactor);
    }

    // High-frequency billowing cauliflower carving
    vec3 noiseCoord = pos * 4.65 + warp * 0.85 - vec3(0.0, gTime * 0.24, 0.0);
    float n = highDetail ? billowFbm5(noiseCoord) : billowFbm3(noiseCoord);

    // Crisp cauliflower edge carving (breaks smooth tube into distinct puffy lobes)
    float erode = mix(0.28, 0.48, bestAgeWeight);
    float dens = smoothstep(erode, erode + 0.28, baseField + (n - 0.47) * 0.98);

    // Sculptural internal puff density contrast
    dens *= (0.45 + 1.15 * n);
    dens = mix(dens, dens * 1.30, heatFactor * 0.45);

    return vec2(dens, heatFactor);
}

float sampleSmokeShadow(vec3 pos) {
    return sampleSmokeField(pos, false).x;
}

// ----------------------------------------------------------------------------
// Analytical 3D Tumbling Dark Shell / Shrapnel Fragments (16 OBBs)
// ----------------------------------------------------------------------------
mat3 rotEuler(vec3 a) {
    vec3 s = sin(a), c = cos(a);
    mat3 rx = mat3(1.0, 0.0, 0.0, 0.0, c.x, -s.x, 0.0, s.x, c.x);
    mat3 ry = mat3(c.y, 0.0, s.y, 0.0, 1.0, 0.0, -s.y, 0.0, c.y);
    mat3 rz = mat3(c.z, -s.z, 0.0, s.z, c.z, 0.0, 0.0, 0.0, 1.0);
    return rz * ry * rx;
}

// Returns vec2(tHit, -1) and outputs normal in local space
float intersectBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outN) {
    vec3 m = 1.0 / (rd + sign(rd) * 1e-6);
    vec3 n = m * ro;
    vec3 k = abs(m) * boxSize;
    vec3 t1 = -n - k;
    vec3 t2 = -n + k;
    float tN = max(max(t1.x, t1.y), t1.z);
    float tF = min(min(t2.x, t2.y), t2.z);
    if (tN > tF || tF < 0.0) return -1.0;
    outN = -sign(rd) * step(t1.yzx, t1.xyz) * step(t1.zxy, t1.xyz);
    return tN > 0.0 ? tN : tF;
}

// Intersect all 16 tumbling shrapnel pieces around the airburst
bool traceShrapnel(vec3 ro, vec3 rd, vec3 keyDir, out float bestT, out vec3 bestCol) {
    bestT = 1e5;
    bestCol = vec3(0.0);
    bool hitAny = false;

    for (int i = 0; i < NUM_SHRAP; i++) {
        float fi = float(i);
        vec3 rnd1 = hash33(vec3(fi * 3.17, fi * 7.41, 1.23)) * 2.0 - 1.0;
        vec3 rnd2 = hash33(vec3(fi * 5.83, 2.71, fi * 9.13));

        // Scatter radially around the burst; a few larger fragments closer to camera as in 10.png
        vec3 dir = normalize(rnd1 + vec3(0.001));
        float dist = mix(0.65, 2.25, rnd2.x) * (0.72 + 0.30 * gAge);
        vec3 pos = BURST_ORIGIN + dir * dist + 0.25 * GRAVITY * gAge * gAge;

        // Place 2 dramatic foreground fragments near lower-left and mid-right edges
        if (i == 0) pos = vec3(-0.92, -1.05, 1.85);
        if (i == 1) pos = vec3( 1.25, -0.78, 1.55);

        // Fragment half-extents: angular flat-chipped casing shards
        float scale = (i < 2) ? 0.065 : mix(0.018, 0.042, rnd2.y);
        vec3 halfExt = scale * vec3(1.15, 0.65, 0.28 + 0.25 * rnd2.z);

        // Quick bounding sphere cull
        vec3 oc = ro - pos;
        float b = dot(oc, rd);
        float rBound = length(halfExt) * 1.15;
        float c = dot(oc, oc) - rBound * rBound;
        if (b * b - c < 0.0) continue;

        // Tumble rotation
        vec3 ang = rnd2 * TAU + gTime * (rnd1 * 3.2 + vec3(0.8));
        mat3 rot = rotEuler(ang);
        mat3 invRot = transpose(rot);

        vec3 lro = invRot * (ro - pos);
        vec3 lrd = invRot * rd;

        vec3 localN;
        float tHit = intersectBox(lro, lrd, halfExt, localN);
        if (tHit > 0.08 && tHit < bestT) {
            bestT = tHit;
            hitAny = true;

            vec3 worldN = normalize(rot * localN);
            vec3 hitP = ro + rd * tHit;
            vec3 toBurst = normalize(BURST_ORIGIN - hitP);
            float burstDiff = max(dot(worldN, toBurst), 0.0);
            float keyDiff = max(dot(worldN, keyDir), 0.0);
            float fres = pow(1.0 - max(dot(worldN, -rd), 0.0), 3.0);

            if (uLightMode == 1) {
                float g = 0.015 + 0.08 * keyDiff + 0.04 * fres;
                bestCol = vec3(g);
            } else {
                // Dark charred matte-metal shrapnel with warm burst rim glint
                vec3 baseSteel = vec3(0.004, 0.0045, 0.006);
                vec3 rimWarm = vec3(0.28, 0.11, 0.025) * pow(burstDiff, 2.0) * (0.35 + 0.65 * fres);
                vec3 skyRim  = vec3(0.018, 0.032, 0.055) * (keyDiff * 0.5 + fres * 0.6);
                bestCol = baseSteel + rimWarm + skyRim;
            }
        }
    }

    return hitAny;
}

// ----------------------------------------------------------------------------
// Henyey-Greenstein Phase Function
// ----------------------------------------------------------------------------
float hgPhase(float cosTheta, float g) {
    float g2 = g * g;
    float denom = 1.0 + g2 - 2.0 * g * cosTheta;
    return (1.0 - g2) / (4.0 * PI * pow(max(denom, 1e-3), 1.5));
}

// ----------------------------------------------------------------------------
// Deep Midnight-Navy / Slate Night Sky & Dark Storm Clouds
// ----------------------------------------------------------------------------
vec3 evalNightSky(vec3 rd, vec2 uv) {
    if (uLightMode == 1) {
        // Dark neutral charcoal-grey studio backdrop (linear 0.010..0.018 -> sRGB ~32..42/255)
        float grad = 0.011 + 0.008 * smoothstep(-0.85, 0.85, rd.y) - 0.003 * dot(uv, uv);
        return vec3(clamp(grad, 0.008, 0.022));
    }

    // Calibrated in linear space for sRGB gamma so darkest corner lands at lumMin ~8-14/255
    float rightBias = smoothstep(-0.65, 0.90, rd.x * 0.85 - rd.y * 0.32);
    vec3 skyDark  = vec3(0.0006, 0.0012, 0.0028); // deep midnight navy (~11/255 sRGB)
    vec3 skySteel = vec3(0.0042, 0.0130, 0.0335); // moody slate-indigo right sky (~42/255 sRGB)
    vec3 skyBase  = mix(skyDark, skySteel, rightBias);

    if (uLightMode == 2) {
        skyBase *= 0.80;
    } else if (uLightMode == 3) {
        skyBase = mix(skyBase, vec3(0.003, 0.007, 0.016), 0.35);
    }

    // Subtle dark storm clouds
    vec2 skyUV = rd.xy / (abs(rd.z) + 0.45);
    vec2 p = skyUV * 2.3 + vec2(gTime * 0.012, -gTime * 0.006);
    float c1 = 0.0;
    float amp = 0.52;
    for (int i = 0; i < 4; i++) {
        c1 += amp * noise2D(p);
        p = mat2(0.8, -0.6, 0.6, 0.8) * p * 2.08 + vec2(7.3, 11.1);
        amp *= 0.5;
    }
    float cloudMask = smoothstep(0.38, 0.78, c1);

    vec3 cloudDark = vec3(0.0005, 0.0009, 0.0022);
    vec3 cloudLit  = vec3(0.0085, 0.0220, 0.0520);
    vec3 cloudCol  = mix(cloudDark, cloudLit, rightBias * 0.85 + 0.15);

    // Subtle localized burst illumination on clouds directly behind the detonation
    vec3 toBurst = normalize(BURST_ORIGIN - vec3(0.0, 0.0, 3.6));
    float burstGlow = exp(-length(rd.xy - toBurst.xy) * 2.8);
    cloudCol += vec3(0.014, 0.024, 0.044) * burstGlow * (0.4 + 0.6 * cloudMask);

    vec3 col = mix(skyBase, cloudCol, cloudMask * 0.70);

    if (uLightMode == 3) {
        float backRim = exp(-length(rd.xy) * 3.2);
        col += vec3(0.022, 0.036, 0.060) * backRim;
    }

    return col;
}

// ----------------------------------------------------------------------------
// Closest distance from ray (ro, rd) to 3D line segment (a, b)
// Returns vec2(distSq, segParam)
// ----------------------------------------------------------------------------
vec2 raySegmentDistSq(vec3 ro, vec3 rd, vec3 a, vec3 b, out float tRay) {
    vec3 u = rd;
    vec3 v = b - a;
    vec3 w = ro - a;
    float aDot = dot(u, u);
    float bDot = dot(u, v);
    float cDot = dot(v, v);
    float dDot = dot(u, w);
    float eDot = dot(v, w);
    float denom = aDot * cDot - bDot * bDot;

    float sc, tc;
    if (denom < 1e-6) {
        sc = 0.0;
        tc = clamp(eDot / max(cDot, 1e-6), 0.0, 1.0);
    } else {
        sc = max((bDot * eDot - cDot * dDot) / denom, 0.0);
        tc = clamp((aDot * eDot - bDot * dDot) / denom, 0.0, 1.0);
        // Re-project sc onto ray for clamped tc
        sc = max(dot(a + v * tc - ro, rd), 0.0);
    }
    tRay = sc;
    vec3 diff = (ro + rd * sc) - (a + v * tc);
    return vec2(dot(diff, diff), tc);
}

// ----------------------------------------------------------------------------
// Tight Incandescent Phosphorus Tips & Fine Trailing Spark Streaks
// ----------------------------------------------------------------------------
vec3 evalTipsAndSparks(vec3 ro, vec3 rd, float maxT, float smokeTransAtCenter) {
    if (uLightMode == 1) {
        return vec3(0.0);
    }

    vec3 accum = vec3(0.0);
    float lightScale = (uLightMode == 2) ? 0.88 : ((uLightMode == 3) ? 0.78 : 1.0);

    vec3 camRight = normalize(cross(rd, vec3(0.0, 1.0, 0.0)));
    vec3 camUp    = cross(camRight, rd);

    for (int i = 0; i < NUM_TRAILS; i++) {
        vec3 tip = gTrails[i].p2;
        vec3 toTip = tip - ro;
        float tProj = dot(toTip, rd);
        if (tProj <= 0.1 || tProj > maxT) continue;

        vec3 offset = (ro + rd * tProj) - tip;
        float d2 = dot(offset, offset);
        float d  = sqrt(d2);

        float heat = gTrails[i].heat * lightScale;
        float vis  = mix(0.55, 1.0, smokeTransAtCenter);

        // Tight exponential burning phosphorus head (ZERO global orange washout!)
        if (d < 0.55) {
            float headSize = (i >= 10) ? 0.016 : 0.028;
            float core      = exp(-d2 / (headSize * headSize * 0.55));
            float innerGlow = exp(-d / (headSize * 2.1));
            float tightHalo = exp(-d * 10.5) * 0.18;

            // Tight subtle 6-point optical diffraction glint (confined near tip)
            vec2 p2d = vec2(dot(offset, camRight), dot(offset, camUp));
            float ang = atan(p2d.y, p2d.x);
            float spikes = pow(abs(cos(ang * 3.0 + float(i) * 0.4)), 24.0) * exp(-d * 26.0) * 0.45;
            float streak = exp(-abs(p2d.y) * 95.0) * exp(-abs(p2d.x) * 22.0) * 0.25;

            vec3 tipCol =
                vec3(3.40, 2.95, 2.10) * core +
                vec3(2.10, 0.78, 0.12) * innerGlow * 0.85 +
                vec3(1.10, 0.36, 0.05) * tightHalo +
                vec3(1.85, 1.05, 0.42) * (spikes + streak);

            accum += tipCol * heat * vis;
        }

        // Fine directional trailing spark streaks peeling off behind each burning tip
        int numSparks = (i >= 10) ? 5 : 7;
        for (int s = 0; s < 7; s++) {
            if (s >= numSparks) break;
            float sf = float(s);
            float seed = float(i) * 19.3 + sf * 6.7;
            float drift = fract(hash11(seed) + gTime * (0.48 + 0.32 * hash11(seed + 1.1)));
            float uSpark = clamp(0.992 - drift * 0.26, 0.68, 0.995);

            vec3 spBase = gTrails[i].p0 + uSpark * (gTrails[i].a + uSpark * gTrails[i].b);
            vec3 spVel  = normalize(gTrails[i].a + 2.0 * uSpark * gTrails[i].b + vec3(1e-4));

            vec3 jitter = (hash33(vec3(seed, float(i), 3.1)) - 0.5) * 2.0;
            vec3 spHead = spBase + jitter * (0.025 + 0.11 * drift) + vec3(0.0, -0.12 * drift * drift, 0.0);
            vec3 spTail = spHead - normalize(spVel - vec3(0.0, 0.35 * drift, 0.0)) * (0.035 + 0.03 * (1.0 - drift));

            // Quick reject before exact ray-segment distance
            vec3 toSp = spHead - ro;
            float tQuick = dot(toSp, rd);
            if (tQuick <= 0.1 || tQuick > maxT) continue;
            vec3 qOff = (ro + rd * tQuick) - spHead;
            if (dot(qOff, qOff) > 0.015) continue;

            float tRay;
            vec2 seg = raySegmentDistSq(ro, rd, spHead, spTail, tRay);
            float sd2 = seg.x;
            float taper = 1.0 - seg.y * 0.7;

            float sparkLife = (1.0 - drift) * smoothstep(0.0, 0.06, drift);
            float sparkCore = exp(-sd2 / (0.000038 * taper * taper));
            float sparkHalo = exp(-sd2 / 0.00035) * 0.22;
            vec3 sparkCol = mix(vec3(1.9, 0.48, 0.05), vec3(2.8, 2.0, 0.95), sparkLife * (1.0 - seg.y));
            accum += sparkCol * (sparkCore + sparkHalo) * sparkLife * heat * vis * 0.95;
        }
    }

    return accum;
}

// ----------------------------------------------------------------------------
// ACES Filmic Tonemapping & Ray-Sphere Intersection
// ----------------------------------------------------------------------------
vec3 acesFilm(vec3 x) {
    const float a = 2.51;
    const float b = 0.03;
    const float c = 2.43;
    const float d = 0.59;
    const float e = 0.14;
    return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}

vec2 intersectSphere(vec3 ro, vec3 rd, vec3 center, float rad) {
    vec3 oc = ro - center;
    float b = dot(oc, rd);
    float c = dot(oc, oc) - rad * rad;
    float h = b * b - c;
    if (h < 0.0) return vec2(-1.0);
    h = sqrt(h);
    return vec2(-b - h, -b + h);
}

// ----------------------------------------------------------------------------
// Main Volumetric Rendering
// ----------------------------------------------------------------------------
void main() {
    initTrails();

    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / min(uResolution.x, uResolution.y);

    // Camera setup
    vec3 ro, ta;
    float focal;
    if (uCamMode == 1) {
        // ~2.5x macro close-up framed on glowing upper-right/foreground tips & crisp curling smoke
        float sway = sin(gTime * 0.35) * 0.05;
        ro    = vec3(0.46 + sway, 0.44, 2.12);
        ta    = vec3(0.50, 0.52, 0.18);
        focal = 2.42;
    } else {
        // Hero full starburst framing matching 10.png
        float sway = sin(gTime * 0.28) * 0.07;
        ro    = vec3(sway, 0.08, 3.65);
        ta    = vec3(0.02, 0.08, 0.0);
        focal = 1.82;
    }

    vec3 cw = normalize(ta - ro);
    vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
    vec3 cv = cross(cu, cw);
    vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);

    // Directional key light configuration per uLightMode
    vec3 keyDir;
    vec3 keyCol;
    vec3 ambTop;
    vec3 ambBot;
    if (uLightMode == 1) {
        // Crisp sculptural neutral grayscale studio lighting (strong key vs deep shadow)
        keyDir = normalize(vec3(0.68, 0.72, 0.35));
        keyCol = vec3(1.55);
        ambTop = vec3(0.085);
        ambBot = vec3(0.016);
    } else if (uLightMode == 2) {
        // Grazing raking key light from left/side for deep sculptural relief
        keyDir = normalize(vec3(-0.92, 0.28, 0.24));
        keyCol = vec3(1.15, 1.24, 1.42);
        ambTop = vec3(0.032, 0.056, 0.105);
        ambBot = vec3(0.008, 0.015, 0.032);
    } else if (uLightMode == 3) {
        // Backlit rim light from behind the airburst
        keyDir = normalize(vec3(0.12, 0.22, -0.96));
        keyCol = vec3(1.45, 1.65, 2.00);
        ambTop = vec3(0.035, 0.062, 0.118);
        ambBot = vec3(0.010, 0.018, 0.038);
    } else {
        // Hero photoreal night sky: cool silver-blue shadowed undersides vs warm-white lit tops
        keyDir = normalize(vec3(0.56, 0.70, 0.44));
        keyCol = vec3(0.76, 0.86, 1.04);
        ambTop = vec3(0.065, 0.115, 0.215);
        ambBot = vec3(0.014, 0.028, 0.058);
    }

    vec3 bgCol = evalNightSky(rd, uv);

    // Analytical intersection with 16 dark tumbling shrapnel fragments
    float tShrap;
    vec3 shrapCol;
    bool hitShrap = traceShrapnel(ro, rd, keyDir, tShrap, shrapCol);

    float cosKey = dot(rd, keyDir);
    float phaseKey = mix(hgPhase(cosKey, 0.32), hgPhase(cosKey, 0.74), (uLightMode == 3) ? 0.85 : 0.22);

    vec2 hit = intersectSphere(ro, rd, BURST_ORIGIN, 3.10);

    vec3  accumLight    = vec3(0.0);
    float transmittance = 1.0;
    float midTrans      = 1.0;

    if (hit.y > 0.0) {
        float tNear = max(hit.x, 0.05);
        float tFar  = min(hit.y, hitShrap ? tShrap : hit.y);
        float tMid  = 0.5 * (tNear + hit.y);

        float dtFine = (uCamMode == 1) ? 0.020 : 0.026;
        float dtSkip = 0.085;
        float jitter = ign(gl_FragCoord.xy + fract(gTime * 7.13) * 64.0);
        float t = tNear + jitter * dtFine;

        float sigmaT = (uLightMode == 1) ? 7.4 : 6.8;

        for (int s = 0; s < 128; s++) {
            if (t >= tFar || transmittance < 0.012) break;

            vec3 pos = ro + rd * t;

            // Fast empty-space skip when outside all trail envelopes
            if (!insideSmokeEnvelope(pos)) {
                t += dtSkip;
                continue;
            }

            vec2 field = sampleSmokeField(pos, true);
            float dens = field.x;

            if (dens > 0.004) {
                // 1. 3-tap Beer-Lambert directional self-shadowing for crisp cauliflower relief
                float sh1 = sampleSmokeShadow(pos + keyDir * 0.06);
                float sh2 = sampleSmokeShadow(pos + keyDir * 0.16);
                float sh3 = sampleSmokeShadow(pos + keyDir * 0.34);
                float shadowOpt = (sh1 * 0.45 + sh2 * 0.35 + sh3 * 0.20) * 5.2;
                float keyShadow = exp(-shadowOpt);

                // Multi-scattering approximation keeps bright puffs creamy while shadows stay deep
                float powder = 1.0 - 0.55 * exp(-dens * 4.5);
                float multiScatt = mix(keyShadow, exp(-shadowOpt * 0.28), 0.24);

                // Local upward ambient occlusion so undersides of curls drop into cool blue-gray shadow
                float upOcc = exp(-sampleSmokeShadow(pos + vec3(0.0, 0.14, 0.0)) * 2.4);
                float upMask = clamp((pos.y - BURST_ORIGIN.y) * 0.45 + 0.55, 0.0, 1.0);
                vec3 ambLight = mix(ambBot, ambTop, upMask * upOcc);

                // 2. Direct key illumination
                vec3 directLight = keyCol * multiScatt * (0.48 + 1.95 * phaseKey) * powder;

                // 3. Localized warm inverse-square illumination from burning tips & detonation core
                vec3 warmLight = vec3(0.0);
                if (uLightMode != 1) {
                    // Inner detonation warm-cream scatter
                    vec3 toCore = BURST_ORIGIN - pos;
                    float dCore2 = dot(toCore, toCore);
                    if (dCore2 < 1.2) {
                        float coreAtten = exp(-dCore2 * 3.8) / (0.16 + dCore2 * 3.2);
                        float coreShadow = exp(-sampleSmokeShadow(pos + normalize(toCore + vec3(1e-4)) * 0.10) * 2.4);
                        warmLight += vec3(1.15, 0.88, 0.62) * coreAtten * coreShadow * 0.68;
                    }

                    // Localized warm orange-gold illumination from nearby burning phosphorus tips
                    for (int i = 0; i < NUM_TRAILS; i++) {
                        vec3 toTip = gTrails[i].p2 - pos;
                        float d2 = dot(toTip, toTip);
                        if (d2 > 0.72) continue;
                        float atten = exp(-d2 * 4.2) / (0.015 + d2 * 10.5);
                        vec3 tipHue = mix(vec3(1.0, 0.38, 0.06), vec3(1.0, 0.86, 0.52), exp(-d2 * 24.0));
                        warmLight += tipHue * atten * gTrails[i].heat * 0.46;
                    }

                    // Fiery hot wake glow immediately behind the burning tip
                    float wakeHeat = field.y;
                    warmLight += vec3(1.45, 0.55, 0.09) * wakeHeat * wakeHeat * 1.35;

                    if (uLightMode == 2) warmLight *= 0.70;
                    if (uLightMode == 3) warmLight *= 0.60;
                }

                // Smoke albedo: cool blue-gray in deep shadow, warm-white on lit crests
                vec3 albedo = (uLightMode == 1)
                    ? vec3(0.86)
                    : mix(vec3(0.58, 0.68, 0.82), vec3(0.96, 0.94, 0.91), clamp(multiScatt * 1.15 + field.y, 0.0, 1.0));

                vec3 S = albedo * (ambLight + directLight + warmLight) * (dens * sigmaT);

                float stepTrans = exp(-dens * sigmaT * dtFine);
                vec3 integ = (S - S * stepTrans) / max(dens * sigmaT, 1e-4);
                accumLight += transmittance * integ;
                transmittance *= stepTrans;
            }

            if (t < tMid) {
                midTrans = transmittance;
            }
            t += dtFine;
        }
    }

    // Composite shrapnel or night sky behind the volumetric smoke
    vec3 backdrop = hitShrap ? shrapCol : bgCol;
    vec3 col = backdrop * transmittance + accumLight;

    // Add tight incandescent phosphorus tips & fine trailing spark streaks
    float tipMaxT = hitShrap ? tShrap : 1e5;
    col += evalTipsAndSparks(ro, rd, tipMaxT, midTrans);

    // Gentle vignette
    float vig = 1.0 - 0.14 * dot(uv, uv);
    col *= clamp(vig, 0.80, 1.0);

    // ACES filmic tonemapping calibrated for %clip < 1%
    float exposure = (uLightMode == 1) ? 1.18 : 1.02;
    col = acesFilm(col * exposure);

    // Soft top-end rolloff so incandescent cores read blazing white-gold without clipping >1%
    col = col / (1.0 + 0.018 * col);

    if (uLightMode == 1) {
        float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(lum);
    } else {
        // Keep darkest midnight-navy corners cleanly above lumMin >= 2 (~8/255 sRGB)
        col = max(col, vec3(0.0005, 0.0008, 0.0016));
    }

    // Linear to sRGB gamma encoding
    col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));

    // Subtle 8-bit dither to prevent banding in the dark night sky
    float dither = (ign(gl_FragCoord.xy) - 0.5) / 255.0;
    col = clamp(col + vec3(dither), 0.0, 1.0);

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierphosphorus-airburst-tendrils
CategoryGame VFX Shaders · Physically-Based VFX
Upstream studyvfx_realistic/lib/10_phosphorus_airburst_tendrils.glsl (commit afde0bc)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size925 lines · 34.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT explosions sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation; Henyey–Greenstein scattering phase function (1941)

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Jorge Jimenez, Interleaved Gradient Noise

All game vfx shaders