Liquid Mercury Singularity shader

morphing-liquid-mercury-singularity.glsl · 921 lines · GLSL ES 3.00 · MIT

Liquid Mercury SingularityGame VFX3s Loop
Liquid Mercury Singularity WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 921 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
morphing-liquid-mercury-singularity.glsl (921 lines)
#version 300 es
// Liquid Mercury Singularity (morphing-liquid-mercury-singularity.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_animated/lib/09_morphing_liquid_mercury_singularity.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;

// -----------------------------------------------------------------------------
// Math & Noise Helpers
// -----------------------------------------------------------------------------

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

float hash21(vec2 p) {
    p = fract(p * vec2(123.34, 456.21));
    p += dot(p, p + 45.32);
    return fract(p.x * p.y);
}

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 vnoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash21(i);
    float b = hash21(i + vec2(1.0, 0.0));
    float c = hash21(i + vec2(0.0, 1.0));
    float d = hash21(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 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 3; i++) {
        v += a * vnoise(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

// Smooth minimum for liquid mercury surface tension fillets
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;
}

// Exact distance to a round cone along unit axis `dir` from r0*dir (radius r1) to L*dir (radius r2)
float sdSpikeCone(vec3 q, vec3 dir, float r0, float L, float r1, float r2) {
    float h = dot(q, dir);
    if (h <= 0.12) return 1e5; // hemisphere cull
    float H = max(L - r0, 0.05);
    float y = h - r0;
    vec3 perp = q - dir * h;
    float x = length(perp);

    float b = (r1 - r2) / H;
    float a = sqrt(max(1.0 - b * b, 1e-4));
    float k = dot(vec2(x, y), vec2(-b, a));
    if (k < 0.0)     return length(vec2(x, y)) - r1;
    if (k > a * H)   return length(vec2(x, y - H)) - r2;
    return dot(vec2(x, y), vec2(a, b)) - r1;
}

// -----------------------------------------------------------------------------
// Choreography State (Seamless 3.0s loop)
// -----------------------------------------------------------------------------

float gPhase;
vec3 gOrbCenter;
mat3 gRingMat;    // world-to-ring-local
mat3 gRingMatInv; // ring-local-to-world

// 18 radial ferrofluid spike directions (unit vectors)
const int NUM_SPIKES = 18;
const vec3 SPIKE_DIRS[NUM_SPIKES] = vec3[NUM_SPIKES](
    vec3( 0.000,  1.000,  0.000), // 0: top apex
    vec3( 0.000, -1.000,  0.000), // 1: bottom apex
    vec3(-0.996,  0.085,  0.020), // 2: left primary
    vec3( 0.992,  0.115,  0.050), // 3: right primary
    vec3(-0.625,  0.768,  0.138), // 4: upper-left crown
    vec3( 0.635,  0.762,  0.125), // 5: upper-right crown
    vec3(-0.742, -0.658,  0.130), // 6: lower-left crown
    vec3( 0.680, -0.715,  0.160), // 7: lower-right crown
    vec3(-0.460,  0.485,  0.744), // 8: front upper-left
    vec3( 0.510,  0.440,  0.739), // 9: front upper-right
    vec3(-0.340, -0.620,  0.707), // 10: front lower-left
    vec3( 0.360, -0.580,  0.731), // 11: front lower-right
    vec3( 0.870, -0.255,  0.422), // 12: right-lower-front
    vec3(-0.885, -0.220,  0.410), // 13: left-lower-front
    vec3(-0.790,  0.460, -0.405), // 14: back upper-left
    vec3( 0.780,  0.470, -0.413), // 15: back upper-right
    vec3(-0.520, -0.550, -0.653), // 16: back lower-left
    vec3( 0.530, -0.530, -0.662)  // 17: back lower-right
);

const float SPIKE_BASE_LEN[NUM_SPIKES] = float[NUM_SPIKES](
    1.36, 1.24, 1.32, 1.34,
    1.30, 1.32, 1.18, 1.20,
    1.04, 1.06, 1.06, 1.04,
    1.12, 1.10, 1.14, 1.14,
    1.08, 1.08
);

const float SPIKE_PHASE[NUM_SPIKES] = float[NUM_SPIKES](
    0.00, 3.14, 1.05, 4.19,
    0.52, 2.62, 3.66, 5.24,
    1.57, 4.71, 2.09, 5.76,
    0.78, 3.92, 2.35, 5.49,
    1.31, 4.45
);

// 9 orbiting chrome droplets: (radius, orbitR, baseAngle, yOffset)
const int NUM_DROPLETS = 9;
const vec4 DROPLETS[NUM_DROPLETS] = vec4[NUM_DROPLETS](
    vec4(0.225, 1.98,  2.88, -0.02), // 0: large left-lower cyan droplet
    vec4(0.215, 2.04, -0.18,  0.04), // 1: large right-upper violet droplet
    vec4(0.148, 1.56,  2.22, -0.28), // 2: medium lower-left-front droplet
    vec4(0.105, 1.66, -2.28,  0.54), // 3: upper-left floating droplet
    vec4(0.098, 1.72, -0.92,  0.56), // 4: upper-right floating droplet
    vec4(0.105, 1.76,  0.68, -0.34), // 5: lower-right floating droplet
    vec4(0.056, 2.26,  0.14, -0.22), // 6: small right outer droplet
    vec4(0.052, 2.15,  3.28,  0.22), // 7: small left outer droplet
    vec4(0.044, 1.50,  1.25, -0.45)  // 8: tiny lower-front accent droplet
);

vec3 getDropletPos(int i) {
    vec4 d = DROPLETS[i];
    float ang = d.z + TAU * gPhase;
    // Slight elliptical orbit in ring-local plane + harmonic vertical bobbing
    float r = d.y * (1.0 + 0.04 * sin(TAU * gPhase + d.z * 2.0));
    float bob = d.w + 0.055 * sin(2.0 * TAU * gPhase + d.z * 1.5);
    vec3 localPos = vec3(cos(ang) * r, bob, sin(ang) * r * 0.94);
    return gOrbCenter + gRingMatInv * localPos;
}

void initChoreography() {
    gPhase = fract(uTime / 3.0);
    gOrbCenter = vec3(0.0, 1.52 + 0.032 * sin(TAU * gPhase), 0.0);

    // Tilted ring frame matching hero reference:
    // Front edge dips down, back rises up; right tilts up, left tilts down.
    float pitch = 0.35;
    float roll  = 0.30;
    mat2 rp = rot2(pitch);
    mat2 rr = rot2(roll);

    // Build ring-local-to-world basis columns
    vec3 ex = vec3(1.0, 0.0, 0.0);
    vec3 ey = vec3(0.0, 1.0, 0.0);
    vec3 ez = vec3(0.0, 0.0, 1.0);

    // Apply pitch (YZ) then roll (XY)
    ex.yz = rp * ex.yz; ey.yz = rp * ey.yz; ez.yz = rp * ez.yz;
    ex.xy = rr * ex.xy; ey.xy = rr * ey.xy; ez.xy = rr * ez.xy;

    gRingMatInv = mat3(ex, ey, ez);
    gRingMat    = transpose(gRingMatInv);
}

// -----------------------------------------------------------------------------
// Scene SDF (Mercury Singularity + Ferrofluid Spikes + Orbiting Droplets)
// -----------------------------------------------------------------------------

float mapMercuryOrb(vec3 p) {
    vec3 q = p - gOrbCenter;
    float r = length(q);
    // Tight bounding sphere skip when far from orb+spikes
    if (r > 2.05) return r - 1.65;

    // Gentle seamless liquid mercury sway
    float sway = 0.075 * sin(TAU * gPhase);
    q.xz = rot2(sway) * q.xz;

    // Undulating liquid mercury surface tension folds on the core orb
    float t1 = TAU * gPhase;
    float w1 = sin(q.x * 3.6 - q.y * 2.9 + t1) * cos(q.z * 3.3 + q.y * 2.1);
    float w2 = sin(q.y * 5.2 + q.x * 2.5 - 2.0 * t1) * 0.48;
    // Sinuous equatorial chrome fold ribbon (bends mirror reflections dramatically)
    float sCurve = q.y + 0.17 * sin(q.x * 3.4 + t1) * cos(q.z * 2.2);
    float ridge = 0.048 * exp(-sCurve * sCurve * 18.0) * sin(q.x * 3.0 - q.z * 2.6 + t1);

    float dOrb = r - 0.655 + 0.032 * w1 + 0.016 * w2 + ridge;

    // Radial magnetic ferrofluid spikes with smooth surface-tension fillets
    float openBoost = 1.0 + 0.10 * clamp(uOpen, 0.0, 1.0);
    float dSpikes = 1e5;

    for (int i = 0; i < NUM_SPIKES; i++) {
        vec3 baseDir = SPIKE_DIRS[i];
        float sp = SPIKE_PHASE[i];
        // Subtle harmonic magnetic lean
        vec3 dir = normalize(baseDir + 0.045 * vec3(
            sin(t1 + sp),
            cos(t1 + sp * 1.3),
            sin(t1 * 2.0 - sp)
        ));
        // Harmonic growth/retraction wave across the sphere
        float wave = 0.13 * sin(t1 + sp) + 0.055 * sin(2.0 * t1 - sp * 1.7);
        float L = (SPIKE_BASE_LEN[i] + wave) * openBoost;
        float r1 = 0.145 + 0.018 * sin(t1 + sp);
        float dC = sdSpikeCone(q, dir, 0.44, L, r1, 0.0045);
        dSpikes = min(dSpikes, dC);
    }

    // Smooth concave meniscus blend where spikes emerge from liquid mercury
    return smin(dOrb, dSpikes, 0.185);
}

// Returns vec2(dist, matId): matId 1.0 = mercury orb, 2.0 = chrome droplet
vec2 mapSceneMat(vec3 p) {
    float dOrb = mapMercuryOrb(p);
    vec2 res = vec2(dOrb, 1.0);

    // Quick bounding sphere for orbiting droplets
    vec3 q = p - gOrbCenter;
    if (dot(q, q) < 11.5) {
        for (int i = 0; i < NUM_DROPLETS; i++) {
            vec3 dp = getDropletPos(i);
            float dDrop = length(p - dp) - DROPLETS[i].x;
            if (dDrop < res.x) {
                res = vec2(dDrop, 2.0);
            }
        }
    }
    return res;
}

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

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

// Fast ambient occlusion for mercury spike crevices
float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 4; i++) {
        float h = 0.04 + 0.11 * float(i);
        float d = mapScene(p + n * h);
        occ += (h - d) * sca;
        sca *= 0.65;
    }
    return clamp(1.0 - 1.8 * occ, 0.15, 1.0);
}

