Thermite Molten Spark Shower shader

thermite-molten-spark-shower.glsl · 1066 lines · GLSL ES 3.00 · MIT

Thermite Molten Spark ShowerGame VFX
Thermite Molten Spark Shower WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 1066 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
thermite-molten-spark-shower.glsl (1066 lines)
#version 300 es
// Thermite Molten Spark Shower (thermite-molten-spark-shower.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_realistic/lib/06_thermite_molten_spark_shower.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;

// ============================================================================
// 06_thermite_molten_spark_shower.glsl
// Photorealistic foundry thermite pour & molten spark shower study.
//
// Scene elements:
//   - Dark pitted cast-iron foundry crucible tilted at top-center-right,
//     suspended by forged chains attached cleanly outside the rim, horizontal
//     trunnion handle, and textured radiant orange-gold molten inner walls
//   - Turbulent white-hot & fiery orange-gold molten thermite stream pouring
//     down onto a clean, rotated, beveled dark brushed steel plate with four
//     recessed corner bolts on a gritty concrete industrial workbench
//   - Incandescent molten impact pool with splash crown, heated steel rim,
//     brushed-metal warm reflections, and fine surface aluminum-oxide ash
//   - Volumetric Beer-Lambert billowing white-grey aluminum-oxide smoke
//     (56 steps) with 5-octave domain-warped FBM turbulence, strong self-
//     shadowing, warm under-lighting from the impact pool, and cool-grey tops
//   - Crisp, thin, curved parabolic ballistic spark arcs (5-segment curved
//     exposure trails) + fast streaks + fine ember flecks bursting from impact
//   - ACES filmic tonemapping with calibrated dark foundry contrast
//     (lumMin >= 2, %black < 2%, %clip < 1%)
//
// Uniforms:
//   uLightMode: 0=hero dark foundry, 1=pure neutral grayscale study (R==G==B),
//               2=grazing raking key light, 3=backlit rim light
//   uCamMode:   0=hero framing, 1=~2.5x macro close-up of impact pool & sparks
// ============================================================================

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

const float MAT_BENCH        = 1.0;
const float MAT_STEEL        = 2.0;
const float MAT_CRUCIBLE_OUT = 3.0;
const float MAT_CRUCIBLE_IN  = 4.0;
const float MAT_MOLTEN       = 5.0;
const float MAT_BOLT         = 6.0;

// Key scene landmarks
const vec3 P_CRUC   = vec3(0.60, 1.70, 0.10);
const vec3 P_SPOUT  = vec3(0.02, 1.53, 0.02);
const vec3 P_IMPACT = vec3(-0.04, 0.02, 0.02);

// ----------------------------------------------------------------------------
// Hash & Multi-Octave Procedural Noise
// ----------------------------------------------------------------------------

float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

vec3 hash31(float p) {
    vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.xxy + p3.yzz) * p3.zyx);
}

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

float noise3(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    f = 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, f.x);
    float nx10 = mix(n010, n110, f.x);
    float nx01 = mix(n001, n101, f.x);
    float nx11 = mix(n011, n111, f.x);
    float nxy0 = mix(nx00, nx10, f.y);
    float nxy1 = mix(nx01, nx11, f.y);
    return mix(nxy0, nxy1, f.z);
}

float fbm3(vec3 p) {
    float v = 0.0;
    float a = 0.5;
    mat3 rot = mat3(
         0.80,  0.60,  0.00,
        -0.60,  0.80,  0.00,
         0.00,  0.00,  1.00
    );
    for (int i = 0; i < 5; i++) {
        v += a * noise3(p);
        p = rot * p * 2.04 + vec3(17.1, 31.7, 11.3);
        a *= 0.5;
    }
    return v;
}

float fbm3Fast(vec3 p) {
    float v = 0.0;
    float a = 0.5;
    for (int i = 0; i < 3; i++) {
        v += a * noise3(p);
        p = p * 2.06 + vec3(13.4, 29.1, 7.7);
        a *= 0.5;
    }
    return v;
}

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

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 rot = mat2(0.8, 0.6, -0.6, 0.8);
    for (int i = 0; i < 4; i++) {
        v += a * noise2(p);
        p = rot * p * 2.04 + vec2(11.3, 27.9);
        a *= 0.5;
    }
    return v;
}

// ----------------------------------------------------------------------------
// Math & SDF Primitives
// ----------------------------------------------------------------------------

mat2 rot2(float a) {
    float c = cos(a), s = sin(a);
    return mat2(c, -s, s, c);
}

float smin(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

float sdRoundBox(vec3 p, vec3 b, float r) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}

float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
    vec3 pa = p - a, ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h) - r;
}

