Fuel-Air Fireball Explosion shader

fuel-air-fireball-explosion.glsl · 814 lines · GLSL ES 3.00 · MIT

Fuel-Air Fireball ExplosionGame VFX
Fuel-Air Fireball Explosion WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 814 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
fuel-air-fireball-explosion.glsl (814 lines)
#version 300 es
// Fuel-Air Fireball Explosion (fuel-air-fireball-explosion.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_realistic/lib/01_fuel_air_fireball_explosion.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;

// ============================================================================
// 01_fuel_air_fireball_explosion.glsl
// Photoreal Oppenheimer / Houdini film-VFX fuel-air mushroom fireball explosion
// on a dark industrial concrete warehouse stage.
//
// Features:
//   - Beer-Lambert volumetric raymarch (64 jittered steps + tight AABB + early
//     transmittance exit < 0.015 + fast macro-envelope skip)
//   - Sharp opaque cauliflower soot billows (5-octave billow/ridged fBM +
//     high-frequency domain-warped curl filaments for crisp hero & 2.5x macro)
//   - Multi-lobe asymmetric mushroom silhouette: blinding white-gold ground
//     ignition, narrow turbulent dark charcoal stem, towering dark-crowned
//     canopy with swirling golden-orange fire breakouts & dark soot crevices
//   - Planck blackbody ramp (4500K white-gold -> 2200K amber -> 1200K ember)
//   - Multi-tap directional self-shadowing + internal multi-center fire scatter
//   - Moody dark industrial concrete warehouse with overhead LED fixtures,
//     expansion-jointed concrete floor with golden-orange specular reflection,
//     and subtle dark blast debris & fine pinpoint base embers
//   - uLightMode: 0=hero dark warehouse, 1=sculptural grayscale clay/density
//     study (strict R==G==B), 2=grazing raking key light, 3=dramatic backlit rim
//   - uCamMode: 0=hero full framing, 1=~2.5x macro close-up of fire-and-soot folds
// ============================================================================

// --- Fast sin-free integer hashes & 3D/2D lattice noise ---
float hash13(vec3 p3) {
    uvec3 q = uvec3(ivec3(floor(p3))) * uvec3(1597334673u, 3812015801u, 2798796415u);
    uint n = (q.x ^ q.y ^ q.z) * 1597334673u;
    return float(n) * (1.0 / 4294967295.0);
}

float hash12(vec2 p2) {
    uvec2 q = uvec2(ivec2(floor(p2))) * uvec2(1597334673u, 3812015801u);
    uint n = (q.x ^ q.y) * 1597334673u;
    return float(n) * (1.0 / 4294967295.0);
}

float vnoise3(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);
    return mix(mix(nx00, nx10, u.y), mix(nx01, nx11, u.y), u.z);
}

float vnoise2(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash12(i + vec2(0.0, 0.0));
    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, 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
);

float fbm3Low(vec3 p) {
    float v = 0.0;
    float a = 0.55;
    for (int i = 0; i < 2; i++) {
        v += a * vnoise3(p);
        p = OCT_ROT * p * 2.06 + vec3(5.3, 11.1, -6.7);
        a *= 0.45;
    }
    return v;
}

// 5-octave cauliflower billow fBM for crisp convex smoke lobes & deep crevices
float billowFbm5(vec3 p) {
    float v = 0.0;
    float a = 0.50;
    for (int i = 0; i < 5; i++) {
        float n = vnoise3(p);
        float b = 1.0 - abs(2.0 * n - 1.0);
        v += a * (b * b);
        p = OCT_ROT * p * 2.11 + vec3(-8.4, 6.2, 14.1);
        a *= 0.49;
    }
    return v;
}

// High-frequency ridged filament fBM for swirling Houdini flame sheets
float ridgedFbm4(vec3 p) {
    float v = 0.0;
    float a = 0.52;
    float w = 1.0;
    for (int i = 0; i < 4; i++) {
        float n = 1.0 - abs(2.0 * vnoise3(p) - 1.0);
        n = n * n * w;
        w = clamp(n * 1.6, 0.25, 1.0);
        v += a * n;
        p = OCT_ROT * p * 2.14 + vec3(11.3, -7.8, 5.9);
        a *= 0.50;
    }
    return v;
}

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 * vnoise2(p);
        p = rot * p * 2.04 + vec2(17.3, -11.9);
        a *= 0.5;
    }
    return v;
}

float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}

float sdEllipsoid(vec3 p, vec3 c, vec3 r) {
    vec3 q = (p - c) / r;
    float k0 = length(q);
    float k1 = length(q / r);
    return k0 * (k0 - 1.0) / max(k1, 1e-4);
}

