Jet Afterburner Mach Diamonds shader

jet-afterburner-mach-diamonds.glsl · 1063 lines · GLSL ES 3.00 · MIT

Jet Afterburner Mach DiamondsGame VFX
Jet Afterburner Mach Diamonds WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 1063 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
jet-afterburner-mach-diamonds.glsl (1063 lines)
#version 300 es
// Jet Afterburner Mach Diamonds (jet-afterburner-mach-diamonds.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_realistic/lib/09_jet_afterburner_mach_diamonds.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;

// ============================================================================
// 09_jet_afterburner_mach_diamonds.glsl
// Photoreal Top-Gun / aerospace test-stand side-profile fighter-jet afterburner
// exhaust: segmented titanium turkey-feather nozzle entering from the left
// (~28% of frame) with incandescent orange-gold corrugated inner liner, upper
// fuselage tailcone pod, vertical fin root, and swept horizontal stabilizers,
// blasting a tightly collimated supersonic blue-violet plasma plume (~35% frame
// height) with 4-5 razor-sharp standing Mach shock diamonds above a dark night
// concrete runway tarmac (bottom ~22%) with warm-orange & blue bounce light.
//
// Uniforms:
//   - uLightMode: 0=hero night test-stand tarmac, 1=pure neutral grayscale study
//     (R==G==B, muted plume emission), 2=grazing raking key light, 3=backlit rim
//   - uCamMode: 0=hero side-profile framing, 1=~2.5x macro close-up of textured
//     nozzle petals, glowing orange interior, and first two Mach shock diamonds
// ============================================================================

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

// World-space layout (jet axis along +X at y = Y_NOZZLE, z = 0)
const float X_EXIT   = -0.46;
const float Y_NOZZLE =  0.05;
const float R_EXIT   =  0.36;
const float Y_GROUND = -0.94;

// ----------------------------------------------------------------------------
// Hash & Smooth Procedural Noise
// ----------------------------------------------------------------------------
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);
}

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

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

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 4; ++i) {
        v += a * vnoise2(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

float fbm3(vec3 p) {
    float v = 0.0;
    float a = 0.50;
    for (int i = 0; i < 4; ++i) {
        v += a * vnoise3(p);
        p = p * 2.04 + vec3(13.7, 29.3, 7.1);
        a *= 0.48;
    }
    return v;
}

// ----------------------------------------------------------------------------
// SDF Primitives & Operators
// ----------------------------------------------------------------------------
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);
}

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

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

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

// ----------------------------------------------------------------------------
// Aircraft Tail & Segmented Titanium Turkey-Feather Nozzle SDF
// Material IDs:
//   1.0 = Outer titanium turkey-feather nozzle petals & actuator collar
//   2.0 = Inner corrugated incandescent combustion liner & flameholder
//   3.0 = Painted aerospace grey fuselage tailcone pod, fin & stabilizers
// ----------------------------------------------------------------------------
float nozzleOuterRadius(float x, float irisOpen) {
    float t = clamp((x - (-1.42)) / (X_EXIT - (-1.42)), 0.0, 1.0);
    float rBase = 0.47;
    float rTip  = R_EXIT + 0.022 * irisOpen;
    float curve = sin(t * PI) * 0.016;
    return mix(rBase, rTip, pow(t, 1.12)) + curve;
}

vec2 mapScene(vec3 p) {
    float iris = clamp(uOpen, 0.0, 1.0);
    vec2 res = vec2(1e5, 0.0);

    // Local coordinates centered on nozzle axis (y = Y_NOZZLE)
    vec3 pn = vec3(p.x, p.y - Y_NOZZLE, p.z);

    // 1. Segmented Titanium Turkey-Feather Exhaust Nozzle
    //    Clean, Lipschitz-safe rounded hollow cone (no aliased/dashed lip!)
    {
        float r = length(pn.yz);
        float ang = atan(pn.z, pn.y);
        float xClamped = clamp(pn.x, -1.55, X_EXIT);
        float rOut = nozzleOuterRadius(xClamped, iris);
        float wallThick = 0.038;

        // 16 overlapping turkey-feather titanium petals (smooth bounded relief)
        float petalCount = 16.0;
        float sector = TAU / petalCount;
        float localAng = mod(ang + PI, sector) / sector;
        float groove = smoothstep(0.0, 0.14, localAng) * smoothstep(1.0, 0.86, localAng);
        float overlapStep = (localAng - 0.5) * 0.0045;
        // Fade petal relief smoothly right at the trailing bevel to keep lip razor-clean
        float petalMask = smoothstep(-1.26, -1.16, pn.x) * smoothstep(X_EXIT + 0.005, X_EXIT - 0.035, pn.x);
        float petalRelief = petalMask * (0.0075 * groove + overlapStep);

        // Actuator shroud collar band around x in [-1.42, -1.18]
        float collarBand = smoothstep(-1.46, -1.38, pn.x) * smoothstep(-1.14, -1.22, pn.x);
        float lug = smoothstep(0.28, 0.44, localAng) * smoothstep(0.72, 0.56, localAng);
        float collarRelief = collarBand * (0.015 + 0.008 * lug);

        // Forward engine cowling fairing (x < -1.38)
        float cowlBand = smoothstep(-1.34, -1.44, pn.x);
        float rOuterFinal = rOut + petalRelief + collarRelief + cowlBand * 0.022;

        // Inner corrugated cooling liner (32 ribs, fading cleanly before lip)
        float innerMask = smoothstep(-1.45, -1.30, pn.x) * smoothstep(X_EXIT + 0.005, X_EXIT - 0.03, pn.x);
        float innerRib = 0.004 * sin(ang * 32.0) * innerMask;
        float rInnerFinal = rOut - wallThick + innerRib;

        // Smooth rounded hollow shell SDF (prevents grazing lip overshoot)
        float rMid = 0.5 * (rOuterFinal + rInnerFinal);
        float rHalf = 0.5 * (rOuterFinal - rInnerFinal);
        float dRad = abs(r - rMid) - rHalf;
        float dAxial = max(pn.x - X_EXIT, -2.50 - pn.x);
        float bevel = 0.005;
        vec2 w = vec2(dRad + bevel, dAxial + bevel);
        float dShell = (length(max(w, 0.0)) + min(max(w.x, w.y), 0.0) - bevel) * 0.72;

        // Inner combustion chamber backplate & flameholder ring deep inside throat
        float dBack = max(r - (rInnerFinal + 0.02), abs(pn.x + 1.52) - 0.09);
        float dFlameRing = max(abs(r - 0.21) - 0.022, abs(pn.x + 1.28) - 0.04);
        float dCoreCone = length(vec2(r, (pn.x + 1.38) * 0.65)) - 0.11;
        float dInnerParts = min(dBack, min(dFlameRing, dCoreCone));

        // Classify inner liner vs outer titanium cleanly
        float isInner = step(r, rMid - 0.003) * step(pn.x, X_EXIT - 0.006);
        float matNozzle = (isInner > 0.5) ? 2.0 : 1.0;

        res = minMat(res, vec2(dShell, matNozzle));
        res = minMat(res, vec2(dInnerParts, 2.0));
    }

    // 2. Upper Fuselage Tailcone / ECM & Drag-Chute Pod (above nozzle, entering left)
    {
        float dPodMain = sdCapsule(p, vec3(-2.60, 0.76, 0.0), vec3(-0.56, 0.74, 0.0), 0.175);
        float dPodBox = sdBox(p - vec3(-1.55, 0.76, 0.0), vec3(0.95, 0.155, 0.165)) - 0.04;
        float dPod = smin(dPodMain, dPodBox, 0.055);

        // Small upper sensor/vent fairing & rounded aft tailcap
        float dSensor = sdCapsule(p, vec3(-1.60, 0.89, 0.0), vec3(-0.62, 0.89, 0.0), 0.052);
        float dNub = length(p - vec3(-0.39, 0.74, 0.0)) - 0.032;
        dPod = smin(dPod, dSensor, 0.03);
        dPod = min(dPod, dNub);

        // Vertical tail fin root sweeping up from top-left of fuselage pod
        vec3 f = p - vec3(-1.45, 1.26, 0.0);
        f.x -= f.y * 0.45;
        float dFin = sdBox(f, vec3(0.50, 0.42, 0.032)) - 0.012;
        dPod = smin(dPod, dFin, 0.045);

        // Fuselage spine fairing bridging nozzle top to upper pod
        float dSpine = sdBox(p - vec3(-1.82, 0.38, 0.0), vec3(0.68, 0.38, 0.24)) - 0.08;
        dPod = smin(dPod, dSpine, 0.06);

        res = minMat(res, vec2(dPod, 3.0));
    }

    // 3. Swept Horizontal Stabilizers / Ventral Tailplanes (near & far sides)
    {
        vec3 s = vec3(p.x, p.y - Y_NOZZLE, abs(p.z));
        vec3 st = s - vec3(-0.68, -0.25, 0.64);
        st.x -= (s.z - 0.36) * 0.50;
        float angS = -0.09;
        mat2 rS = mat2(cos(angS), -sin(angS), sin(angS), cos(angS));
        st.yz = rS * st.yz;
        float spanT = clamp((s.z - 0.28) / 0.85, 0.0, 1.0);
        float halfChord = mix(0.42, 0.16, spanT);
        float halfThick = mix(0.022, 0.008, spanT);
        float dStab = sdBox(st, vec3(halfChord, halfThick, 0.42)) - 0.006;
        dStab = max(dStab, 0.30 - s.z);

        res = minMat(res, vec2(dStab, 3.0));
    }

    return res;
}

