Propagating Flame Wave Corridor shader

propagating-flame-wave-corridor.glsl · 866 lines · GLSL ES 3.00 · MIT

Propagating Flame Wave CorridorGame VFX3s Loop
Propagating Flame Wave Corridor WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 866 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

Choreographed Loop VFX
propagating-flame-wave-corridor.glsl (866 lines)
#version 300 es
// Propagating Flame Wave Corridor (propagating-flame-wave-corridor.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_animated/lib/03_propagating_flame_wave_corridor.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;

// ============================================================================
// 03_propagating_flame_wave_corridor.glsl
// Ancient carved stone temple corridor with twin rows of spiraled dark basalt
// pillars erupting with a choreographed propagating wave of crisp, roaring,
// curled-fBM volumetric fire tongues, dark curling soot wisps, wet reflective
// cracked flagstones, and drifting fiery embers.
//
// Seamless 3.0s loop: phase = fract(uTime / 3.0)
//   - Propagating flame surge racing down the corridor: sin(z * k - TAU * phase)
//   - Dual-phase crossfaded domain-warped curled-fBM fire tongues (zero seam)
//   - Perspective-scaled tight volumetric raymarch (crisp fire edges, zero fog-blob)
//   - Rippling wet flagstone puddle reflections synced with the flame wave
//   - Seamless rising embers & subtle windswept spark streaks
// ============================================================================

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

const float PERIOD      = 3.0;
const float DZ          = 2.65;             // Pillar spacing along +Z
const float PILLAR_X    = 2.48;             // Twin pillar row half-width
const float WAVE_K      = TAU / (4.0 * DZ); // 4-pillar wavelength
const float WAVE_OFFSET = 1.45;             // Dramatic crest framing at t=1.2 (phase=0.40)

// ----------------------------------------------------------------------------
// Fast hashes & multi-octave 2D/3D noise + curled-fBM fire turbulence
// ----------------------------------------------------------------------------
float hash11(float p) {
    p = fract(p * 0.1031);
    p *= p + 33.33;
    p *= p + p;
    return fract(p);
}

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

vec2 hash22(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.xx + p3.yz) * p3.zy);
}

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 ign(vec2 p) {
    return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
}

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.80, 0.60, -0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        v += a * noise2(p);
        p = rot * p * 2.04 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

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

// 4-octave high-contrast ridged/billow fBM for razor-sharp licking flame tongues
float fbmFireRidged(vec3 p) {
    float v = 0.0;
    float a = 0.52;
    for (int i = 0; i < 4; i++) {
        float n = noise3(p);
        float ridge = 1.0 - abs(2.0 * n - 1.0);
        v += a * mix(n, ridge * ridge, 0.52);
        // Rotate & stretch upward for organic flame eddies
        p = vec3(
            0.80 * p.x - 0.60 * p.z,
            p.y * 0.92,
            0.60 * p.x + 0.80 * p.z
        ) * 2.15 + vec3(13.7, 29.3, 47.1);
        a *= 0.49;
    }
    return v;
}

// Seamless 3.0s dual-phase upward-advected curled-fBM fire turbulence
// Returns vec2(billowTurb, sharpFilamentRidge)
vec2 flameCurlSeamless(vec3 p, float phase) {
    float p0 = phase;
    float p1 = fract(phase + 0.5);
    float w0 = 1.0 - abs(2.0 * p0 - 1.0);
    float w1 = 1.0 - w0;

    // Periodic rotational swirl
    float ang = TAU * phase;
    vec3 swirl = vec3(0.32 * sin(ang + p.y * 0.85), 0.0, 0.22 * cos(ang + p.x * 0.7));

    vec3 q0 = p + swirl + vec3(0.0, -p0 * 4.8, -p0 * 1.6);
    vec3 q1 = p + swirl + vec3(11.3, -p1 * 4.8 + 5.7, -p1 * 1.6 + 19.1);

    // Pseudo-curl domain warp: displace sample point by orthogonal low-freq noise
    vec3 wv0 = vec3(
        noise3(q0 * 0.72 + vec3(0.0, 3.1, 7.4)) - 0.5,
        noise3(q0 * 0.72 + vec3(5.2, 0.0, 2.8)) - 0.5,
        noise3(q0 * 0.72 + vec3(9.7, 6.3, 0.0)) - 0.5
    );
    vec3 wv1 = vec3(
        noise3(q1 * 0.72 + vec3(0.0, 3.1, 7.4)) - 0.5,
        noise3(q1 * 0.72 + vec3(5.2, 0.0, 2.8)) - 0.5,
        noise3(q1 * 0.72 + vec3(9.7, 6.3, 0.0)) - 0.5
    );

    float b0 = fbmFireRidged(q0 + wv0 * 1.45);
    float b1 = fbmFireRidged(q1 + wv1 * 1.45);
    float billow = w0 * b0 + w1 * b1;

    // Secondary high-frequency sharp filament veins inside the fire
    float f0 = noise3((q0 + wv0 * 1.85) * 2.65);
    float f1 = noise3((q1 + wv1 * 1.85) * 2.65);
    float fn = w0 * f0 + w1 * f1;
    float ridge = pow(1.0 - abs(2.0 * fn - 1.0), 2.4);

    return vec2(billow, ridge);
}