// --- Planck Blackbody emission ramp (temp in 0..1.6) ---
vec3 blackbody(float temp) {
    float t = clamp(temp, 0.0, 1.65);
    vec3 c0 = vec3(0.01,  0.002, 0.0005);  // 800K dark ember
    vec3 c1 = vec3(0.65,  0.065, 0.004);   // 1200K deep red-orange
    vec3 c2 = vec3(2.10,  0.46,  0.025);   // 1800K fiery orange
    vec3 c3 = vec3(3.80,  1.58,  0.20);    // 2400K golden amber
    vec3 c4 = vec3(5.60,  3.70,  1.25);    // 3400K hot yellow-white
    vec3 c5 = vec3(8.50,  7.60,  6.10);    // 4500K blinding white-gold

    vec3 col = mix(c0, c1, smoothstep(0.06, 0.24, t));
    col = mix(col, c2, smoothstep(0.24, 0.48, t));
    col = mix(col, c3, smoothstep(0.48, 0.76, t));
    col = mix(col, c4, smoothstep(0.76, 1.06, t));
    col = mix(col, c5, smoothstep(1.06, 1.48, t));
    return col * smoothstep(0.05, 0.16, t);
}

// --- Macro envelope SDF of the mushroom fireball ---
float mushroomMacroSDF(vec3 p, float t, float scale) {
    vec3 q = p / scale;
    q.y -= 0.03 * sin(t * 1.15);

    // 1. Ground ignition pool & licking flames (y = 0.0..0.70)
    float dBase  = sdEllipsoid(q, vec3( 0.00, 0.18, 0.04), vec3(0.60, 0.38, 0.54));
    float dBaseL = sdEllipsoid(q, vec3(-0.34, 0.44, 0.05), vec3(0.32, 0.34, 0.32));
    float dBaseR = sdEllipsoid(q, vec3( 0.28, 0.36, 0.06), vec3(0.30, 0.30, 0.30));
    dBase = smin(dBase, smin(dBaseL, dBaseR, 0.20), 0.22);

    // 2. Narrow turbulent rising stem (y = 0.40..1.52)
    float sy = clamp(q.y, 0.38, 1.56);
    vec2 stemCenter = vec2(0.04 * sin(sy * 2.6 - 0.5) - 0.03, 0.02 * cos(sy * 2.0));
    float stemRad = mix(0.34, 0.52, smoothstep(0.55, 1.52, sy))
                  + 0.14 * exp(-pow((sy - 1.02) * 4.2, 2.0)); // mid-stem dark billow collar
    float dStem = length(vec3(q.x - stemCenter.x, (q.y - sy) * 1.35, q.z - stemCenter.y)) - stemRad;

    // 3. Asymmetric towering mushroom cap (matches 01.png silhouette)
    float dCapMain  = sdEllipsoid(q, vec3( 0.04, 2.16, -0.02), vec3(0.98, 0.68, 0.88));
    float dCapLeft  = sdEllipsoid(q, vec3(-0.80, 1.98,  0.14), vec3(0.70, 0.58, 0.66));
    float dCapMidR  = sdEllipsoid(q, vec3( 0.42, 1.96,  0.24), vec3(0.60, 0.52, 0.56));
    float dCapFarR  = sdEllipsoid(q, vec3( 0.98, 1.82, -0.06), vec3(0.58, 0.54, 0.56));
    float dCapCrown = sdEllipsoid(q, vec3( 0.14, 2.60, -0.08), vec3(0.62, 0.44, 0.56));
    float dCapLowL  = sdEllipsoid(q, vec3(-0.56, 1.56,  0.04), vec3(0.46, 0.32, 0.44));

    float dCap = smin(dCapMain, dCapLeft, 0.28);
    dCap = smin(dCap, dCapMidR, 0.24);
    dCap = smin(dCap, dCapFarR, 0.26);
    dCap = smin(dCap, dCapCrown, 0.24);
    dCap = smin(dCap, dCapLowL, 0.22);

    float d = smin(dBase, dStem, 0.24);
    d = smin(d, dCap, 0.28);

    // Clean floor contact at y = 0
    d = max(d, -q.y - 0.01);
    return d * scale;
}

// --- Toroidal & buoyant advection ---
vec3 advectPos(vec3 p, float t) {
    float capMask = smoothstep(1.10, 1.80, p.y);
    float upSpeed = mix(0.75, 0.34, capMask);

    float rXZ = length(p.xz) + 1e-4;
    vec2 radDir = p.xz / rXZ;
    vec2 toro = vec2(rXZ - 0.74, p.y - 2.00);
    float rollAngle = t * 0.45 * capMask * exp(-dot(toro, toro) * 0.95);
    float cs = cos(rollAngle), sn = sin(rollAngle);
    vec2 toroRot = vec2(toro.x * cs - toro.y * sn, toro.x * sn + toro.y * cs);
    vec2 toroDelta = toroRot - toro;

    vec3 adv = p;
    adv.xz += radDir * toroDelta.x * 0.55;
    adv.y  += toroDelta.y * 0.55 - t * upSpeed;
    return adv;
}