// Unit-sign tetrahedral SDF normal
vec3 calcNormal(vec3 p) {
    const float eps = 0.0014;
    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
    );
}

// Ambient occlusion for SDF aircraft geometry
float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 5; ++i) {
        float h = 0.018 + 0.065 * float(i);
        float d = mapScene(p + n * h).x;
        occ += (h - d) * sca;
        sca *= 0.68;
    }
    return clamp(1.0 - 2.6 * occ, 0.14, 1.0);
}

// Soft shadow against aircraft geometry
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;
        res = min(res, k * h / max(t, 0.001));
        t += clamp(h, 0.02, 0.20);
        if (res < 0.01 || t > maxt) break;
    }
    return clamp(res, 0.0, 1.0);
}

// ----------------------------------------------------------------------------
// PBR Helpers (GGX + Anisotropic Titanium Specular)
// ----------------------------------------------------------------------------
float D_GGX(float NdotH, float alpha) {
    float a2 = alpha * alpha;
    float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
    return a2 / (PI * d * d + 1e-6);
}

float D_GGX_Aniso(float NdotH, float TdotH, float BdotH, float ax, float ay) {
    float d = (TdotH * TdotH) / (ax * ax) + (BdotH * BdotH) / (ay * ay) + NdotH * NdotH;
    return 1.0 / (PI * ax * ay * d * d + 1e-6);
}

float G_SchlickGGX(float NdotV, float k) {
    return NdotV / (NdotV * (1.0 - k) + k + 1e-6);
}

float G_Smith(float NdotV, float NdotL, float rough) {
    float r = rough + 1.0;
    float k = (r * r) * 0.125;
    return G_SchlickGGX(NdotV, k) * G_SchlickGGX(NdotL, k);
}

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

