Arcane Hex Shield Barrier shader

arcane-shield-hex-barrier.glsl · 855 lines · GLSL ES 3.00 · MIT

Arcane Hex Shield BarrierGame VFX3s Loop
Arcane Hex Shield Barrier WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 855 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

Arcane Palette
arcane-shield-hex-barrier.glsl (855 lines)
#version 300 es
// Arcane Hex Shield Barrier (arcane-shield-hex-barrier.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_palette/lib/02_arcane_shield_hex_barrier.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;

// Unified Arcane Palette (Locked)
const vec3 COL_OBSIDIAN = vec3(0.03, 0.05, 0.12);
const vec3 COL_COBALT   = vec3(0.08, 0.32, 0.98);
const vec3 COL_CYAN     = vec3(0.10, 0.85, 1.00);
const vec3 COL_GOLD     = vec3(1.00, 0.68, 0.18);
const vec3 COL_CORE     = vec3(1.00, 0.96, 0.86);

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

const vec3 SPHERE_CENTER = vec3(0.0, 1.06, 0.0);
const float SPHERE_RADIUS = 0.95;

// -----------------------------------------------------------------------------
// Hash & Noise Utilities (NaN-safe, deterministic)
// -----------------------------------------------------------------------------
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);
}

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 noise2D(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = 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, u.x), mix(c, d, u.x), u.y);
}

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

float noise3D(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);
}

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

// -----------------------------------------------------------------------------
// Exact Icosahedral Geodesic Goldberg GP(2,0) Hex/Pentagon Lattice on Sphere
// Zero polar pinching, exact 3-way vertex nodes, exact world cell centers.
// -----------------------------------------------------------------------------
struct GeodesicInfo {
    float edgeDist;
    float nodeDist;
    float cellHash;
    vec3 cellCenter;
    float isPentagon;
};

GeodesicInfo evalGeodesicHex(vec3 dir) {
    // Icosahedral [5, 3] Coxeter reflection plane normal:
    // n1 = (1,0,0), n2 = (0,1,0), n3 = (-0.5, -phi/2, (phi-1)/2)
    const vec3 n3 = vec3(-0.5, -0.80901699437, 0.30901699437);

    // Fundamental domain Voronoi sites for GP(2,0) (30 hexagons at v2 + 12 pentagons at v5)
    const vec3 v2 = vec3(0.0, 0.0, 1.0);
    const vec3 v5 = vec3(0.52573111212, 0.0, 0.85065080835);
    const vec3 v3 = vec3(0.0, 0.35682208977, 0.93417235896);
    // Adjacent hexagon site reflected across n3, and its mirror across n1 (x=0)
    const vec3 s2 = vec3(0.30901699437, 0.50000000000, 0.80901699437);
    const vec3 s3 = vec3(-0.30901699437, 0.50000000000, 0.80901699437);
    // Exact 3-way vertex inside fundamental domain (pentagon + 2 hexagons)
    const vec3 V0 = vec3(0.26800150160, 0.19471449603, 0.94354834406);

    vec3 p = normalize(dir);
    mat3 M = mat3(1.0);

    for (int i = 0; i < 5; i++) {
        if (p.x < 0.0) {
            p.x = -p.x;
            M[0].x = -M[0].x;
            M[1].x = -M[1].x;
            M[2].x = -M[2].x;
        }
        if (p.y < 0.0) {
            p.y = -p.y;
            M[0].y = -M[0].y;
            M[1].y = -M[1].y;
            M[2].y = -M[2].y;
        }
        float t = dot(p, n3);
        if (t < 0.0) {
            p -= 2.0 * t * n3;
            M[0] -= 2.0 * dot(M[0], n3) * n3;
            M[1] -= 2.0 * dot(M[1], n3) * n3;
            M[2] -= 2.0 * dot(M[2], n3) * n3;
        }
    }

    // Evaluate dot similarities to the 4 candidate sites around the fundamental triangle
    float d0 = dot(p, v2);
    float d1 = dot(p, v5);
    float d2 = dot(p, s2);
    float d3 = dot(p, s3);

    vec3 site1 = v2;
    float m1 = d0;
    float isPent = 0.0;

    vec3 site2 = v5;
    float m2 = d1;

    if (d1 > m1) {
        site2 = site1; m2 = m1;
        site1 = v5;    m1 = d1;
        isPent = 1.0;
    }

    if (d2 > m1) {
        site2 = site1; m2 = m1;
        site1 = s2;    m1 = d2;
        isPent = 0.0;
    } else if (d2 > m2) {
        site2 = s2;    m2 = d2;
    }

    if (d3 > m1) {
        site2 = site1; m2 = m1;
        site1 = s3;    m1 = d3;
        isPent = 0.0;
    } else if (d3 > m2) {
        site2 = s3;    m2 = d3;
    }

    float denom = max(length(site1 - site2), 1e-4);
    float edgeDist = max(0.0, (m1 - m2) / denom);
    float nodeDist = min(length(p - V0), length(p - v3));

    vec3 worldCenter = normalize(transpose(M) * site1);
    float cHash = hash13(floor(worldCenter * 100.0 + 0.5));

    GeodesicInfo info;
    info.edgeDist = edgeDist;
    info.nodeDist = nodeDist;
    info.cellHash = cHash;
    info.cellCenter = worldCenter;
    info.isPentagon = isPent;
    return info;
}