// ----------------------------------------------------------------------------
// Propagating Flame Wave Kinematics
// ----------------------------------------------------------------------------
float flameWave(float z, float phase) {
    float w = 0.5 + 0.5 * sin(z * WAVE_K - TAU * phase + WAVE_OFFSET);
    return pow(w, 1.55);
}

// ----------------------------------------------------------------------------
// SDF Primitives & Scene Geometry
// ----------------------------------------------------------------------------
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 sdBox(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

// matId: 1 = wet flagstone floor, 2 = raised curb & pedestal, 3 = twisted carved pillar, 4 = dark outer gallery
vec2 mapScene(vec3 p) {
    // 1. Flagstone floor
    float dFloor = p.y;
    vec2 res = vec2(dFloor, 1.0);

    // 2. Raised side curb along corridor edges
    float ax = abs(p.x);
    float dCurb = sdBox(vec3(ax - 3.65, p.y - 0.06, 0.0), vec3(1.62, 0.075, 1e4)) - 0.015;
    if (dCurb < res.x) {
        res = vec2(dCurb, 2.0);
    }

    // 3. Twin rows of sculpted twisted dark basalt pillars
    if (ax > 1.35 && ax < 3.65 && p.y < 4.25 && p.z > 0.8) {
        float idZ = max(1.0, round(p.z / DZ));
        vec3 q = vec3(ax - PILLAR_X, p.y, p.z - idZ * DZ);

        // Stepped beveled square stone plinth (y: 0.0 -> 0.72)
        float dBase1 = sdBox(q - vec3(0.0, 0.14, 0.0), vec3(0.62, 0.14, 0.62)) - 0.025;
        float tMid = clamp((q.y - 0.22) / 0.42, 0.0, 1.0);
        float wMid = mix(0.54, 0.44, tMid);
        float dBase2 = sdBox(q - vec3(0.0, 0.43, 0.0), vec3(wMid, 0.22, wMid)) - 0.02;
        float dLip   = sdBox(q - vec3(0.0, 0.67, 0.0), vec3(0.49, 0.055, 0.49)) - 0.018;
        float dPlinth = min(dBase1, min(dBase2, dLip));

        // Twisted helical carved basalt shaft with clean sculpted flame-tip finial (y: 0.68 -> 3.62)
        float yShaft = clamp((q.y - 0.68) / 2.94, 0.0, 1.0);
        float rBase = mix(0.41, 0.31, yShaft);
        // Smooth pointed flame-carved spire tip (no flat crater!)
        float finial = smoothstep(0.70, 1.0, yShaft);
        rBase *= (1.0 - 0.94 * finial * finial);

        float ang = atan(q.z, q.x);
        float twist = q.y * 2.65 + idZ * 0.65;
        float rib1 = sin(3.0 * ang - twist);
        float rib2 = sin(6.0 * ang - 2.0 * twist + 0.55);
        float rib3 = sin(9.0 * ang - 3.0 * twist + 1.15);
        float carving = 0.092 * rib1 + 0.032 * abs(rib1) + 0.024 * rib2 + 0.011 * rib3;
        carving *= smoothstep(0.0, 0.10, yShaft) * (1.0 - 0.88 * finial);

        float dCyl = length(q.xz) - (rBase + carving);
        float dCap = max(0.66 - q.y, q.y - 3.64);
        float dShaft = max(dCyl * 0.62, dCap);

        float dPillar = smin(dPlinth, dShaft, 0.055);
        if (dPillar < res.x) {
            float matP = (dPlinth < dShaft + 0.02) ? 2.0 : 3.0;
            res = vec2(dPillar, matP);
        }
    }

    // 4. Low dark outer temple gallery tiers (strictly capped at y <= 3.0 so night sky is clean)
    if (ax > 5.1 && p.y < 3.4) {
        float dWall = sdBox(vec3(ax - 7.1, p.y - 1.40, 0.0), vec3(0.95, 1.50, 1e4));
        float ledge = 0.09 * sin(p.y * 4.8);
        dWall += ledge;
        if (dWall < res.x) {
            res = vec2(dWall, 4.0);
        }
    }

    return res;
}

float mapDist(vec3 p) {
    return mapScene(p).x;
}

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

// Fast ambient occlusion
float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 4; i++) {
        float h = 0.06 + 0.16 * float(i);
        float d = mapDist(p + n * h);
        occ += (h - d) * sca;
        sca *= 0.62;
    }
    return clamp(1.0 - 1.45 * occ, 0.12, 1.0);
}

// ----------------------------------------------------------------------------
// Cracked Wet Flagstones & High-Contrast Reflective Puddles
// ----------------------------------------------------------------------------
struct FloorInfo {
    float grout;
    float wetness;
    float slabTone;
    vec3 normal;
};