// ----------------------------------------------------------------------------
// Supersonic Compressible Afterburner Plume & Standing Mach Shock Diamonds
// Tightly collimated (~nozzle diameter), wispy turbulent blue-to-violet shear,
// and 4-5 razor-sharp standing Mach shock diamonds (white-hot cores + orange-
// gold tips + crisp oblique shock chevrons).
// ----------------------------------------------------------------------------
void sampleAfterburnerVolume(
    vec3 p,
    float t,
    float iris,
    bool isGray,
    out vec3 emitOut,
    out float sigmaOut
) {
    emitOut = vec3(0.0);
    sigmaOut = 0.0;

    float dx = p.x - (X_EXIT - 0.05);
    if (dx < 0.0 || dx > 2.85) return;

    vec2 pyz = vec2(p.y - Y_NOZZLE, p.z);
    float r = length(pyz);
    float ang = atan(pyz.y, pyz.x);

    // High-frequency supersonic acoustic flutter & gentle 3s cycle breathing
    float pulse = 1.0 + 0.022 * sin(t * 15.0) + 0.015 * sin(t * 29.0 + 1.2);
    float slowBreath = 1.0 + 0.03 * sin(t * TAU / 3.0);
    float throttle = mix(0.95, 1.16, iris) * pulse;

    // 5 periodic standing Mach shock cells across dx in [0, 2.35]
    float cellLen = 0.44 * mix(1.0, 1.06, iris) * slowBreath;
    float phase = dx / cellLen + 0.025 * dx * dx;
    float cellIdx = floor(phase);
    float u = fract(phase); // 0..1 inside each cell

    // Tightly collimated supersonic jet radius (matches nozzle R_EXIT = 0.36)
    float rExitNow = R_EXIT * (0.94 + 0.04 * iris);
    float rJet = rExitNow * mix(1.0, 0.84, clamp(dx / 2.6, 0.0, 1.0))
                 * (1.0 + 0.025 * sin(u * TAU) * exp(-dx * 0.5));

    // Outer shear layer grows gently downstream and is strongly sculpted by FBM wisps
    float rShear = rJet * (1.04 + 0.22 * pow(clamp(dx / 2.4, 0.0, 1.0), 1.05));
    if (r > rShear * 1.45) return;

    // Fast advected turbulent Kelvin-Helmholtz shear wisps (4-oct domain-warped FBM)
    vec3 flowLow  = vec3(p.x * 2.2 - t * 9.5, pyz.x * 5.2, pyz.y * 5.2);
    float nLow    = fbm3(flowLow);
    vec3 flowHigh = vec3(p.x * 5.4 - t * 19.0, pyz.x * 11.5 + (nLow - 0.5) * 1.8, pyz.y * 11.5 + (nLow - 0.5) * 1.8);
    float nHigh   = fbm3(flowHigh);
    float turb    = nLow * 0.58 + nHigh * 0.42;

    // Turbulent edge erosion creates organic wispy shear boundary (no smooth sine outline!)
    float edgeWarp = (turb - 0.48) * 0.20 * smoothstep(0.0, 0.45, dx);

    // Longitudinal supersonic Mach slip-streaks from the 16 nozzle petals
    float slip = 0.5 + 0.5 * sin(ang * 16.0 + nLow * 2.6);

    // Axial envelope
    float entryFade = smoothstep(0.0, 0.05, dx);
    float exitFade  = exp(-pow(max(dx - 0.30, 0.0) / (2.25 * mix(1.0, 1.10, iris)), 1.95));
    float env = entryFade * exitFade;

    // ------------------------------------------------------------------------
    // 1. Standing Mach Shock Diamonds (Cells 0..4) — Razor-Sharp Nodes
    // ------------------------------------------------------------------------
    float cellFade = exp(-cellIdx * 0.22) * smoothstep(5.3, 4.0, cellIdx);
    float cellFlicker = 0.94 + 0.06 * sin(t * 33.0 + cellIdx * 2.3) * cos(t * 21.0 - cellIdx * 1.7);

    // Crisp double-cone Mach diamond node at u = 0.50
    float du = u - 0.50;
    float axScale = (du < 0.0) ? 0.135 : 0.215;
    float radCore = mix(0.115, 0.082, clamp(cellIdx / 4.2, 0.0, 1.0));
    float dDiamond = abs(du) / axScale + r / (radCore * (1.0 + 0.07 * (nHigh - 0.5)));

    // Blazing white-hot compressed Mach diamond core
    float diamondCore = exp(-pow(dDiamond * 1.65, 2.3)) * cellFade * cellFlicker;
    // Ultra-crisp inner Mach disk pinch right at u = 0.50
    float machDisk = exp(-pow(du / 0.042, 2.0) - pow(r / (radCore * 0.48), 2.0)) * cellFade * cellFlicker;

    // Luminous orange-gold / incandescent amber tip & trailing Mach-stem glow
    float duTip = u - 0.535;
    float dTip = abs(duTip) / 0.155 + r / (radCore * 0.92);
    float orangeTip = exp(-pow(dTip * 1.25, 1.9)) * cellFade * (0.88 + 0.28 * smoothstep(0.0, 2.5, cellIdx));

    // Delicate, razor-thin oblique Mach shock cones (< > chevrons)
    float rCone = mix(radCore * 0.45, rJet * 0.86, clamp(abs(du) * 2.15, 0.0, 1.0));
    float shockCone = exp(-pow((r - rCone) / (0.015 + 0.005 * cellIdx), 2.0));
    shockCone *= smoothstep(rJet * 0.96, rJet * 0.52, r) * cellFade * entryFade;

    // ------------------------------------------------------------------------
    // 2. Translucent Blue-to-Violet Turbulent Outer Shear Layer
    // ------------------------------------------------------------------------
    float rNorm = r / max(rShear + edgeWarp, 0.05);
    // Hollow shear boundary ring (brighter along the supersonic mixing edges)
    float shearEdge = exp(-pow((rNorm - 0.78) / 0.28, 2.0));
    float shearCore = exp(-pow(rNorm, 2.4));
    float wispDetail = smoothstep(0.26, 0.80, turb);
    float shearPlume = (shearEdge * 0.92 + shearCore * 0.28) * (0.42 + 0.88 * wispDetail) * env;
    shearPlume *= mix(0.76 + 0.36 * slip, 1.0, clamp(dx / 1.4, 0.0, 1.0));

    // Subtle supersonic inner axial slip-streams near nozzle exit
    float coreStreams = exp(-pow(r / (rJet * 0.78), 2.6)) * (0.45 + 0.55 * slip) * env * 0.42;

    // Incandescent orange-gold throat spill right at nozzle exit (dx < 0.35)
    float throatSpill = exp(-dx * 6.5) * exp(-pow(r / (rExitNow * 0.88), 2.8)) * smoothstep(-0.04, 0.03, dx);

    // ------------------------------------------------------------------------
    // Spectral Coloring (Linear HDR, balanced for crisp night contrast)
    // ------------------------------------------------------------------------
    vec3 colBlueCH    = vec3(0.12, 0.44, 1.00);
    vec3 colViolet    = vec3(0.52, 0.18, 0.98);
    vec3 colCyanShock = vec3(0.48, 0.80, 1.00);
    vec3 colWhiteHot  = vec3(1.00, 0.97, 0.92);
    vec3 colOrangeTip = vec3(1.00, 0.48, 0.07);
    vec3 colThroat    = vec3(1.00, 0.50, 0.09);

    // Smooth blue-to-violet transition downstream & radially outward
    float violetMix = smoothstep(0.25, 1.75, dx) * 0.68 + 0.32 * smoothstep(0.60, 1.10, rNorm);
    vec3 shearCol = mix(colBlueCH, colViolet, clamp(violetMix, 0.0, 1.0));

    vec3 emit = vec3(0.0);
    // Wispy blue-violet shear layer (kept translucent so Mach diamonds pop!)
    emit += shearCol * shearPlume * 0.92 * throttle;
    // Subtle supersonic inner core slip-streams
    emit += mix(colBlueCH, colCyanShock, 0.5) * coreStreams * 0.48 * throttle;
    // Thin crisp oblique Mach shock chevrons
    emit += mix(colCyanShock, colViolet, 0.25) * shockCone * 1.45 * throttle;
    // Blazing Mach diamond cores & Mach disks
    emit += colWhiteHot * (diamondCore * 3.20 + machDisk * 3.80) * throttle;
    // Vivid orange-gold diamond tips & Mach-stem compression halos
    emit += colOrangeTip * orangeTip * 2.50 * throttle;
    // Throat orange-gold spill
    emit += colThroat * throatSpill * 2.10 * throttle;

    float density = shearPlume * 0.25 + shockCone * 0.35 + diamondCore * 0.55;

    if (isGray) {
        // Grayscale study: muted neutral shock-cell density structure
        float structVal = shockCone * 0.85 + diamondCore * 1.25 + shearPlume * 0.32 + coreStreams * 0.20;
        emitOut = vec3(structVal * 0.62);
        sigmaOut = density * 0.72;
    } else {
        emitOut = emit;
        sigmaOut = density * 0.22;
    }
}