// -----------------------------------------------------------------------------
// Analytical Ray-Sphere Intersection
// -----------------------------------------------------------------------------
vec2 intersectSphere(vec3 ro, vec3 rd, vec3 center, float radius) {
    vec3 oc = ro - center;
    float b = dot(oc, rd);
    float c = dot(oc, oc) - radius * radius;
    float h = b * b - c;
    if (h < 0.0) return vec2(-1.0);
    h = sqrt(max(h, 0.0));
    return vec2(-b - h, -b + h);
}

// -----------------------------------------------------------------------------
// Wet Obsidian Floor Heightfield & Normal (Unit-Weight Tetrahedral / Central)
// -----------------------------------------------------------------------------
float floorTileHeight(vec2 xz, out float groutMask, out float wetness, out vec2 tileId) {
    // Diagonal tile layout (~30 deg) matching reference
    vec2 p = rot2D(0.52) * xz * 0.92;
    tileId = floor(p);
    vec2 f = fract(p) - 0.5;
    vec2 d = abs(f);
    float edge = 0.5 - max(d.x, d.y);

    // Crisp beveled dark obsidian tile joints
    groutMask = smoothstep(0.008, 0.028, edge);
    float bevel = smoothstep(0.008, 0.055, edge);

    // Per-tile subtle elevation & micro-variation
    float tRand = hash12(tileId);
    float micro = fbm2D(xz * 3.8 + tRand * 11.0);
    float puddleNoise = fbm2D(xz * 0.85 + vec2(4.2, -1.7));

    // Wetter around the central arcane pool & diagonal puddles
    float radialWet = exp(-dot(xz, xz) * 0.32);
    wetness = clamp(smoothstep(0.32, 0.68, puddleNoise + 0.35 * radialWet) * groutMask, 0.0, 1.0);

    // Concentric water ripple height under the barrier
    float r = length(xz);
    float phase = mod(uTime, 3.0) / 3.0;
    float rippleEnv = exp(-r * 1.45) * smoothstep(0.06, 0.25, r);
    float ripple = sin(r * 26.0 - phase * TAU * 2.0) * 0.0035 * rippleEnv;

    float h = bevel * 0.012 + (tRand - 0.5) * 0.004 + (micro - 0.5) * 0.005 * (1.0 - 0.75 * wetness) + ripple;
    return h;
}

vec3 floorNormal(vec2 xz, out float groutMask, out float wetness, out vec2 tileId) {
    float g0, w0;
    vec2 id0;
    float h0 = floorTileHeight(xz, groutMask, wetness, tileId);

    // Unit-weight tetrahedral-style stencil in XZ
    const vec2 k = vec2(1.0, -1.0);
    const float eps = 0.004;
    float h1 = floorTileHeight(xz + k.xx * eps, g0, w0, id0);
    float h2 = floorTileHeight(xz + k.xy * eps, g0, w0, id0);
    float h3 = floorTileHeight(xz + k.yx * eps, g0, w0, id0);
    float h4 = floorTileHeight(xz + k.yy * eps, g0, w0, id0);

    vec3 n = vec3(
        (h3 + h4) - (h1 + h2),
        4.0 * eps,
        (h2 + h4) - (h1 + h3)
    );
    // Add subtle per-tile planar tilt
    vec2 tilt = (hash22(tileId) - 0.5) * 0.018 * groutMask * (1.0 - 0.6 * wetness);
    n.xz += tilt * (4.0 * eps);
    return normalize(n);
}

// -----------------------------------------------------------------------------
// Studio Environment Backdrop (Dark Moody Arena + Softboxes)
// -----------------------------------------------------------------------------
vec3 evalStudioEnv(vec3 rd, int lightMode) {
    float y = clamp(rd.y, -1.0, 1.0);
    vec3 base = mix(COL_OBSIDIAN * 0.08, COL_OBSIDIAN * 0.42, smoothstep(-0.15, 0.65, y));

    // Studio key softbox (upper right-front)
    vec3 keyDir = normalize(vec3(0.58, 0.68, 0.45));
    if (lightMode == 2) keyDir = normalize(vec3(0.92, 0.24, 0.30));
    if (lightMode == 3) keyDir = normalize(vec3(-0.20, 0.45, -0.87));

    float keySpec = pow(max(dot(rd, keyDir), 0.0), 22.0);
    float rimSpec = pow(max(dot(rd, normalize(vec3(-0.72, 0.52, -0.46))), 0.0), 14.0);
    float softBand = smoothstep(0.02, 0.35, y) * smoothstep(0.85, 0.25, y) * 0.032;

    vec3 col = base + vec3(0.08, 0.14, 0.28) * softBand;
    col += vec3(0.32, 0.48, 0.78) * keySpec * (lightMode == 3 ? 1.15 : 0.55);
    col += vec3(0.14, 0.38, 0.72) * rimSpec * (lightMode == 2 ? 0.85 : 0.32);

    if (lightMode == 1) {
        float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
        return vec3(lum * 1.15 + 0.025);
    }
    return col;
}

// -----------------------------------------------------------------------------
// Arcane Rune SDFs (2D billboard local coords in [-1, 1])
// -----------------------------------------------------------------------------
float sdSegment(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
    return length(pa - ba * h);
}

