Spiraling Water Dragon Vortex shader

spiraling-water-dragon-vortex.glsl · 994 lines · GLSL ES 3.00 · MIT

Spiraling Water Dragon VortexGame VFX
Spiraling Water Dragon Vortex WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 994 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
spiraling-water-dragon-vortex.glsl (994 lines)
#version 300 es
// Spiraling Water Dragon Vortex (spiraling-water-dragon-vortex.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_animated/lib/08_spiraling_water_dragon_vortex.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;

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

// -----------------------------------------------------------------------------
// Math, Hash & Turbulent 3D / 2D Noise Helpers (100% NaN-safe, zero textures)
// -----------------------------------------------------------------------------
float sq(float x) {
    return x * x;
}

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

// Fast 3-octave turbulent liquid fBM
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.03 + vec2(11.3, 27.9);
        a *= 0.5;
    }
    return v;
}

float fbm3(vec3 p) {
    float v = 0.0;
    float a = 0.5;
    for (int i = 0; i < 3; i++) {
        v += a * vnoise3(p);
        p = p * 2.05 + vec3(17.1, 31.7, 13.3);
        a *= 0.5;
    }
    return v;
}

// Periodic liquid turbulence on cylindrical/helical coordinates (100% seamless in phase!)
// Uses 2D torus-mapped trig basis so there are zero seams at ang = +/-PI or phase = 0/1
float fluidTurbulence(float sMat, float phi, float y) {
    float a1 = TAU * sMat;
    float a2 = phi;
    float n = 0.0;
    n += 0.45 * sin(a1 * 3.0 + a2 * 2.0 + sin(a1 * 2.0 - a2));
    n += 0.30 * cos(a1 * 5.0 - a2 * 3.0 + cos(a1 * 4.0 + a2 * 2.0));
    n += 0.16 * sin(a1 * 9.0 + a2 * 4.0 + y * 1.8);
    n += 0.09 * cos(a1 * 13.0 - a2 * 5.0 - y * 2.4);
    return n;
}

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 smax(float a, float b, float k) {
    return -smin(-a, -b, k);
}

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

// -----------------------------------------------------------------------------
// Seamless 3.0s Phase & Organic Helical Water Vortex Spine
// -----------------------------------------------------------------------------
float getPhase() {
    return fract(uTime / LOOP_DUR);
}

// Widening dynamic tornado-funnel radius
float helixRadius(float y) {
    float h = clamp(y / 4.4, 0.0, 1.0);
    return 0.20 + 1.68 * pow(max(h, 1e-6), 1.18) + 0.14 * sin(h * TAU * 0.85);
}

// Flowing organic water ribbon thickness (surges & tapers dynamically)
float helixTubeR(float y) {
    float h = clamp(y / 4.4, 0.0, 1.0);
    float base = mix(0.18, 0.36, smoothstep(0.0, 0.50, h));
    float topTaper = 1.0 - 0.38 * smoothstep(0.68, 1.05, h);
    return base * topTaper;
}

// Delicate translucent dragon-scale water facets (strictly periodic in sMat with period 1.0)
vec2 waterDragonScales(float sMat, float phi) {
    vec2 uv = vec2(sMat * 18.0, (phi / TAU) * 7.0);
    vec2 f1 = fract(uv) - 0.5;
    vec2 f2 = fract(uv + 0.5) - 0.5;
    float d1 = length(f1 * vec2(1.0, 1.3)) + 0.08 * abs(f1.x);
    float d2 = length(f2 * vec2(1.0, 1.3)) + 0.08 * abs(f2.x);
    bool u1 = d1 < d2;
    float d = u1 ? d1 : d2;
    vec2 f = u1 ? f1 : f2;
    float dome = smoothstep(0.54, 0.16, d);
    float edge = smoothstep(0.52, 0.42, d) * smoothstep(0.30, 0.44, d);
    float overlap = clamp(0.5 - f.x * 0.55, 0.0, 1.0);
    return vec2(dome * (0.65 + 0.35 * overlap), edge);
}

// Domain-warped organic liquid caustic web (seamless in phase)
float liquidCaustics(vec2 uv, float phase) {
    float t = TAU * phase;
    vec2 w = uv + 0.35 * vec2(sin(uv.y * 2.2 + t), cos(uv.x * 2.1 - t));
    float c1 = abs(sin(w.x * 2.8 + cos(w.y * 2.5 + t)));
    float c2 = abs(cos(w.y * 3.1 - sin(w.x * 2.6 - t)));
    float web = 1.0 - min(c1, c2);
    return pow(max(web, 1e-6), 4.0);
}

// -----------------------------------------------------------------------------
// Scene SDF
// matId: 1.0 = spiraling translucent water vortex, 2.0 = crashing maelstrom waves,
//        3.0 = flying water spray streaks, 4.0 = faceted ice/water crystal shards
// -----------------------------------------------------------------------------
struct HitInfo {
    float d;
    float matId;
    float sMat;
    float phi;
    float foam;
    float scaleZone;
};