// -----------------------------------------------------------------------------
// Studio Environment & Arena Lighting
// -----------------------------------------------------------------------------

vec3 sampleStudioEnv(vec3 rd, bool isBackground) {
    vec3 dir = normalize(rd);
    float y = dir.y;
    float az = atan(dir.z, dir.x);
    float t1 = TAU * gPhase;

    // Mode 1: Pure Neutral Grayscale Studio Form Study (strictly R==G==B)
    if (uLightMode == 1) {
        float base = mix(0.012, 0.035, smoothstep(-0.2, 0.7, y));
        float ribs = 0.008 * smoothstep(0.6, 0.95, sin(az * 24.0)) * smoothstep(0.0, 0.45, y);
        base += ribs;
        if (!isBackground) {
            // Crisp high-contrast studio softboxes for sculptural metallic form
            vec3 topLeft = normalize(vec3(-0.36, 0.72, 0.59));
            float sb1 = smoothstep(0.84, 0.92, dot(dir, topLeft));
            vec3 rightStrip = normalize(vec3(0.82, 0.20, 0.53));
            float sb2 = smoothstep(0.84, 0.93, dot(dir, rightStrip));
            vec3 leftStrip = normalize(vec3(-0.86, -0.05, 0.50));
            float sb3 = smoothstep(0.83, 0.92, dot(dir, leftStrip));
            float horiz = exp(-pow((y - 0.025) / 0.018, 2.0)) * smoothstep(0.15, 0.55, abs(dir.x));
            float upperBand = exp(-pow((y - 0.34) / 0.026, 2.0)) * 0.85;
            float darkBand = smoothstep(0.45, 0.88, dot(dir, vec3(0.0, 0.05, 1.0)));
            float lum = base * (1.0 - 0.78 * darkBand)
                      + 2.85 * sb1 + 1.85 * sb2 + 1.75 * sb3 + 1.35 * horiz + upperBand;
            if (y < 0.0) {
                float fl = mix(0.015, 0.14, exp(y * 4.0));
                lum = mix(lum, fl, smoothstep(0.0, -0.06, y));
            }
            return vec3(lum);
        } else {
            float horiz = exp(-pow((y - 0.015) / 0.014, 2.0)) * smoothstep(0.22, 0.55, abs(dir.x));
            return vec3(base + 0.45 * horiz);
        }
    }

    // Hero / Grazing / Backlit studio palette
    vec3 cyanCol    = vec3(0.06, 0.88, 1.00);
    vec3 magentaCol = vec3(0.86, 0.22, 1.00);
    vec3 whiteCol   = vec3(0.96, 0.98, 1.00);

    // Deep dramatic obsidian-indigo concert arena base (lumMin ~8-12, never pure black)
    vec3 col = mix(vec3(0.0024, 0.0032, 0.0060), vec3(0.0052, 0.0065, 0.0120), smoothstep(-0.15, 0.65, y));

    // Subtle dark concert-arena architectural tiers & vertical truss structure
    float tier = smoothstep(0.20, 0.85, sin(y * 38.0)) * smoothstep(0.02, 0.35, y) * smoothstep(0.65, 0.15, y);
    float truss = smoothstep(0.78, 0.98, sin(az * 26.0)) * smoothstep(0.05, 0.55, y);
    col += vec3(0.0035, 0.0046, 0.0085) * (0.55 * tier + 0.50 * truss);

    // Mode 2: Grazing Key Light (strong raking directional studio key from right)
    if (uLightMode == 2) {
        vec3 keyDir = normalize(vec3(0.88, 0.38, 0.28));
        float keyBox = smoothstep(0.82, 0.93, dot(dir, keyDir));
        float keyHalo = pow(max(dot(dir, keyDir), 0.0), 14.0);
        vec3 fillDir = normalize(vec3(-0.85, 0.10, 0.50));
        float fillBox = smoothstep(0.76, 0.92, dot(dir, fillDir));
        col *= 0.65;
        if (!isBackground) {
            col += vec3(1.0, 0.95, 0.88) * (3.6 * keyBox + 0.45 * keyHalo);
            col += vec3(0.12, 0.34, 0.56) * 0.48 * fillBox;
        } else {
            col += vec3(0.85, 0.80, 0.72) * 0.22 * keyHalo;
        }
        return col;
    }

    // Mode 3: Backlit Rim Light (dramatic studio backlight silhouetting dark chrome spikes, dark sky)
    if (uLightMode == 3) {
        vec3 backDir = normalize(vec3(0.0, 0.10, -0.995));
        vec3 rimGrad = mix(cyanCol, magentaCol, smoothstep(-0.35, 0.35, dir.x));
        if (!isBackground) {
            float backCore = smoothstep(0.82, 0.96, dot(dir, backDir));
            float rimSideL = smoothstep(0.78, 0.94, dot(dir, normalize(vec3(-0.78, 0.22, -0.58))));
            float rimSideR = smoothstep(0.78, 0.94, dot(dir, normalize(vec3( 0.78, 0.22, -0.58))));
            float rimTop   = smoothstep(0.84, 0.96, dot(dir, normalize(vec3(0.0, 0.85, -0.52))));
            col *= 0.35;
            col += rimGrad * 2.4 * backCore + cyanCol * 2.4 * rimSideL + magentaCol * 2.4 * rimSideR + whiteCol * 1.4 * rimTop;
        } else {
            // Focused backlight halo behind the orb without washing out the dark studio sky
            float tightGlow = pow(max(dot(dir, backDir), 0.0), 22.0);
            float softGlow  = pow(max(dot(dir, backDir), 0.0), 7.0);
            col += rimGrad * (0.14 * softGlow + 0.55 * tightGlow);
        }
        return col;
    }

    // Mode 0: Hero Dark Glossy Studio Stage with Neon Cyan/Magenta Rim Reflections
    float shimmer = 0.94 + 0.06 * sin(t1 + dir.x * 2.5);

    // Horizontal stage perimeter neon LED light bars near the horizon
    float hBarMask = exp(-pow((y - 0.016) / 0.012, 2.0));
    float hBarHalo = exp(-pow((y - 0.016) / 0.055, 2.0));
    float leftSide  = smoothstep(-0.20, -0.46, dir.x);
    float rightSide = smoothstep( 0.20,  0.46, dir.x);

    vec3 leftNeon  = mix(cyanCol, whiteCol, 0.45) * (1.45 * hBarMask + 0.16 * hBarHalo) * leftSide;
    vec3 rightNeon = mix(magentaCol, whiteCol, 0.40) * (1.45 * hBarMask + 0.16 * hBarHalo) * rightSide;
    col += (leftNeon + rightNeon) * shimmer;

    if (!isBackground) {
        // High-contrast mirror studio reflection bands for razor-sharp sculptural liquid mercury
        // Deep dark obsidian mirror base so bright softboxes carve dramatic chrome contrast
        col *= 0.28;

        // 1. Crisp overhead-left cool-white studio softbox (hard specular edge)
        vec3 sbTopL = normalize(vec3(-0.32, 0.72, 0.61));
        float topBox = smoothstep(0.85, 0.915, dot(dir, sbTopL));
        float topRim = smoothstep(0.74, 0.86, dot(dir, sbTopL)) * 0.22;

        // 2. Sharp horizontal studio reflection ribbons (wrap across sinuous liquid folds)
        float upperStrip = exp(-pow((y - 0.30) / 0.022, 2.0)) * smoothstep(-0.6, 0.6, -dir.x);
        float lowerStrip = exp(-pow((y + 0.16) / 0.024, 2.0));

        // 3. Vivid cyan neon studio wall/strip on left & lower-left (crisp edge)
        vec3 sbCyan = normalize(vec3(-0.84, -0.08, 0.53));
        float cyanBox = smoothstep(0.76, 0.89, dot(dir, sbCyan));
        float cyanSweep = smoothstep(-0.22, -0.68, dir.x) * exp(-pow((y + 0.18) / 0.24, 2.0));

        // 4. Vivid magenta-violet neon studio wall/strip on right & lower-right (crisp edge)
        vec3 sbMag = normalize(vec3(0.82, -0.04, 0.57));
        float magBox = smoothstep(0.76, 0.89, dot(dir, sbMag));
        float magSweep = smoothstep(0.22, 0.68, dir.x) * exp(-pow((y + 0.16) / 0.24, 2.0));

        // 5. Overhead cool violet-white ceiling studio strip
        vec3 sbTopR = normalize(vec3(0.46, 0.78, 0.42));
        float topR = smoothstep(0.85, 0.92, dot(dir, sbTopR));

        // Velvety dark mirror trough across front-center for bold chrome silhouette
        float darkTrough = smoothstep(0.48, 0.86, dot(dir, normalize(vec3(0.02, 0.06, 0.998))));
        float horizDark  = exp(-pow((y - 0.08) / 0.085, 2.0)) * smoothstep(0.45, 0.10, abs(dir.x));
        float troughMask = 1.0 - clamp(0.82 * darkTrough + 0.65 * horizDark, 0.0, 0.90);

        col += whiteCol * (3.15 * topBox + topRim + 1.10 * upperStrip);
        col += cyanCol * (2.35 * cyanBox + 0.95 * cyanSweep + 0.65 * lowerStrip * smoothstep(0.15, -0.45, dir.x)) * shimmer;
        col += magentaCol * (2.35 * magBox + 0.98 * magSweep + 0.65 * lowerStrip * smoothstep(-0.15, 0.45, dir.x)) * shimmer;
        col += mix(magentaCol, whiteCol, 0.55) * 1.45 * topR;
        col *= troughMask;

        // Lower hemisphere reflects dark glossy cyan/magenta-washed stage floor
        if (y < 0.0) {
            vec3 floorRefl = mix(cyanCol, magentaCol, smoothstep(-0.45, 0.45, dir.x)) * 0.48;
            floorRefl *= exp(y * 3.2);
            floorRefl += vec3(0.004, 0.006, 0.012);
            col = mix(col, floorRefl, smoothstep(0.0, -0.07, y));
        }
    }

    return col;
}