float evalRuneSDF(vec2 uv, int type) {
    if (type == 0) {
        // 4-pointed concave arcane star diamond
        vec2 a = abs(uv);
        float d1 = sdSegment(a, vec2(0.0, 0.62), vec2(0.20, 0.20));
        float d2 = sdSegment(a, vec2(0.20, 0.20), vec2(0.62, 0.0));
        float inner = abs(length(a) - 0.14);
        return min(min(d1, d2), inner + 0.03);
    } else if (type == 1) {
        // Twin-pillar arcane 'H' / Gemini sigil
        vec2 p = uv;
        float curveX = 0.22 - 0.07 * cos(p.y * 3.0);
        float pillars = abs(abs(p.x) - curveX);
        pillars = max(pillars, abs(p.y) - 0.52);
        float topBar = sdSegment(vec2(abs(p.x), p.y), vec2(0.0, 0.48), vec2(0.38, 0.54));
        float botBar = sdSegment(vec2(abs(p.x), p.y), vec2(0.0, -0.48), vec2(0.38, -0.54));
        float midBar = sdSegment(p, vec2(-0.22, 0.04), vec2(0.22, 0.04));
        return min(min(pillars, midBar), min(topBar, botBar));
    } else if (type == 2) {
        // Arcane lozenge with inner cross
        vec2 a = abs(uv);
        float d = sdSegment(a, vec2(0.0, 0.58), vec2(0.46, 0.0));
        float c1 = sdSegment(a, vec2(0.0, 0.0), vec2(0.24, 0.24));
        return min(d, c1 + 0.015);
    } else {
        // Arcane chalice / crescent glyph
        float arc = abs(length(uv - vec2(0.0, 0.18)) - 0.34);
        arc = max(arc, uv.y - 0.22);
        float stem = sdSegment(uv, vec2(0.0, -0.16), vec2(0.0, -0.54));
        float base = sdSegment(uv, vec2(-0.28, -0.54), vec2(0.28, -0.54));
        return min(arc, min(stem, base));
    }
}