HitInfo sdWaterDragonVortex(vec3 p, float phase) {
    HitInfo res;
    res.d = 1e5;
    res.matId = 1.0;
    res.sMat = 0.0;
    res.phi = 0.0;
    res.foam = 0.0;
    res.scaleZone = 0.0;

    float r = length(p.xz);
    float ang = atan(p.z, p.x + 1e-6);
    const float pitch = 1.72;

    float turn = (p.y / pitch) - (ang / TAU + phase);
    float kBase = floor(turn + 0.5);
    float kAlt = kBase + (turn > kBase ? 1.0 : -1.0);

    float bestD = 1e5;
    float bestS = 0.0;
    float bestPhi = 0.0;
    float bestFoam = 0.0;
    float bestZone = 0.0;

    for (int i = 0; i < 2; i++) {
        float k = (i == 0) ? kBase : kAlt;
        float sMat = k + ang / TAU;
        float yk = (sMat + phase) * pitch;

        // Active height [0.04, 4.85]
        float yClamped = clamp(yk, 0.04, 4.85);
        float endCap = abs(yk - yClamped);

        float Rk = helixRadius(yClamped);
        float tubeR = helixTubeR(yClamped);

        // Organic surging radial & thickness billows along the vortex (no rigid hose!)
        float surge = 0.14 * sin(TAU * 2.0 * sMat) + 0.07 * cos(TAU * 3.0 * sMat + 1.1);
        Rk += surge * smoothstep(0.25, 1.1, yClamped);
        tubeR *= (0.86 + 0.22 * sin(TAU * 3.0 * sMat + 0.5));

        float slope = pitch / (TAU * max(Rk, 0.22));
        float invCos = inversesqrt(1.0 + slope * slope);

        vec2 q = vec2(r - Rk, (p.y - yClamped) * invCos);
        float phi = atan(q.y, q.x + 1e-6);

        // Turbulent billowing water cross-section:
        // Swept liquid ribbon + centrifugal flaring splash sheet peeling outward (dr > 0)
        float turb = fluidTurbulence(sMat, phi, yClamped);
        float outerFin = smoothstep(0.05, 0.88, cos(phi - 0.35));
        float finBillow = 0.5 + 0.5 * sin(TAU * 5.0 * sMat + phi * 1.5 + turb * 1.2);
        float sheetExt = tubeR * 0.62 * outerFin * (0.35 + 0.65 * finBillow) * smoothstep(0.30, 1.35, yClamped);

        // Flatten slightly into a dynamic curved liquid ribbon (conservatively Lipschitz-normalized)
        vec2 qRib = vec2(q.x * 0.85, q.y * 1.22);
        float dBody = (length(qRib) - (tubeR + sheetExt)) / 1.22;
        // Organic liquid billowing surface displacement
        dBody -= 0.028 * turb * smoothstep(0.18, 0.9, yClamped);
        dBody = (sqrt(sq(max(dBody, 0.0)) + sq(endCap)) + min(dBody, 0.0)) * 0.62;

        // Secondary curling centrifugal splash ribbon peeling off the outer coil
        float sTend = sMat + 0.24;
        float tendBreak = smoothstep(-0.20, 0.70, sin(TAU * 3.0 * sTend + 0.8));
        float Rtend = Rk + tubeR * 1.48 + 0.22 * sin(TAU * 2.0 * sTend);
        float yTend = yClamped + 0.22 * cos(TAU * 3.0 * sTend);
        float tendR = (0.014 + 0.048 * tendBreak) * smoothstep(0.45, 1.4, yClamped);
        vec2 qt = vec2(r - Rtend, (p.y - yTend) * invCos);
        float dTend = length(qt) - tendR;
        dTend = sqrt(sq(max(dTend, 0.0)) + sq(endCap)) + min(dTend, 0.0);

        // Third fine inner spray filament
        float sTend2 = sMat - 0.38;
        float Rtend2 = Rk + tubeR * 1.18 + 0.16 * cos(TAU * 4.0 * sTend2);
        float yTend2 = yClamped - 0.18 * sin(TAU * 2.0 * sTend2);
        float tendR2 = (0.010 + 0.028 * (0.5 + 0.5 * cos(TAU * 5.0 * sTend2))) * smoothstep(0.7, 1.8, yClamped);
        float dTend2 = length(vec2(r - Rtend2, (p.y - yTend2) * invCos)) - tendR2;
        dTend2 = sqrt(sq(max(dTend2, 0.0)) + sq(endCap)) + min(dTend2, 0.0);

        float dComb = smin(dBody, dTend, 0.09);
        dComb = smin(dComb, dTend2, 0.06);

        // Lacy white seafoam on outer centrifugal splash lip & breaking tendrils
        float foam = outerFin * smoothstep(0.42, 0.95, finBillow + 0.25 * turb) * 0.85;
        if (dTend < dBody + 0.04) foam = max(foam, 0.72 * tendBreak);
        if (dTend2 < dBody + 0.03) foam = max(foam, 0.65);

        // Subtle scale facet zone on the hero mid-upper outer-lower flank
        float scaleZone = smoothstep(0.55, 1.45, yClamped) * smoothstep(4.55, 3.60, yClamped);
        scaleZone *= smoothstep(-0.3, 0.75, cos(phi + 0.65));

        if (dComb < bestD) {
            bestD = dComb;
            bestS = sMat;
            bestPhi = phi;
            bestFoam = clamp(foam, 0.0, 1.0);
            bestZone = scaleZone;
        }
    }

    res.d = bestD;
    res.sMat = bestS;
    res.phi = bestPhi;
    res.foam = bestFoam;
    res.scaleZone = bestZone;
    return res;
}