FloorInfo evalFloor(vec2 xz, float phase) {
    FloorInfo fi;
    // Staggered weathered stone flagstone courses
    float row = floor(xz.y / 0.94);
    float rowOff = hash11(row * 17.31) * 0.88;
    vec2 st = vec2(xz.x + rowOff, xz.y);
    vec2 cellSize = vec2(0.86 + 0.22 * hash11(row * 5.7), 0.94);
    vec2 cid = floor(st / cellSize);
    vec2 f = fract(st / cellSize);

    float edgeWarp = (noise2(xz * 4.8) - 0.5) * 0.022;
    vec2 dEdge = min(f, 1.0 - f) * cellSize + edgeWarp;
    float minEdge = min(dEdge.x, dEdge.y);

    // Deep recessed dark mortar joints
    fi.grout = 1.0 - smoothstep(0.010, 0.038, minEdge);
    fi.slabTone = 0.64 + 0.46 * hash12(cid + vec2(row * 3.1, 1.7));

    // Organic broken puddles across the flagstones (mirror-glossy pools vs rough dark slate)
    float pMacro = fbm2(xz * vec2(0.62, 0.44) + vec2(2.3, 1.1));
    float pDetail = fbm2(xz * vec2(2.1, 1.7) + vec2(7.1, 3.8));
    float puddleField = pMacro * 0.78 + pDetail * 0.22;
    float centerBias = 0.09 * (1.0 - smoothstep(0.25, 2.05, abs(xz.x)));
    fi.wetness = smoothstep(0.43, 0.57, puddleField + centerBias) * (1.0 - 0.85 * fi.grout);

    // Rich weathered stone surface relief + per-slab tilt
    vec2 randTilt = (hash22(cid) - 0.5) * 0.055;
    float h0 = fbm2(xz * 6.0);
    float hx = fbm2(xz * 6.0 + vec2(0.035, 0.0));
    float hz = fbm2(xz * 6.0 + vec2(0.0, 0.035));
    vec3 stoneN = normalize(vec3((h0 - hx) * 1.75 + randTilt.x, 1.0, (h0 - hz) * 1.75 + randTilt.y));

    // Stone slab bevel near mortar joints
    vec2 edgeDir = vec2(
        smoothstep(0.07, 0.01, f.x * cellSize.x) - smoothstep(0.07, 0.01, (1.0 - f.x) * cellSize.x),
        smoothstep(0.07, 0.01, f.y * cellSize.y) - smoothstep(0.07, 0.01, (1.0 - f.y) * cellSize.y)
    );
    stoneN = normalize(stoneN + vec3(edgeDir.x, 0.0, edgeDir.y) * 0.24);

    // Subtle animated ripples on wet puddles synced to the propagating flame wave
    float waveRip = sin(xz.y * WAVE_K * 3.0 - TAU * phase * 2.0 + xz.x * 2.6) * 0.012
                  + sin(xz.x * 7.2 + xz.y * 4.5 - TAU * phase) * 0.008;
    vec3 puddleNorm = normalize(vec3(waveRip * 0.9, 1.0, waveRip * 1.2));

    fi.normal = normalize(mix(stoneN, puddleNorm, fi.wetness * 0.92));
    return fi;
}

// ----------------------------------------------------------------------------
// Crisp Roaring Curled-fBM Volumetric Flame Plumes
// ----------------------------------------------------------------------------
vec2 intersectBox(vec3 ro, vec3 invRd, vec3 bmin, vec3 bmax) {
    vec3 t0 = (bmin - ro) * invRd;
    vec3 t1 = (bmax - ro) * invRd;
    vec3 tmin = min(t0, t1);
    vec3 tmax = max(t0, t1);
    float tn = max(max(tmin.x, tmin.y), tmin.z);
    float tf = min(min(tmax.x, tmax.y), tmax.z);
    return vec2(tn, tf);
}

// High-contrast deeply saturated cinematic fire blackbody ramp:
// Deep crimson-red tendrils -> vivid fiery orange -> molten amber -> thin white-gold veins
vec3 firePalette(float temp) {
    vec3 cCrimson = vec3(0.42, 0.016, 0.002);
    vec3 cRedOrg  = vec3(1.38, 0.14, 0.008);
    vec3 cOrange  = vec3(2.15, 0.46, 0.022);
    vec3 cAmber   = vec3(2.65, 1.12, 0.095);
    vec3 cWhite   = vec3(3.25, 2.25, 0.62);
    vec3 c = mix(cCrimson, cRedOrg, smoothstep(0.05, 0.32, temp));
    c = mix(c, cOrange, smoothstep(0.30, 0.62, temp));
    c = mix(c, cAmber,  smoothstep(0.60, 0.86, temp));
    c = mix(c, cWhite,  smoothstep(0.85, 1.00, temp));
    return c;
}