// Volumetric overhead stage spotlight cones in the background arena
vec3 evalStageSpotlights(vec2 uv) {
    if (uLightMode == 2) return vec3(0.0);

    float t1 = TAU * gPhase;
    float breathe = 0.92 + 0.08 * sin(t1);

    // Upper-left cyan spotlight beam descending toward center
    vec2 pL = uv - vec2(-0.62, 0.52);
    vec2 dL = normalize(vec2(0.42, -0.91));
    float alongL = dot(pL, dL);
    float perpL  = abs(dot(pL, vec2(-dL.y, dL.x)));
    float coneL  = exp(-pow(perpL / (0.075 + max(alongL, 0.0) * 0.24), 2.0))
                 * smoothstep(-0.08, 0.12, alongL)
                 * exp(-max(alongL, 0.0) * 1.65);
    float bulbL  = exp(-dot(pL, pL) * 42.0);

    // Upper-right magenta-violet spotlight beam descending toward center
    vec2 pR = uv - vec2(0.62, 0.52);
    vec2 dR = normalize(vec2(-0.42, -0.91));
    float alongR = dot(pR, dR);
    float perpR  = abs(dot(pR, vec2(-dR.y, dR.x)));
    float coneR  = exp(-pow(perpR / (0.075 + max(alongR, 0.0) * 0.24), 2.0))
                 * smoothstep(-0.08, 0.12, alongR)
                 * exp(-max(alongR, 0.0) * 1.65);
    float bulbR  = exp(-dot(pR, pR) * 42.0);

    if (uLightMode == 1) {
        float lum = (0.07 * (coneL + coneR) + 0.28 * (bulbL + bulbR)) * breathe;
        return vec3(lum);
    }

    vec3 cL = vec3(0.14, 0.62, 0.92) * (0.11 * coneL + 0.45 * bulbL);
    vec3 cR = vec3(0.72, 0.26, 0.96) * (0.11 * coneR + 0.45 * bulbR);
    return (cL + cR) * breathe;
}