// --- Volumetric density + temperature evaluation ---
// Returns vec3(density, temperature, surfaceBillow)
vec3 sampleVolume(vec3 p, float t, float scale, bool fastShadow) {
    float macroD = mushroomMacroSDF(p, t, scale);
    if (macroD > 0.36) return vec3(0.0);

    vec3 ap = advectPos(p, t);

    if (fastShadow) {
        float nLow = fbm3Low(ap * 1.75);
        float d = macroD + (0.48 - nLow) * 0.42;
        float dens = smoothstep(0.06, -0.14, d) * 2.2;
        return vec3(dens, 0.0, 0.0);
    }

    // Two-stage curled domain warp for crisp Houdini flame tendrils & cauliflower folds
    vec3 wp = ap * 1.55;
    vec3 warp1 = vec3(
        fbm3Low(wp + vec3(0.0, 0.0, 0.0)) - 0.5,
        fbm3Low(wp + vec3(5.2, 1.3, 2.8)) - 0.5,
        fbm3Low(wp + vec3(-3.1, 6.7, -4.2)) - 0.5
    );
    vec3 cp = ap + warp1 * 0.46;

    // Fine high-frequency curl offset for wispy macro-detail flame filaments
    vec3 cpFine = cp * 3.1 + vec3(
        vnoise3(cp * 4.8) - 0.5,
        vnoise3(cp * 4.8 + 7.1) - 0.5,
        vnoise3(cp * 4.8 - 4.3) - 0.5
    ) * 0.55;

    float billow = billowFbm5(cp * 2.05);
    float ridges = ridgedFbm4(cpFine - vec3(0.0, t * 0.65, 0.0));

    // Carve sharp cauliflower billows & deep crevices into the macro envelope
    float disp = (0.44 - billow) * 0.48 + (0.45 - ridges) * 0.16;
    float d = macroD + disp;

    // Crisp opaque boundary transition (eliminates blurry Gaussian look!)
    float dens = smoothstep(0.045, -0.055, d);
    if (dens < 0.002) return vec3(0.0);

    // Dense interior core with sculpted surface relief
    dens *= (1.6 + 2.4 * smoothstep(-0.01, -0.22, d));
    dens *= mix(0.65, 1.35, billow);

    // --- Temperature / Combustion Field ---
    vec3 q = p / scale;
    float temp = 0.0;

    // Filamentary flame modulation: creates sharp glowing ribbons & dark soot veins
    float filament = smoothstep(0.24, 0.76, ridges * 0.72 + (1.0 - billow) * 0.42);
    float hotVeins = smoothstep(0.45, 0.88, ridges);

    // 1. Blinding white-gold ground ignition pool (y = 0..0.48)
    float rGround = length(vec3(q.x * 1.25, (q.y - 0.14) * 1.65, (q.z - 0.06) * 1.30));
    float groundCore = exp(-rGround * rGround * 5.8) * 1.75;
    // Licking turbulent orange-gold flames rising around the ignition core
    float rFlame = length(vec3(q.x * 1.45, (q.y - 0.38) * 1.05, q.z * 1.40));
    float baseFlames = exp(-rFlame * rFlame * 2.6) * (0.35 + 1.15 * filament)
                     * smoothstep(0.95, 0.36, q.y);
    temp = max(groundCore, baseFlames);

    // 2. Left-upper mushroom cap incandescent fire breakout (iconic bright left lobe in 01.png)
    vec3 pLeftFire = q - vec3(-0.72, 2.02, 0.18);
    float dLeftFire = length(pLeftFire / vec3(0.64, 0.52, 0.54));
    float leftHotspot = exp(-dot((q - vec3(-0.56, 2.20, 0.24)) * vec3(2.5, 2.9, 2.4),
                                 (q - vec3(-0.56, 2.20, 0.24)) * vec3(2.5, 2.9, 2.4)));
    float leftFire = smoothstep(1.12, 0.10, dLeftFire) * (0.20 + 1.05 * filament + 0.35 * hotVeins)
                   + leftHotspot * (0.55 + 0.45 * filament);
    temp = max(temp, leftFire);

    // 3. Center-right curled golden-orange fire vortex peeking through soot
    vec3 pRightFire = q - vec3(0.36, 1.98, 0.28);
    float dRightFire = length(pRightFire / vec3(0.52, 0.45, 0.48));
    float rightFire = smoothstep(1.06, 0.12, dRightFire) * (0.18 + 0.96 * filament + 0.28 * hotVeins);
    temp = max(temp, rightFire);

    // 4. Glowing lower-center fire bridge connecting left & right lobes
    vec3 pBridge = q - vec3(-0.14, 1.84, 0.24);
    float dBridge = length(pBridge / vec3(0.48, 0.30, 0.38));
    float bridgeFire = smoothstep(1.00, 0.15, dBridge) * smoothstep(0.32, 0.80, ridges) * 0.88;
    temp = max(temp, bridgeFire);

    // 5. Right-side glowing ember crevice
    vec3 pEdgeEmber = q - vec3(0.84, 1.78, 0.14);
    float edgeEmber = exp(-dot(pEdgeEmber, pEdgeEmber) * 7.5) * smoothstep(0.38, 0.82, ridges) * 0.78;
    temp = max(temp, edgeEmber);

    // 6. Dense dark charcoal soot pockets & foreground soot tendrils occluding fire
    float sootCrevice = smoothstep(0.54, 0.78, billow) * smoothstep(0.52, 0.18, ridges);
    float stemCool    = smoothstep(0.52, 0.85, q.y) * smoothstep(1.56, 1.18, q.y);
    float crownCool   = smoothstep(2.26, 2.66, q.y);
    float rightCool   = smoothstep(0.64, 1.08, q.x) * smoothstep(1.38, 1.78, q.y);
    float coolFactor  = clamp(stemCool * 0.94 + crownCool * 0.92 + rightCool * 0.88 + sootCrevice * 0.72, 0.0, 0.97);

    // Protect blinding ground ignition core from soot cooling
    coolFactor *= smoothstep(0.34, 0.56, q.y);
    temp *= (1.0 - coolFactor);
    temp *= (1.0 + 0.10 * uOpen);

    return vec3(dens, temp, billow);
}