// Violent, organic crashing maelstrom waves on the dark glossy floor (ZERO plastic bowl!)
HitInfo sdMaelstromWaves(vec3 p, float phase) {
    HitInfo res;
    res.d = 1e5;
    res.matId = 2.0;
    res.sMat = 0.0;
    res.phi = 0.0;
    res.foam = 0.0;
    res.scaleZone = 0.0;

    if (p.y > 1.55) return res;

    float r = length(p.xz);
    float ang = atan(p.z, p.x + 1e-6);
    float t = TAU * phase;

    // 1) Swirling spiral wave arms emerging directly from the dark glossy floor (no raised disc!)
    float swirl = ang * 4.0 - r * 3.4 - t;
    float spiralRidge = pow(max(0.5 + 0.5 * sin(swirl), 1e-6), 2.2);
    float radialFade = exp(-sq((r - 0.85) * 0.72)) * smoothstep(2.65, 1.15, r);
    // Low ragged water sheet flush with floor at outer edge, surging upward near vortex eye
    float eyeSurge = 0.24 * exp(-r * r * 1.4);
    float waveFloorH = eyeSurge + (0.04 + 0.18 * spiralRidge) * radialFade - 0.035;
    // Carve troughs below y=0 so the dark glossy reflective floor peeks cleanly between outer tongues!
    float dSwirl = p.y - waveFloorH;
    dSwirl = max(dSwirl, r - (2.48 + 0.28 * sin(ang * 5.0 - t) + 0.14 * cos(ang * 9.0 + t)));

    // 2) Towering ragged breaking ocean wave on the RIGHT (matching ref 08.png!)
    float dAngR = atan(sin(ang - 0.12), cos(ang - 0.12));
    float envR = exp(-sq(dAngR * 1.45));
    float crestR = (0.98 + 0.14 * sin(ang * 6.0 - t) + 0.08 * cos(ang * 13.0 + t * 2.0)) * envR;
    float yNormR = clamp(p.y / max(crestR, 0.12), 0.0, 1.0);
    // Dramatic curling ocean-wave arch leaning outward & curling back at the lip
    float archR = 1.72 + 0.42 * yNormR * yNormR - 0.18 * sin(yNormR * PI) + 0.08 * sin(ang * 7.0 - t);
    float thickR = mix(0.24, 0.045, yNormR) * smoothstep(0.08, 0.45, envR);
    // Ragged turbulent crest breakup
    float ragR = 0.065 * sin(ang * 14.0 - t * 2.0) + 0.04 * cos(ang * 23.0 + t + p.y * 5.0);
    float dWaveR = length(vec2(r - archR, max(0.0, p.y - (crestR + ragR * envR)))) - max(thickR, 0.012);
    // Hollow barrel under the curling wave!
    float dBarrelR = -(length(vec2(r - (archR - 0.22), p.y - 0.34 * envR)) - 0.28 * envR);
    dWaveR = smax(dWaveR, dBarrelR, 0.06);
    dWaveR = max(dWaveR, abs(dAngR) - 1.35);

    // 3) Sweeping translucent water ribbon + crashing splash on the LEFT (matching ref 08.png!)
    float dAngL = atan(sin(ang - 2.82), cos(ang - 2.82));
    float envL = exp(-sq(dAngL * 1.55));
    float crestL = (0.76 + 0.12 * sin(ang * 5.0 - t) + 0.06 * cos(ang * 11.0 + t)) * envL;
    float yNormL = clamp(p.y / max(crestL, 0.12), 0.0, 1.0);
    float archL = 1.65 + 0.38 * yNormL * yNormL + 0.07 * sin(ang * 6.0 - t);
    float thickL = mix(0.20, 0.042, yNormL) * smoothstep(0.08, 0.45, envL);
    float ragL = 0.055 * sin(ang * 12.0 - t * 2.0) + 0.035 * cos(ang * 19.0 - t);
    float dWaveL = length(vec2(r - archL, max(0.0, p.y - (crestL + ragL * envL)))) - max(thickL, 0.012);
    dWaveL = max(dWaveL, abs(dAngL) - 1.25);

    // Graceful soaring translucent water arc sweeping upward on far left foreground
    float arcAng = atan(sin(ang - 2.55), cos(ang - 2.55));
    float arcT = clamp((arcAng + 0.92) / 1.84, 0.0, 1.0);
    float arcR = mix(1.38, 2.72, arcT) + 0.06 * sin(arcT * TAU - t);
    float arcY = 0.06 + 0.96 * pow(max(arcT, 1e-6), 1.55) + 0.04 * sin(arcT * PI * 3.0 - t);
    float arcRad = mix(0.095, 0.022, arcT) * smoothstep(0.0, 0.08, arcT) * smoothstep(1.0, 0.88, arcT);
    float dArc = length(vec2(r - arcR, p.y - arcY)) - max(arcRad, 0.008);
    dArc = max(dArc, abs(arcAng) - 0.94);

    // 4) Back turbulent wave crest for 3D depth
    float dAngB = atan(sin(ang + 1.55), cos(ang + 1.55));
    float envB = exp(-sq(dAngB * 1.8));
    float crestB = 0.58 * envB;
    float yNormB = clamp(p.y / max(crestB, 0.12), 0.0, 1.0);
    float dWaveB = length(vec2(r - (1.68 + 0.25 * yNormB), max(0.0, p.y - crestB))) - mix(0.18, 0.04, yNormB) * envB;
    dWaveB = max(dWaveB, abs(dAngB) - 1.1);

    float d = smin(dSwirl, dWaveR, 0.14);
    d = smin(d, dWaveL, 0.14);
    d = smin(d, dWaveB, 0.14);
    d = smin(d, dArc, 0.09);

    // Crisp lacy seafoam ONLY on the violent breaking crests & sharpest spiral wakes
    float crestFoamR = smoothstep(0.58, 0.92, yNormR) * smoothstep(0.25, 0.72, envR);
    float crestFoamL = smoothstep(0.60, 0.94, yNormL) * smoothstep(0.25, 0.72, envL);
    float swirlFoam = pow(max(sin(swirl), 1e-6), 10.0) * smoothstep(2.3, 0.35, r) * 0.65;
    float arcFoam = smoothstep(0.62, 0.95, arcT) * 0.55 * step(dArc, dSwirl + 0.05);

    res.d = d;
    res.sMat = swirl / TAU;
    res.phi = max(yNormR, yNormL) * PI;
    res.foam = clamp(max(max(crestFoamR, crestFoamL), max(swirlFoam, arcFoam)), 0.0, 1.0);
    return res;
}