// -----------------------------------------------------------------------------
// Shade Geodesic Energy Barrier Shell (Front or Back Face)
// -----------------------------------------------------------------------------
vec4 shadeBarrierShell(
    vec3 hitPos,
    vec3 normal,
    vec3 viewDir,
    bool isFront,
    float loopPhase,
    float openAmt,
    int lightMode
) {
    // Rotate sphere slightly so a crisp hexagon faces the front-center
    vec3 localDir = normalize(hitPos - SPHERE_CENTER);
    vec3 latticeDir = localDir;
    latticeDir.xz = rot2D(0.04 * sin(loopPhase * TAU)) * latticeDir.xz;
    latticeDir.yz = rot2D(0.08) * latticeDir.yz;

    GeodesicInfo geo = evalGeodesicHex(latticeDir);

    // Impact point on the front face
    vec3 impactDir = normalize(vec3(0.01, 0.03, 1.0));
    float cosTheta = clamp(dot(localDir, impactDir), -1.0, 1.0);
    float theta = acos(cosTheta);

    // Per-cell angular distance from impact for crisp quantized hex shockwave
    float cellTheta = acos(clamp(dot(geo.cellCenter, impactDir), -1.0, 1.0));

    // Seamless 3-second pulsing shockwaves radiating from impact across the sphere
    float wave1Pos = fract(loopPhase) * (PI + 0.65) - 0.15;
    float wave2Pos = fract(loopPhase + 0.5) * (PI + 0.65) - 0.15;

    float shock1 = exp(-pow((theta - wave1Pos) * 5.2, 2.0)) * smoothstep(PI + 0.2, 0.15, wave1Pos);
    float shock2 = exp(-pow((theta - wave2Pos) * 5.8, 2.0)) * smoothstep(PI + 0.2, 0.15, wave2Pos) * 0.65;
    float cellShock = exp(-pow((cellTheta - wave1Pos) * 4.5, 2.0)) * smoothstep(PI + 0.2, 0.15, wave1Pos);
    float shock = (shock1 + shock2) * (1.0 + 0.75 * openAmt);

    // Crisp glowing hex lattice lines & bright 3-way vertex nodes
    float lineSharp = smoothstep(0.022, 0.004, geo.edgeDist);
    float lineHalo  = exp(-geo.edgeDist * 26.0);
    float nodeCore  = smoothstep(0.042, 0.012, geo.nodeDist);
    float nodeHalo  = exp(-geo.nodeDist * 22.0);

    // Impact starburst & golden-cyan hex fracture glow
    float burstPulse = 0.88 + 0.12 * sin(loopPhase * TAU * 2.0);
    float burstScale = mix(1.0, 1.55, openAmt) * burstPulse;

    // Tangent-plane polar coordinates around impactDir for sharp starburst rays
    vec3 upRef = vec3(0.0, 1.0, 0.0);
    vec3 impRight = normalize(cross(upRef, impactDir));
    vec3 impUp = cross(impactDir, impRight);
    vec2 impUV = vec2(dot(localDir, impRight), dot(localDir, impUp));
    float impAng = atan(impUV.y, impUV.x);

    float rayNoise1 = noise2D(vec2(impAng * 9.0, theta * 4.5 - loopPhase * 6.0));
    float rayNoise2 = noise2D(vec2(impAng * 22.0 + 3.1, theta * 7.0 - loopPhase * 9.0));
    float starRays = pow(clamp(0.55 * rayNoise1 + 0.45 * rayNoise2, 0.0, 1.0), 2.8);
    float fineSpikes = pow(max(0.0, cos(impAng * 18.0 + sin(impAng * 5.0) * 1.4)), 8.0);
    float rayMask = exp(-theta * mix(4.2, 2.9, openAmt)) * (0.28 + 1.25 * starRays + 0.55 * fineSpikes);

    float impactCore = exp(-theta * theta * mix(46.0, 28.0, openAmt)) * burstScale;
    float impactCorona = exp(-theta * mix(5.2, 3.6, openAmt)) * burstScale;

    // uOpen hex fracture veins inside cells near the impact
    float fracNoise = abs(noise3D(localDir * 14.0 + geo.cellHash * 7.0) - 0.5);
    float fracLines = smoothstep(0.052, 0.008, fracNoise) * exp(-theta * 2.4) * (0.14 + 0.85 * openAmt);

    // Swirling arcane plasma inside the translucent dome
    float swirl = noise3D(localDir * 3.8 + vec3(0.0, -loopPhase * 1.5, loopPhase * 0.8));
    float swirl2 = noise3D(localDir * 7.5 - vec3(loopPhase * 1.2, 0.0, 0.0));
    float plasma = 0.55 * swirl + 0.45 * swirl2;

    // Fresnel & glass specular
    float ndotv = clamp(abs(dot(normal, viewDir)), 0.0, 1.0);
    float fresnel = pow(1.0 - ndotv, 3.0);
    float rimTight = pow(1.0 - ndotv, 5.5);

    // Subtle hex bevel normal perturbation for studio specular / clay mode
    vec3 geomN = normal;
    if (isFront) {
        vec3 tang = normalize(cross(normal, geo.cellCenter + vec3(1e-4)));
        geomN = normalize(normal + tang * (lineHalo * 0.10));
    }

    // Studio key light direction
    vec3 keyDir = normalize(vec3(0.58, 0.66, 0.48));
    if (lightMode == 2) keyDir = normalize(vec3(0.92, 0.24, 0.30));
    if (lightMode == 3) keyDir = normalize(vec3(-0.20, 0.45, -0.87));

    vec3 H = normalize(keyDir + viewDir);
    float specSoftbox = smoothstep(0.962, 0.994, dot(geomN, H)) * 0.75;
    float specBroad   = pow(max(dot(geomN, H), 0.0), 48.0) * 0.32;

    // Neutral Grayscale Mode (uLightMode == 1, emission muted)
    if (lightMode == 1) {
        float diff = max(dot(geomN, keyDir), 0.0) * 0.50 + 0.16;
        float gray = diff * (0.18 + 0.16 * geo.cellHash) + fresnel * 0.48 + specSoftbox * 0.68 + specBroad * 0.28;
        gray += (lineSharp * 0.35 + nodeCore * 0.42 + fracLines * 0.18) * (isFront ? 1.0 : 0.42);
        gray += impactCore * 0.15;
        float alpha = isFront ? clamp(0.24 + fresnel * 0.55 + lineSharp * 0.42 + specSoftbox * 0.38, 0.0, 0.92)
                              : clamp(0.14 + fresnel * 0.28 + lineSharp * 0.22, 0.0, 0.62);
        return vec4(vec3(gray), alpha);
    }

    // Hero / Grazing / Backlit Shading (Locked Arcane Palette — Deep Translucent Cobalt Glass)
    vec3 col = vec3(0.0);

    // Deep translucent cobalt-indigo energy membrane (no milky wash)
    vec3 cellTint = mix(COL_COBALT * 0.55, COL_CYAN * 0.72, 0.14 + 0.24 * plasma + 0.16 * geo.cellHash);
    col += cellTint * (0.06 + 0.34 * fresnel);

    // Crisp glowing cyan hex lattice ribs + bright 3-way nodes
    vec3 edgeCol = mix(COL_CYAN, COL_CORE, 0.25 + 0.40 * shock + 0.30 * impactCorona);
    vec3 nodeCol = mix(COL_CYAN, COL_CORE, 0.55 + 0.25 * shock);
    float latticeWeight = isFront ? 1.0 : 0.38;

    col += edgeCol * lineSharp * (1.15 + 0.75 * shock) * latticeWeight;
    col += mix(COL_COBALT, COL_CYAN, 0.55) * lineHalo * (0.32 + 0.42 * shock) * latticeWeight;
    col += nodeCol * nodeCore * 1.45 * latticeWeight;
    col += COL_CYAN * nodeHalo * 0.38 * latticeWeight;

    // Quantized hex cell illumination on shockwave pass
    col += mix(COL_COBALT, COL_CYAN, 0.55) * cellShock * (0.28 + 0.35 * openAmt) * (isFront ? 0.75 : 0.35);

    // Tight warm arcane-gold starburst impact & hex fracture (strongest on front face)
    if (isFront) {
        col += COL_GOLD * (impactCorona * 0.95 + rayMask * 1.15 * burstScale);
        col += COL_CORE * (impactCore * 1.55 + rayMask * impactCore * 0.75);
        col += mix(COL_GOLD, COL_CORE, 0.42) * fracLines * 1.15;
    } else {
        col += COL_GOLD * impactCorona * 0.16;
    }

    // Electric cyan-white Fresnel rim brightening
    float rimBoost = (lightMode == 3) ? 1.45 : ((lightMode == 2) ? 1.20 : 1.0);
    col += mix(COL_CYAN, COL_CORE, 0.32) * rimTight * 1.35 * rimBoost * (isFront ? 1.0 : 0.48);

    // Crisp glassy studio specular reflection on outer shell
    if (isFront) {
        vec3 reflDir = reflect(-viewDir, geomN);
        vec3 envRefl = evalStudioEnv(reflDir, lightMode);
        col += envRefl * (0.14 + 0.48 * fresnel);
        col += mix(COL_CYAN, COL_CORE, 0.85) * (specSoftbox * 0.85 + specBroad * 0.35);
    }

    // Bottom energy tether glow where dome hovers over floor pool
    float bottomGlow = smoothstep(-0.42, -0.96, localDir.y);
    col += mix(COL_CYAN, COL_CORE, 0.38) * bottomGlow * 0.65;

    float alpha = isFront
        ? clamp(0.14 + 0.50 * fresnel + 0.58 * lineSharp + 0.32 * impactCore + 0.28 * specSoftbox, 0.0, 0.95)
        : clamp(0.10 + 0.28 * fresnel + 0.32 * lineSharp, 0.0, 0.68);

    return vec4(col, alpha);
}