// --- Ray-AABB intersection ---
vec2 intersectBox(vec3 ro, vec3 rd, vec3 bMin, vec3 bMax) {
    vec3 invD = 1.0 / rd;
    vec3 t0 = (bMin - ro) * invD;
    vec3 t1 = (bMax - ro) * invD;
    vec3 tmin = min(t0, t1);
    vec3 tmax = max(t0, t1);
    float tNear = max(max(tmin.x, tmin.y), tmin.z);
    float tFar  = min(min(tmax.x, tmax.y), tmax.z);
    return vec2(max(tNear, 0.0), tFar);
}

// --- Industrial Concrete Warehouse Stage ---
struct RoomHit {
    float t;
    vec3 pos;
    vec3 n;
    int id; // 0=floor, 1=back wall, 2=side wall, 3=ceiling
};

RoomHit intersectRoom(vec3 ro, vec3 rd) {
    RoomHit h;
    h.t = 1e6;
    h.n = vec3(0.0, 1.0, 0.0);
    h.id = 0;

    if (rd.y < -1e-4) {
        float t = (0.0 - ro.y) / rd.y;
        if (t > 0.0 && t < h.t) { h.t = t; h.n = vec3(0.0, 1.0, 0.0); h.id = 0; }
    }
    if (rd.y > 1e-4) {
        float t = (4.10 - ro.y) / rd.y;
        if (t > 0.0 && t < h.t) { h.t = t; h.n = vec3(0.0, -1.0, 0.0); h.id = 3; }
    }
    if (rd.z < -1e-4) {
        float t = (-5.6 - ro.z) / rd.z;
        if (t > 0.0 && t < h.t) { h.t = t; h.n = vec3(0.0, 0.0, 1.0); h.id = 1; }
    }
    if (rd.x < -1e-4) {
        float t = (-4.4 - ro.x) / rd.x;
        if (t > 0.0 && t < h.t) { h.t = t; h.n = vec3(1.0, 0.0, 0.0); h.id = 2; }
    }
    if (rd.x > 1e-4) {
        float t = (4.4 - ro.x) / rd.x;
        if (t > 0.0 && t < h.t) { h.t = t; h.n = vec3(-1.0, 0.0, 0.0); h.id = 2; }
    }
    h.pos = ro + rd * h.t;
    return h;
}