// -----------------------------------------------------------------------------
// Tilted Iridescent Cyan-Violet Glowing Orbital Stardust Rings
// -----------------------------------------------------------------------------

vec3 evalRingLayer(vec3 rro, vec3 rrd, float planeY, float tMax, bool mainPlane, float layerSeed) {
    if (abs(rrd.y) < 1e-4) return vec3(0.0);
    float t = (planeY - rro.y) / rrd.y;
    if (t <= 0.02 || t >= tMax) return vec3(0.0);

    vec3 q = rro + rrd * t;
    float r = length(q.xz);
    if (r < 1.18 || r > 2.75) return vec3(0.0);

    float ang = atan(q.z, q.x); // [-PI, PI]
    float t1 = TAU * gPhase;

    // World position for consistent left=cyan, right=violet iridescence
    vec3 wPos = gOrbCenter + gRingMatInv * q;
    float sideMix = smoothstep(-1.55, 1.55, wPos.x + 0.35 * (wPos.y - gOrbCenter.y));
    vec3 cyanCol   = vec3(0.08, 0.94, 1.00);
    vec3 violetCol = vec3(0.68, 0.26, 1.00);
    vec3 magCol    = vec3(0.95, 0.38, 0.96);
    vec3 ringCol   = mix(cyanCol, mix(violetCol, magCol, 0.35 + 0.35 * sin(ang * 2.0 - t1)), sideMix);

    if (uLightMode == 1) {
        ringCol = vec3(0.88);
    }

    vec3 accum = vec3(0.0);

    // 1. Interwoven glowing plasma filament ribbons (on main plane)
    if (mainPlane) {
        float r0 = 1.90 + 0.042 * sin(3.0 * ang - t1);
        float r1 = 2.00 + 0.048 * sin(2.0 * ang + t1 + 1.2);
        float r2 = 2.09 + 0.036 * sin(4.0 * ang - 2.0 * t1 + 2.4);

        float f0 = exp(-pow((r - r0) / 0.017, 2.0)) * (0.65 + 0.35 * sin(2.0 * ang - t1));
        float f1 = exp(-pow((r - r1) / 0.024, 2.0)) * (0.68 + 0.32 * cos(3.0 * ang - t1 + 0.7));
        float f2 = exp(-pow((r - r2) / 0.014, 2.0)) * (0.55 + 0.45 * sin(2.0 * ang + t1 + 2.1));

        // Soft volumetric ribbon aura
        float aura = exp(-pow((r - 1.99) / 0.18, 2.0)) * 0.22;

        // Grazing view boost (clamped cleanly so it never blows out)
        float viewBoost = clamp(0.65 + 0.35 / max(abs(rrd.y), 0.22), 0.7, 1.45);
        float ribbon = (f0 * 0.95 + f1 * 0.85 + f2 * 0.70 + aura) * viewBoost;

        accum += ringCol * ribbon;
        // Hot white-cyan core along the razor filaments
        accum += vec3(0.88, 0.96, 1.0) * (f0 * 0.45 + f2 * 0.35) * (uLightMode == 1 ? 0.5 : 1.0);

        // Two orbiting bright flare knots on the ring (like hero reference)
        float aKnot1 = -2.35 + t1;
        float aKnot2 =  0.72 + t1;
        vec2 k1Pos = vec2(cos(aKnot1), sin(aKnot1)) * 1.98;
        vec2 k2Pos = vec2(cos(aKnot2), sin(aKnot2)) * 2.00;
        float dK1 = length(q.xz - k1Pos);
        float dK2 = length(q.xz - k2Pos);
        float flare1 = exp(-dK1 * 9.0) * 1.35 + exp(-dK1 * 28.0) * 1.6;
        float flare2 = exp(-dK2 * 9.0) * 1.35 + exp(-dK2 * 28.0) * 1.6;
        vec3 flareCol = (uLightMode == 1) ? vec3(0.95) : mix(cyanCol, vec3(0.92, 0.98, 1.0), 0.55);
        accum += flareCol * (flare1 + flare2);
    }

    // 2. Seamless orbiting stardust particle field (2x2 neighbor polar cells -> zero boundary clipping)
    float radialMask = exp(-pow((r - 1.98) / 0.34, 2.0));
    if (radialMask > 0.02) {
        const float N_ANG = 84.0;
        const float N_RAD = 9.0;
        // Shift angle by -t1 (1 full revolution per 3.0s) -> exact integer wrap mod N_ANG!
        float rotAng = mod(ang - t1 + TAU * 2.0, TAU);
        float uA = rotAng * (N_ANG / TAU);
        float uR = (r - 1.35) * (N_RAD / 1.30);

        vec2 uCoord = vec2(uA, uR);
        vec2 baseCell = floor(uCoord - 0.5);
        vec2 metricScale = vec2(TAU * r / N_ANG, 1.30 / N_RAD);

        for (int ox = 0; ox <= 1; ox++) {
            for (int oy = 0; oy <= 1; oy++) {
                vec2 c = baseCell + vec2(float(ox), float(oy));
                float cidA = mod(c.x + N_ANG, N_ANG);
                float presence = step(0.34, hash21(vec2(cidA + 7.1, c.y + layerSeed * 31.0)));
                if (presence < 0.5) continue;

                vec2 rnd = hash22(vec2(cidA, c.y + layerSeed * 19.0));
                vec2 pCenter = c + 0.20 + 0.60 * rnd;
                vec2 dMetric = (uCoord - pCenter) * metricScale;
                float dist2 = dot(dMetric, dMetric);

                float sz = mix(0.00016, 0.00088, rnd.x * rnd.y);
                float twinkle = 0.62 + 0.38 * sin(2.0 * t1 + rnd.x * TAU * 3.0);
                float spark = exp(-dist2 / sz) * twinkle * radialMask;

                vec3 sparkCol = (uLightMode == 1)
                    ? vec3(0.95)
                    : mix(ringCol, vec3(0.95, 0.99, 1.0), 0.45 + 0.40 * rnd.y);
                accum += sparkCol * spark * (mainPlane ? 1.40 : 0.90);
            }
        }
    }

    return accum;
}