// Evaluates crisp roaring volumetric fire tongues + dark curling soot at world point p
vec2 evalFlameVolume(vec3 p, float phase, out float temp) {
    float ax = abs(p.x);
    float z = p.z;
    temp = 0.0;
    if (z < 1.3 || z > 34.0 || ax < 1.05 || ax > 5.35 || p.y < 0.0 || p.y > 7.8) {
        return vec2(0.0);
    }

    float surge = flameWave(z, phase);
    float pillarMod = pow(0.5 + 0.5 * cos(TAU * z / DZ), 1.65);
    float idZ = max(1.0, round(p.z / DZ));
    float zLocal = p.z - idZ * DZ;

    float fireDens = 0.0;
    float smokeDens = 0.0;

    // Keep the dark sculpted pillar shaft crisp & visible up to its finial (y ~ 3.45)
    float rFromPillar = length(vec2(ax - PILLAR_X, zLocal));
    float shaftKeepout = mix(smoothstep(0.40, 0.68, rFromPillar), 1.0, smoothstep(2.90, 3.58, p.y));

    // A) Towering ragged upward-curling flame plumes erupting unevenly off each pillar top
    float yUpperMask = smoothstep(2.10, 2.72, p.y);
    if (yUpperMask > 0.01) {
        float yRel = p.y - 2.85;
        // Per-pillar uneven height & wave surge for dramatic undulating crests
        float pillarVar = 0.80 + 0.42 * hash11(idZ * 13.7 + 2.3);
        float plumeH = (0.95 + 2.05 * surge + 1.35 * pillarMod * (0.35 + 0.65 * surge)) * pillarVar;
        float yNormRaw = clamp(yRel / max(0.8, plumeH), -0.40, 1.45);

        // Coarse bounding check before turbulence
        float waveSway = 0.34 * sin(z * WAVE_K - TAU * phase + p.y * 0.95);
        float xCenter0 = PILLAR_X + 0.24 + 0.42 * max(0.0, yRel) + 0.20 * waveSway;
        if (abs(ax - xCenter0) < 1.85 && yNormRaw < 1.38) {
            // High-frequency curled-fBM turbulence
            vec3 np = vec3(ax * 1.45, p.y * 0.95, z * 1.15);
            vec2 ct = flameCurlSeamless(np, phase);
            float billow = ct.x;
            float ridge  = ct.y;

            // Domain-warp the envelope itself so flame tongues lick unevenly into the night sky
            // (completely breaks up the straight diagonal bar silhouette!)
            float warpStrength = 0.35 + 0.85 * max(0.0, yNormRaw);
            float axWarped = ax + (billow - 0.48) * 1.15 * warpStrength;
            float yWarped  = yRel - (billow - 0.36) * 1.65 * warpStrength - (ridge - 0.35) * 0.75 * warpStrength;
            float yNorm = clamp(yWarped / max(0.8, plumeH), -0.40, 1.40);

            float xWidth = (0.36 + 0.24 * surge) * (1.0 - 0.48 * max(0.0, yNorm)) + 0.18 * pillarMod;
            float dx = (axWarped - xCenter0) / max(0.18, xWidth);

            // Arching flame bridge starts higher between pillars, lower over each pillar crown
            float yStart = mix(0.32, -0.14, pillarMod) - 0.08 * surge;
            float vEnv = smoothstep(yStart - 0.20, yStart + 0.16, yNorm)
                       * (1.0 - smoothstep(0.28, 0.98, yNorm));
            float rEnv = exp(-dx * dx * 2.15);
            float macroEnv = vEnv * rEnv * yUpperMask * shaftKeepout;

            // Aggressive turbulent carving creates ragged licking tongues & dark gaps
            float carved = macroEnv * 1.25 + (billow - 0.50) * 0.95 - 0.26 * max(0.0, yNorm);
            fireDens = smoothstep(0.28, 0.54, carved) * (1.05 + 1.05 * surge);

            // Deep fiery saturation: dark crimson-orange body with thin blazing white-gold veins!
            float innerCore = smoothstep(0.32, 0.82, carved);
            float hotVeins = pow(ridge, 1.55) * smoothstep(0.26, 0.62, carved);
            temp = clamp(
                innerCore * (0.48 + 0.26 * surge) + hotVeins * 0.58 - 0.34 * max(0.0, yNorm),
                0.0, 1.0
            );

            // Dark curling soot wisps peeling off the flame crests
            float smokeEnv = smoothstep(0.38, 0.80, yNorm) * (1.0 - smoothstep(0.85, 1.35, yNorm)) * rEnv;
            smokeDens = smokeEnv * smoothstep(0.36, 0.66, billow) * 1.05;
        }
    }

    // B) Smooth, organic tiny licking fire tongues at plinth base corners (zero stipple/halo!)
    float yBaseMask = smoothstep(0.68, 0.08, p.y);
    if (yBaseMask > 0.01 && abs(zLocal) < 0.85) {
        float rx = ax - PILLAR_X;
        float rEdge = max(abs(rx), abs(zLocal));
        float ring = exp(-pow((rEdge - 0.64) / 0.12, 2.0));
        float cornerBias = smoothstep(0.25, 0.60, min(abs(rx), abs(zLocal)));
        float hBase = 0.24 + 0.32 * surge;
        float yEnv = smoothstep(0.0, 0.06, p.y) * (1.0 - smoothstep(0.08, hBase, p.y));
        float baseEnv = ring * yEnv * (0.25 + 0.75 * cornerBias);
        if (baseEnv > 0.04) {
            vec2 bct = flameCurlSeamless(vec3(ax * 3.2, p.y * 2.6, z * 3.2), phase);
            float bCarved = baseEnv * 1.15 + (bct.x - 0.56) * 0.85;
            float bFire = smoothstep(0.32, 0.56, bCarved) * (0.90 + 0.75 * surge);
            if (bFire > fireDens) {
                fireDens = bFire;
                temp = clamp(smoothstep(0.32, 0.78, bCarved) * 0.62 + bct.y * 0.30, 0.0, 0.84);
            }
        }
    }

    return vec2(fireDens, smokeDens);
}