vec3 shadeWarehouse(RoomHit h, vec3 ro, vec3 rd, float t) {
    vec3 p = h.pos;
    vec3 n = h.n;
    vec3 v = -rd;
    bool isClay = (uLightMode == 1);

    vec3 firePos0 = vec3( 0.00, 0.24, 0.06);
    vec3 firePos1 = vec3(-0.70, 2.02, 0.20);
    vec3 firePos2 = vec3( 0.36, 1.98, 0.26);
    float fireFlicker = 0.96 + 0.04 * sin(t * 9.3) * cos(t * 5.7);

    vec3 albedo = vec3(0.075, 0.078, 0.084);
    float rough = 0.72;

    if (h.id == 0) {
        // --- Concrete Warehouse Floor ---
        float cLow = fbm2(p.xz * 0.55);
        float cMid = fbm2(p.xz * 2.6 + vec2(3.1, -1.7));
        float cFine = vnoise2(p.xz * 16.0);

        // Expansion joint seams (central front-to-back seam at x = 0.06 matching 01.png)
        float seamX = smoothstep(0.018, 0.003, abs(p.x - 0.06));
        float seamZ = smoothstep(0.018, 0.004, abs(fract((p.z + 1.1) * 0.35) - 0.5) / 0.35);
        float seam = clamp(seamX + seamZ * 0.72, 0.0, 1.0);

        // Micro-normal roughness for shimmering concrete reflection
        float eps = 0.022;
        float h0 = fbm2(p.xz * 4.2);
        float hx = fbm2((p.xz + vec2(eps, 0.0)) * 4.2);
        float hz = fbm2((p.xz + vec2(0.0, eps)) * 4.2);
        n = normalize(vec3((h0 - hx) * 2.4 + seamX * sign(p.x - 0.06) * 0.14, 1.0, (h0 - hz) * 2.4));

        albedo = mix(vec3(0.045, 0.047, 0.052), vec3(0.095, 0.098, 0.104), cLow * 0.6 + cMid * 0.4);
        albedo *= mix(0.86, 1.10, cFine);
        albedo *= (1.0 - 0.72 * seam);

        // Blast soot ring around base
        float rBase = length(p.xz);
        float scorch = exp(-pow(max(rBase - 0.42, 0.0) * 1.15, 2.0));
        albedo *= mix(1.0, 0.32, scorch * (isClay ? 0.20 : 0.80));

        rough = clamp(0.22 + 0.42 * cMid + 0.10 * cFine + 0.22 * seam, 0.16, 0.85);
    } else if (h.id == 1 || h.id == 2) {
        // --- Dark Industrial Concrete Walls ---
        vec2 wuv = (h.id == 1) ? p.xy : vec2(p.z, p.y);
        float grime = fbm2(wuv * 0.65 + vec2(2.3, 1.1));
        float grain = fbm2(wuv * 3.4);

        float pilaster = smoothstep(0.14, 0.06, abs(fract((wuv.x + 1.2) * 0.38) - 0.5) / 0.38);
        float hSeam1 = smoothstep(0.025, 0.006, abs(wuv.y - 1.30));
        float hSeam2 = smoothstep(0.025, 0.006, abs(wuv.y - 2.55));
        float hSeam3 = smoothstep(0.030, 0.008, abs(wuv.y - 3.55));
        float hSeam = clamp(hSeam1 + hSeam2 + hSeam3, 0.0, 1.0);

        float heightShade = smoothstep(0.0, 0.30, wuv.y) * mix(0.32, 1.0, smoothstep(4.1, 2.5, wuv.y));

        albedo = mix(vec3(0.042, 0.045, 0.050), vec3(0.095, 0.100, 0.108), grime * 0.7 + grain * 0.3);
        albedo *= mix(1.0, 0.74, pilaster * 0.70);
        albedo *= (1.0 - 0.48 * hSeam);
        albedo *= mix(0.45, 1.0, heightShade);
        rough = 0.86;
    } else {
        // --- Dark Structural Warehouse Ceiling ---
        float trussX = smoothstep(0.055, 0.015, abs(fract(p.x * 0.58) - 0.5));
        float trussZ = smoothstep(0.055, 0.015, abs(fract(p.z * 0.48) - 0.5));
        albedo = vec3(0.018, 0.020, 0.025) * (1.0 - 0.38 * max(trussX, trussZ));
        rough = 0.92;
    }

    if (isClay) {
        // Clean mid-dark studio concrete backdrop so white/gray sculptural cloud pops
        float g = dot(albedo, vec3(0.299, 0.587, 0.114));
        albedo = vec3(mix(0.055, 0.14, clamp(g * 9.0, 0.0, 1.0)));
    }

    vec3 col = vec3(0.0);

    if (isClay) {
        vec3 keyDir = normalize(vec3(-0.72, 0.65, 0.42));
        float ndl = max(dot(n, keyDir), 0.0);
        // Soft projected shadow from the mushroom cloud onto the studio floor
        float floorShadow = 1.0;
        if (h.id == 0) {
            vec2 shCenter = vec2(0.85, -0.45);
            float dSh = length((p.xz - shCenter) / vec2(1.45, 1.15));
            floorShadow = mix(0.32, 1.0, smoothstep(0.35, 1.25, dSh));
        }
        col += albedo * (0.28 + 0.95 * ndl * floorShadow);
    } else {
        // Cool overhead industrial warehouse ceiling lights
        vec3 ceilL1 = vec3(-1.85, 4.02, -1.25);
        vec3 ceilL2 = vec3( 2.35, 4.02, -1.55);

        vec3 d1 = ceilL1 - p;
        float dist1 = length(d1);
        float ndl1 = max(dot(n, d1 / dist1), 0.0);
        float att1 = 1.0 / (1.0 + 0.18 * dist1 * dist1);

        vec3 d2 = ceilL2 - p;
        float dist2 = length(d2);
        float ndl2 = max(dot(n, d2 / dist2), 0.0);
        float att2 = 1.0 / (1.0 + 0.18 * dist2 * dist2);

        float stageGain = (uLightMode == 0) ? 1.0 : (uLightMode == 2 ? 0.55 : 0.40);
        vec3 coolLight = vec3(0.68, 0.76, 0.90) * 1.15 * stageGain;
        col += albedo * coolLight * (ndl1 * att1 + ndl2 * att2);

        // Low cool-neutral ambient fill (keeps lumMin ~ 8-15, never washed out!)
        col += albedo * vec3(0.11, 0.12, 0.145) * stageGain;

        if (uLightMode == 2) {
            vec3 rakeDir = normalize(vec3(-0.92, 0.24, 0.30));
            float ndlR = max(dot(n, rakeDir), 0.0);
            vec3 hR = normalize(rakeDir + v);
            float specR = pow(max(dot(n, hR), 0.0), mix(12.0, 56.0, 1.0 - rough)) * 0.30;
            col += (albedo * ndlR + vec3(specR)) * vec3(1.15, 0.90, 0.68) * 0.95;
        } else if (uLightMode == 3) {
            vec3 backDir = normalize(vec3(0.12, 0.48, -0.86));
            float ndlB = max(dot(n, backDir), 0.0);
            vec3 hB = normalize(backDir + v);
            float specB = pow(max(dot(n, hB), 0.0), mix(14.0, 68.0, 1.0 - rough)) * 0.45;
            col += (albedo * ndlB + vec3(specB)) * vec3(0.72, 0.84, 1.08) * 1.10;
        }

        // Fireball lighting: strong on concrete floor reflection, tightly attenuated on back walls
        float wallDamp = (h.id == 0) ? 1.0 : 0.36;

        // Ground ignition core (blinding white-gold reflection streaking forward on floor)
        vec3 lf0 = firePos0 - p;
        float df0 = length(lf0);
        vec3 ldir0 = lf0 / max(df0, 1e-4);
        float ndl0 = max(dot(n, ldir0), 0.0);
        float att0 = 1.0 / (0.20 + 0.62 * df0 * df0);
        vec3 hf0 = normalize(ldir0 + v);
        float specPow0 = mix(14.0, 110.0, pow(1.0 - rough, 2.0));
        float spec0 = pow(max(dot(n, hf0), 0.0), specPow0) * (0.35 + 0.75 * (1.0 - rough));
        float fresnel = pow(1.0 - max(dot(n, v), 0.0), 4.0);
        vec3 fCol0 = vec3(3.8, 2.25, 0.85) * fireFlicker * wallDamp;
        col += (albedo * ndl0 * 1.15 + vec3(spec0 * (0.28 + 0.85 * fresnel))) * fCol0 * att0;

        // Mushroom cap golden-orange bounce lights
        vec3 lf1 = firePos1 - p;
        float df1 = length(lf1);
        vec3 ldir1 = lf1 / max(df1, 1e-4);
        float att1f = 1.0 / (1.0 + 0.48 * df1 * df1);
        float ndl1f = max(dot(n, ldir1), 0.0);
        vec3 hf1 = normalize(ldir1 + v);
        float spec1f = pow(max(dot(n, hf1), 0.0), 22.0) * (0.14 + 0.55 * fresnel);
        col += (albedo * ndl1f + vec3(spec1f)) * vec3(2.1, 0.88, 0.16) * att1f * fireFlicker * wallDamp;

        vec3 lf2 = firePos2 - p;
        float df2 = length(lf2);
        vec3 ldir2 = lf2 / max(df2, 1e-4);
        float att2f = 1.0 / (1.1 + 0.52 * df2 * df2);
        float ndl2f = max(dot(n, ldir2), 0.0);
        col += albedo * ndl2f * vec3(1.6, 0.65, 0.12) * att2f * fireFlicker * wallDamp;

        // Subtle overhead rectangular LED panel fixtures on ceiling (matches 01.png)
        if (h.id == 3) {
            vec2 lp1 = abs(p.xz - vec2(-1.85, -1.25));
            vec2 lp2 = abs(p.xz - vec2( 2.35, -1.55));
            float panel1 = smoothstep(0.32, 0.22, lp1.x) * smoothstep(0.18, 0.11, lp1.y);
            float panel2 = smoothstep(0.32, 0.22, lp2.x) * smoothstep(0.18, 0.11, lp2.y);
            col += vec3(1.45, 1.55, 1.70) * max(panel1, panel2) * stageGain;
        }
    }

    // Contact AO under the base soot skirt (keep central ignition core bright)
    if (h.id == 0) {
        float r = length(p.xz);
        float baseAO = mix(0.35, 1.0, smoothstep(0.45, 1.55, r));
        baseAO = mix(1.0, baseAO, smoothstep(0.32, 0.68, r));
        col *= baseAO;
    }

    // Subtle dark warehouse atmospheric depth haze
    float haze = 1.0 - exp(-h.t * 0.04);
    vec3 hazeCol = isClay ? vec3(0.055) : vec3(0.010, 0.011, 0.014);
    col = mix(col, hazeCol, haze * 0.35);

    return col;
}