vec3 evalOrbitalRing(vec3 ro, vec3 rd, float tMax) {
    vec3 rro = gRingMat * (ro - gOrbCenter);
    vec3 rrd = gRingMat * rd;

    vec3 col = vec3(0.0);
    col += evalRingLayer(rro, rrd,  0.000, tMax, true,  0.0);
    col += evalRingLayer(rro, rrd,  0.085, tMax, false, 1.0);
    col += evalRingLayer(rro, rrd, -0.085, tMax, false, 2.0);
    return col;
}

// -----------------------------------------------------------------------------
// Raymarching (Primary & Reflection)
// -----------------------------------------------------------------------------

// Ray-sphere bounding test around gOrbCenter
vec2 intersectSphere(vec3 ro, vec3 rd, vec3 c, float r) {
    vec3 oc = ro - c;
    float b = dot(oc, rd);
    float cc = dot(oc, oc) - r * r;
    float h = b * b - cc;
    if (h < 0.0) return vec2(-1.0);
    h = sqrt(h);
    return vec2(-b - h, -b + h);
}

vec2 marchPrimary(vec3 ro, vec3 rd, float tMax) {
    vec2 bs = intersectSphere(ro, rd, gOrbCenter, 3.15);
    if (bs.y < 0.0 || bs.x > tMax) return vec2(-1.0, 0.0);

    float t = max(0.01, bs.x);
    float tEnd = min(tMax, bs.y);

    for (int i = 0; i < 92; i++) {
        vec3 p = ro + rd * t;
        vec2 h = mapSceneMat(p);
        if (h.x < 0.0012) {
            return vec2(t, h.y);
        }
        t += h.x * 0.86;
        if (t > tEnd) break;
    }
    return vec2(-1.0, 0.0);
}