float sdCappedCylinder(vec3 p, float h, float r) {
    vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float sdTorus(vec3 p, vec2 t) {
    vec2 q = vec2(length(p.xz) - t.x, p.y);
    return length(q) - t.y;
}

float sdHexPrism(vec3 p, vec2 h) {
    const vec3 k = vec3(-0.8660254, 0.5, 0.57735);
    p = abs(p);
    p.xz -= 2.0 * min(dot(k.xy, p.xz), 0.0) * k.xy;
    vec2 d = vec2(
        length(p.xz - vec2(clamp(p.x, -k.z * h.x, k.z * h.x), h.x)) * sign(p.z - h.x),
        p.y - h.y
    );
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

// Local coordinates inside the tilted crucible
vec3 toCrucibleSpace(vec3 p) {
    vec3 q = p - P_CRUC;
    float tiltAnim = 0.012 * sin(uTime * TAU * 0.333);
    q.yz = rot2(0.50) * q.yz;
    q.xy = rot2(0.96 + tiltAnim) * q.xy;
    return q;
}

// Centerline of the pouring molten thermite stream for s in [0, 1]
vec3 evalStreamPos(float s) {
    vec3 p0 = P_SPOUT;
    vec3 p1 = vec3(0.00, 0.92, 0.06);
    vec3 p2 = P_IMPACT;
    float u = 1.0 - s;
    vec3 pos = u * u * p0 + 2.0 * u * s * p1 + s * s * p2;
    float wave = sin(s * 12.0 - uTime * 9.5) * 0.012 + sin(s * 25.0 - uTime * 16.0) * 0.005;
    float env = sin(s * PI);
    pos.x += wave * env;
    pos.z += cos(s * 14.0 - uTime * 11.0) * 0.008 * env;
    return pos;
}

vec2 distToStream(vec3 p) {
    float minD = 1e5;
    float bestS = 0.0;
    vec3 a = evalStreamPos(0.0);
    for (int i = 1; i <= 6; i++) {
        float s1 = float(i) / 6.0;
        vec3 b = evalStreamPos(s1);
        vec3 pa = p - a;
        vec3 ba = b - a;
        float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
        float d = length(pa - ba * h);
        if (d < minD) {
            minD = d;
            bestS = mix(float(i - 1) / 6.0, s1, h);
        }
        a = b;
    }
    return vec2(minD, bestS);
}

// ----------------------------------------------------------------------------
// Scene SDF
// ----------------------------------------------------------------------------

vec2 opU(vec2 a, vec2 b) {
    return (a.x < b.x) ? a : b;
}

vec2 mapScene(vec3 p) {
    vec2 res = vec2(1e5, 0.0);

    // 1. Gritty concrete foundry workbench (y = -0.10) with subtle ash relief
    float benchDisp = (fbm2(p.xz * 2.4) - 0.5) * 0.010 + (noise2(p.xz * 15.0) - 0.5) * 0.0025;
    float dBench = (p.y + 0.10) + benchDisp;
    res = vec2(dBench, MAT_BENCH);

    // 2. Clean rotated beveled dark steel plate with 4 recessed corner bolt holes
    vec3 sp = p - vec3(0.0, -0.052, 0.0);
    sp.xz = rot2(-0.62) * sp.xz;
    float plateHalf = 0.88;
    float dPlate = sdRoundBox(sp, vec3(plateHalf, 0.042, plateHalf), 0.011);

    // Subtle burn-through crater right under the molten impact pool
    float rImpact = length(p.xz - P_IMPACT.xz);
    float crater = smoothstep(0.24, 0.04, rImpact) * 0.020;
    dPlate += crater * smoothstep(-0.02, 0.05, sp.y);

    // 4 recessed corner bolt holes with chamfered rims
    vec3 bsp = sp;
    bsp.xz = abs(bsp.xz) - vec2(plateHalf - 0.13);
    float dHole = sdCappedCylinder(bsp - vec3(0.0, 0.015, 0.0), 0.08, 0.046);
    float dChamfer = length(vec2(length(bsp.xz) - 0.046, bsp.y - 0.052)) - 0.010;
    dPlate = max(dPlate, -min(dHole, dChamfer));
    res = opU(res, vec2(dPlate, MAT_STEEL));

    // Recessed hex bolts sitting cleanly inside each corner hole
    float dBoltHead = sdHexPrism(bsp - vec3(0.0, 0.014, 0.0), vec2(0.030, 0.014));
    float dBoltWash = sdCappedCylinder(bsp - vec3(0.0, 0.001, 0.0), 0.007, 0.040);
    res = opU(res, vec2(min(dBoltHead, dBoltWash), MAT_BOLT));

    // 3. Tilted Cast-Iron Foundry Crucible (bounding sphere accelerated)
    float dCrucBound = length(p - P_CRUC) - 1.45;
    if (dCrucBound < res.x) {
        vec3 cp = toCrucibleSpace(p);
        float hNorm = clamp((cp.y + 0.44) / 0.88, 0.0, 1.0);
        float rOuter = mix(0.35, 0.49, smoothstep(0.0, 1.0, hNorm));
        // Thickened cast-iron lip collar around rim
        rOuter += 0.042 * exp(-pow((cp.y - 0.39) * 11.0, 2.0));
        // Pouring spout lobe along -X near rim
        float spoutAngle = atan(cp.z, -cp.x);
        float spoutLobe = exp(-spoutAngle * spoutAngle * 6.5) * smoothstep(0.16, 0.42, cp.y);
        rOuter += 0.078 * spoutLobe;

        // Rough cast-iron pitting displacement
        float pit = (fbm3Fast(cp * 10.5) - 0.5) * 0.018;

        float dOuter = max(length(cp.xz) - (rOuter + pit), abs(cp.y) - 0.44);
        float dBaseRound = length(cp - vec3(0.0, -0.18, 0.0)) - 0.43;
        dOuter = smin(dOuter, dBaseRound, 0.08);

        // Hollow interior cavity
        float rInner = rOuter - 0.080 - 0.014 * spoutLobe;
        float dCavity = max(length(cp.xz) - rInner, -(cp.y + 0.24));
        // Pouring notch carved into spout rim
        float dSpoutNotch = sdCapsule(cp, vec3(-0.32, 0.38, 0.0), vec3(-0.62, 0.44, 0.0), 0.095);
        dCavity = min(dCavity, dSpoutNotch);

        float dCrucWall = max(dOuter, -dCavity);

        // Clean inner vs outer material split (rim bevel stays cast-iron exterior)
        float matCruc = (length(cp.xz) < rInner + 0.016 && cp.y > -0.25 && cp.y < 0.39)
                      ? MAT_CRUCIBLE_IN : MAT_CRUCIBLE_OUT;
        res = opU(res, vec2(dCrucWall, matCruc));

        // Heavy trunnion band & horizontal steel handle extending right (+X)
        float dBand = sdTorus(cp - vec3(0.0, 0.02, 0.0), vec2(0.46, 0.036));
        float dHandle = sdCapsule(p, P_CRUC + vec3(0.32, 0.02, 0.00), P_CRUC + vec3(1.65, 0.14, 0.04), 0.064);
        float dCollar = sdCapsule(p, P_CRUC + vec3(0.34, 0.02, 0.00), P_CRUC + vec3(0.50, 0.04, 0.01), 0.094);
        float dTrunnion = min(dBand, min(dHandle, dCollar));

        // Forged iron chains attached cleanly OUTSIDE the left and right rim lugs
        // Left lug sits outside the upper-left outer rim (never entering the mouth)
        vec3 lugL = P_CRUC + vec3(-0.68, 0.36, -0.10);
        vec3 lugR = P_CRUC + vec3( 0.54, 0.14,  0.02);
        float dLugL = sdCapsule(p, P_CRUC + vec3(-0.54, 0.30, -0.06), lugL, 0.032);
        float dLugR = sdCapsule(p, P_CRUC + vec3( 0.44, 0.10,  0.02), lugR, 0.034);

        vec3 cL = p - lugL;
        float dChainL = 1e5;
        if (cL.y > 0.0 && length(cL.xz) < 0.16) {
            vec3 q = cL;
            float idx = floor(q.y / 0.092);
            q.y = mod(q.y, 0.092) - 0.046;
            if (mod(idx, 2.0) > 0.5) q.xz = q.zx;
            dChainL = sdTorus(q.xzy, vec2(0.036, 0.013));
        }

        vec3 cR = p - lugR;
        float dChainR = 1e5;
        if (cR.y > 0.0 && length(cR.xz) < 0.16) {
            vec3 q = cR;
            float idx = floor(q.y / 0.092);
            q.y = mod(q.y, 0.092) - 0.046;
            if (mod(idx, 2.0) > 0.5) q.xz = q.zx;
            dChainR = sdTorus(q.xzy, vec2(0.036, 0.013));
        }

        float dHardware = min(dTrunnion, min(min(dLugL, dLugR), min(dChainL, dChainR)));
        res = opU(res, vec2(dHardware, MAT_CRUCIBLE_OUT));

        // Molten pool sloshing inside the lower crucible cavity & lip
        float poolSurf = p.y - (P_SPOUT.y + 0.04) + 0.008 * sin(p.x * 24.0 + uTime * 6.0) * cos(p.z * 22.0 - uTime * 5.0);
        float dCrucPool = max(poolSurf, length(cp.xz) - (rInner + 0.01));
        dCrucPool = max(dCrucPool, -(cp.y + 0.22));
        res = opU(res, vec2(dCrucPool, MAT_MOLTEN));
    }

    // 4. Blazing Molten Thermite Pouring Stream & Incandescent Impact Pool
    vec2 st = distToStream(p);
    float sParam = st.y;
    float baseRad = mix(0.068, 0.036, smoothstep(0.0, 0.65, sParam))
                  + 0.020 * smoothstep(0.78, 1.0, sParam);
    // Turbulent downward-rushing liquid ripples
    float flowPhase = p.y * 6.5 + uTime * 9.5;
    float turb = (fbm3Fast(vec3(p.x * 14.0, flowPhase, p.z * 14.0)) - 0.5) * 0.032;
    float dStream = st.x - (baseRad + turb);

    // Incandescent molten impact pool + dynamic splash crown on the steel plate
    vec3 ip = p - P_IMPACT;
    float rPool = length(ip.xz);
    float angPool = atan(ip.z, ip.x);
    float poolRipple = 0.014 * sin(rPool * 36.0 - uTime * 11.5)
                     + 0.012 * sin(angPool * 6.0 + uTime * 6.0) * smoothstep(0.04, 0.22, rPool);
    float poolRad = 0.20 + 0.025 * sin(angPool * 3.0 - uTime * 3.2);
    float dPool = length(vec3(ip.x, (ip.y + 0.005) * 3.8, ip.z)) - (poolRad + poolRipple);

    float splashRing = length(vec2(rPool - 0.12, ip.y - 0.032 - 0.022 * sin(angPool * 7.0 + uTime * 9.5))) - 0.028;
    float dMolten = smin(dStream, smin(dPool, splashRing, 0.055), 0.075);
    res = opU(res, vec2(dMolten, MAT_MOLTEN));

    return res;
}

// Unit-sign tetrahedral normal
vec3 calcNormal(vec3 p) {
    const float eps = 0.0018;
    const vec2 k = vec2(1.0, -1.0);
    return normalize(
        k.xyy * mapScene(p + k.xyy * eps).x +
        k.yyx * mapScene(p + k.yyx * eps).x +
        k.yxy * mapScene(p + k.yxy * eps).x +
        k.xxx * mapScene(p + k.xxx * eps).x
    );
}

float calcSoftShadow(vec3 ro, vec3 rd, float mint, float maxt, float k) {
    float res = 1.0;
    float t = mint;
    for (int i = 0; i < 28; i++) {
        float h = mapScene(ro + rd * t).x;
        if (h < 0.0015) return 0.0;
        res = min(res, k * h / t);
        t += clamp(h, 0.02, 0.22);
        if (t > maxt) break;
    }
    return clamp(res, 0.0, 1.0);
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 4; i++) {
        float h = 0.025 + 0.075 * float(i);
        float d = mapScene(p + n * h).x;
        occ += (h - d) * sca;
        sca *= 0.65;
    }
    return clamp(1.0 - 2.2 * occ, 0.15, 1.0);
}

// ----------------------------------------------------------------------------
// Volumetric Beer-Lambert Aluminum-Oxide Smoke (Turbulent 5-Octave Billows)
// ----------------------------------------------------------------------------

float sampleSmokeDensity(vec3 p) {
    if (p.y < -0.06 || p.y > 2.15 || p.x < -1.45 || p.x > 1.75 || p.z < -1.10 || p.z > 1.20) {
        return 0.0;
    }

    float openBoost = 1.0 + 0.15 * clamp(uOpen, 0.0, 1.0);

    // Domain warp coordinates before evaluating plume shape so no sphere silhouettes ever show
    vec3 adv = vec3(-0.22, -0.58, -0.06) * uTime;
    vec3 wp = p + 0.28 * (vec3(
        fbm3Fast(p * 1.65 + adv),
        fbm3Fast(p * 1.65 + adv + vec3(5.2, 1.3, 8.7)),
        fbm3Fast(p * 1.65 + adv + vec3(9.1, 6.4, 2.3))
    ) - 0.5);

    // Organic buoyant plume drifting right under the crucible + curling left at base
    vec3 pRight1 = (wp - vec3(0.45, 0.68, -0.02)) / vec3(0.78, 0.54, 0.64);
    vec3 pRight2 = (wp - vec3(0.95, 1.08, -0.12)) / vec3(0.68, 0.48, 0.58);
    vec3 pLeft   = (wp - vec3(-0.42, 0.34, 0.14)) / vec3(0.56, 0.34, 0.46);
    vec3 pBase   = (wp - vec3(0.08, 0.26, 0.06))  / vec3(0.52, 0.32, 0.44);
    vec3 pUpper  = (wp - vec3(0.14, 1.06, 0.02))  / vec3(0.44, 0.40, 0.40);

    float env = max(1.0 - dot(pRight1, pRight1), 1.0 - dot(pRight2, pRight2));
    env = max(env, max(1.0 - dot(pLeft, pLeft), 1.0 - dot(pBase, pBase)));
    env = max(env, 0.78 * (1.0 - dot(pUpper, pUpper)));
    if (env <= -0.18) return 0.0;

    // 5-octave multi-scale cauliflower billow turbulence + wispy shredded edges
    vec3 q = wp * 2.55 + adv * 1.35;
    float fBase = fbm3(q);
    // Ridged billow puffs (1 - |2n-1|) for crisp cauliflower structure
    float ridge = 1.0 - abs(2.0 * fbm3Fast(q * 2.15 + vec3(fBase * 1.6, -fBase * 1.1, fBase * 0.8)) - 1.0);
    float fineWisp = fbm3Fast(q * 4.4 + vec3(0.0, -uTime * 1.1, 0.0));

    float turb = fBase * 0.52 + ridge * 0.30 + fineWisp * 0.18;
    float d = smoothstep(0.36, 0.84, env * 0.72 + (turb - 0.36) * 1.15);

    // Wispy alpha feathering near plate and around the immediate impact/stream core
    d *= smoothstep(-0.02, 0.09, p.y);
    vec2 st = distToStream(p);
    d *= smoothstep(0.05, 0.17, st.x);

    // Keep the immediate macro splash core unobstructed so impact slag reads sharply
    float rImp = length(p - (P_IMPACT + vec3(0.0, 0.05, 0.0)));
    d *= smoothstep(0.16, 0.34, rImp);

    return d * 1.95 * openBoost;
}

float hgPhase(float cosTheta, float g) {
    float g2 = g * g;
    return (1.0 - g2) / (4.0 * PI * pow(max(1.0 + g2 - 2.0 * g * cosTheta, 0.01), 1.5));
}

// Volumetric Beer-Lambert raymarch (56 steps)
vec4 marchSmoke(vec3 ro, vec3 rd, float tMax, float jitter, vec3 keyDir, vec3 keyCol, float glowScale) {
    vec3 bMin = vec3(-1.45, -0.06, -1.10);
    vec3 bMax = vec3( 1.75,  2.15,  1.20);
    vec3 invRd = 1.0 / (rd + vec3(1e-6));
    vec3 t0s = (bMin - ro) * invRd;
    vec3 t1s = (bMax - ro) * invRd;
    vec3 tsm = min(t0s, t1s);
    vec3 tsM = max(t0s, t1s);
    float tNear = max(max(tsm.x, tsm.y), max(tsm.z, 0.08));
    float tFar  = min(min(tsM.x, tsM.y), min(tsM.z, tMax));

    if (tNear >= tFar) return vec4(0.0, 0.0, 0.0, 1.0);

    const int STEPS = 56;
    float dt = (tFar - tNear) / float(STEPS);
    float t = tNear + dt * jitter;

    vec3 accum = vec3(0.0);
    float T = 1.0;

    // White-grey aluminum-oxide smoke albedo
    vec3 smokeAlbedo = (uLightMode == 1) ? vec3(0.76) : vec3(0.84, 0.86, 0.90);
    vec3 coolTopCol = (uLightMode == 1) ? vec3(0.24)
                    : (uLightMode == 3) ? vec3(0.18, 0.21, 0.26)
                    : vec3(0.13, 0.16, 0.20);
    vec3 shadowCol  = (uLightMode == 1) ? vec3(0.06) : vec3(0.026, 0.032, 0.044);

    for (int i = 0; i < STEPS; i++) {
        if (T < 0.015) break;
        vec3 p = ro + rd * t;
        float dens = sampleSmokeDensity(p);

        if (dens > 0.004) {
            // 1. Inverse-square warm under-lighting from impact pool with strong self-shadowing
            vec3 toImp = (P_IMPACT + vec3(0.0, 0.06, 0.0)) - p;
            float distImp2 = dot(toImp, toImp);
            float distImp = sqrt(distImp2);
            vec3 lImpDir = toImp / max(distImp, 1e-4);

            float odImp = sampleSmokeDensity(p + lImpDir * min(0.14, distImp * 0.35)) * 0.28
                        + sampleSmokeDensity(p + lImpDir * min(0.32, distImp * 0.70)) * 0.42;
            float attenImp = exp(-odImp * 2.6) / (0.12 + distImp2 * 4.2);
            float phaseImp = mix(0.32, hgPhase(dot(rd, lImpDir), 0.45) * 2.6, 0.55);

            // 2. Localized molten stream glow (tight falloff so upper-right smoke stays cool white-grey)
            vec2 st = distToStream(p);
            float attenStream = exp(-st.x * 2.6) / (0.14 + st.x * st.x * 7.5);
            vec3 streamCol = vec3(1.0, 0.48, 0.12) * attenStream * 0.95 * glowScale;

            vec3 warmUnder = vec3(1.0, 0.52, 0.15) * attenImp * phaseImp * 3.2 * glowScale
                           + streamCol;

            // 3. Directional key + cool industrial top-lighting with multi-step self-shadowing
            float odKey = sampleSmokeDensity(p + keyDir * 0.18) * 0.38
                        + sampleSmokeDensity(p + keyDir * 0.42) * 0.52;
            float transKey = exp(-odKey * 2.4);
            float phaseKey = mix(0.45, hgPhase(dot(rd, keyDir), 0.42) * 2.8, 0.45);
            vec3 dirLight = keyCol * transKey * phaseKey;

            // Vertical self-shadowing for cool white-grey tops & deep slate-grey crevices
            float odUp = sampleSmokeDensity(p + vec3(0.0, 0.22, 0.0)) * 0.55;
            float topVis = exp(-odUp * 2.2) * smoothstep(-0.02, 1.15, p.y);
            vec3 coolAmbient = mix(shadowCol, coolTopCol, topVis);

            // Powder multi-scattering highlights on thin wispy edges
            float powder = 1.0 - exp(-dens * 2.2);
            vec3 totalLight = (warmUnder + dirLight * (0.45 + 0.55 * powder) + coolAmbient) * smokeAlbedo;

            float sigmaT = 1.95;
            float stepT = exp(-sigmaT * dens * dt);
            vec3 stepScat = totalLight * (1.0 - stepT) / sigmaT * 1.45;
            accum += T * stepScat;
            T *= stepT;
        }
        t += dt;
    }

    return vec4(accum, T);
}

// ----------------------------------------------------------------------------
// Crisp Analytic Ray-Tested Parabolic Ballistic Spark Arcs & Ember Flecks
// ----------------------------------------------------------------------------

vec3 raySegmentDist(vec3 ro, vec3 rd, vec3 a, vec3 b) {
    vec3 u = rd;
    vec3 v = b - a;
    vec3 w = ro - a;
    float bDot = dot(u, v);
    float cDot = dot(v, v);
    float dDot = dot(u, w);
    float eDot = dot(v, w);
    float denom = cDot - bDot * bDot;

    float tN = (denom < 1e-6) ? eDot : (eDot - bDot * dDot);
    float s = clamp(tN / max(denom < 1e-6 ? cDot : denom, 1e-6), 0.0, 1.0);

    vec3 segPt = a + v * s;
    float rayT = max(dot(segPt - ro, rd), 0.0);
    vec3 rayPt = ro + rd * rayT;
    return vec3(length(rayPt - segPt), s, rayT);
}

vec3 evalSparkPos(vec3 pStart, vec3 v0, float tau) {
    const float G = 4.5;
    const float drag = 0.48;
    float e = (1.0 - exp(-drag * tau)) / drag;
    vec3 p = pStart + v0 * e;
    p.y -= 0.5 * G * tau * tau;

    // Elastic ricochet off steel plate / workbench
    if (p.y < 0.008) {
        float over = 0.008 - p.y;
        p.y = 0.008 + over * 0.40;
    }
    return p;
}

vec3 renderSparks(vec3 ro, vec3 rd, float sceneT, float glowScale) {
    if (glowScale < 0.05) return vec3(0.0);

    vec3 sparkAccum = vec3(0.0);
    float openSpread = 1.0 + 0.25 * clamp(uOpen, 0.0, 1.0);
    float radScale = (uCamMode == 1) ? 0.65 : 1.0;

    // Tier 1: 10 long, graceful, curved parabolic hero spark arcs (5 curved segments each)
    for (int i = 0; i < 10; i++) {
        float fi = float(i);
        vec3 h1 = hash31(fi * 17.31 + 3.7);
        vec3 h2 = hash31(fi * 43.17 + 19.1);

        float life = mix(0.88, 1.32, h1.x);
        float age = mod(uTime * 0.90 + h1.y * life, life);

        float azim, elev, speed;
        if (i < 7) {
            // Iconic sweeping left-side parabolic golden arcs matching 06.png
            azim = mix(2.55, 3.55, h2.x);
            elev = mix(0.56, 1.12, h2.y);
            speed = mix(2.1, 3.3, h2.z) * openSpread;
        } else {
            // Sweeping foreground & right-side arcs
            azim = mix(-0.75, 1.05, h2.x);
            elev = mix(0.42, 0.96, h2.y);
            speed = mix(1.8, 2.8, h2.z) * openSpread;
        }

        vec3 v0 = vec3(cos(azim) * cos(elev), sin(elev), sin(azim) * cos(elev)) * speed;
        vec3 pStart = P_IMPACT + vec3((h1.z - 0.5) * 0.10, 0.03, (h2.z - 0.5) * 0.10);

        float dtShutter = 0.22;
        float t0 = max(age - dtShutter, 0.0);
        float t1 = age;
        if (t1 - t0 < 0.012) continue;

        float lifeFade = smoothstep(0.0, 0.05, age) * smoothstep(life, life * 0.58, age);
        float coreRad = 0.0019 * radScale;
        float haloRad = 0.0068 * radScale;

        for (int sIdx = 0; sIdx < 5; sIdx++) {
            float f0 = float(sIdx) / 5.0;
            float f1 = float(sIdx + 1) / 5.0;
            vec3 a = evalSparkPos(pStart, v0, mix(t0, t1, f0));
            vec3 b = evalSparkPos(pStart, v0, mix(t0, t1, f1));

            vec3 rs = raySegmentDist(ro, rd, a, b);
            if (rs.z > sceneT + 0.025) continue;

            if (rs.x < haloRad * 2.8) {
                float segS = mix(f0, f1, rs.y);
                float core = exp(-(rs.x * rs.x) / (coreRad * coreRad));
                float halo = exp(-(rs.x * rs.x) / (haloRad * haloRad)) * 0.24;
                float trailRamp = pow(segS, 1.2) * 0.85 + 0.15;
                vec3 sCol = mix(vec3(3.4, 0.72, 0.06), vec3(6.2, 4.1, 1.55), pow(segS, 1.7));
                sparkAccum += sCol * (core + halo) * trailRamp * lifeFade * glowScale;
            }
        }
    }

    // Tier 2: 28 fast short crisp streaks + fine ember flecks bursting from impact splash
    for (int i = 0; i < 28; i++) {
        float fi = float(i) + 20.0;
        vec3 h1 = hash31(fi * 23.71 + 7.1);
        vec3 h2 = hash31(fi * 59.13 + 31.4);

        bool isFleck = (i >= 16);
        float life = isFleck ? mix(0.35, 0.75, h1.x) : mix(0.45, 0.85, h1.x);
        float age = mod(uTime * 1.10 + h1.y * life, life);

        // Bias spray heavily left/up with some right/foreground bursts
        float azim = (h1.z < 0.62) ? mix(2.15, 3.85, h2.x) : (h2.x * TAU);
        float elev = mix(0.20, 1.28, h2.y);
        float speed = (isFleck ? mix(0.85, 2.35, h2.z) : mix(1.5, 3.5, h2.z)) * openSpread;

        vec3 v0 = vec3(cos(azim) * cos(elev), sin(elev), sin(azim) * cos(elev)) * speed;
        vec3 pStart = P_IMPACT + vec3((h1.z - 0.5) * 0.14, 0.03, (h2.z - 0.5) * 0.14);

        float dtShutter = isFleck ? 0.018 : 0.052;
        float t0 = max(age - dtShutter, 0.0);
        float t1 = age;
        if (t1 - t0 < 0.004) continue;

        vec3 a = evalSparkPos(pStart, v0, t0);
        vec3 b = evalSparkPos(pStart, v0, t1);
        vec3 rs = raySegmentDist(ro, rd, a, b);
        if (rs.z > sceneT + 0.025) continue;

        float coreRad = (isFleck ? 0.0015 : 0.0018) * radScale;
        float haloRad = (isFleck ? 0.0048 : 0.0060) * radScale;

        if (rs.x < haloRad * 2.8) {
            float lifeFade = smoothstep(0.0, 0.04, age) * smoothstep(life, life * 0.55, age);
            float core = exp(-(rs.x * rs.x) / (coreRad * coreRad));
            float halo = exp(-(rs.x * rs.x) / (haloRad * haloRad)) * 0.22;
            float trailRamp = pow(rs.y, 1.3) * 0.8 + 0.2;
            vec3 sCol = mix(vec3(3.2, 0.68, 0.05), vec3(5.8, 3.6, 1.25), pow(rs.y, 1.6));
            sparkAccum += sCol * (core + halo) * trailRamp * lifeFade * glowScale;
        }
    }

    return sparkAccum;
}

// ----------------------------------------------------------------------------
// Dark Industrial Foundry Background
// ----------------------------------------------------------------------------

vec3 evalBackground(vec3 rd) {
    if (uLightMode == 1) {
        float grad = smoothstep(-0.3, 0.7, rd.y);
        return vec3(mix(0.13, 0.22, grad));
    }

    // Crisp dark cool-charcoal industrial workshop (dark left, cool slate-grey haze right)
    float gradY = smoothstep(-0.35, 0.70, rd.y);
    float rightHaze = smoothstep(-0.15, 0.85, rd.x) * smoothstep(-0.25, 0.55, rd.y);

    vec3 baseBg = mix(vec3(0.0045, 0.0055, 0.0080), vec3(0.009, 0.013, 0.020), gradY);
    baseBg += vec3(0.048, 0.062, 0.082) * rightHaze * (0.55 + 0.45 * fbm2(rd.xy * 4.5 + vec2(0.0, -uTime * 0.06)));

    // Out-of-focus dark industrial steel columns & crossbeams
    float beamX = smoothstep(0.065, 0.015, abs(fract(rd.x * 2.5 + 0.35) - 0.5));
    float beamY = smoothstep(0.050, 0.015, abs(fract(rd.y * 3.0 + 0.20) - 0.5));
    float girders = clamp(beamX * 0.7 + beamY * 0.5, 0.0, 1.0) * smoothstep(-0.1, 0.4, rd.y);
    baseBg *= (1.0 - 0.50 * girders);

    if (uLightMode == 3) {
        float backGlow = pow(max(dot(rd, normalize(vec3(-0.22, 0.38, -0.90))), 0.0), 6.0);
        baseBg += vec3(0.28, 0.32, 0.38) * backGlow;
    }

    return baseBg;
}

// ----------------------------------------------------------------------------
// PBR Shading (GGX Specular + Molten Bounce + Detailed Incandescence)
// ----------------------------------------------------------------------------

float D_GGX(float ndh, float rough) {
    float a = rough * rough;
    float a2 = a * a;
    float d = ndh * ndh * (a2 - 1.0) + 1.0;
    return a2 / (PI * d * d + 1e-6);
}

float G_SchlickGGX(float ndv, float rough) {
    float r = rough + 1.0;
    float k = (r * r) / 8.0;
    return ndv / (ndv * (1.0 - k) + k + 1e-6);
}

vec3 fresnelSchlick(float cosTheta, vec3 F0) {
    return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}

vec3 evalBRDF(vec3 n, vec3 v, vec3 l, vec3 albedo, float rough, float metallic) {
    vec3 h = normalize(v + l);
    float ndl = max(dot(n, l), 0.0);
    float ndv = max(dot(n, v), 0.001);
    float ndh = max(dot(n, h), 0.0);
    float vdh = max(dot(v, h), 0.0);

    vec3 F0 = mix(vec3(0.04), albedo, metallic);
    float D = D_GGX(ndh, rough);
    float G = G_SchlickGGX(ndv, rough) * G_SchlickGGX(ndl, rough);
    vec3 F = fresnelSchlick(vdh, F0);

    vec3 spec = (D * G * F) / (4.0 * ndv * ndl + 0.001);
    vec3 kD = (1.0 - F) * (1.0 - metallic);
    return (kD * albedo / PI + spec) * ndl;
}

// Physical blackbody incandescent ramp (saturated fiery orange -> blazing gold -> white-hot)
vec3 thermiteColor(float temp) {
    temp = clamp(temp, 0.0, 1.0);
    vec3 c0 = vec3(0.45, 0.04, 0.005); // dark red-orange slag
    vec3 c1 = vec3(2.20, 0.40, 0.025); // deep fiery molten orange
    vec3 c2 = vec3(4.60, 1.95, 0.22);  // incandescent orange-gold
    vec3 c3 = vec3(7.40, 6.20, 3.60);  // white-hot thermite core
    if (temp < 0.35) return mix(c0, c1, temp / 0.35);
    if (temp < 0.72) return mix(c1, c2, (temp - 0.35) / 0.37);
    return mix(c2, c3, (temp - 0.72) / 0.28);
}

vec3 shadeSurface(vec3 p, vec3 n, vec3 v, float mat, vec3 keyDir, vec3 keyCol, float glowScale) {
    vec3 albedo = vec3(0.15);
    float rough = 0.65;
    float metallic = 0.0;
    vec3 emissive = vec3(0.0);

    if (mat == MAT_BENCH) {
        // Gritty dark industrial concrete workbench with scattered white-grey oxide ash dust
        float g1 = fbm2(p.xz * 3.8);
        float g2 = fbm2(p.xz * 22.0);
        albedo = mix(vec3(0.042, 0.042, 0.045), vec3(0.125, 0.122, 0.120), g1 * 0.65 + g2 * 0.35);
        rough = mix(0.86, 0.58, g2);
        metallic = 0.04;

        // Subtle powdery white-grey ash scattered on the concrete near the front-left of the plate
        float ashPatch = smoothstep(0.55, 0.05, length(p.xz - vec2(-0.48, 0.72)))
                       * smoothstep(0.32, 0.74, fbm2(p.xz * 8.5));
        albedo = mix(albedo, vec3(0.42, 0.44, 0.47), ashPatch * 0.75);
        rough = mix(rough, 0.94, ashPatch);
    } else if (mat == MAT_STEEL) {
        // Brushed dark oxidized steel plate with scorched heat halo & scattered slag/ash specks
        vec3 sp = p - vec3(0.0, -0.052, 0.0);
        sp.xz = rot2(-0.62) * sp.xz;
        float brush = fbm2(vec2(sp.x * 52.0, sp.z * 7.0));
        float millScale = fbm2(sp.xz * 4.6);
        albedo = mix(vec3(0.085, 0.095, 0.11), vec3(0.22, 0.235, 0.255), brush * 0.55 + millScale * 0.45);
        rough = mix(0.22, 0.46, millScale * 0.65 + brush * 0.35);
        metallic = 0.84;

        float rImp = length(p.xz - P_IMPACT.xz);
        // Darkened heat-scorched ring around the molten burn-through
        float scorch = smoothstep(0.52, 0.20, rImp);
        albedo = mix(albedo, vec3(0.035, 0.038, 0.052), scorch * 0.60);

        // Fine scattered white-grey aluminum-oxide ash/slag dust on the plate surface
        float ashDust = smoothstep(0.54, 0.82, fbm2(sp.xz * 10.5)) * smoothstep(0.20, 0.60, rImp);
        float ashSpecks = smoothstep(0.76, 0.88, noise2(sp.xz * 48.0)) * smoothstep(0.18, 0.72, rImp);
        float ashAmt = clamp(ashDust * 0.55 + ashSpecks * 0.65, 0.0, 0.85);
        albedo = mix(albedo, vec3(0.48, 0.50, 0.53), ashAmt);
        rough = mix(rough, 0.90, ashAmt);
        metallic = mix(metallic, 0.10, ashAmt);

        // Glowing cherry-orange heated steel rim bordering the molten impact pool
        float hotRim = smoothstep(0.30, 0.17, rImp) * smoothstep(-0.015, 0.0, p.y);
        float slagNoise = fbm2(p.xz * 26.0);
        emissive += thermiteColor(hotRim * (0.45 + 0.35 * slagNoise)) * hotRim * 0.78 * glowScale;
    } else if (mat == MAT_BOLT) {
        albedo = vec3(0.13, 0.14, 0.15);
        rough = 0.34;
        metallic = 0.86;
    } else if (mat == MAT_CRUCIBLE_OUT) {
        // Rough pitted dark cast-iron / graphite crucible exterior
        float pit = fbm3(p * 11.5);
        float fine = noise3(p * 38.0);
        albedo = mix(vec3(0.050, 0.048, 0.048), vec3(0.145, 0.140, 0.138), pit * 0.65 + fine * 0.35);
        rough = mix(0.42, 0.82, pit);
        metallic = 0.58;

        // Subtle glowing orange slag veins near the pouring lip
        vec3 cp = toCrucibleSpace(p);
        float nearLip = smoothstep(0.24, 0.43, cp.y);
        float vein = smoothstep(0.76, 0.90, fbm3Fast(cp * 15.0));
        emissive += vec3(1.9, 0.42, 0.04) * vein * nearLip * 0.45 * glowScale;
    } else if (mat == MAT_CRUCIBLE_IN) {
        // Textured radiant orange-gold refractory interior wall (never flat pale-yellow)
        vec3 cp = toCrucibleSpace(p);
        float depthHeat = smoothstep(0.42, -0.18, cp.y);
        float wallTex = fbm3(cp * 9.5 + vec3(0.0, -uTime * 0.12, 0.0));
        // Glowing thermal crack network across inner refractory wall
        float crack = 1.0 - smoothstep(0.0, 0.14, abs(fbm3Fast(cp * 15.0) - 0.52));
        albedo = vec3(0.16, 0.10, 0.06);
        rough = 0.78;
        metallic = 0.08;

        // Keep temperature in rich fiery orange-to-gold range (0.28..0.76) with hot crack veins
        float temp = clamp(0.24 + 0.36 * depthHeat + 0.24 * (wallTex - 0.35) + 0.26 * crack, 0.08, 0.86);
        emissive = thermiteColor(temp) * (0.48 + 0.35 * depthHeat + 0.28 * crack) * glowScale;
    } else if (mat == MAT_MOLTEN) {
        // Turbulent fiery orange-gold edges around a blazing white-hot core
        float flow1 = fbm3(vec3(p.x * 13.0, p.y * 7.0 + uTime * 10.0, p.z * 13.0));
        float flow2 = fbm3Fast(vec3(p.x * 26.0, p.y * 14.0 + uTime * 16.0, p.z * 26.0));
        float ndv = max(dot(n, v), 0.0);

        // Strong Fresnel-like thermal falloff: white-hot core -> fiery orange-gold edges
        float coreMask = pow(ndv, 1.55);
        float temp = clamp(0.32 + 0.52 * coreMask + 0.26 * (flow1 - 0.46) + 0.12 * (flow2 - 0.5), 0.18, 1.0);

        // Extra white-hot splash energy right at the impact pool center
        float impBoost = smoothstep(0.26, 0.04, length(p - P_IMPACT));
        temp = clamp(temp + 0.18 * impBoost, 0.0, 1.0);

        albedo = (uLightMode == 1) ? vec3(0.74) : vec3(0.22, 0.12, 0.06);
        rough = 0.24;
        metallic = 0.12;
        emissive = thermiteColor(temp) * (0.85 + 0.35 * impBoost) * glowScale;
    }

    // Neutral grayscale study mode: mute emission so geometry & smoke read cleanly
    if (uLightMode == 1) {
        float aLum = dot(albedo, vec3(0.299, 0.587, 0.114));
        albedo = vec3(aLum * 1.15 + 0.06);
        emissive = vec3(dot(emissive, vec3(0.299, 0.587, 0.114))) * 0.06;
    }

    float ao = calcAO(p, n);
    vec3 col = vec3(0.0);

    // 1. Directional key light
    float shKey = calcSoftShadow(p + n * 0.006, keyDir, 0.03, 4.5, 10.0);
    col += evalBRDF(n, v, keyDir, albedo, rough, metallic) * keyCol * shKey;

    // 2. Inverse-square warm point light from the incandescent impact pool
    vec3 toImp = (P_IMPACT + vec3(0.0, 0.10, 0.0)) - p;
    float dImp2 = dot(toImp, toImp);
    float dImp = sqrt(dImp2);
    vec3 lImp = toImp / max(dImp, 1e-4);
    float attenImp = 1.0 / (0.14 + dImp2 * 2.6);
    float shImp = (mat == MAT_MOLTEN) ? 1.0 : calcSoftShadow(p + n * 0.008, lImp, 0.04, max(dImp - 0.15, 0.05), 8.0);
    vec3 impLightCol = vec3(1.0, 0.48, 0.12) * 5.2 * attenImp * shImp * glowScale;
    col += evalBRDF(n, v, lImp, albedo, rough, metallic) * impLightCol;

    // 3. Molten pouring stream mid-height warm bounce light
    vec3 pMidStream = vec3(0.01, 0.78, 0.05);
    vec3 toSt = pMidStream - p;
    float dSt2 = dot(toSt, toSt);
    vec3 lSt = toSt / max(sqrt(dSt2), 1e-4);
    float attenSt = 1.0 / (0.26 + dSt2 * 2.4);
    vec3 stLightCol = vec3(1.0, 0.52, 0.15) * 2.2 * attenSt * glowScale;
    col += evalBRDF(n, v, lSt, albedo, rough, metallic) * stLightCol;

    // 4. Cool industrial workshop ambient fill (keeps shadows rich without crushing blacks)
    vec3 skyFill = (uLightMode == 1) ? vec3(0.32)
                 : (uLightMode == 3) ? vec3(0.14, 0.17, 0.22)
                 : vec3(0.052, 0.068, 0.090);
    vec3 gndFill = (uLightMode == 1) ? vec3(0.12) : vec3(0.022, 0.018, 0.015);
    vec3 amb = mix(gndFill, skyFill, n.y * 0.5 + 0.5) * ao;
    col += albedo * amb * (1.0 - metallic * 0.65);

    // Cool workshop specular rim/reflection on metallic steel plate & crucible rim
    float fres = pow(1.0 - max(dot(n, v), 0.0), 4.0);
    col += mix(vec3(0.04), albedo, metallic) * skyFill * (0.35 + 0.75 * fres) * (1.0 - rough * 0.72) * ao;

    col += emissive;
    return col;
}

// ----------------------------------------------------------------------------
// ACES Filmic Tonemapping
// ----------------------------------------------------------------------------

vec3 acesFilmic(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);
}

// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------

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

    // Camera rig: uCamMode 0 = hero framing, 1 = ~2.5x macro close-up of impact pool & steel plate
    vec3 ro, ta;
    float focal;
    if (uCamMode == 1) {
        ro = vec3(0.34, 0.48, 1.32);
        ta = vec3(-0.02, 0.08, 0.02);
        focal = 2.05;
    } else {
        ro = vec3(-0.02, 1.44, 3.68);
        ta = vec3(0.04, 0.80, 0.0);
        focal = 1.62;
    }

    vec3 ww = normalize(ta - ro);
    vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
    vec3 vv = cross(uu, ww);
    vec3 rd = normalize(uv.x * uu + uv.y * vv + focal * ww);

    // Lighting rig by uLightMode
    vec3 keyDir;
    vec3 keyCol;
    float glowScale = 1.0;

    if (uLightMode == 1) {
        keyDir = normalize(vec3(0.45, 0.78, 0.42));
        keyCol = vec3(1.55);
        glowScale = 0.14;
    } else if (uLightMode == 2) {
        keyDir = normalize(vec3(-0.90, 0.22, 0.37));
        keyCol = vec3(1.85, 1.68, 1.48);
        glowScale = 0.72;
    } else if (uLightMode == 3) {
        keyDir = normalize(vec3(-0.22, 0.42, -0.88));
        keyCol = vec3(1.95, 2.05, 2.25);
        glowScale = 0.82;
    } else {
        keyDir = normalize(vec3(0.55, 0.72, 0.42));
        keyCol = vec3(0.38, 0.44, 0.54);
        glowScale = 1.0;
    }

    // Primary raymarch
    float t = 0.05;
    float maxT = 16.0;
    float hitMat = 0.0;
    for (int i = 0; i < 110; i++) {
        vec2 h = mapScene(ro + rd * t);
        if (h.x < 0.0014) {
            hitMat = h.y;
            break;
        }
        t += h.x * 0.85;
        if (t > maxT) break;
    }

    vec3 col;
    float sceneT = maxT;
    if (hitMat > 0.5) {
        sceneT = t;
        vec3 p = ro + rd * t;
        vec3 n = calcNormal(p);
        col = shadeSurface(p, n, -rd, hitMat, keyDir, keyCol, glowScale);

        // Subtle atmospheric perspective into dark workshop
        float fog = 1.0 - exp(-max(t - 3.4, 0.0) * 0.12);
        col = mix(col, evalBackground(rd), clamp(fog, 0.0, 0.85));
    } else {
        col = evalBackground(rd);
    }

    // Volumetric Beer-Lambert turbulent aluminum-oxide smoke (56 steps)
    float jitter = hash12(gl_FragCoord.xy + fract(uTime * 17.3) * 100.0);
    vec4 smoke = marchSmoke(ro, rd, sceneT, jitter, keyDir, keyCol, glowScale);
    col = col * smoke.a + smoke.rgb;

    // Analytic ray-tested curved parabolic ballistic spark arcs & fine ember flecks
    vec3 sparks = renderSparks(ro, rd, sceneT, (uLightMode == 1) ? 0.16 : glowScale);
    if (uLightMode == 1) {
        sparks = vec3(dot(sparks, vec3(0.299, 0.587, 0.114)));
    }
    col += sparks;

    // Tight localized optical bloom halo around impact pool (restrained in macro view)
    if (uLightMode != 1) {
        vec3 toImpRay = P_IMPACT + vec3(0.0, 0.05, 0.0) - ro;
        float tProj = max(dot(toImpRay, rd), 0.0);
        if (tProj < sceneT + 0.18) {
            float dImpRay = length(toImpRay - rd * tProj);
            float haloGain = (uCamMode == 1) ? 0.28 : 0.48;
            float impGlow = exp(-dImpRay * 6.5) * haloGain + exp(-dImpRay * 2.4) * (haloGain * 0.18);
            col += vec3(1.0, 0.48, 0.12) * impGlow * glowScale;
        }
    }

    // Calibrated exposure (macro view slightly tighter so slag & reflections read crisply)
    float exposure = (uLightMode == 1) ? 1.05 : ((uCamMode == 1) ? 0.68 : 0.86);
    col *= exposure;
    col = acesFilmic(col);
    // Gentle highlight roll-off above 0.955 to keep %clip < 1%
    col = min(col, vec3(0.955) + 0.030 * tanh((max(col - 0.955, 0.0)) * 20.0));

    // Gentle vignette keeping dark corners above lumMin >= 2
    float vig = 1.0 - 0.16 * dot(uv, uv);
    col *= clamp(vig, 0.78, 1.0);

    // Subtle ambient floor before sRGB so lumMin >= 2 and %black < 2%
    col = max(col, vec3(0.0016));

    // Linear -> sRGB
    col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));

    // Enforce strict R == G == B in neutral grayscale study mode
    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.299, 0.587, 0.114)));
    }

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierthermite-molten-spark-shower
CategoryGame VFX Shaders · Physically-Based VFX
Upstream studyvfx_realistic/lib/06_thermite_molten_spark_shower.glsl (commit afde0bc)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size1066 lines · 41.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT fire sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation; Henyey–Greenstein scattering phase function (1941); Planck blackbody radiation color approximation

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal

All game vfx shaders