// --- ACES Filmic Tonemapping with soft highlight shoulder ---
vec3 acesFilmicSoft(vec3 x) {
    float a = 2.51;
    float b = 0.03;
    float c = 2.43;
    float d = 0.59;
    float e = 0.14;
    vec3 y = (x * (a * x + b)) / (x * (c * x + d) + e);
    return y * (0.984 / (1.0 + 0.012 * max(max(x.r, x.g), x.b)));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;
    float t = uTime;
    float scale = 1.0 + 0.08 * clamp(uOpen, 0.0, 1.0);
    bool isClay = (uLightMode == 1);

    // --- Camera Setup (uv.y spans [-0.5, +0.5]) ---
    vec3 ro, ta;
    float fl;
    if (uCamMode == 1) {
        // ~2.5x macro close-up of the rolling incandescent fire-and-soot folds in the cap
        ro = vec3(-0.22 + 0.05 * sin(t * 0.6), 2.02, 2.95);
        ta = vec3(-0.18, 2.04, 0.15);
        fl = 1.65;
    } else {
        // Hero full framing: full dark-crowned mushroom cap + narrow stem + bright base + floor reflection
        ro = vec3(0.0, 1.15, 6.20);
        ta = vec3(0.0, 1.45, 0.0);
        fl = 1.48;
    }

    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(uu * uv.x + vv * uv.y + ww * fl);

    // --- Background: Industrial Concrete Warehouse ---
    RoomHit hit = intersectRoom(ro, rd);
    vec3 bgCol = shadeWarehouse(hit, ro, rd, t);

    // --- Volumetric Raymarch through Tight Fireball AABB ---
    vec3 bMin = vec3(-1.75, 0.0, -1.35) * scale;
    vec3 bMax = vec3( 1.75, 3.15, 1.35) * scale;
    vec2 boxT = intersectBox(ro, rd, bMin, bMax);

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

    if (boxT.y > boxT.x && boxT.x < hit.t) {
        float tStart = boxT.x;
        float tEnd = min(boxT.y, hit.t);
        float span = tEnd - tStart;

        const int STEPS = 64;
        float dt = span / float(STEPS);

        float ign = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715))));
        float rayT = tStart + ign * dt;

        // Light rig per uLightMode
        vec3 keyDir, keyCol, ambBot, ambTop;
        if (uLightMode == 1) {
            // Sculptural neutral clay/density study (high key-to-ambient contrast)
            keyDir = normalize(vec3(-0.72, 0.65, 0.42));
            keyCol = vec3(2.45);
            ambBot = vec3(0.032);
            ambTop = vec3(0.088);
        } else if (uLightMode == 2) {
            // Grazing raking key light
            keyDir = normalize(vec3(-0.95, 0.20, 0.24));
            keyCol = vec3(1.85, 1.40, 0.98);
            ambBot = vec3(0.025, 0.022, 0.024);
            ambTop = vec3(0.055, 0.062, 0.078);
        } else if (uLightMode == 3) {
            // Dramatic backlit rim light
            keyDir = normalize(vec3(0.14, 0.38, -0.91));
            keyCol = vec3(2.40, 2.25, 2.55);
            ambBot = vec3(0.024, 0.022, 0.026);
            ambTop = vec3(0.052, 0.062, 0.082);
        } else {
            // Hero photoreal dark industrial warehouse stage
            keyDir = normalize(vec3(-0.22, 0.93, 0.28));
            keyCol = vec3(0.75, 0.82, 0.96);
            ambBot = vec3(0.026, 0.022, 0.022);
            ambTop = vec3(0.060, 0.068, 0.082);
        }

        float cosTheta = dot(rd, keyDir);
        float gPhase = (uLightMode == 3) ? 0.62 : (uLightMode == 2 ? 0.38 : 0.22);
        float hg = (1.0 - gPhase * gPhase) / (4.0 * 3.14159 * pow(1.0 + gPhase * gPhase - 2.0 * gPhase * cosTheta, 1.5));
        float phase = mix(0.70, 1.0 + hg * 3.6, (uLightMode == 1) ? 0.12 : 0.68);

        vec3 fCore0 = vec3( 0.00, 0.20, 0.05) * scale;
        vec3 fCore1 = vec3(-0.70, 2.02, 0.18) * scale;
        vec3 fCore2 = vec3( 0.36, 1.98, 0.26) * scale;

        for (int i = 0; i < STEPS; i++) {
            if (T < 0.015) break;
            vec3 p = ro + rd * rayT;
            rayT += dt;

            vec3 vol = sampleVolume(p, t, scale, false);
            float dens = vol.x;
            if (dens < 0.004) continue;

            float temp = vol.y;
            float billow = vol.z;

            // Multi-tap directional self-shadowing (tight near-tap captures crisp billow crevices!)
            float shDens = 0.0;
            shDens += sampleVolume(p + keyDir * 0.09, t, scale, true).x * 0.14;
            shDens += sampleVolume(p + keyDir * 0.28, t, scale, true).x * 0.26;
            shDens += sampleVolume(p + keyDir * 0.65, t, scale, true).x * 0.42;
            if (isClay) {
                shDens += sampleVolume(p + keyDir * 1.15, t, scale, true).x * 0.50;
            }
            float shadowT = exp(-shDens * (isClay ? 3.2 : 2.6));

            // Micro-crevice occlusion from billow relief
            float creviceAO = mix(0.35, 1.12, billow);

            float hFrac = clamp(p.y / (3.0 * scale), 0.0, 1.0);
            vec3 ambient = mix(ambBot, ambTop, hFrac) * creviceAO * (0.35 + 0.65 * shadowT);

            // Deep dark charcoal-black soot in hero; clean sculptural clay in mode 1
            vec3 sootAlbedo = isClay ? vec3(0.46) : vec3(0.038, 0.037, 0.042);

            // Beer-powder silver-lining on backlit/grazing edges
            float powder = 1.0 - 0.72 * exp(-dens * 1.6);
            vec3 scatter = sootAlbedo * (keyCol * shadowT * phase * creviceAO * (0.50 + 0.50 * powder) + ambient);

            vec3 emit = vec3(0.0);
            if (!isClay) {
                // Incandescent Planck blackbody emission
                emit = blackbody(temp) * (2.6 + 1.4 * smoothstep(0.65, 1.35, temp));

                // Internal warm orange bounce lighting from nearby fire cores onto soot bellies
                float d0 = dot(p - fCore0, p - fCore0);
                float d1 = dot(p - fCore1, p - fCore1);
                float d2 = dot(p - fCore2, p - fCore2);
                float internalFire = 1.25 / (0.14 + d0 * 3.4)
                                   + 0.95 / (0.18 + d1 * 3.8)
                                   + 0.70 / (0.20 + d2 * 4.0);
                float intAtten = exp(-dens * 0.32) * creviceAO;
                scatter += vec3(1.15, 0.36, 0.04) * sootAlbedo * internalFire * intAtten * 2.4;
            } else {
                // Subtle rim light from right-back in clay mode to separate silhouette cleanly
                vec3 rimDir = normalize(vec3(0.72, 0.48, -0.50));
                float rSh = exp(-sampleVolume(p + rimDir * 0.25, t, scale, true).x * 1.8);
                scatter += sootAlbedo * vec3(0.38) * rSh * creviceAO;
            }

            // High-opacity Beer-Lambert integration for thick, dense soot plumes
            float sigma = dens * (isClay ? 6.2 : 6.4);
            float stepT = exp(-sigma * dt);
            vec3 stepLight = (scatter * sigma + emit * (1.25 + 1.15 * dens)) * (1.0 - stepT) / max(sigma, 1e-4);

            accum += T * stepLight;
            T *= stepT;
        }
    }

    vec3 col = accum + T * bgCol;

    // --- Optical Glow, Dark Blast Debris & Fine Pinpoint Base Embers ---
    if (!isClay) {
        // Intense compact optical bloom around the blinding ground ignition core
        vec3 corePos = vec3(0.0, 0.20, 0.06) * scale;
        vec3 toCore = corePos - ro;
        float proj = dot(toCore, rd);
        if (proj > 0.0 && proj < hit.t + 0.5) {
            float perpDist = length(toCore - rd * proj);
            float halo = exp(-perpDist * perpDist * 4.5) * 0.38
                       + exp(-perpDist * perpDist * 18.0) * 0.65;
            col += vec3(1.75, 0.95, 0.35) * halo * (0.30 + 0.70 * T);
        }

        // Subtle warm bloom around left cap fire pocket
        vec3 capCore = vec3(-0.66, 2.06, 0.20) * scale;
        vec3 toCap = capCore - ro;
        float projC = dot(toCap, rd);
        if (projC > 0.0) {
            float perpC = length(toCap - rd * projC);
            float capHalo = exp(-perpC * perpC * 3.2) * 0.14;
            col += vec3(1.30, 0.46, 0.08) * capHalo * (0.20 + 0.80 * T);
        }

        // Subtle flying dark blast debris specks & fine pinpoint base embers (hero framing)
        if (uCamMode == 0) {
            vec2 baseCenter = vec2(0.0, -0.28);
            float blastZone = smoothstep(0.34, 0.08, length((uv - baseCenter) * vec2(1.0, 1.35)));
            if (blastZone > 0.01) {
                // Layer 1: tiny irregular dark blast debris flecks
                vec2 dUV = uv * 58.0 + vec2(0.0, -t * 1.4);
                vec2 dCell = floor(dUV);
                vec2 dF = fract(dUV) - 0.5;
                float dRnd = hash12(dCell);
                if (dRnd > 0.87) {
                    vec2 jit = vec2(hash12(dCell + 19.3), hash12(dCell + 41.7)) - 0.5;
                    vec2 qd = dF - jit * 0.65;
                    // Irregular angular speck shape
                    float ang = hash12(dCell + 7.2) * 6.283;
                    float cs = cos(ang), sn = sin(ang);
                    qd = mat2(cs, -sn, sn, cs) * qd;
                    float speck = smoothstep(0.11, 0.04, length(qd * vec2(1.0, 1.65)));
                    col = mix(col, vec3(0.012, 0.011, 0.012), speck * blastZone * 0.85);
                }

                // Layer 2: pinpoint hot micro-embers rising right near the ground ignition
                vec2 eUV = uv * 74.0 + vec2(sin(uv.y * 12.0 + t * 2.0) * 0.35, -t * 3.2);
                vec2 eCell = floor(eUV);
                vec2 eF = fract(eUV) - 0.5;
                float eRnd = hash12(eCell + 101.5);
                if (eRnd > 0.93) {
                    vec2 jit = vec2(hash12(eCell + 31.1), hash12(eCell + 67.4)) - 0.5;
                    float dE = length(eF - jit * 0.65);
                    float emberZone = smoothstep(0.24, 0.04, length((uv - vec2(0.0, -0.30)) * vec2(1.1, 1.5)));
                    float ember = smoothstep(0.075, 0.012, dE) * emberZone;
                    col += vec3(3.2, 1.35, 0.22) * ember * (0.45 + 0.55 * T);
                }
            }
        }
    }

    // --- Exposure, ACES Filmic Tonemapping, & sRGB Gamma ---
    float exposure = isClay ? 1.05 : 1.02;
    col *= exposure;
    col = acesFilmicSoft(max(col, vec3(0.0)));

    // Minimal linear floor lift so lumMin lands ~ 8-15 (0% crushed black, rich dark warehouse!)
    col = max(col, vec3(0.0010));

    // Gentle cinematic vignette
    float vig = 1.0 - 0.18 * dot(uv, uv);
    col *= clamp(vig, 0.78, 1.0);

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

    // Strict R == G == B guarantee for clay/density study mode
    if (isClay) {
        float luma = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(luma);
    }

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierfuel-air-fireball-explosion
CategoryGame VFX Shaders · Physically-Based VFX
Upstream studyvfx_realistic/lib/01_fuel_air_fireball_explosion.glsl (commit afde0bc)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size814 lines · 32.5 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

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

Adapted upstream mathematical helpers

Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Jorge Jimenez, Interleaved Gradient Noise

All game vfx shaders