// ----------------------------------------------------------------------------
// Ray-Cylinder Bounding Intersection for Tight Plume Volume
// Plume lives in x in [X_EXIT - 0.06, 2.40], radial r <= 0.66 around y = Y_NOZZLE
// ----------------------------------------------------------------------------
vec2 intersectPlumeBounds(vec3 ro, vec3 rd) {
    vec3 ron = vec3(ro.x, ro.y - Y_NOZZLE, ro.z);

    float invX = 1.0 / (abs(rd.x) < 1e-5 ? 1e-5 : rd.x);
    float tx0 = (X_EXIT - 0.06 - ron.x) * invX;
    float tx1 = (2.40 - ron.x) * invX;
    float tMinX = min(tx0, tx1);
    float tMaxX = max(tx0, tx1);

    float R = 0.66;
    float a = dot(rd.yz, rd.yz);
    float b = dot(ron.yz, rd.yz);
    float c = dot(ron.yz, ron.yz) - R * R;

    float tMinC = -1e5;
    float tMaxC =  1e5;
    if (a > 1e-6) {
        float disc = b * b - a * c;
        if (disc < 0.0) return vec2(1.0, -1.0);
        float s = sqrt(disc);
        tMinC = (-b - s) / a;
        tMaxC = (-b + s) / a;
    } else if (c > 0.0) {
        return vec2(1.0, -1.0);
    }

    float tNear = max(max(tMinX, tMinC), 0.04);
    float tFar  = min(tMaxX, tMaxC);
    return vec2(tNear, tFar);
}

// ----------------------------------------------------------------------------
// Background Night Aerospace Test-Stand & Dark Hangar Truss Silhouette
// Calibrated for lumMin ~ 6-12 (<2% black) and dramatic night contrast.
// ----------------------------------------------------------------------------
vec3 renderBackground(vec3 rd, vec2 uvShimmer, int lightMode) {
    vec3 rds = normalize(rd + vec3(0.0, uvShimmer.y, uvShimmer.x));

    float elev = clamp(rds.y * 0.5 + 0.5, 0.0, 1.0);
    float horiz = smoothstep(-0.04, 0.28, rds.y);

    // Deep midnight test-stand night sky (lumMin ~ 6-12 in sRGB)
    vec3 skyLow  = vec3(0.0042, 0.0058, 0.0105);
    vec3 skyHigh = vec3(0.0014, 0.0019, 0.0036);
    vec3 col = mix(skyLow, skyHigh, pow(horiz, 0.72));

    if (lightMode == 2) {
        col = mix(vec3(0.0065, 0.0070, 0.0105), vec3(0.0020, 0.0025, 0.0045), pow(horiz, 0.8));
    } else if (lightMode == 3) {
        vec3 rimDir = normalize(vec3(0.45, 0.35, -0.82));
        float rimHalo = pow(max(dot(rds, rimDir), 0.0), 7.0);
        col += vec3(0.055, 0.085, 0.155) * rimHalo;
    }

    // Distant steel hangar trusses & blast-bay columns
    float az = atan(rds.x, -rds.z);
    float trussX = fract(az * 8.0 + 0.32);
    float trussY = fract(rds.y * 15.0);
    float vBeam = smoothstep(0.085, 0.025, abs(trussX - 0.5));
    float hBeam = smoothstep(0.065, 0.02, abs(trussY - 0.5));
    float diagBeam = smoothstep(0.055, 0.015, abs(fract(az * 8.0 + rds.y * 7.5) - 0.5));
    float hangarMask = smoothstep(0.38, 0.16, rds.y) * smoothstep(-0.02, 0.03, rds.y);
    float truss = max(vBeam, max(hBeam * 0.65, diagBeam * 0.40)) * hangarMask;

    // Subtle exhaust bounce on distant hangar trusses behind the plume
    float plumeAlign = exp(-pow(rds.y - 0.02, 2.0) * 28.0) * smoothstep(-0.45, 0.55, rds.x);
    vec3 trussCol = vec3(0.0012, 0.0015, 0.0028) + vec3(0.006, 0.008, 0.022) * plumeAlign;
    col = mix(col, trussCol, truss * 0.65);

    // Subtle distant hangar bay lights near the horizon
    float bayRow = smoothstep(0.022, 0.004, abs(rds.y - 0.025));
    float bayCell = smoothstep(0.24, 0.06, abs(fract(az * 4.5 + 0.18) - 0.5));
    float bayLight = bayRow * bayCell * smoothstep(-0.25, 0.5, rds.x);
    col += vec3(0.024, 0.020, 0.016) * bayLight;

    // Subtle red aviation obstruction beacon high in the dark truss (like 09.png)
    vec3 beaconDir = normalize(vec3(-0.08, 0.165, -0.983));
    float bDot = max(dot(rds, beaconDir), 0.0);
    float beaconCore = pow(bDot, 5500.0);
    float beaconHalo = pow(bDot, 260.0);
    float beaconPulse = 0.85 + 0.15 * sin(uTime * TAU / 3.0);
    col += vec3(1.0, 0.14, 0.04) * (beaconCore * 1.2 + beaconHalo * 0.055) * beaconPulse;

    if (lightMode == 1) {
        float bgLum = mix(0.095, 0.045, pow(elev, 0.7));
        bgLum = mix(bgLum, bgLum * 0.78, truss * 0.35);
        return vec3(bgLum);
    }

    return col;
}