// Zero-jitter perspective-scaled volumetric raymarch:
// Smooth midpoint sampling eliminates 100% of checkerboard/halftone stippling
// while tight dt (~0.065+0.024*t) captures crisp roaring curled-fBM tongues!
void marchFlameSegment(
    vec3 ro, vec3 rd, float t0, float t1, float phase,
    int maxSteps, float stepScale, float emScale, inout vec3 accumCol, inout float trans
) {
    if (t1 <= t0 || trans < 0.02) return;
    float t = t0;
    for (int i = 0; i < 60; i++) {
        if (i >= maxSteps || t >= t1 || trans < 0.02) break;
        float dt = (0.068 + 0.025 * t) * stepScale;
        float tSample = t + 0.5 * dt;
        if (tSample > t1) break;
        vec3 p = ro + rd * tSample;

        float temp;
        vec2 dens = evalFlameVolume(p, phase, temp);
        float fd = dens.x;
        float sd = dens.y;

        if (fd > 0.005 || sd > 0.005) {
            float distAtten = exp(-tSample * 0.032);
            float sigma = (fd * 2.35 + sd * 1.35) * dt;
            float stepT = exp(-sigma);
            float weight = trans * (1.0 - stepT);

            if (uLightMode == 1) {
                float g = (fd * 0.48 + sd * 0.16) / max(fd * 2.35 + sd * 1.35, 1e-4);
                accumCol += weight * vec3(g * 1.20) * distAtten;
            } else {
                vec3 fireC = firePalette(temp) * emScale;
                vec3 sootC = vec3(0.032, 0.012, 0.006);
                vec3 scatterCol = (fireC * fd * 2.35 + sootC * sd * 1.35) / max(fd * 2.35 + sd * 1.35, 1e-4);
                accumCol += weight * scatterCol * distAtten;
            }
            trans *= stepT;
        }
        t += dt;
    }
}