// Organic velocity-stretched water spray streaks + 4 asymmetric faceted crystal shards
HitInfo sdSprayAndShards(vec3 p, float phase) {
    HitInfo res;
    res.d = 1e5;
    res.matId = 3.0;
    res.sMat = 0.0;
    res.phi = 0.0;
    res.foam = 0.0;
    res.scaleZone = 0.0;

    if (p.y < 0.30 || p.y > 4.75) return res;
    float r = length(p.xz);
    float R_ref = helixRadius(p.y);
    if (abs(r - (R_ref + 0.52)) > 1.35) return res;

    float t = TAU * phase;

    // 1) Velocity-stretched flying water droplets/streaks orbiting seamlessly
    const float nSec = 11.0;
    const float tierH = 0.88;
    float ang = atan(p.z, p.x + 1e-6);
    float angShift = ang - (TAU / nSec) * phase;
    float secId = floor((angShift + PI) / (TAU / nSec));
    float secWrap = mod(secId, nSec);
    float secCenterAng = (secId + 0.5) * (TAU / nSec) - PI + (TAU / nSec) * phase;
    float tierId = floor((p.y - 0.35) / tierH);

    float h1 = hash12(vec2(secWrap, tierId));
    float h2 = hash12(vec2(secWrap + 17.3, tierId + 5.9));
    float h3 = hash12(vec2(secWrap + 31.1, tierId + 13.7));

    if (h3 > 0.30) {
        float dAng = secCenterAng + (h1 - 0.5) * 0.36 + 0.07 * sin(t + h2 * TAU);
        float dY = 0.35 + (tierId + 0.5) * tierH + (h2 - 0.5) * 0.44 + 0.09 * cos(t + h1 * TAU);
        float dR = helixRadius(clamp(dY, 0.25, 4.5)) + 0.34 + 0.42 * h1;
        vec3 cPos = vec3(dR * cos(dAng), dY, dR * sin(dAng));

        vec3 tang = normalize(vec3(-sin(dAng), 0.54, cos(dAng)));
        float halfLen = 0.006 + 0.022 * h2;
        float rad = 0.004 + 0.0075 * h3;
        float dStreak = sdCapsule(p, cPos - tang * halfLen, cPos + tang * halfLen, rad);
        if (dStreak < res.d) {
            res.d = dStreak;
            res.matId = 3.0;
        }
    }

    // 2) 4 asymmetric hero faceted ice-water crystal shards (matching ref 08.png)
    vec3 shardPos[4];
    shardPos[0] = vec3(-2.15, 1.32, 0.78);
    shardPos[1] = vec3( 2.35, 1.55, 0.58);
    shardPos[2] = vec3( 1.92, 3.32, 0.28);
    shardPos[3] = vec3(-1.58, 3.62, 0.38);

    for (int i = 0; i < 4; i++) {
        float fi = float(i);
        vec3 sp = shardPos[i];
        sp.y += 0.11 * sin(t + fi * 1.57);
        sp.x += 0.08 * cos(t + fi * 2.1);

        vec3 q = p - sp;
        if (dot(q, q) < 0.25) {
            float ca = cos(t + fi * 1.2);
            float sa = sin(t + fi * 1.2);
            q.xz = mat2(ca, -sa, sa, ca) * q.xz;
            q.xy = mat2(0.866, -0.5, 0.5, 0.866) * q.xy;
            q.y *= 0.66;
            float sz = (i < 2) ? 0.11 : 0.078;
            float dShard = (abs(q.x) + abs(q.y) + abs(q.z) - sz) * 0.57735;
            if (dShard < res.d) {
                res.d = dShard;
                res.matId = 4.0;
            }
        }
    }

    return res;
}

HitInfo mapScene(vec3 p, float phase) {
    HitInfo hDragon = sdWaterDragonVortex(p, phase);
    HitInfo hWaves = sdMaelstromWaves(p, phase);

    HitInfo res = hDragon;
    float dBlend = smin(hDragon.d, hWaves.d, 0.18);
    if (hWaves.d < hDragon.d) {
        res = hWaves;
        res.d = dBlend;
    } else {
        res.d = dBlend;
        float baseMix = smoothstep(0.65, 0.12, p.y);
        res.foam = max(res.foam, hWaves.foam * baseMix);
    }

    HitInfo hSpray = sdSprayAndShards(p, phase);
    if (hSpray.d < res.d) {
        res = hSpray;
    }
    return res;
}

float mapDist(vec3 p, float phase) {
    return mapScene(p, phase).d;
}

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

// Glassy liquid surface ripple & delicate translucent water-scale normal detail
vec3 perturbLiquidNormal(vec3 p, vec3 n, HitInfo h, float phase, out float scaleEdge) {
    scaleEdge = 0.0;
    if (h.matId != 1.0 && h.matId != 2.0) return n;

    float ang = atan(p.z, p.x + 1e-6);
    vec3 tAzim = normalize(vec3(-sin(ang), 0.38, cos(ang)));
    vec3 T = normalize(tAzim - n * dot(tAzim, n));
    vec3 B = normalize(cross(n, T));

    if (h.matId == 1.0) {
        const float epsS = 0.005;
        const float epsP = 0.030;
        // Flowing organic liquid ripples along the rushing water stream
        float r0 = fluidTurbulence(h.sMat * 1.5, h.phi, p.y);
        float rS = fluidTurbulence((h.sMat + epsS) * 1.5, h.phi, p.y);
        float rP = fluidTurbulence(h.sMat * 1.5, h.phi + epsP, p.y);

        // Delicate glassy water-dragon scale facets on the mid-upper flank
        vec2 sc0 = waterDragonScales(h.sMat, h.phi);
        vec2 scS = waterDragonScales(h.sMat + epsS, h.phi);
        vec2 scP = waterDragonScales(h.sMat, h.phi + epsP);
        scaleEdge = sc0.y * h.scaleZone;

        float dS = ((rS - r0) / epsS) * 0.012 + ((scS.x - sc0.x) / epsS) * 0.016 * h.scaleZone;
        float dP = ((rP - r0) / epsP) * 0.014 + ((scP.x - sc0.x) / epsP) * 0.014 * h.scaleZone;
        return normalize(n - (T * dS + B * dP * 0.35));
    } else {
        // Turbulent churning ocean-wave ripples (strictly periodic in phase)
        float t = TAU * phase;
        float w1 = (sin(p.x * 7.5 + p.z * 5.5 - t) + 0.5 * cos(p.x * 13.0 - p.z * 9.0 + t * 2.0)) * 0.055;
        float w2 = (cos(p.z * 8.5 - p.x * 6.0 + t) + 0.5 * sin(p.z * 14.0 + p.x * 11.0 - t * 2.0)) * 0.055;
        return normalize(n + T * w1 + B * w2);
    }
}

float calcAO(vec3 p, vec3 n, float phase) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 4; i++) {
        float h = 0.06 + 0.16 * float(i);
        float d = mapDist(p + n * h, phase);
        occ += max(h - d, 0.0) * sca;
        sca *= 0.62;
    }
    return clamp(1.0 - 1.10 * occ, 0.25, 1.0);
}