// ----------------------------------------------------------------------------
// Runway Tarmac Floor Shading (Analytic Plane y = Y_GROUND, bottom ~22%)
// Crisp concrete slab seams, double yellow taxiway stripe, grounded shadow,
// and warm orange-gold + cool blue-violet reflection under the plume.
// ----------------------------------------------------------------------------
vec3 shadeTarmac(vec3 p, vec3 V, int lightMode, float iris) {
    vec3 N = vec3(0.0, 1.0, 0.0);

    vec2 gUV = p.xz;
    float slabX = smoothstep(0.020, 0.004, abs(fract(gUV.x * 0.52 + 0.15) - 0.5));
    float slabZ = smoothstep(0.022, 0.005, abs(fract(gUV.y * 0.45 + 0.10) - 0.5));
    float joint = max(slabX, slabZ);

    float macroTex = fbm2(gUV * 0.90);
    float microTex = vnoise2(gUV * 20.0);
    float grain    = vnoise2(gUV * 58.0);

    float bumpX = vnoise2(gUV * 26.0 + vec2(0.03, 0.0)) - vnoise2(gUV * 26.0 - vec2(0.03, 0.0));
    float bumpZ = vnoise2(gUV * 26.0 + vec2(0.0, 0.03)) - vnoise2(gUV * 26.0 - vec2(0.0, 0.03));
    N = normalize(vec3(bumpX * 0.16, 1.0, bumpZ * 0.16));

    // Painted yellow-ochre double taxiway stripe (echoing 09.png)
    float stripeCoord = p.z - 0.75 - 0.05 * p.x;
    float stripeA = smoothstep(0.032, 0.018, abs(stripeCoord));
    float stripeB = smoothstep(0.032, 0.018, abs(stripeCoord - 0.095));
    float stripeFar = smoothstep(0.045, 0.024, abs(p.z + 1.35 - 0.12 * p.x));
    float paintWear = smoothstep(0.22, 0.78, macroTex * 0.65 + microTex * 0.35);
    float yellowMask = clamp((stripeA + stripeB + stripeFar * 0.85) * (0.68 + 0.32 * paintWear), 0.0, 1.0);

    // Concrete runway albedo
    vec3 concreteCol = mix(vec3(0.085, 0.086, 0.090), vec3(0.145, 0.140, 0.132), macroTex);
    concreteCol *= (0.86 + 0.24 * microTex) * (0.93 + 0.10 * grain);
    concreteCol *= (1.0 - 0.62 * joint);

    float skid = smoothstep(0.55, 0.85, fbm2(vec2(p.x * 0.3, p.z * 2.4)));
    concreteCol *= (1.0 - 0.20 * skid);

    vec3 paintCol = vec3(0.56, 0.36, 0.05) * (0.85 + 0.25 * microTex);
    vec3 albedo = mix(concreteCol, paintCol, yellowMask);

    float rough = clamp(mix(0.56, 0.36, skid) - 0.10 * (1.0 - microTex), 0.24, 0.76);
    vec3 F0 = vec3(0.04);

    if (lightMode == 1) {
        float lumAlb = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
        albedo = vec3(lumAlb * 1.15);
    }

    vec3 col = vec3(0.0);

    // 1. Key Light (Test-stand overhead floodlight or mode-specific key)
    vec3 Lkey = normalize(vec3(-0.32, 0.82, 0.48));
    vec3 keyCol = vec3(0.30, 0.33, 0.40);
    if (lightMode == 1) {
        Lkey = normalize(vec3(0.35, 0.78, 0.52));
        keyCol = vec3(0.95);
    } else if (lightMode == 2) {
        Lkey = normalize(vec3(-0.82, 0.24, 0.52));
        keyCol = vec3(0.88, 0.80, 0.68);
    } else if (lightMode == 3) {
        Lkey = normalize(vec3(0.45, 0.42, -0.78));
        keyCol = vec3(0.60, 0.72, 0.94);
    }

    float NdotV = max(dot(N, V), 0.001);
    float NdotL = max(dot(N, Lkey), 0.0);
    float sh = calcSoftShadow(p + N * 0.018, Lkey, 0.06, 5.5, 5.5);
    vec3 H = normalize(V + Lkey);
    vec3 F = F_Schlick(max(dot(V, H), 0.0), F0);
    float D = D_GGX(max(dot(N, H), 0.0), rough * rough);
    float G = G_Smith(NdotV, NdotL, rough);
    vec3 spec = (D * G * F) / (4.0 * NdotV * NdotL + 0.001);
    vec3 diff = (1.0 - F) * albedo / PI;
    col += (diff + spec) * keyCol * NdotL * sh;

    // 2. Ambient night fill
    vec3 ambCol = (lightMode == 1) ? vec3(0.14) : vec3(0.018, 0.022, 0.034);
    col += albedo * ambCol;

    // 3. Focused Warm-Orange & Cool Blue-Violet Under-Plume Bounce on Tarmac
    if (lightMode != 1) {
        float flicker = 0.96 + 0.04 * sin(uTime * 23.0);
        float throttle = mix(0.95, 1.15, iris) * flicker;

        // Warm orange-gold reflection directly beneath nozzle throat & early plume
        vec3 pThroat = vec3(X_EXIT + 0.22, Y_NOZZLE, 0.0);
        vec3 toT = pThroat - p;
        float distT = length(toT);
        vec3 Lt = toT / max(distT, 0.001);
        float NdotLt = max(dot(N, Lt), 0.0);
        float attenT = 1.0 / (1.0 + 0.85 * distT * distT);
        float coneT = smoothstep(-0.40, 0.35, -Lt.x);
        vec3 Ht = normalize(V + Lt);
        vec3 specT = D_GGX(max(dot(N, Ht), 0.0), rough * rough) * G_Smith(NdotV, NdotLt, rough)
                   * F_Schlick(max(dot(V, Ht), 0.0), F0) / (4.0 * NdotV * NdotLt + 0.001);
        vec3 throatLight = vec3(1.00, 0.44, 0.09) * 1.15 * throttle * attenT * (0.30 + 0.70 * coneT);
        col += (albedo / PI + specT * 1.4) * throatLight * NdotLt;

        // Subtle blue-violet + warm-gold Mach-diamond ripple reflection along tarmac
        float xClamped = clamp(p.x, X_EXIT + 0.08, 1.75);
        vec3 pPlume = vec3(xClamped, Y_NOZZLE, 0.0);
        vec3 toP = pPlume - p;
        float distP = length(toP);
        vec3 Lp = toP / max(distP, 0.001);
        float NdotLp = max(dot(N, Lp), 0.0);
        float attenP = 1.0 / (1.0 + 0.75 * distP * distP);
        float diamondWave = 0.5 + 0.5 * cos((xClamped - X_EXIT) * TAU / 0.44 - PI);
        vec3 plumeCol = mix(vec3(0.24, 0.40, 1.00), vec3(1.00, 0.52, 0.16), 0.48 * diamondWave);
        vec3 Hp = normalize(V + Lp);
        vec3 specP = D_GGX(max(dot(N, Hp), 0.0), rough * rough) * G_Smith(NdotV, NdotLp, rough)
                   * F_Schlick(max(dot(V, Hp), 0.0), F0) / (4.0 * NdotV * NdotLp + 0.001);
        col += (albedo / PI + specP * 1.35) * plumeCol * 0.78 * throttle * attenP * NdotLp;
    }

    // Gentle distance fog into dark night horizon
    float distCam = length(p.xz);
    float fog = 1.0 - exp(-pow(max(distCam - 5.2, 0.0) * 0.11, 1.6));
    vec3 fogCol = (lightMode == 1) ? vec3(0.08) : vec3(0.0045, 0.0060, 0.0105);
    col = mix(col, fogCol, clamp(fog, 0.0, 0.90));

    return col;
}