void renderFlameCorridors(
    vec3 ro, vec3 rd, float tMax, float phase,
    int maxSteps, float stepScale, float emScale, inout vec3 accumCol, inout float trans
) {
    vec3 invRd = 1.0 / (rd + vec3(1e-6));
    vec2 hL = intersectBox(ro, invRd, vec3(-5.35, 0.0, 1.25), vec3(-1.05, 7.75, 34.0));
    vec2 hR = intersectBox(ro, invRd, vec3( 1.05, 0.0, 1.25), vec3( 5.35, 7.75, 34.0));

    hL.x = max(hL.x, 0.15); hL.y = min(hL.y, tMax);
    hR.x = max(hR.x, 0.15); hR.y = min(hR.y, tMax);

    bool hitL = (hL.y > hL.x);
    bool hitR = (hR.y > hR.x);

    if (hitL && hitR) {
        if (hL.x < hR.x) {
            marchFlameSegment(ro, rd, hL.x, hL.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
            marchFlameSegment(ro, rd, hR.x, hR.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
        } else {
            marchFlameSegment(ro, rd, hR.x, hR.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
            marchFlameSegment(ro, rd, hL.x, hL.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
        }
    } else if (hitL) {
        marchFlameSegment(ro, rd, hL.x, hL.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
    } else if (hitR) {
        marchFlameSegment(ro, rd, hR.x, hR.y, phase, maxSteps, stepScale, emScale, accumCol, trans);
    }
}

// ----------------------------------------------------------------------------
// Crisp Seamless Rising Embers & Organic Windswept Spark Streaks (Zero Arches!)
// ----------------------------------------------------------------------------
vec3 renderEmbers(vec2 uv, float phase) {
    vec3 glow = vec3(0.0);

    // 42 seamless rising glowing embers + windswept directional spark streaks
    for (int i = 0; i < 42; i++) {
        float fi = float(i);
        float seedA = hash11(fi * 19.13 + 1.7);
        float seedB = hash11(fi * 43.71 + 5.3);
        float seedC = hash11(fi * 71.29 + 9.1);
        float seedD = hash11(fi * 97.37 + 3.1);

        float speed = (i < 16) ? 1.0 : 2.0;
        float life = fract(seedA + phase * speed);
        float side = (mod(fi, 2.0) < 0.5) ? -1.0 : 1.0;

        // Organic curved drift rising out of the twin flame rows into the dark sky
        float ex = side * mix(0.10, 1.24, seedB) + 0.11 * sin(TAU * life + fi * 1.7) - side * 0.14 * life;
        float ey = mix(-0.78, 1.02, life);

        vec2 d = uv - vec2(ex, ey);
        float ang = side * (0.28 + 0.35 * seedD) + 0.25 * cos(TAU * life + fi);
        mat2 r = mat2(cos(ang), -sin(ang), sin(ang), cos(ang));
        d = r * d;

        // Mix of pinpoint glowing embers & thin fast windswept spark streaks
        bool isStreak = (i >= 30);
        d.x *= isStreak ? 6.5 : 2.2;

        float sz = isStreak ? mix(0.0045, 0.009, seedC) : mix(0.0032, 0.011, seedC * seedC);
        if (i < 3) sz *= 2.4; // occasional soft foreground bokeh ember
        float dist2 = dot(d, d);
        float fade = sin(PI * life);
        float spark = exp(-dist2 / (sz * sz)) * fade;

        vec3 eCol = mix(vec3(2.4, 0.32, 0.03), vec3(3.4, 1.85, 0.38), seedC);
        glow += eCol * spark * (i < 3 ? 0.50 : 1.05);
    }

    return glow;
}

// ----------------------------------------------------------------------------
// Dynamic Fire Lighting & Shading on Carved Stone & Wet Flagstones
// ----------------------------------------------------------------------------
vec3 shadeSurface(vec3 p, vec3 n, vec3 v, float matId, float phase) {
    float ao = calcAO(p, n);

    vec3 albedo = vec3(0.058, 0.048, 0.044);
    float rough = 0.50;
    float wet = 0.0;
    float f0 = 0.04;

    if (matId < 1.5) {
        // Wet cracked flagstone floor
        FloorInfo fi = evalFloor(p.xz, phase);
        n = fi.normal;
        wet = fi.wetness;
        vec3 stoneCol = vec3(0.052, 0.048, 0.047) * fi.slabTone;
        albedo = mix(stoneCol, vec3(0.011, 0.010, 0.011), fi.grout);
        albedo *= mix(1.0, 0.42, wet);
        rough = mix(0.46, 0.035, wet);
        f0 = mix(0.04, 0.16, wet);
    } else if (matId < 2.5) {
        // Raised curb & heavy stepped stone plinth
        float grain = fbm2(p.xz * 4.5 + p.y * 2.8);
        albedo = vec3(0.048, 0.040, 0.036) * (0.68 + 0.52 * grain);
        rough = 0.44;
    } else if (matId < 3.5) {
        // Sculpted dark obsidian/basalt twisted pillar with charred micro-grain
        float ang = atan(p.z, abs(p.x) - PILLAR_X);
        float grain = fbm2(vec2(ang * 3.0, p.y * 4.2));
        albedo = vec3(0.042, 0.032, 0.028) * (0.65 + 0.58 * grain);
        // Glossy carved basalt ridges catch intense fiery orange-gold specular edges!
        rough = 0.19 + 0.15 * grain;
        f0 = 0.085;
    } else {
        // Low dark outer temple gallery tiers
        float tier = smoothstep(0.45, 0.55, sin(p.y * 5.2));
        albedo = vec3(0.018, 0.016, 0.020) * (0.55 + 0.45 * tier);
        rough = 0.68;
    }

    // Mode 1: Pure neutral grayscale architectural & form study
    if (uLightMode == 1) {
        albedo = vec3(dot(albedo, vec3(0.333))) * 3.4 + vec3(0.08);
        vec3 lKey  = normalize(vec3(0.35, 0.78, -0.52));
        vec3 lFill = normalize(vec3(-0.65, 0.35, -0.45));
        vec3 lRim  = normalize(vec3(0.0, 0.25, 0.97));
        float ndl1 = max(dot(n, lKey), 0.0);
        float ndl2 = max(dot(n, lFill), 0.0);
        vec3 h1 = normalize(lKey + v);
        float spec1 = pow(max(dot(n, h1), 0.0), mix(14.0, 96.0, 1.0 - rough));
        float rim = pow(1.0 - max(dot(n, v), 0.0), 3.0) * max(dot(n, lRim), 0.0);
        float shade = (0.14 + 0.68 * ndl1 + 0.22 * ndl2) * ao + 0.35 * spec1 + 0.22 * rim;
        return vec3(shade) * albedo * 1.45 + vec3(0.035 * ao);
    }

    vec3 col = vec3(0.0);

    // 1. Dynamic fiery illumination from nearest twin pillar flame crests
    float zNearest = max(DZ, round(p.z / DZ) * DZ);
    float surgeNear = flameWave(zNearest, phase);
    float firePower = (0.68 + 1.32 * surgeNear);
    vec3 fireCol = mix(vec3(1.55, 0.38, 0.045), vec3(2.45, 0.98, 0.16), surgeNear);

    for (int s = 0; s < 2; s++) {
        float side = (s == 0) ? -1.0 : 1.0;
        vec3 lPos = vec3(side * (PILLAR_X + 0.22), 3.60 + 0.45 * surgeNear, zNearest);
        vec3 lVec = lPos - p;
        float dist2 = dot(lVec, lVec) + 0.80;
        vec3 lDir = normalize(lVec);
        float atten = firePower * 3.3 / dist2;

        float ndl = max(dot(n, lDir), 0.0);
        vec3 h = normalize(lDir + v);
        float shininess = mix(14.0, 140.0, 1.0 - rough);
        float spec = pow(max(dot(n, h), 0.0), shininess) * (0.28 + 0.72 * (1.0 - rough));
        float fres = f0 + (1.0 - f0) * pow(1.0 - max(dot(n, v), 0.0), 4.0);

        col += (albedo * ndl * 1.15 + vec3(spec * fres * 1.55)) * fireCol * atten;
    }

    // Subtle localized base-flame bounce on plinths & adjacent floor
    {
        float side = (p.x >= 0.0) ? 1.0 : -1.0;
        vec3 bPos = vec3(side * (PILLAR_X - 0.60), 0.24, zNearest);
        vec3 bVec = bPos - p;
        float bDist2 = dot(bVec, bVec) + 0.35;
        vec3 bDir = normalize(bVec);
        float bNdl = max(dot(n, bDir), 0.0);
        col += albedo * bNdl * vec3(1.65, 0.44, 0.05) * (0.42 + 0.48 * surgeNear) / bDist2;
    }

    // 2. Distant corridor golden furnace backlight & razor-sharp fiery rim on twisted pillars
    {
        vec3 fDir = normalize(vec3(0.0, 0.14, 1.0));
        float ndlF = max(dot(n, fDir), 0.0);
        vec3 hF = normalize(fDir + v);
        float specF = pow(max(dot(n, hF), 0.0), mix(18.0, 130.0, 1.0 - rough));
        float fres = f0 + (1.0 - f0) * pow(1.0 - max(dot(n, v), 0.0), 4.0);
        float backBoost = (uLightMode == 3) ? 3.1 : 0.85;
        vec3 furnaceCol = vec3(1.45, 0.68, 0.18) * backBoost;
        col += (albedo * ndlF * 0.32 + vec3(specF * fres * 0.90)) * furnaceCol;

        if (matId > 2.5 && matId < 3.5) {
            // Crisp fiery orange-gold specular rim tracing every carved helical groove
            float rim = pow(1.0 - max(dot(n, v), 0.0), 2.5);
            float rimBoost = (uLightMode == 3) ? 2.55 : 1.35;
            float grooveCatch = 0.42 + 0.58 * max(0.0, n.y * 0.55 + abs(n.x) * 0.45);
            col += fireCol * rim * rimBoost * grooveCatch * (0.48 + 0.52 * surgeNear) * ao;
        }
    }

    // 3. Mode 2: Dramatic grazing directional key light
    if (uLightMode == 2) {
        col *= 0.38;
        vec3 gDir = normalize(vec3(0.88, 0.25, -0.40));
        float gNdl = max(dot(n, gDir), 0.0);
        vec3 gH = normalize(gDir + v);
        float gSpec = pow(max(dot(n, gH), 0.0), mix(18.0, 96.0, 1.0 - rough));
        col += (albedo * gNdl * 2.5 + vec3(gSpec * 0.65)) * vec3(1.55, 0.88, 0.42);
    }

    // 4. Wet flagstone floor real-time volumetric fire reflection (crisp puddle mirrors!)
    if (matId < 1.5 && wet > 0.02) {
        vec3 rDir = reflect(-v, n);
        rDir.y = max(rDir.y, 0.015);
        vec3 reflFire = vec3(0.0);
        float reflT = 1.0;
        float emScale = (uLightMode == 2) ? 0.55 : 1.05;
        renderFlameCorridors(p + n * 0.03, rDir, 26.0, phase, 26, 1.45, emScale, reflFire, reflT);

        float vpAlign = pow(max(dot(rDir, normalize(vec3(0.0, 0.04, 1.0))), 0.0), 16.0);
        vec3 reflBg = vec3(0.008, 0.005, 0.006) + vec3(1.25, 0.55, 0.14) * vpAlign;
        vec3 reflCol = reflFire + reflT * reflBg;

        float fres = f0 + (1.0 - f0) * pow(1.0 - max(dot(n, v), 0.0), 3.2);
        col += reflCol * fres * (0.14 + 1.35 * wet);
    }

    // Deep moody charcoal shadow floor
    vec3 amb = vec3(0.006, 0.0048, 0.0058) * (0.45 + 0.55 * ao);
    col = col * ao + amb;

    return col;
}

// ----------------------------------------------------------------------------
// ACES Filmic Tonemapping (safe headroom: %clip < 1%)
// ----------------------------------------------------------------------------
vec3 acesTonemap(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 = (2.0 * gl_FragCoord.xy - uResolution.xy) / uResolution.y;
    float phase = fract(uTime / PERIOD);

    // Camera setup
    vec3 ro, ta;
    float focal;
    if (uCamMode == 1) {
        // ~2.5x macro close-up on foreground carved twisted pillar, roaring fire tongues & wet flagstones
        ro = vec3(0.28, 1.56, 0.12);
        ta = vec3(2.38, 1.92, 2.68);
        focal = 1.90;
    } else {
        // Hero dramatic one-point perspective down the temple corridor
        ro = vec3(0.0, 1.22, -1.18);
        ta = vec3(0.0, 1.38, 12.0);
        focal = 1.25;
    }

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

    // Primary geometry raymarch
    float t = 0.0;
    float matId = -1.0;
    const float MAX_DIST = 40.0;
    for (int i = 0; i < 72; i++) {
        vec3 p = ro + rd * t;
        vec2 h = mapScene(p);
        if (h.x < 0.0018 * (1.0 + t * 0.25)) {
            matId = h.y;
            break;
        }
        t += h.x * 0.85;
        if (t > MAX_DIST) break;
    }

    // Deep dramatic sooty night sky + tight golden vanishing-point furnace glow
    float vpAlign = max(dot(rd, normalize(vec3(0.0, 0.01, 1.0))), 0.0);
    float vpGlow1 = pow(vpAlign, 20.0);
    float vpGlow2 = pow(vpAlign, 72.0);
    float waveBgPulse = 0.88 + 0.12 * sin(TAU * phase);

    vec3 bgCol;
    if (uLightMode == 1) {
        bgCol = vec3(0.14 + 0.22 * vpGlow1 + 0.14 * smoothstep(-0.3, 0.5, -rd.y));
    } else {
        vec3 skyTop = vec3(0.0035, 0.0030, 0.0040);
        vec3 skyLow = vec3(0.018, 0.008, 0.005);
        bgCol = mix(skyLow, skyTop, smoothstep(-0.02, 0.38, rd.y));

        // Subtle dark wispy smoke clouds catching warm under-glow from the corridor fires
        vec2 skyUV = rd.xy / (rd.z + 0.65);
        float skyN = fbm2(skyUV * 3.6 + vec2(sin(TAU * phase), cos(TAU * phase)) * 0.14);
        bgCol += vec3(0.028, 0.010, 0.004) * skyN * smoothstep(-0.05, 0.32, rd.y) * smoothstep(0.62, 0.08, rd.y);

        // Tight golden-amber furnace mist at the far vanishing point
        float backMult = (uLightMode == 3) ? 1.85 : (uLightMode == 2 ? 0.55 : 1.0);
        bgCol += (vec3(0.75, 0.28, 0.05) * vpGlow1 + vec3(1.65, 0.88, 0.24) * vpGlow2) * waveBgPulse * backMult;
    }

    vec3 surfCol = bgCol;
    float tHit = (matId > 0.0) ? t : MAX_DIST;

    if (matId > 0.0) {
        vec3 p = ro + rd * t;
        vec3 n = calcNormal(p);
        surfCol = shadeSurface(p, n, -rd, matId, phase);

        // Atmospheric golden corridor mist in the far distance
        float fogAmt = 1.0 - exp(-t * t * 0.0018);
        vec3 fogCol = (uLightMode == 1)
            ? vec3(0.22 + 0.16 * vpGlow1)
            : (vec3(0.04, 0.018, 0.008) + vec3(1.25, 0.54, 0.12) * vpGlow1 * waveBgPulse);
        surfCol = mix(surfCol, fogCol, clamp(fogAmt, 0.0, 0.90));
    }

    // Crisp roaring volumetric flame corridors (tight perspective stepping!)
    vec3 flameCol = vec3(0.0);
    float flameTrans = 1.0;
    float emScale = (uLightMode == 2) ? 0.58 : (uLightMode == 3 ? 0.90 : 1.04);
    int volSteps = (uCamMode == 1) ? 56 : 52;
    float stepScale = (uCamMode == 1) ? 0.85 : 1.0;
    renderFlameCorridors(ro, rd, tHit, phase, volSteps, stepScale, emScale, flameCol, flameTrans);

    vec3 col = surfCol * flameTrans + flameCol;

    // Seamless rising embers & windswept spark streaks
    if (uLightMode != 1) {
        float emberScale = (uLightMode == 2) ? 0.55 : 1.0;
        col += renderEmbers(uv, phase) * emberScale;
    }

    // Exposure + ACES filmic tonemapping (%clip < 1%)
    float exposure = (uLightMode == 1) ? 0.92 : 0.74;
    col = acesTonemap(col * exposure);

    // Subtle dark linear floor: guarantees lumMin >= 10, %black = 0%
    float floorLift = (uLightMode == 1) ? 0.008 : 0.0014;
    col = col * 0.98 + floorLift;

    // Subtle cinematic vignette
    float vig = 1.0 - 0.16 * dot(uv * vec2(0.75, 0.85), uv * vec2(0.75, 0.85));
    col *= clamp(vig, 0.76, 1.0);

    // Gamma encode
    col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));

    // Strict R == G == B guarantee for uLightMode == 1
    if (uLightMode == 1) {
        float g = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(g);
    }

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierpropagating-flame-wave-corridor
CategoryGame VFX Shaders · Choreographed Loop VFX
Upstream studyvfx_animated/lib/03_propagating_flame_wave_corridor.glsl (commit f3d2025)
Animation cycle3.0 s seamless harmonic loop
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size866 lines · 34.0 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

Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation; 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 · Jorge Jimenez, Interleaved Gradient Noise

All game vfx shaders