vec2 marchReflection(vec3 ro, vec3 rd, float tMax) {
    vec2 bs = intersectSphere(ro, rd, gOrbCenter, 3.15);
    if (bs.y < 0.0 || bs.x > tMax) return vec2(-1.0, 0.0);

    float t = max(0.015, bs.x);
    float tEnd = min(tMax, bs.y);

    for (int i = 0; i < 46; i++) {
        vec3 p = ro + rd * t;
        vec2 h = mapSceneMat(p);
        if (h.x < 0.0025) {
            return vec2(t, h.y);
        }
        t += h.x * 0.90;
        if (t > tEnd) break;
    }
    return vec2(-1.0, 0.0);
}

// -----------------------------------------------------------------------------
// Mirror-Polished Liquid Mercury & Chrome Droplet Shading
// -----------------------------------------------------------------------------

vec3 shadeChromeSimple(vec3 p, vec3 n, vec3 rd, float matId) {
    vec3 rDir = reflect(rd, n);
    float ndv = clamp(dot(n, -rd), 0.0, 1.0);
    float fres = mix(0.74, 0.98, pow(1.0 - ndv, 4.0));

    vec3 env = sampleStudioEnv(rDir, false);
    // Include orbital ring reflection inside chrome
    env += evalOrbitalRing(p + n * 0.01, rDir, 8.0) * 0.85;

    // Crisp studio specular glints
    vec3 keyDir = (uLightMode == 2)
        ? normalize(vec3(0.88, 0.38, 0.28))
        : normalize(vec3(-0.34, 0.72, 0.60));
    vec3 hVec = normalize(keyDir - rd);
    float spec = pow(max(dot(n, hVec), 0.0), 96.0);
    vec3 specCol = (uLightMode == 1) ? vec3(1.1) : vec3(1.0, 0.99, 1.0) * 1.15;

    // Subtle iridescent chrome tint at grazing angles in hero mode
    vec3 tint = vec3(1.0);
    if (uLightMode == 0) {
        float side = smoothstep(-0.6, 0.6, n.x);
        vec3 rimHue = mix(vec3(0.62, 0.96, 1.0), vec3(0.92, 0.68, 1.0), side);
        tint = mix(vec3(0.95, 0.97, 1.0), rimHue, pow(1.0 - ndv, 2.2) * 0.45);
    }

    vec3 col = env * fres * tint + specCol * spec;
    // Slight droplet glass-mercury edge glow
    if (matId > 1.5 && uLightMode == 0) {
        float side = smoothstep(-1.2, 1.2, p.x);
        vec3 rimC = mix(vec3(0.10, 0.92, 1.0), vec3(0.82, 0.34, 1.0), side);
        col += rimC * pow(1.0 - ndv, 3.2) * 0.55;
    }
    return col;
}