vec2 intersectCylinderY(vec3 ro, vec3 rd, float rad, float yMin, float yMax) {
    float a = dot(rd.xz, rd.xz);
    float b = 2.0 * dot(ro.xz, rd.xz);
    float c = dot(ro.xz, ro.xz) - rad * rad;
    float disc = b * b - 4.0 * a * c;
    if (disc < 0.0) return vec2(1e5, -1e5);
    float sqd = sqrt(disc);
    float t0 = (-b - sqd) / (2.0 * max(a, 1e-6));
    float t1 = (-b + sqd) / (2.0 * max(a, 1e-6));

    float ty0 = (yMin - ro.y) / (abs(rd.y) < 1e-5 ? 1e-5 : rd.y);
    float ty1 = (yMax - ro.y) / (abs(rd.y) < 1e-5 ? 1e-5 : rd.y);
    if (ty0 > ty1) { float tmp = ty0; ty0 = ty1; ty1 = tmp; }

    return vec2(max(max(t0, ty0), 0.0), min(t1, ty1));
}

// -----------------------------------------------------------------------------
// Dark Dramatic Stadium Arena Backdrop (100% NaN-safe, deep dark contrast)
// -----------------------------------------------------------------------------
vec3 renderArenaBackdrop(vec3 rd, float phase) {
    if (uLightMode == 1) {
        float g = mix(0.018, 0.055, smoothstep(-0.15, 0.65, rd.y));
        return vec3(g);
    }

    float ang = atan(rd.z, rd.x + 1e-6);
    float elev = rd.y;

    // Deep dramatic stadium night backdrop (linear ~0.0014 -> sRGB ~13/255)
    vec3 col = mix(vec3(0.0020, 0.0036, 0.0056), vec3(0.0010, 0.0018, 0.0030), smoothstep(0.0, 0.65, elev));

    // Subtle dark stadium barrier wall & warm amber rim accent
    float barrier = smoothstep(-0.01, 0.01, elev) * smoothstep(0.06, 0.03, elev);
    col += vec3(0.0030, 0.0050, 0.0072) * barrier;
    float trimLine = exp(-sq((elev - 0.044) * 145.0));
    col += vec3(0.028, 0.016, 0.005) * trimLine;

    // Subtle tiered dark stadium seating
    float seatingBand = smoothstep(0.055, 0.085, elev) * smoothstep(0.24, 0.18, elev);
    float rows = 0.5 + 0.5 * sin(elev * 175.0);
    col += vec3(0.0014, 0.0025, 0.0040) * seatingBand * (0.35 + 0.65 * rows);

    // Overhead circular truss rig
    float trussBand = smoothstep(0.27, 0.29, elev) * smoothstep(0.39, 0.36, elev);
    float trussBars = smoothstep(0.78, 0.96, abs(sin(ang * 24.0 + elev * 42.0)));
    float trussRails = exp(-sq((elev - 0.295) * 95.0)) + exp(-sq((elev - 0.375) * 95.0));
    col += vec3(0.0055, 0.0085, 0.0125) * (trussBand * trussBars * 0.55 + trussRails * 0.65);

    // Overhead stadium spotlights on upper-left & upper-right
    float lampAng = fract((ang / TAU) * 16.0);
    float lampSpot = exp(-sq((lampAng - 0.5) * 11.0)) * exp(-sq((elev - 0.335) * 52.0));
    float sideMask = smoothstep(0.28, 0.62, abs(rd.x));
    col += vec3(0.46, 0.64, 0.78) * lampSpot * (0.14 + 0.86 * sideMask);

    // Subtle diagonal volumetric spotlight shafts
    vec2 sL = normalize(vec2(-0.76, 0.44));
    vec2 sR = normalize(vec2( 0.76, 0.44));
    float beamL = pow(max(1e-6, 1.0 - abs(dot(rd.xy, vec2(sL.y, -sL.x)) + 0.14) * 4.4), 3.5)
                * smoothstep(0.40, 0.02, elev) * smoothstep(-0.08, 0.35, -rd.x);
    float beamR = pow(max(1e-6, 1.0 - abs(dot(rd.xy, vec2(sR.y, -sR.x)) - 0.14) * 4.4), 3.5)
                * smoothstep(0.40, 0.02, elev) * smoothstep(-0.08, 0.35, rd.x);
    col += vec3(0.009, 0.022, 0.030) * (beamL + beamR);

    // Crisp bokeh emerald/cyan/amber stadium accent lights
    float t = TAU * phase;
    for (int i = 0; i < 6; i++) {
        float fi = float(i);
        float a0 = -2.55 + fi * 0.46 + (i >= 3 ? 2.15 : 0.0);
        float e0 = 0.12 + 0.14 * fract(fi * 0.618) + 0.012 * sin(t + fi * 1.7);
        float da = atan(sin(ang - a0), cos(ang - a0));
        float dist2 = da * da + sq(elev - e0) * 1.6;
        float bokeh = exp(-dist2 * 850.0);
        vec3 bCol = (i == 1 || i == 4) ? vec3(0.05, 0.74, 0.52)
                  : (i == 2) ? vec3(0.75, 0.44, 0.12)
                  : vec3(0.08, 0.58, 0.78);
        col += bCol * bokeh * 0.42;
    }

    return clamp(col, vec3(0.0), vec3(4.0));
}