// -----------------------------------------------------------------------------
// Volumetric Interior Glow & Downward Energy Column
// -----------------------------------------------------------------------------
vec3 evalInteriorVolume(vec3 ro, vec3 rd, vec2 tHit, float loopPhase, float openAmt, int lightMode) {
    if (lightMode == 1) return vec3(0.0);

    vec3 pMid = ro + rd * (0.5 * (tHit.x + tHit.y));
    float chord = max(0.0, tHit.y - tHit.x);
    vec3 rel = pMid - SPHERE_CENTER;

    // Central impact core interior volumetric glow (tight & warm gold)
    vec3 corePos = SPHERE_CENTER + vec3(0.01, 0.02, 0.35);
    vec3 toCore = corePos - ro;
    float tClosest = clamp(dot(toCore, rd), tHit.x, tHit.y);
    float dCore = length((ro + rd * tClosest) - corePos);
    float coreVol = exp(-dCore * dCore * 11.5) * (0.55 + 0.42 * openAmt);

    // Downward energy waterfall / column from core to bottom floor pool
    float tCol = clamp(dot(SPHERE_CENTER - ro, rd), tHit.x, tHit.y);
    vec3 pCol = ro + rd * tCol;
    float colRad = length(pCol.xz - SPHERE_CENTER.xz);
    float colYMask = smoothstep(SPHERE_CENTER.y + 0.08, SPHERE_CENTER.y - 0.18, pCol.y)
                   * smoothstep(0.0, 0.16, pCol.y);
    float colNoise = noise3D(vec3(pCol.xz * 7.0, pCol.y * 5.0 + loopPhase * 4.0));
    float energyCol = exp(-colRad * colRad * 26.0) * colYMask * (0.55 + 0.55 * colNoise);

    // Deep interior cobalt nebula
    float neb = noise3D(rel * 2.8 + vec3(0.0, loopPhase * 1.2, 0.0));
    vec3 vol = COL_COBALT * chord * 0.065 * (0.4 + 0.6 * neb);
    vol += mix(COL_GOLD, COL_CORE, 0.45) * coreVol * 0.62;
    vol += mix(COL_CYAN, COL_CORE, 0.48) * energyCol * 0.58;
    return vol;
}

// -----------------------------------------------------------------------------
// Concentric Cyan Water/Energy Rings on Wet Floor (y = 0)
// -----------------------------------------------------------------------------
vec3 evalFloorRings(vec2 xz, float loopPhase, float openAmt, int lightMode) {
    float r = length(xz);

    // Animated outward-expanding concentric rings (3s seamless loop)
    float ringWave1 = pow(0.5 + 0.5 * sin(r * 22.0 - loopPhase * TAU * 2.0), 7.0);
    float ringWave2 = pow(0.5 + 0.5 * sin(r * 35.0 - loopPhase * TAU * 3.0 + 1.2), 9.0);
    float shimmer = 0.72 + 0.28 * noise2D(vec2(atan(xz.y, xz.x) * 4.0, r * 6.0 - loopPhase * 6.0));

    float ringEnv = smoothstep(0.16, 0.34, r) * smoothstep(1.32, 0.48, r);
    float rings = (ringWave1 * 0.85 + ringWave2 * 0.55) * ringEnv * shimmer;

    // Bright glowing central energy pool directly under the dome
    float pool = exp(-r * r * 18.0) * (0.95 + 0.35 * openAmt);
    float poolRing = exp(-pow((r - 0.32) * 15.0, 2.0)) * 0.85;

    // Tight cobalt-cyan illumination spill on wet floor (keeps outer tiles dark obsidian)
    float spill = exp(-r * 2.05) * (0.28 + 0.16 * openAmt);

    if (lightMode == 1) {
        return vec3((rings * 0.12 + pool * 0.15) * 0.42);
    }

    vec3 col = COL_COBALT * spill * 0.72;
    col += mix(COL_CYAN, COL_CORE, 0.30) * rings * (1.05 + 0.32 * openAmt);
    col += mix(COL_CYAN, COL_CORE, 0.60) * (pool + poolRing * 0.60);
    return col;
}