// ----------------------------------------------------------------------------
// Aircraft Surface Shading (Anisotropic Titanium Nozzle, Glowing Liner, Tail)
// ----------------------------------------------------------------------------
vec3 shadeAircraft(vec3 p, vec3 N, vec3 V, float matId, int lightMode, float iris) {
    float ao = calcAO(p, N);
    vec3 pn = vec3(p.x, p.y - Y_NOZZLE, p.z);
    float ang = atan(pn.z, pn.y);
    float NdotV = max(dot(N, V), 0.001);

    vec3 albedo = vec3(0.30, 0.33, 0.37);
    float metallic = 0.0;
    float rough = 0.44;
    vec3 emission = vec3(0.0);
    bool useAniso = false;

    if (matId < 1.5) {
        // Material 1: Segmented Titanium Turkey-Feather Exhaust Nozzle
        useAniso = true;
        metallic = 0.88;
        float tAxial = clamp((pn.x - (-1.42)) / (X_EXIT - (-1.42)), 0.0, 1.0);
        float petalSector = mod(ang + PI, TAU / 16.0) / (TAU / 16.0);
        float petalId = floor((ang + PI) / (TAU / 16.0));
        float petalRand = hash11(petalId * 13.7 + 4.1);

        float brush  = fbm2(vec2(pn.x * 3.8, ang * 44.0));
        float patina = fbm2(vec2(pn.x * 8.0 + petalRand * 5.0, ang * 10.0));

        vec3 tiBase = mix(vec3(0.28, 0.26, 0.24), vec3(0.46, 0.43, 0.39), brush * 0.65 + petalRand * 0.35);

        // Thermal oxide tempering iridescence bands (straw -> bronze -> violet near lip)
        float heatBand = tAxial + (patina - 0.5) * 0.16;
        vec3 oxStraw  = vec3(0.58, 0.44, 0.24);
        vec3 oxBronze = vec3(0.44, 0.24, 0.16);
        vec3 oxViolet = vec3(0.24, 0.24, 0.42);
        vec3 tiTinted = mix(tiBase, oxStraw, smoothstep(0.22, 0.54, heatBand) * 0.48);
        tiTinted = mix(tiTinted, oxBronze, smoothstep(0.52, 0.78, heatBand) * 0.52);
        tiTinted = mix(tiTinted, oxViolet, smoothstep(0.72, 0.94, heatBand) * 0.42);

        // Darkened inter-petal seams & carbon soot near trailing edge
        float grooveDark = smoothstep(0.0, 0.12, petalSector) * smoothstep(1.0, 0.88, petalSector);
        float lipSoot = smoothstep(0.82, 1.0, tAxial) * (0.50 + 0.50 * patina);
        albedo = tiTinted * mix(0.36, 1.0, grooveDark) * (1.0 - 0.42 * lipSoot);

        // Rivet line along actuator collar
        float rivetBand = smoothstep(0.025, 0.008, abs(pn.x + 1.22));
        float rivetDot  = smoothstep(0.18, 0.08, abs(fract(petalSector * 3.0) - 0.5));
        albedo *= (1.0 - 0.35 * rivetBand * rivetDot);

        rough = clamp(0.24 + 0.22 * patina + 0.18 * lipSoot - 0.06 * brush, 0.16, 0.64);

        // Clean warm incandescent bevel glow right on the lip edge
        float lipHot = smoothstep(X_EXIT - 0.025, X_EXIT, pn.x) * smoothstep(-0.2, 0.6, N.x);
        if (lightMode != 1) {
            emission += vec3(1.0, 0.44, 0.08) * lipHot * 0.72 * mix(0.92, 1.15, iris);
        }
    } else if (matId < 2.5) {
        // Material 2: Inner Corrugated Incandescent Combustion Liner
        metallic = 0.60;
        rough = 0.40;
        albedo = vec3(0.20, 0.15, 0.12);

        float depth = clamp((X_EXIT - pn.x) / 0.95, 0.0, 1.0);
        float corrug = 0.5 + 0.5 * sin(ang * 32.0);
        float linerRings = 0.5 + 0.5 * sin(pn.x * 38.0);
        float flicker = 0.95 + 0.05 * sin(uTime * 27.0 + pn.x * 10.0 + ang * 3.0);

        vec3 colLip  = vec3(0.95, 0.36, 0.04);
        vec3 colMid  = vec3(1.00, 0.64, 0.14);
        vec3 colDeep = vec3(1.00, 0.90, 0.56);
        vec3 glowCol = mix(colLip, colMid, smoothstep(0.0, 0.48, depth));
        glowCol = mix(glowCol, colDeep, smoothstep(0.45, 0.92, depth));

        // Subtle cool blue-violet plasma reflection right at lip interior
        float lipBlue = smoothstep(0.14, 0.0, depth);
        glowCol = mix(glowCol, vec3(0.55, 0.65, 1.00), lipBlue * 0.24);

        float ribMod = 0.74 + 0.26 * corrug;
        float ringMod = 0.86 + 0.14 * linerRings;
        float intensity = mix(0.95, 2.25, pow(depth, 0.75)) * ribMod * ringMod * flicker * mix(0.95, 1.16, iris);

        if (lightMode == 1) {
            emission = vec3(0.20 + 0.22 * depth * ribMod);
            albedo = vec3(0.32);
        } else {
            emission = glowCol * intensity;
        }
    } else {
        // Material 3: Tactical Aerospace Grey Fuselage Tailcone, Fin & Stabilizers
        metallic = 0.20;
        float panelNoise = fbm2(p.xy * 4.5 + p.z * 2.5);
        float micro = vnoise2(p.xy * 32.0);

        float seamX = smoothstep(0.010, 0.0025, abs(fract(p.x * 1.9 + 0.1) - 0.5));
        float seamY = smoothstep(0.010, 0.0025, abs(fract(p.y * 2.4) - 0.5));
        float seam = max(seamX, seamY);

        vec3 paintBase = mix(vec3(0.28, 0.31, 0.35), vec3(0.36, 0.39, 0.44), panelNoise);
        paintBase *= (0.94 + 0.10 * micro) * (1.0 - 0.42 * seam);

        albedo = paintBase;
        rough = clamp(0.38 + 0.14 * panelNoise + 0.15 * seam, 0.24, 0.64);
    }

    if (lightMode == 1) {
        float lumA = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
        albedo = vec3(lumA * 1.15);
    }

    vec3 F0 = mix(vec3(0.04), albedo, metallic);
    vec3 col = vec3(0.0);

    vec3 T = normalize(vec3(1.0, 0.0, 0.0) - N * N.x);
    vec3 B = normalize(cross(N, T));

    // 1. Primary Key Light
    vec3 Lkey = normalize(vec3(-0.25, 0.80, 0.54));
    vec3 keyCol = vec3(0.46, 0.52, 0.64);
    if (lightMode == 1) {
        Lkey = normalize(vec3(0.35, 0.78, 0.52));
        keyCol = vec3(1.25);
    } else if (lightMode == 2) {
        Lkey = normalize(vec3(-0.85, 0.25, 0.46));
        keyCol = vec3(1.40, 1.25, 1.05);
    } else if (lightMode == 3) {
        Lkey = normalize(vec3(0.55, 0.42, -0.72));
        keyCol = vec3(1.35, 1.50, 1.85);
    }

    {
        float NdotL = max(dot(N, Lkey), 0.0);
        float sh = calcSoftShadow(p + N * 0.012, Lkey, 0.03, 3.0, 6.0);
        vec3 H = normalize(V + Lkey);
        vec3 F = F_Schlick(max(dot(V, H), 0.0), F0);
        float D = useAniso
            ? D_GGX_Aniso(max(dot(N, H), 0.0), dot(T, H), dot(B, H), rough * rough * 0.52, rough * rough * 1.65)
            : D_GGX(max(dot(N, H), 0.0), rough * rough);
        float G = G_Smith(NdotV, NdotL, rough);
        vec3 spec = (D * G * F) / (4.0 * NdotV * NdotL + 0.001);
        vec3 diff = (1.0 - F) * (1.0 - metallic) * albedo / PI;
        col += (diff + spec) * keyCol * NdotL * sh;
    }

    // 2. Secondary Cool Fill Light
    {
        vec3 Lfill = normalize(vec3(-0.55, 0.22, 0.80));
        vec3 fillCol = (lightMode == 1) ? vec3(0.42) : vec3(0.11, 0.14, 0.22);
        if (lightMode == 3) fillCol = vec3(0.05, 0.07, 0.12);
        float NdotL = max(dot(N, Lfill), 0.0);
        vec3 H = normalize(V + Lfill);
        vec3 F = F_Schlick(max(dot(V, H), 0.0), F0);
        float D = D_GGX(max(dot(N, H), 0.0), rough * rough);
        float G = G_Smith(NdotV, NdotL, rough);
        vec3 spec = (D * G * F) / (4.0 * NdotV * NdotL + 0.001);
        vec3 diff = (1.0 - F) * (1.0 - metallic) * albedo / PI;
        col += (diff + spec) * fillCol * NdotL * ao;
    }

    // 3. Afterburner Throat & Plume Bounce Light on Aircraft Tail & Petals
    if (lightMode != 1) {
        float flicker = 0.96 + 0.04 * sin(uTime * 23.0);
        float throttle = mix(0.95, 1.15, iris) * flicker;

        // Warm orange-gold bounce onto underside of upper tailcone pod & stabilizers
        vec3 pBurn = vec3(X_EXIT + 0.14, Y_NOZZLE, 0.0);
        vec3 toBurn = pBurn - p;
        float dB = length(toBurn);
        vec3 Lb = toBurn / max(dB, 0.001);
        float NdotLb = max(dot(N, Lb), 0.0);
        float attenB = 1.0 / (1.0 + 1.10 * dB * dB);
        vec3 Hb = normalize(V + Lb);
        vec3 Fb = F_Schlick(max(dot(V, Hb), 0.0), F0);
        float Db = D_GGX(max(dot(N, Hb), 0.0), rough * rough);
        float Gb = G_Smith(NdotV, NdotLb, rough);
        vec3 specB = (Db * Gb * Fb) / (4.0 * NdotV * NdotLb + 0.001);
        vec3 diffB = (1.0 - Fb) * (1.0 - metallic) * albedo / PI;
        col += (diffB + specB * 1.2) * vec3(1.00, 0.45, 0.08) * 1.85 * throttle * attenB * NdotLb;

        // Blue-violet bounce from Mach plume onto nozzle petals & stabilizers
        vec3 pJet = vec3(X_EXIT + 0.72, Y_NOZZLE, 0.0);
        vec3 toJet = pJet - p;
        float dJ = length(toJet);
        vec3 Lj = toJet / max(dJ, 0.001);
        float NdotLj = max(dot(N, Lj), 0.0);
        float attenJ = 1.0 / (1.0 + 0.75 * dJ * dJ);
        vec3 Hj = normalize(V + Lj);
        vec3 Fj = F_Schlick(max(dot(V, Hj), 0.0), F0);
        float Dj = D_GGX(max(dot(N, Hj), 0.0), rough * rough);
        float Gj = G_Smith(NdotV, NdotLj, rough);
        vec3 specJ = (Dj * Gj * Fj) / (4.0 * NdotV * NdotLj + 0.001);
        vec3 diffJ = (1.0 - Fj) * (1.0 - metallic) * albedo / PI;
        col += (diffJ + specJ * 1.3) * vec3(0.32, 0.48, 1.00) * 1.35 * throttle * attenJ * NdotLj;
    }

    // 4. Environment Reflection & Subtle Hemisphere Ambient
    vec3 R = reflect(-V, N);
    float fres = pow(1.0 - NdotV, 5.0);
    vec3 envSky = mix(vec3(0.015, 0.022, 0.038), vec3(0.075, 0.105, 0.165), smoothstep(-0.2, 0.7, R.y));
    if (lightMode == 1) {
        envSky = vec3(mix(0.14, 0.34, smoothstep(-0.2, 0.8, R.y)));
    } else if (lightMode == 3) {
        envSky += vec3(0.30, 0.40, 0.62) * fres * 1.6;
    }
    vec3 envGround = (lightMode == 1) ? vec3(0.10) : vec3(0.045, 0.034, 0.028);
    vec3 envCol = mix(envGround, envSky, smoothstep(-0.15, 0.15, R.y));
    col += envCol * mix(F0, vec3(1.0), fres * (1.0 - rough)) * (1.0 - rough * 0.65) * ao;
    col += albedo * (1.0 - metallic) * ((lightMode == 1) ? vec3(0.14) : vec3(0.024, 0.030, 0.046)) * ao;

    col += emission;
    return col;
}

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

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

    float iris = clamp(uOpen, 0.0, 1.0);
    bool isGray = (uLightMode == 1);

    // Camera setup:
    //   uCamMode == 0: Hero side-profile (nozzle left ~28%, collimated plume ~35%
    //     height with 4-5 Mach diamonds, tarmac cleanly in bottom ~22%)
    //   uCamMode == 1: ~2.5x macro close-up on textured titanium nozzle petals,
    //     glowing orange inner liner, and first two crisp Mach shock diamonds
    vec3 camPos;
    vec3 camTarget;
    float focalLen;

    if (uCamMode == 1) {
        camPos    = vec3(0.42, 0.06, 2.28);
        camTarget = vec3(-0.26, 0.05, 0.0);
        focalLen  = 2.15;
    } else {
        camPos    = vec3(1.15, -0.02, 5.20);
        camTarget = vec3(0.18, -0.14, 0.0);
        focalLen  = 1.88;
    }

    vec3 cw = normalize(camTarget - camPos);
    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 + focalLen * cw);

    // 1. Raymarch Aircraft SDF
    float tHit = 1e5;
    float matHit = 0.0;
    {
        float t = 0.05;
        for (int i = 0; i < 96; ++i) {
            vec3 p = camPos + rd * t;
            vec2 h = mapScene(p);
            if (h.x < 0.0009) {
                tHit = t;
                matHit = h.y;
                break;
            }
            t += h.x;
            if (t > 16.0) break;
        }
    }

    // 2. Analytic Intersection with Runway Tarmac Plane (y = Y_GROUND)
    float tGround = 1e5;
    if (rd.y < -0.0005) {
        float tg = (Y_GROUND - camPos.y) / rd.y;
        if (tg > 0.05 && tg < 48.0) {
            tGround = tg;
        }
    }

    float tScene = min(tHit, tGround);

    // 3. Subtle Thermal Heat-Shimmer Refraction Behind the Plume
    vec2 bounds = intersectPlumeBounds(camPos, rd);
    vec2 heatShimmer = vec2(0.0);
    if (bounds.x < bounds.y && bounds.x < tScene) {
        float tMid = 0.5 * (bounds.x + min(bounds.y, tScene));
        vec3 pMid = camPos + rd * tMid;
        float dxM = pMid.x - X_EXIT;
        if (dxM > -0.04 && dxM < 2.4) {
            float rM = length(vec2(pMid.y - Y_NOZZLE, pMid.z));
            float shimmerEnv = exp(-pow(rM / 0.52, 2.0)) * smoothstep(0.0, 0.2, dxM) * smoothstep(2.4, 0.8, dxM);
            float sx = vnoise2(vec2(pMid.x * 9.0 - uTime * 24.0, pMid.y * 12.0)) - 0.5;
            float sy = vnoise2(vec2(pMid.x * 9.0 - uTime * 24.0 + 13.7, pMid.y * 12.0 + 5.3)) - 0.5;
            heatShimmer = vec2(sx, sy) * 0.014 * shimmerEnv;
        }
    }

    // 4. Shade Opaque Scene (Aircraft, Runway Tarmac, or Dark Night Hangar)
    vec3 col = vec3(0.0);
    if (tHit < tGround && tHit < 16.0) {
        vec3 p = camPos + rd * tHit;
        vec3 N = calcNormal(p);
        col = shadeAircraft(p, N, -rd, matHit, uLightMode, iris);
    } else if (tGround < 48.0) {
        vec3 p = camPos + rd * tGround;
        col = shadeTarmac(p, -rd, uLightMode, iris);
    } else {
        col = renderBackground(rd, heatShimmer, uLightMode);
    }

    // 5. Volumetric Supersonic Exhaust Plume & Mach Shock Diamonds (56 steps)
    if (bounds.x < bounds.y && bounds.x < tScene) {
        float tStart = bounds.x;
        float tEnd   = min(bounds.y, tScene);
        const int STEPS = 56;
        float dt = (tEnd - tStart) / float(STEPS);

        float dither = hash12(fragCoord + fract(uTime * 17.3) * 100.0);
        float t = tStart + dt * dither;

        vec3 accumEmit = vec3(0.0);
        float transmittance = 1.0;

        for (int i = 0; i < STEPS; ++i) {
            vec3 p = camPos + rd * t;
            vec3 emit;
            float sigma;
            sampleAfterburnerVolume(p, uTime, iris, isGray, emit, sigma);

            if (sigma > 1e-4 || dot(emit, emit) > 1e-6) {
                float stepAtten = exp(-sigma * dt);
                accumEmit += transmittance * emit * dt;
                transmittance *= stepAtten;
                if (transmittance < 0.02) break;
            }
            t += dt;
        }

        col = col * transmittance + accumEmit;
    }

    // 6. Exposure, ACES Filmic Tonemapping, & Safe Dynamic Range Calibration
    //    Calibrated for lumMin ~ 6-12 (<2% black) and %clip < 1%
    float exposure = isGray ? 1.05 : 0.96;
    col = acesFilm(col * exposure);

    // Gentle highlight shoulder rolloff keeps Mach cores glowing without clipping
    col = col / (1.0 + 0.022 * col);

    // Subtle dark floor keeps night shadows cleanly above pure black (lumMin >= 6)
    col = max(col, vec3(0.0008));

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

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

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierjet-afterburner-mach-diamonds
CategoryGame VFX Shaders · Physically-Based VFX
Upstream studyvfx_realistic/lib/09_jet_afterburner_mach_diamonds.glsl (commit afde0bc)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size1063 lines · 43.3 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT scifi 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

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