// -----------------------------------------------------------------------------
// Dark Glossy Tiled Arena Floor with Vibrant Caustic Vortex Reflections (y = 0)
// -----------------------------------------------------------------------------
vec3 renderArenaFloor(vec3 p, vec3 rd, float phase) {
    float r = length(p.xz);
    float ang = atan(p.z, p.x + 1e-6);

    if (uLightMode == 1) {
        float base = mix(0.048, 0.020, smoothstep(0.8, 7.0, r));
        float shadow = 1.0 - 0.55 * exp(-r * r * 0.28);
        float ring = smoothstep(0.015, 0.0, abs(fract(r * 0.45) - 0.5) - 0.485);
        return vec3(base * shadow + ring * 0.012);
    }

    // Deep glossy obsidian-slate arena floor
    vec3 col = vec3(0.0018, 0.0032, 0.0052);

    // Crisp sleek concentric rings + radial tile seams (matching ref 08.png foreground)
    float ringSeam = smoothstep(0.022, 0.0, abs(fract(r * 0.52) - 0.5) - 0.482);
    float radSeam = smoothstep(0.010, 0.0, abs(fract((ang / TAU) * 16.0) - 0.5) - 0.493) * smoothstep(1.3, 2.2, r);
    float seam = max(ringSeam, radSeam) * exp(-r * 0.14);
    col += vec3(0.018, 0.038, 0.054) * seam;

    // Shimmering turquoise-aqua caustic web radiating from the crashing maelstrom
    float caust = liquidCaustics(p.xz * 1.15, phase);
    float caustFade = exp(-r * 0.42) * smoothstep(7.0, 1.2, r);
    col += vec3(0.025, 0.42, 0.52) * caust * caustFade * 0.85;

    // Glossy mirror-like wet reflection of the glowing water vortex column (seamless in phase)
    float tFl = TAU * phase;
    vec3 nFloor = normalize(vec3(
        sin(p.x * 6.0 + p.z * 4.5 - tFl) * 0.014,
        1.0,
        cos(p.z * 6.0 - p.x * 4.5 + tFl) * 0.014
    ));
    vec3 rDir = reflect(rd, nFloor);
    float fres = pow(max(1.0 - max(dot(-rd, nFloor), 0.0), 1e-6), 4.0);

    // Rich turquoise reflection pool directly beneath & in front of the vortex
    float reflSpiral = 0.55 + 0.45 * sin(p.z * 4.5 - TAU * phase + sin(p.x * 3.8));
    float reflColGlow = exp(-sq(p.x * 0.72)) * exp(-sq((r - 2.45) * 0.48));
    vec3 vortexRefl = vec3(0.02, 0.54, 0.62) * reflColGlow * reflSpiral * 1.05;

    // Intense glowing cyan maelstrom eye reflection
    float eyeRefl = exp(-r * r * 0.30);
    vortexRefl += vec3(0.10, 0.72, 0.76) * eyeRefl * 0.65;

    // Emerald-cyan LED floor accent strip on left (like ref 08.png bottom-left)
    float ledStrip = exp(-sq((r - 3.55) * 6.5)) * exp(-sq(sin((ang - 2.18) * 0.5) * 6.5));
    col += vec3(0.04, 0.76, 0.48) * ledStrip * 0.65;

    vec3 skyRefl = renderArenaBackdrop(rDir, phase);
    col += (skyRefl * 0.55 + vortexRefl) * (0.28 + 0.72 * fres);

    float fog = 1.0 - exp(-r * 0.075);
    col = mix(col, vec3(0.0016, 0.0030, 0.0048), clamp(fog, 0.0, 0.85));

    return clamp(col, vec3(0.0), vec3(4.0));
}