// -----------------------------------------------------------------------------
// Shade Wet Tiled Obsidian Arena Floor (with Real Sphere Reflection!)
// -----------------------------------------------------------------------------
vec3 shadeFloor(vec3 hitPos, vec3 rd, float loopPhase, float openAmt, int lightMode) {
    float groutMask, wetness;
    vec2 tileId;
    vec3 N = floorNormal(hitPos.xz, groutMask, wetness, tileId);
    vec3 V = -rd;

    float tRand = hash12(tileId);
    vec3 albedo = mix(COL_OBSIDIAN * 0.08, COL_OBSIDIAN * (0.18 + 0.10 * tRand), groutMask);
    if (lightMode == 1) {
        albedo = vec3(mix(0.03, 0.08 + 0.03 * tRand, groutMask));
    }

    // Studio key light
    vec3 keyDir = normalize(vec3(0.55, 0.72, 0.42));
    if (lightMode == 2) keyDir = normalize(vec3(0.92, 0.20, 0.32));
    if (lightMode == 3) keyDir = normalize(vec3(-0.18, 0.42, -0.89));

    float ndotl = max(dot(N, keyDir), 0.0);
    float ndotv = clamp(dot(N, V), 0.0, 1.0);
    float fresnel = mix(0.04, 0.85, pow(1.0 - ndotv, 4.0));
    float gloss = mix(0.22, 0.92, wetness);

    // Direct studio diffuse + GGX-like specular on wet obsidian tiles
    vec3 H = normalize(keyDir + V);
    float specPow = mix(20.0, 150.0, wetness);
    float spec = pow(max(dot(N, H), 0.0), specPow) * mix(0.10, 0.85, wetness);

    vec3 keyCol = (lightMode == 1) ? vec3(0.75) : vec3(0.45, 0.65, 0.95);
    vec3 col = albedo * (0.20 + 0.65 * ndotl) + keyCol * spec * 0.45;

    // Analytical reflection of studio environment + glowing arcane dome!
    vec3 R = reflect(rd, N);
    vec3 envRefl = evalStudioEnv(R, lightMode);

    vec2 reflSphere = intersectSphere(hitPos + N * 0.005, R, SPHERE_CENTER, SPHERE_RADIUS);
    if (reflSphere.x > 0.0) {
        vec3 spHit = hitPos + N * 0.005 + R * reflSphere.x;
        vec3 spN = normalize(spHit - SPHERE_CENTER);
        vec4 spShade = shadeBarrierShell(spHit, spN, -R, true, loopPhase, openAmt, lightMode);
        envRefl = mix(envRefl, spShade.rgb, spShade.a * mix(0.35, 0.88, wetness));
    }

    col += envRefl * fresnel * gloss * (lightMode == 1 ? 0.48 : 0.82);

    // Add concentric cyan water/energy rings + central energy pool
    vec3 rings = evalFloorRings(hitPos.xz, loopPhase, openAmt, lightMode);
    col += rings * mix(0.65, 1.12, wetness);

    // Warm golden reflection flecks from orbiting runes on the wet foreground tiles
    if (lightMode != 1) {
        float fgDist = length(hitPos.xz - vec2(0.18, 1.38));
        float sparkleField = pow(noise2D(hitPos.xz * 14.0 + vec2(0.0, loopPhase * 2.0)), 6.0);
        float goldSpill = exp(-fgDist * 1.75) * wetness * (0.18 + 1.5 * sparkleField);
        col += COL_GOLD * goldSpill * 0.48;
    }

    // Atmospheric distance fade into deep obsidian navy
    float dist = length(hitPos.xz);
    float fog = 1.0 - exp(-dist * dist * 0.025);
    vec3 fogCol = (lightMode == 1) ? vec3(0.045) : (COL_OBSIDIAN * 0.14);
    col = mix(col, fogCol, clamp(fog, 0.0, 0.90));

    return col;
}