vec3 shadeMercury(vec3 p, vec3 n, vec3 rd, float matId) {
    vec3 rDir = reflect(rd, n);
    float ndv = clamp(dot(n, -rd), 0.0, 1.0);
    float ao  = calcAO(p, n);

    // Metallic liquid mercury Fresnel: deep contrast at normal incidence, pure mirror at grazing
    float f0   = (matId > 1.5) ? 0.78 : 0.76;
    float fres = mix(f0, 0.99, pow(1.0 - ndv, 3.6));

    // Secondary self-reflection (orb <-> spikes <-> droplets)
    vec3 reflCol;
    vec2 rHit = marchReflection(p + n * 0.015, rDir, 6.5);
    if (rHit.x > 0.0) {
        vec3 rp = p + n * 0.015 + rDir * rHit.x;
        vec3 rn = calcNormal(rp);
        reflCol = shadeChromeSimple(rp, rn, rDir, rHit.y) * 0.88;
        // Add ring light if ring lies between p and rp
        reflCol += evalOrbitalRing(p + n * 0.015, rDir, rHit.x) * 0.85;
    } else {
        reflCol = sampleStudioEnv(rDir, false);
        reflCol += evalOrbitalRing(p + n * 0.015, rDir, 10.0) * 0.90;
    }

    // Crisp studio specular highlights
    vec3 keyDir = (uLightMode == 2)
        ? normalize(vec3(0.88, 0.38, 0.28))
        : normalize(vec3(-0.34, 0.72, 0.60));
    float spec1 = pow(max(dot(n, normalize(keyDir - rd)), 0.0), 120.0);

    vec3 rimDirL = normalize(vec3(-0.85, -0.05, 0.45));
    vec3 rimDirR = normalize(vec3( 0.85, -0.05, 0.45));
    float specL = pow(max(dot(n, normalize(rimDirL - rd)), 0.0), 48.0);
    float specR = pow(max(dot(n, normalize(rimDirR - rd)), 0.0), 48.0);

    vec3 col = reflCol * fres * mix(0.45, 1.0, ao);

    if (uLightMode == 1) {
        col += vec3(1.15) * spec1 + vec3(0.45) * (specL + specR);
    } else if (uLightMode == 2) {
        col += vec3(1.5, 1.42, 1.32) * spec1 + vec3(0.20, 0.40, 0.65) * specL * 0.35;
    } else if (uLightMode == 3) {
        float backRim = pow(1.0 - ndv, 2.8);
        vec3 rimHue = mix(vec3(0.15, 0.92, 1.0), vec3(0.85, 0.35, 1.0), smoothstep(-0.6, 0.6, n.x));
        col += rimHue * backRim * 2.1 + vec3(1.1) * spec1 * 0.5;
    } else {
        // Hero cyan/magenta sculptural studio highlights & thin-film rim sheen
        float side = smoothstep(-0.75, 0.75, n.x + 0.25 * (p.x - gOrbCenter.x));
        vec3 rimHue = mix(vec3(0.10, 0.92, 1.0), vec3(0.86, 0.32, 1.0), side);
        col += vec3(1.05, 1.08, 1.12) * spec1;
        col += vec3(0.10, 0.90, 1.00) * specL * 0.55;
        col += vec3(0.88, 0.28, 1.00) * specR * 0.55;
        col += rimHue * pow(1.0 - ndv, 3.5) * (matId > 1.5 ? 0.62 : 0.38);
    }

    return col;
}

// -----------------------------------------------------------------------------
// Dark Glossy Tiled Studio Stage Floor (y = 0)
// -----------------------------------------------------------------------------

vec3 shadeFloor(vec3 p, vec3 rd) {
    // Glossy studio tile grid + weathered/scuffed roughness patches
    vec2 tileUV = p.xz / 1.35;
    vec2 grid = abs(fract(tileUV - 0.5) - 0.5) / max(fwidth(tileUV), vec2(0.004));
    float seam = 1.0 - clamp(min(grid.x, grid.y), 0.0, 1.0);
    // Smooth analytical fallback for tile seam width at distance
    vec2 gDist = abs(fract(tileUV - 0.5) - 0.5);
    float seamMask = smoothstep(0.016, 0.002, min(gDist.x, gDist.y)) * exp(-length(p.xz) * 0.09);
    seam = max(seam * 0.35, seamMask);

    // Organic matte/glossy scuff variation across tiles (like hero reference)
    float scuff = fbm2(p.xz * 1.15 + vec2(3.2, -1.7));
    float gloss = smoothstep(0.32, 0.68, scuff); // 0 = matte dark scuff, 1 = mirror glossy

    // Subtle floor normal perturbation from tile seams & micro-scuffs
    float n1 = vnoise(p.xz * 3.5);
    float n2 = vnoise(p.xz * 3.5 + vec2(0.08, 0.0));
    float n3 = vnoise(p.xz * 3.5 + vec2(0.0, 0.08));
    vec3 fn = normalize(vec3(
        (n1 - n2) * (0.045 - 0.025 * gloss),
        1.0,
        (n1 - n3) * (0.045 - 0.025 * gloss)
    ));

    vec3 rDir = reflect(rd, fn);
    rDir.y = max(rDir.y, 0.005);

    // Trace reflection of mercury orb + spikes + droplets + orbital ring
    vec3 reflCol;
    vec2 rHit = marchReflection(p + vec3(0.0, 0.01, 0.0), rDir, 12.0);
    if (rHit.x > 0.0) {
        vec3 rp = p + vec3(0.0, 0.01, 0.0) + rDir * rHit.x;
        vec3 rn = calcNormal(rp);
        reflCol = shadeChromeSimple(rp, rn, rDir, rHit.y);
        // Also reflect glowing orbital ring in front of the reflected orb
        reflCol += evalOrbitalRing(p + vec3(0.0, 0.01, 0.0), rDir, rHit.x) * 0.95;
    } else {
        reflCol = sampleStudioEnv(rDir, false);
        reflCol += evalOrbitalRing(p + vec3(0.0, 0.01, 0.0), rDir, 14.0) * 0.95;
    }

    // Contact shadow / soft ambient occlusion directly beneath the hovering orb & droplets
    float rOrbXZ = length(p.xz - gOrbCenter.xz);
    float orbShadow = mix(0.28, 1.0, smoothstep(0.25, 1.95, rOrbXZ));
    // Ring shadow subtle halo on floor
    float ringShadow = mix(0.76, 1.0, smoothstep(0.0, 0.45, abs(rOrbXZ - 1.85)));
    float shadow = orbShadow * ringShadow;

    float ndv = clamp(dot(fn, -rd), 0.0, 1.0);
    float fres = mix(0.16, 0.88, pow(1.0 - ndv, 3.2));
    float reflStrength = fres * mix(0.28, 0.92, gloss) * (1.0 - 0.72 * seam);

    // Dark obsidian floor albedo
    vec3 baseCol = (uLightMode == 1)
        ? vec3(0.012)
        : vec3(0.0032, 0.0040, 0.0068);

    // Subtle colored stage bounce under the orb
    if (uLightMode == 0) {
        float side = smoothstep(-2.2, 2.2, p.x);
        vec3 stageGlow = mix(vec3(0.05, 0.38, 0.52), vec3(0.42, 0.14, 0.55), side);
        baseCol += stageGlow * 0.055 * exp(-rOrbXZ * 0.50);
    }

    vec3 col = baseCol * shadow + reflCol * reflStrength * mix(0.50, 1.0, shadow);
    // Faint specular glint along tile grout edges
    vec3 seamTint = (uLightMode == 1) ? vec3(0.06) : vec3(0.025, 0.042, 0.068);
    col = mix(col, seamTint * shadow, seam * 0.55);

    // Distance fog blending smoothly into dark arena horizon
    float dist = length(p.xz);
    float fog = 1.0 - exp(-dist * dist * 0.0075);
    vec3 horizonCol = sampleStudioEnv(vec3(rd.x, 0.02, rd.z), true);
    col = mix(col, horizonCol, clamp(fog, 0.0, 0.85));

    return col;
}

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

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 Entry
// -----------------------------------------------------------------------------