// -----------------------------------------------------------------------------
// True Translucent Refracting Liquid Water Shading (Beer-Lambert + Fresnel + Foam)
// -----------------------------------------------------------------------------
vec3 shadeTranslucentWater(vec3 p, vec3 nGeom, vec3 rd, HitInfo h, float phase) {
    vec3 v = -rd;
    float scaleEdge = 0.0;
    vec3 n = perturbLiquidNormal(p, nGeom, h, phase, scaleEdge);
    if (dot(n, v) < 0.02) {
        n = normalize(n - v * (dot(n, v) - 0.04));
    }

    float ao = calcAO(p, nGeom, phase);
    float NdotV = max(dot(n, v), 0.0);
    // Physical water Schlick Fresnel (IOR 1.333 -> F0 = 0.02)
    float fres = 0.02 + 0.98 * pow(max(1.0 - NdotV, 1e-6), 4.2);

    // Light rig per uLightMode
    vec3 L1, L2, L1Col, L2Col;
    float specBoost = 1.0;
    float backSSS = 0.0;

    if (uLightMode == 1) {
        L1 = normalize(vec3(0.55, 0.78, 0.42));
        L2 = normalize(vec3(-0.72, 0.35, -0.45));
        L1Col = vec3(1.25);
        L2Col = vec3(0.42);
    } else if (uLightMode == 2) {
        L1 = normalize(vec3(0.94, 0.18, 0.28));
        L2 = normalize(vec3(-0.65, 0.52, -0.42));
        L1Col = vec3(1.50, 1.42, 1.30);
        L2Col = vec3(0.14, 0.38, 0.50);
        specBoost = 1.50;
    } else if (uLightMode == 3) {
        L1 = normalize(vec3(-0.18, 0.42, -0.89));
        L2 = normalize(vec3(0.55, 0.65, 0.45));
        L1Col = vec3(1.25, 1.60, 1.70);
        L2Col = vec3(0.22, 0.44, 0.55);
        backSSS = 1.0;
    } else {
        L1 = normalize(vec3(0.58, 0.76, 0.44));
        L2 = normalize(vec3(-0.66, 0.60, -0.36));
        L1Col = vec3(1.00, 1.22, 1.32);
        L2Col = vec3(0.42, 0.82, 0.95);
    }

    float diff1 = max(dot(n, L1), 0.0);
    float diff2 = max(dot(n, L2), 0.0);
    float wrapDiff1 = max((dot(n, L1) + 0.35) / 1.35, 0.0);

    vec3 H1 = normalize(L1 + v);
    vec3 H2 = normalize(L2 + v);
    float spec1 = pow(max(dot(n, H1), 1e-6), 160.0) * 1.85 + pow(max(dot(n, H1), 1e-6), 28.0) * 0.24;
    float spec2 = pow(max(dot(n, H2), 1e-6), 96.0) * 0.85;

    // Bubbly lacy seafoam mask (high-frequency organic froth breakup!)
    float foamNoise = fbm2(vec2(h.sMat * 16.0, h.phi * 3.2 + p.y * 3.8));
    float fineBubbles = vnoise2(vec2(h.sMat * 42.0, p.y * 14.0 + h.phi * 8.0));
    float foamSignal = h.foam + (foamNoise - 0.52) * 0.55 + (fineBubbles - 0.5) * 0.25;
    float foamAmt = smoothstep(0.54, 0.88, foamSignal);
    if (h.matId >= 3.0) foamAmt = 0.0;

    // -------------------------------------------------------------------------
    // Mode 1: High-Contrast Glossy Grayscale Liquid-Sculpture Study (R == G == B)
    // -------------------------------------------------------------------------
    if (uLightMode == 1) {
        float baseVal = mix(0.14, 0.62, foamAmt);
        float rim = pow(max(1.0 - NdotV, 1e-6), 3.0) * 0.68;
        float lum = baseVal * (0.12 * ao + 0.72 * wrapDiff1 + 0.25 * diff2)
                  + (spec1 * 0.72 + spec2 * 0.32) * (1.0 - 0.45 * foamAmt)
                  + rim * (0.65 + 0.35 * ao)
                  + scaleEdge * 0.08;
        return vec3(lum);
    }

    // -------------------------------------------------------------------------
    // Modes 0, 2, 3: True Translucent Refracting Liquid Water + Caustics + Foam
    // -------------------------------------------------------------------------
    // 1) Refracted background ray & optical thickness estimation (2 cheap SDF probes)
    vec3 rdRefr = refract(rd, n, 1.0 / 1.333);
    if (dot(rdRefr, rdRefr) < 0.01) rdRefr = rd;

    float dIn1 = -mapDist(p + rdRefr * 0.16, phase);
    float dIn2 = -mapDist(p + rdRefr * 0.42, phase);
    float thickness = clamp(max(dIn1, 0.0) * 1.5 + max(dIn2, 0.0) * 1.9, 0.04, 1.25);

    // Sample background behind the translucent water along refracted ray
    vec3 bgBehind;
    if (rdRefr.y < -0.01) {
        float tF = -p.y / rdRefr.y;
        bgBehind = (tF > 0.0 && tF < 25.0)
            ? renderArenaFloor(p + rdRefr * tF, rdRefr, phase)
            : renderArenaBackdrop(rdRefr, phase);
    } else {
        bgBehind = renderArenaBackdrop(rdRefr, phase);
    }

    // 2) Beer-Lambert ocean-water absorption (crystal clear on thin edges, rich dark emerald-sapphire in thick mass!)
    vec3 absorption = exp(-thickness * vec3(6.2, 1.85, 1.15));
    vec3 deepTeal   = vec3(0.002, 0.044, 0.105);
    vec3 emeraldMid = vec3(0.008, 0.25, 0.37);
    vec3 bioAqua    = vec3(0.10, 0.88, 0.86);
    vec3 iceRim     = vec3(0.76, 0.98, 1.00);
    vec3 foamCol    = vec3(0.88, 0.96, 1.00);

    // 3) Internal swirling caustic filaments & glowing vortex eye scattering
    float r = length(p.xz);
    float coreProx = exp(-p.y * 0.95) * exp(-r * 1.45);
    vec2 caustUV = (h.matId == 1.0)
        ? vec2(h.sMat * 4.5, h.phi * 1.1)
        : vec2(p.x * 1.4, p.z * 1.4);
    float caust = liquidCaustics(caustUV, phase);

    float bioScale = (uLightMode == 2) ? 0.42 : (uLightMode == 3 ? 0.78 : 1.0);
    float macroAtten = (uCamMode == 1) ? 0.82 : 1.0;

    // Deepen base wave body slightly so curling ocean waves read as rich dark sapphire-teal
    float baseDepthBoost = (h.matId == 2.0) ? 0.65 : 1.0;

    // Internal volume scatter (deep ocean sapphire-teal in thick shadows, bright translucent aqua where lit)
    float scatterDepth = 1.0 - exp(-thickness * 2.4);
    vec3 volumeScatter = mix(deepTeal, emeraldMid, clamp(wrapDiff1 * 0.62 + (1.0 - NdotV) * 0.32, 0.0, 1.0))
                       * (0.24 * ao + 0.74 * wrapDiff1 * L1Col + 0.30 * diff2 * L2Col) * baseDepthBoost;

    // Subsurface forward-scattering through translucent water ridges
    float sss = pow(max(clamp(dot(v, -L1 + n * 0.42), 0.0, 1.0), 1e-6), 3.0);
    float sssWeight = (uLightMode == 3) ? 1.55 : (uLightMode == 2 ? 0.35 : 0.58);
    volumeScatter += bioAqua * sss * sssWeight * (0.42 + 0.58 * (1.0 - scatterDepth)) * baseDepthBoost;

    // Internal bioluminescent caustics + vortex core glow + subtle glassy scale edge shimmer
    vec3 internalGlow = bioAqua * caust * (0.30 + 0.36 * (1.0 - scatterDepth)) * bioScale * macroAtten * baseDepthBoost;
    internalGlow += vec3(0.20, 0.94, 0.94) * coreProx * 0.95 * bioScale * macroAtten;
    internalGlow += iceRim * scaleEdge * 0.22 * macroAtten;

    // Combine refracted background (attenuated by Beer-Lambert) + internal liquid volume
    vec3 transmitted = bgBehind * absorption * 1.20 + volumeScatter * scatterDepth + internalGlow;

    // Tiny sparkling crystal-clear water droplets (transparent + sharp white-cyan glint, NEVER opaque pills!)
    if (h.matId == 3.0) {
        transmitted = bgBehind * 0.85 + iceRim * (0.28 + 0.95 * fres) + L1Col * spec1 * 1.4;
    }

    // Faceted crystal shard override (sapphire-ice prismatic sparkle)
    if (h.matId == 4.0) {
        transmitted = mix(vec3(0.14, 0.36, 0.78), iceRim, clamp(fres * 0.85 + spec1 * 0.55, 0.0, 1.0));
    }

    // 4) Crisp Fresnel glass-water reflection + stadium specular glints
    vec3 envRefl = renderArenaBackdrop(reflect(rd, n), phase);
    float rimPower = (uLightMode == 3) ? 1.65 : 1.05;
    vec3 reflected = envRefl * 0.95 + iceRim * rimPower * (0.35 + 0.65 * ao);

    vec3 waterLit = mix(transmitted, reflected, clamp(fres * 1.15, 0.0, 0.92));
    waterLit += (L1Col * spec1 + L2Col * spec2) * specBoost * (1.0 - 0.50 * foamAmt);

    if (backSSS > 0.5) {
        float backRim = pow(max(1.0 - NdotV, 1e-6), 2.2) * (0.4 + 0.6 * max(dot(n, L1), 0.0));
        waterLit += vec3(0.52, 0.98, 1.0) * backRim * 0.95;
    }

    // 5) Crisp lacy white seafoam crests floating on top of the translucent water
    vec3 foamLit = foamCol * (0.24 * ao + 0.74 * wrapDiff1 * (L1Col * 0.75 + 0.25) + 0.22 * diff2)
                 + bioAqua * coreProx * 0.25 * bioScale;
    return mix(waterLit, foamLit, foamAmt);
}