// -----------------------------------------------------------------------------
// Floating Golden Rune Motes & Sparkles Orbiting Outside the Dome
// -----------------------------------------------------------------------------
vec3 evalFloatingRunes(
    vec3 ro,
    vec3 rd,
    vec3 camRight,
    vec3 camUp,
    float maxT,
    float loopPhase,
    float openAmt,
    int lightMode
) {
    if (lightMode == 1) return vec3(0.0);

    vec3 accum = vec3(0.0);
    float loopAng = loopPhase * TAU;

    // 14 crisp golden arcane rune glyphs orbiting in a halo outside the barrier
    for (int i = 0; i < 14; i++) {
        float fi = float(i);
        float baseAng = fi * (TAU / 14.0) + 0.22;
        // Orbit slowly around Z/Y so runes frame the sphere silhouette like reference
        float ang = baseAng + 0.14 * sin(loopAng + fi * 0.9);
        float rad = SPHERE_RADIUS + 0.21 + 0.07 * sin(fi * 2.3 + loopAng);
        float zOff = 0.28 * cos(fi * 1.7 + loopAng);

        vec3 rPos = SPHERE_CENTER + vec3(cos(ang) * rad, sin(ang) * rad * 0.98 + 0.04, zOff);
        // Keep runes above the floor
        rPos.y = max(rPos.y, 0.16);

        // Intersect ray with camera-facing billboard plane at rPos
        float denom = dot(rd, normalize(rPos - ro));
        vec3 toRune = rPos - ro;
        float t = dot(toRune, rd);
        if (t <= 0.1 || t > maxT) continue;

        vec3 closest = ro + rd * t;
        vec3 diff = closest - rPos;
        float size = 0.078 + 0.014 * sin(fi * 3.1);
        vec2 uv = vec2(dot(diff, camRight), dot(diff, camUp)) / size;

        if (dot(uv, uv) > 1.8) continue;

        // Slight gentle rotation per rune
        uv = rot2D(0.18 * sin(loopAng + fi * 1.3)) * uv;

        int rType = i - (i / 4) * 4;
        float d = evalRuneSDF(uv, rType);
        float stroke = smoothstep(0.095, 0.025, d);
        float glow = exp(-d * 7.5) * 0.45;
        float twinkle = 0.78 + 0.22 * sin(loopAng * 2.0 + fi * 1.9);

        accum += (COL_CORE * stroke * 0.95 + COL_GOLD * (stroke * 0.75 + glow)) * twinkle * (1.0 + 0.30 * openAmt);
    }

    // 3 foreground runes hovering low over the wet obsidian tiles (matching reference!)
    for (int j = 0; j < 3; j++) {
        float fj = float(j);
        vec3 rPos = vec3(
            mix(-0.08, 0.55, fj * 0.5) + 0.04 * sin(loopAng + fj * 2.1),
            0.09 + 0.03 * sin(loopAng + fj * 1.7),
            1.42 + fj * 0.24
        );
        float t = dot(rPos - ro, rd);
        if (t <= 0.1 || t > maxT) continue;

        vec3 diff = (ro + rd * t) - rPos;
        vec2 uv = vec2(dot(diff, camRight), dot(diff, camUp)) / 0.095;
        if (dot(uv, uv) > 1.8) continue;

        float d = evalRuneSDF(uv, j == 0 ? 1 : (j == 1 ? 3 : 0));
        float stroke = smoothstep(0.095, 0.025, d);
        float glow = exp(-d * 6.5) * 0.42;
        accum += (COL_CORE * stroke * 0.85 + COL_GOLD * (stroke * 0.85 + glow)) * 0.92;
    }

    // 26 fine golden sparkle motes orbiting outside the dome & near the wet pool
    for (int k = 0; k < 26; k++) {
        float fk = float(k);
        float a = fk * 2.3999632 + loopAng * (mod(fk, 2.0) < 0.5 ? 0.5 : -0.5);
        float elev = sin(fk * 1.7 + loopAng) * 0.85;
        float r = SPHERE_RADIUS + 0.10 + 0.26 * hash12(vec2(fk, 7.3));
        vec3 mPos = SPHERE_CENTER + vec3(cos(a) * r, elev * 0.95, sin(a) * r * 0.65);
        if (k >= 20) {
            // Foreground floor sparkle trail
            float tF = (fk - 20.0) / 6.0;
            mPos = vec3(0.45 + 0.28 * sin(tF * 5.0 + loopAng), 0.06 + 0.08 * tF, 0.95 + tF * 0.85);
        }
        mPos.y = max(mPos.y, 0.04);

        float t = dot(mPos - ro, rd);
        if (t <= 0.1 || t > maxT) continue;
        float dist = length((ro + rd * t) - mPos);
        float mote = exp(-dist * dist * 4200.0);
        float halo = exp(-dist * 52.0) * 0.22;
        float tw = 0.65 + 0.35 * sin(loopAng * 3.0 + fk * 2.7);
        accum += (COL_CORE * mote * 1.4 + COL_GOLD * halo) * tw;
    }

    return accum;
}

// -----------------------------------------------------------------------------
// ACES Filmic Tone Mapping (Calibrated for <1% clipped, <2% black, 0% NaN)
// -----------------------------------------------------------------------------
vec3 acesToneMap(vec3 x) {
    x = max(x, vec3(0.0));
    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)) / max(x * (c * x + d) + e, vec3(1e-5)), 0.0, 1.0);
}