void main() {
    initChoreography();

    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;

    // Camera framing
    vec3 ro, ta;
    float fl;
    if (uCamMode == 1) {
        // ~2.5x macro close-up on glossy mirror-chrome ferrofluid spikes & orbiting droplets
        ro = vec3(0.48, 1.66, 2.48);
        ta = vec3(0.06, 1.50, 0.00);
        fl = 2.02;
    } else {
        // Hero framing pulled back so full tilted stardust ring & droplets fit with generous margin
        ro = vec3(0.00, 1.54, 6.42);
        ta = vec3(0.00, 1.26, 0.00);
        fl = 1.62;
    }

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

    // Analytical glossy floor intersection (y = 0)
    float tFloor = (rd.y < -0.0004) ? (-ro.y / rd.y) : -1.0;
    float tMax   = (tFloor > 0.0) ? min(tFloor, 26.0) : 26.0;

    // Primary raymarch against mercury orb, spikes, and orbiting droplets
    vec2 hit = marchPrimary(ro, rd, tMax);

    vec3 col;
    float tOcclude;

    if (hit.x > 0.0) {
        tOcclude = hit.x;
        vec3 p = ro + rd * hit.x;
        vec3 n = calcNormal(p);
        col = shadeMercury(p, n, rd, hit.y);
    } else if (tFloor > 0.0 && tFloor < 26.0) {
        tOcclude = tFloor;
        vec3 p = ro + rd * tFloor;
        col = shadeFloor(p, rd);
    } else {
        tOcclude = 26.0;
        col = sampleStudioEnv(rd, true);
        col += evalStageSpotlights(uv);
    }

    // Composite tilted iridescent cyan-violet orbital stardust rings with exact depth occlusion
    vec3 ringLight = evalOrbitalRing(ro, rd, tOcclude);
    col += ringLight;

    // Subtle atmospheric glow halo tightly around the singularity (preserves dark arena contrast)
    vec3 toOrb = gOrbCenter - ro;
    float proj = max(dot(toOrb, rd), 0.0);
    float dRayOrb = length(toOrb - rd * proj);
    float halo = exp(-dRayOrb * 2.15) * 0.036;
    if (uLightMode == 0) {
        vec3 haloCol = mix(vec3(0.06, 0.72, 0.95), vec3(0.68, 0.22, 0.98), smoothstep(-0.35, 0.35, uv.x));
        col += haloCol * halo;
    } else if (uLightMode == 1) {
        col += vec3(0.025) * halo;
    } else if (uLightMode == 3) {
        vec3 haloCol = mix(vec3(0.10, 0.85, 1.0), vec3(0.84, 0.30, 1.0), smoothstep(-0.35, 0.35, uv.x));
        col += haloCol * exp(-dRayOrb * 2.45) * 0.14;
    }

    // Gentle studio vignette (keeping corners comfortably above lumMin >= 4)
    float vig = 1.0 - 0.24 * dot(uv, uv);
    col *= clamp(vig, 0.70, 1.0);

    // Subtle dark studio ambient floor lift so shadows stay rich obsidian (lumMin ~8-12, 0% black)
    col = max(col, vec3(0.00075, 0.00085, 0.00125));

    // Exposure & ACES filmic tonemapping (controlled peaks so %clip < 1%)
    col *= 0.88;
    col = acesTonemap(col) * 0.985;

    // Gamma encode (sRGB approximation)
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));

    // Strict R==G==B guarantee in neutral grayscale chrome/form study mode
    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

Identifiermorphing-liquid-mercury-singularity
CategoryGame VFX Shaders · Choreographed Loop VFX
Upstream studyvfx_animated/lib/09_morphing_liquid_mercury_singularity.glsl (commit f3d2025)
Animation cycle3.0 s seamless harmonic loop
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size921 lines · 35.8 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

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · 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