// -----------------------------------------------------------------------------
// ACES Filmic Tonemapping (soft highlight knee -> %clip < 1%)
// -----------------------------------------------------------------------------
vec3 acesTonemap(vec3 x) {
    x = x / (1.0 + 0.08 * 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) * 0.965;
}

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

    vec3 ro, ta;
    float fov;
    if (uCamMode == 1) {
        // ~2.5x macro close-up on the translucent billowing aqua helix, splash ribbons & flying spray
        ro = vec3(0.72, 2.30, 3.25);
        ta = vec3(0.62, 2.20, 0.15);
        fov = 2.05;
    } else {
        // Hero full vortex framing showing glossy reflective arena floor + crashing maelstrom + soaring coils
        ro = vec3(0.0, 1.98, 7.25);
        ta = vec3(0.0, 1.88, 0.0);
        fov = 1.40;
    }

    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 + fov * cw);

    float tFloor = (rd.y < -0.0005) ? (-ro.y / rd.y) : 1e5;
    if (tFloor < 0.0) tFloor = 1e5;

    vec2 bnd = intersectCylinderY(ro, rd, 3.55, -0.02, 5.05);
    float tStart = bnd.x;
    float tEnd = min(bnd.y, tFloor);

    bool hit = false;
    float t = tStart;
    HitInfo hitInfo;
    hitInfo.d = 1e5;
    hitInfo.matId = 0.0;
    hitInfo.sMat = 0.0;
    hitInfo.phi = 0.0;
    hitInfo.foam = 0.0;
    hitInfo.scaleZone = 0.0;

    // Turbulent backlit ocean spray mist & glowing maelstrom eye aura
    float sprayMist = 0.0;
    float eyeMist = 0.0;

    if (tStart < tEnd) {
        float tPrev = tStart;
        for (int i = 0; i < 120; i++) {
            vec3 p = ro + rd * t;
            HitInfo h = mapScene(p, phase);

            if (h.d < 0.0015) {
                // 5-step binary bisection between tPrev and t if we stepped inside (h.d < 0)
                // Guarantees exact zero-isosurface landing -> ZERO concentric fingerprint rings!
                if (h.d < 0.0 && i > 0) {
                    float a = tPrev;
                    float b = t;
                    for (int j = 0; j < 5; j++) {
                        float m = 0.5 * (a + b);
                        if (mapDist(ro + rd * m, phase) < 0.0) {
                            b = m;
                        } else {
                            a = m;
                        }
                    }
                    t = 0.5 * (a + b);
                    h = mapScene(ro + rd * t, phase);
                }
                hit = true;
                hitInfo = h;
                break;
            }

            // Conservative Lipschitz-safe step factor (0.30)
            float stepLen = clamp(h.d * 0.30, 0.0005, 0.28);

            // Organic turbulent spray mist tightly hugging breaking waves & outer vortex sheets
            if (h.d < 0.34) {
                float near = 1.0 - h.d / 0.34;
                float mistNoise = 0.5 + 0.5 * sin(p.y * 5.0 - TAU * phase + sin(p.x * 4.2) + cos(p.z * 4.2));
                sprayMist += near * near * stepLen * (0.08 + 0.16 * mistNoise);
            }
            // Concentrated bioluminescent mist at the maelstrom vortex eye
            vec3 eyeDiff = p - vec3(0.0, 0.30, 0.0);
            float eyeD2 = dot(eyeDiff, eyeDiff);
            if (eyeD2 < 2.1) {
                eyeMist += exp(-eyeD2 * 2.3) * stepLen * 0.22;
            }

            tPrev = t;
            t += stepLen;
            if (t > tEnd) break;
        }
    }

    vec3 col;
    if (hit) {
        vec3 p = ro + rd * t;
        vec3 n = calcNormal(p, phase);
        col = shadeTranslucentWater(p, n, rd, hitInfo, phase);
    } else if (tFloor < 45.0) {
        vec3 pFloor = ro + rd * tFloor;
        col = renderArenaFloor(pFloor, rd, phase);
    } else {
        col = renderArenaBackdrop(rd, phase);
    }

    // Add turbulent ocean spray mist & glowing maelstrom eye (muted in grayscale mode)
    if (uLightMode != 1) {
        float glowScale = (uLightMode == 2) ? 0.45 : (uLightMode == 3 ? 0.85 : 1.0);
        if (uCamMode == 1) glowScale *= 0.65;
        col += vec3(0.04, 0.42, 0.50) * min(sprayMist, 0.52) * 0.52 * glowScale;
        col += vec3(0.16, 0.90, 0.92) * min(eyeMist, 0.78) * 0.54 * glowScale;
    }

    // Subtle cinematic vignette
    float vig = 1.0 - 0.20 * dot(uv, uv);
    col *= clamp(vig, 0.76, 1.0);

    // ACES filmic tonemap + sRGB gamma
    col = acesTonemap(max(col, vec3(0.0)));
    col = pow(max(col, vec3(1e-6)), vec3(1.0 / 2.2));

    if (uLightMode == 1) {
        float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
        col = vec3(lum);
    } else {
        col = max(col, vec3(0.022, 0.030, 0.042));
    }

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierspiraling-water-dragon-vortex
CategoryGame VFX Shaders · Choreographed Loop VFX
Upstream studyvfx_animated/lib/08_spiraling_water_dragon_vortex.glsl (commit f3d2025)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size994 lines · 39.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT water sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation

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
Arcane Slash Arc WebGL2 procedural shader3s Loop

Sweeping violet-cyan spell blade arc with glowing runes, trailing motes, and arena floor bounce.

Game VFX