// -----------------------------------------------------------------------------
// Main Entry
// -----------------------------------------------------------------------------
void main() {
    vec2 res = max(uResolution, vec2(1.0));
    vec2 uv = (gl_FragCoord.xy - 0.5 * res) / min(res.x, res.y);

    float loopPhase = mod(max(uTime, 0.0), 3.0) / 3.0;
    float openAmt = clamp(uOpen, 0.0, 1.0);

    // Camera setup: uCamMode 0 = Hero 3/4 high-angle, uCamMode 1 = ~2.5x Macro on impact & hexes
    vec3 ro = vec3(0.0, 2.02, 3.02);
    vec3 ta = vec3(0.0, 0.92, 0.0);
    float fov = 1.18;

    if (uCamMode == 1) {
        // ~2.3x macro angled across the geodesic hex ribs, 3-way nodes & starburst edge
        ro = vec3(0.46, 1.40, 2.38);
        ta = vec3(0.18, 1.12, 0.48);
        fov = 2.10;
    }

    vec3 camForward = normalize(ta - ro);
    vec3 camRight   = normalize(cross(camForward, vec3(0.0, 1.0, 0.0)));
    vec3 camUp      = cross(camRight, camForward);
    vec3 rd         = normalize(camForward * fov + camRight * uv.x + camUp * uv.y);

    // 1. Background & Wet Obsidian Arena Floor (y = 0)
    vec3 col = evalStudioEnv(rd, uLightMode);
    float sceneT = 1e5;

    if (rd.y < -1e-4) {
        float tFloor = -ro.y / rd.y;
        if (tFloor > 0.0 && tFloor < 60.0) {
            sceneT = tFloor;
            vec3 floorPos = ro + rd * tFloor;
            col = shadeFloor(floorPos, rd, loopPhase, openAmt, uLightMode);
        }
    }

    // 2. Spherical Geodesic Hex-Cell Arcane Barrier (Back Shell -> Interior -> Front Shell)
    vec2 tSphere = intersectSphere(ro, rd, SPHERE_CENTER, SPHERE_RADIUS);
    if (tSphere.x > 0.0 && tSphere.x < sceneT) {
        // Subtle refractive distortion of the background/floor seen through the dome
        vec3 frontPos = ro + rd * tSphere.x;
        vec3 frontN   = normalize(frontPos - SPHERE_CENTER);
        vec3 refrDir  = refract(rd, frontN, 0.94);
        if (length(refrDir) > 0.1 && refrDir.y < -1e-4) {
            float tRefrFloor = -frontPos.y / refrDir.y;
            if (tRefrFloor > 0.0 && tRefrFloor < 40.0) {
                vec3 rfPos = frontPos + refrDir * tRefrFloor;
                vec3 rfCol = shadeFloor(rfPos, refrDir, loopPhase, openAmt, uLightMode);
                col = mix(col, rfCol, 0.65);
            }
        }

        // Deep cobalt-indigo glass absorption of background
        if (uLightMode != 1) {
            col *= vec3(0.45, 0.58, 0.88);
        }

        // Back shell hex lattice (dimmer, deeper cobalt)
        if (tSphere.y > 0.0 && tSphere.y < sceneT) {
            vec3 backPos = ro + rd * tSphere.y;
            vec3 backN   = normalize(backPos - SPHERE_CENTER);
            vec4 backShade = shadeBarrierShell(backPos, -backN, -rd, false, loopPhase, openAmt, uLightMode);
            col = mix(col, backShade.rgb, backShade.a * 0.45) + backShade.rgb * 0.35;
        }

        // Interior arcane volume & downward energy waterfall
        col += evalInteriorVolume(ro, rd, tSphere, loopPhase, openAmt, uLightMode);

        // Front shell hex lattice, nodes, starburst impact, Fresnel rim & specular
        vec4 frontShade = shadeBarrierShell(frontPos, frontN, -rd, true, loopPhase, openAmt, uLightMode);
        col = mix(col, frontShade.rgb, frontShade.a * 0.42) + frontShade.rgb * 0.68;
    }

    // Soft outer atmospheric bloom halo around the dome silhouette
    if (uLightMode != 1) {
        vec3 toCenter = SPHERE_CENTER - ro;
        float tNear = max(dot(toCenter, rd), 0.0);
        float dAxis = length((ro + rd * tNear) - SPHERE_CENTER);
        float outerHalo = exp(-max(dAxis - SPHERE_RADIUS, 0.0) * 6.5) * smoothstep(SPHERE_RADIUS * 0.78, SPHERE_RADIUS, dAxis);
        col += mix(COL_COBALT, COL_CYAN, 0.55) * outerHalo * 0.24;

        // Tight impact bloom glow radiating subtly from the starburst
        vec3 impWorld = SPHERE_CENTER + normalize(vec3(0.01, 0.03, 1.0)) * SPHERE_RADIUS;
        float tImp = max(dot(impWorld - ro, rd), 0.0);
        float dImp = length((ro + rd * tImp) - impWorld);
        float impBloom = exp(-dImp * 5.2) * (0.12 + 0.10 * openAmt);
        col += mix(COL_GOLD, COL_CORE, 0.38) * impBloom;
    }

    // 3. Floating Golden Rune Motes & Sparkles Orbiting Outside
    float runeMaxT = (tSphere.x > 0.0) ? min(sceneT, tSphere.x + 0.35) : sceneT;
    col += evalFloatingRunes(ro, rd, camRight, camUp, runeMaxT, loopPhase, openAmt, uLightMode);

    // Subtle filmic vignette & gentle shadow lift (keeps lumMin ~8-16, <2% black)
    float vig = 1.0 - 0.24 * dot(uv, uv);
    col *= vig;
    col += (uLightMode == 1) ? vec3(0.004) : (COL_OBSIDIAN * 0.03);

    // Exposure + ACES tone mapping + sRGB gamma
    col = acesToneMap(col * 0.82);
    col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierarcane-shield-hex-barrier
CategoryGame VFX Shaders · Arcane Palette
Upstream studyvfx_palette/lib/02_arcane_shield_hex_barrier.glsl (commit 913ad1c)
Animation cycle3.0 s seamless harmonic loop
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size855 lines · 34.1 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT arcane sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Voronoi/Worley cellular fields (Voronoi 1908; Worley 1996); 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, tetrahedral SDF normal

All game vfx shaders