AnimatedBuoyant high-voltage thermal plasma arc climbing twin electrodes with ionized violet aura.
lichtenberg-fractal-lightning.glsl · 790 lines · GLSL ES 3.00 · MIT
#version 300 es
// Lichtenberg Fractal Lightning (lichtenberg-fractal-lightning.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/03_lichtenberg_electrical_breakdown.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;
// ============================================================================
// Constants & Geometry Parameters
// ============================================================================
const float PI = 3.14159265359;
const float IOR_PMMA = 1.49;
// Acrylic block dimensions: rests flush on y = 0.0
const vec3 BLOCK_HALF = vec3(1.12, 0.74, 0.34);
const vec3 BLOCK_CENTER = vec3(0.0, 0.74, 0.0);
const vec3 PIT_POS = vec3(0.0, 1.48, 0.0);
// ============================================================================
// Math & Procedural Noise Utilities
// ============================================================================
float hash11(float p) {
p = fract(p * 0.1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float hash13(vec3 p3) {
p3 = fract(p3 * 0.1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
float valueNoise3D(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);
}
// Sharp-kinked piecewise-linear displacement for authentic jagged lightning
vec3 lightningJitter(vec3 p) {
vec3 a = abs(fract(p * 1.9 + vec3(0.13, 0.47, 0.81)) - 0.5) * 2.0 - 0.5;
vec3 b = abs(fract(p * 4.7 + vec3(0.61, 0.23, 0.37)) - 0.5) * 2.0 - 0.5;
vec3 c = abs(fract(p * 10.3 + vec3(0.29, 0.71, 0.19)) - 0.5) * 2.0 - 0.5;
return a * 0.55 + b * 0.30 + c * 0.15;
}
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
// ============================================================================
// SDF Primitives & Hero Geometry
// ============================================================================
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
// Chamfered & micro-rounded thick PMMA acrylic block resting strictly on y = 0.0
float sdAcrylicBlock(vec3 p) {
vec3 q = p - BLOCK_CENTER;
const float r = 0.016;
float dBox = sdRoundBox(q, BLOCK_HALF - vec3(r), r);
// 45-degree optical bevels along all 12 edges
vec3 aq = abs(q);
float bevel = 0.042;
float cXY = (aq.x + aq.y - (BLOCK_HALF.x + BLOCK_HALF.y - bevel)) * 0.70710678;
float cXZ = (aq.x + aq.z - (BLOCK_HALF.x + BLOCK_HALF.z - bevel)) * 0.70710678;
float cYZ = (aq.y + aq.z - (BLOCK_HALF.y + BLOCK_HALF.z - bevel)) * 0.70710678;
float dBevel = max(cXY, max(cXZ, cYZ));
// Smooth chamfer intersection preserving exact y = 0.0 contact plane
float k = 0.008;
float h = clamp(0.5 - 0.5 * (dBevel - dBox) / k, 0.0, 1.0);
float dBlock = mix(dBevel, dBox, h) + k * h * (1.0 - h);
// Conical discharge entry crater carved into top center
vec3 pq = p - PIT_POS;
float rPit = length(pq.xz);
float pitDepth = 0.11;
float pitRadius = mix(0.016, 0.095, clamp((pq.y + pitDepth) / pitDepth, 0.0, 1.0));
float dCrater = max(rPit - pitRadius, -(pq.y + pitDepth));
dCrater = max(dCrater, pq.y - 0.04);
// Smoothly carve the discharge crater from the acrylic block
float kc = 0.012;
float hc = clamp(0.5 - 0.5 * (dBlock + dCrater) / kc, 0.0, 1.0);
return mix(dBlock, -dCrater, hc) + kc * hc * (1.0 - hc);
}
// Machined metallic discharge entry pit collar & central tungsten strike pin
float sdMetallicPit(vec3 p) {
vec3 pq = p - PIT_POS;
float r = length(pq.xz);
// Recessed machined ring/ferrule seated inside top crater mouth
vec2 torusQ = vec2(r - 0.072, pq.y + 0.008);
float dRing = length(torusQ) - 0.016;
// Inner tapered metallic cup lining the crater wall
float cupR = mix(0.018, 0.076, clamp((pq.y + 0.095) / 0.095, 0.0, 1.0));
float dCup = abs(r - cupR) - 0.007;
dCup = max(dCup, abs(pq.y + 0.050) - 0.045);
// Sharp central tungsten discharge pin pointing downward into acrylic
float pinR = mix(0.003, 0.018, clamp((pq.y + 0.115) / 0.115, 0.0, 1.0));
float dPin = max(r - pinR, abs(pq.y + 0.058) - 0.060);
return min(dRing, min(dCup, dPin));
}
float mapHero(vec3 p) {
return min(sdAcrylicBlock(p), sdMetallicPit(p));
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// Zero-bug rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * mapHero(p + k.xyy * e) +
k.yyx * mapHero(p + k.yyx * e) +
k.yxy * mapHero(p + k.yxy * e) +
k.xxx * mapHero(p + k.xxx * e)
);
}
vec3 calcAcrylicNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * sdAcrylicBlock(p + k.xyy * e) +
k.yyx * sdAcrylicBlock(p + k.yyx * e) +
k.yxy * sdAcrylicBlock(p + k.yxy * e) +
k.xxx * sdAcrylicBlock(p + k.xxx * e)
);
}
// Zero-bug rule (3): Clean hero-only soft shadow (no ph)
float calcSoftShadow(vec3 ro, vec3 rd, float tMin, float tMax, float k) {
float res = 1.0;
float t = tMin;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.001) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
return clamp(res, 0.0, 1.0);
}
// ============================================================================
// 3D Fractal Branching Lichtenberg Dielectric Breakdown Field
// ============================================================================
float sdCapsule3D(vec3 p, vec3 a, vec3 b, float ra, float rb) {
vec3 pa = p - a;
vec3 ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// Evaluates one folded 3D fractal Lichtenberg branch tree
// Returns vec4(distance, branchOrder 0..1, arcFromPit 0..1, fractureHash 0..1)
vec4 evalBranchTree(vec3 q, float seed, float openVal) {
float dMin = 1e5;
float bestOrder = 1.0;
float bestArc = 1.0;
float bestHash = 0.5;
vec3 bp = q;
float sc = 1.0;
float arcAccum = 0.0;
float branchId = seed;
// 7 generations of razor-sharp 3D dendritic dielectric breakdown
for (int i = 0; i < 7; i++) {
float fi = float(i);
float order = fi / 6.0;
// Sharp angular lightning kinks along current limb
vec3 kink = lightningJitter(bp * (2.3 + fi * 0.65) + vec3(branchId * 3.7, fi * 1.9, branchId * 2.3));
vec3 jp = bp + vec3(kink.x, 0.0, kink.z * 0.65) * (0.068 / (1.0 + fi * 0.08));
// Limb length and needle-tapered hairline radius
float segLen = mix(0.36, 0.44, openVal) * (1.0 - fi * 0.025);
float rTop = mix(0.0052, 0.00085, order) * mix(0.92, 1.08, openVal);
float rBot = rTop * mix(0.45, 0.14, order);
// Distance to current segment from y=0 to y=segLen
float hy = clamp(jp.y / segLen, 0.0, 1.0);
vec3 segPt = vec3(0.0, hy * segLen, 0.0);
float rHere = mix(rTop, rBot, pow(hy, 0.85));
float dSeg = (length(jp - segPt) - rHere) / sc;
// Delicate lateral dendritic micro-spurs branching off each segment
if (i >= 1) {
vec3 sp = jp;
sp.y -= segLen * (0.35 + 0.35 * hash11(branchId * 9.3 + fi));
sp.x = abs(sp.x);
sp.xy = rot2(-0.78) * sp.xy;
float spurLen = segLen * 0.46;
float hs = clamp(sp.y / spurLen, 0.0, 1.0);
float rSpur = mix(rTop * 0.52, 0.00025, hs);
float dSpur = (length(sp - vec3(0.0, hs * spurLen, 0.0)) - rSpur) / sc;
dSeg = min(dSeg, dSpur);
}
// Deeper outer generations extend smoothly with uOpen
float genFade = smoothstep(4.4 + openVal * 2.8, 2.8 + openVal * 2.5, fi);
dSeg += (1.0 - genFade) * 0.04;
if (dSeg < dMin) {
dMin = dSeg;
bestOrder = order;
bestArc = clamp((arcAccum + hy * segLen / sc) * 0.72, 0.0, 1.0);
bestHash = hash11(branchId * 17.3 + floor(jp.y * 26.0));
}
// Advance into next child branch generation
float sgnX = (bp.x >= 0.0) ? 1.0 : -1.0;
float sgnZ = (bp.z >= 0.0) ? 1.0 : -1.0;
float hFork = hash11(branchId * 11.7 + sgnX * 2.31 + fi * 4.13);
float hForkZ = hash11(branchId * 7.9 + sgnZ * 3.17 + fi * 2.71);
float forkY = segLen * (0.54 + 0.32 * hFork);
arcAccum += forkY / sc;
bp.y -= forkY;
// Mirror-fold X and Z with organic angular asymmetry
bp.x = abs(bp.x) - 0.007 * (1.0 - order * 0.5);
float angXY = (0.48 + 0.24 * hFork) * (1.0 - 0.04 * fi);
bp.xy = rot2(-angXY) * bp.xy;
// Controlled 3D Z-branching keeps fractures inside thick acrylic slab
float angYZ = (0.16 + 0.14 * hForkZ) * sgnZ * (1.0 - order * 0.35);
bp.yz = rot2(angYZ) * bp.yz;
// Scale down child branch space
float scaleStep = 1.40 + 0.07 * hFork;
bp *= scaleStep;
sc *= scaleStep;
branchId = fract(branchId * 2.37 + sgnX * 0.41 + sgnZ * 0.19 + 0.13);
}
return vec4(dMin, bestOrder, bestArc, bestHash);
}
// Combined 3D Lichtenberg tree inside the PMMA block
vec4 evalLichtenberg(vec3 p, float openVal) {
// Anchor right below the tungsten strike pin in the metallic entry pit
vec3 q = vec3(p.x, (PIT_POS.y - 0.095) - p.y, p.z);
// Keep fractures strictly enclosed within acrylic volume with smooth margin
vec3 aq = abs(p - BLOCK_CENTER);
vec3 margin = BLOCK_HALF - vec3(0.095, 0.085, 0.075);
float wallMask = max(aq.x - margin.x, max(aq.y - margin.y, aq.z - margin.z));
if (wallMask > 0.14) {
return vec4(wallMask, 1.0, 1.0, 0.5);
}
// Central primary downward cascade
vec4 tMain = evalBranchTree(q, 0.37, openVal);
// Secondary sweeping lateral crown leader (tilted left-forward)
vec3 qL = q;
qL.y -= 0.05;
qL.xy = rot2(0.56) * qL.xy;
qL.yz = rot2(0.14) * qL.yz;
vec4 tLeft = evalBranchTree(qL * 1.16, 0.73, openVal);
tLeft.x /= 1.16;
// Tertiary sweeping lateral crown leader (tilted right-back)
vec3 qR = q;
qR.y -= 0.08;
qR.xy = rot2(-0.62) * qR.xy;
qR.yz = rot2(-0.16) * qR.yz;
vec4 tRight = evalBranchTree(qR * 1.20, 0.19, openVal);
tRight.x /= 1.20;
// Combine leaders
vec4 res = (tMain.x < tLeft.x) ? tMain : tLeft;
res = (res.x < tRight.x) ? res : tRight;
// Add crisp central entry-pit stem connecting tungsten pin to crown
vec3 stemJit = lightningJitter(q * 8.5) * 0.007;
float dStem = sdCapsule3D(q + stemJit, vec3(0.0, -0.02, 0.0), vec3(0.0, 0.16, 0.0), 0.0068, 0.0042);
if (dStem < res.x) {
res = vec4(dStem, 0.0, clamp(q.y * 0.5, 0.0, 0.15), 0.85);
}
// Softly terminate fractures before touching outer acrylic walls
if (wallMask > 0.0) {
res.x += wallMask * 1.65;
}
return res;
}
// ============================================================================
// Studio Lighting Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 getKeyLightDir() {
if (uLightMode == 2) {
// Grazing raking side light
return normalize(vec3(-0.92, 0.25, 0.30));
} else if (uLightMode == 3) {
// Backlit studio light
return normalize(vec3(0.12, 0.44, -0.89));
}
// Hero & Neutral studio key light
return normalize(vec3(-0.56, 0.72, 0.41));
}
vec3 evalStudioSky(vec3 rd) {
bool isNeutral = (uLightMode == 1);
float y = clamp(rd.y, -1.0, 1.0);
// Base dark cyclorama studio dome (calibrated so neutral mode has strong dark contrast ~25-55/255)
vec3 zenCol = isNeutral ? vec3(0.014) : vec3(0.032, 0.038, 0.052);
vec3 horCol = isNeutral ? vec3(0.030) : vec3(0.055, 0.062, 0.080);
vec3 flrCol = isNeutral ? vec3(0.011) : vec3(0.022, 0.025, 0.032);
vec3 env = (y >= 0.0)
? mix(horCol, zenCol, pow(y, 0.65))
: mix(horCol, flrCol, pow(-y, 0.55));
// Overhead diffused studio softbox (gives crisp top bevel reflections)
float topBox = smoothstep(0.68, 0.86, rd.y) *
smoothstep(0.58, 0.28, abs(rd.x)) *
smoothstep(0.55, 0.25, abs(rd.z));
vec3 topCol = isNeutral ? vec3(1.15) : vec3(1.05, 1.08, 1.16);
env += topBox * topCol * (uLightMode == 2 ? 0.55 : 0.95);
// Key softbox (left-front or grazing depending on uLightMode)
vec3 keyDir = getKeyLightDir();
float keyDot = dot(rd, keyDir);
float keyBox = smoothstep(0.84, 0.96, keyDot);
vec3 keyCol = isNeutral ? vec3(1.55) : vec3(1.45, 1.50, 1.62);
if (uLightMode == 2) keyCol = vec3(1.85, 1.78, 1.68);
if (uLightMode == 3) keyCol = vec3(1.90, 2.05, 2.35);
env += keyBox * keyCol;
// Cool rim strip softbox (right-rear edge catchlight for acrylic bevels)
vec3 rimDir = normalize(vec3(0.76, 0.42, -0.50));
float rimBox = smoothstep(0.86, 0.97, dot(rd, rimDir));
vec3 rimCol = isNeutral ? vec3(1.10) : vec3(0.78, 0.98, 1.35);
env += rimBox * rimCol * (uLightMode == 3 ? 1.35 : 0.85);
// Backlit studio cyclorama panel in uLightMode == 3
if (uLightMode == 3) {
float backPanel = smoothstep(0.52, 0.86, -rd.z) *
smoothstep(0.75, 0.15, abs(rd.x)) *
smoothstep(-0.15, 0.22, rd.y) *
smoothstep(0.82, 0.45, rd.y);
env += backPanel * vec3(1.35, 1.50, 1.75);
}
// Negative-fill flags (dark matte studio panels flanking sides for crystal edge contrast)
float negFlagL = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(-0.94, 0.08, -0.32))));
float negFlagR = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(0.95, 0.05, 0.28))));
float negMask = clamp(negFlagL + negFlagR, 0.0, 1.0);
env = mix(env, env * 0.24, negMask * 0.82);
return env;
}
// ============================================================================
// Bench Shading (with Hero Soft Shadow & Refracted Plasma Spill)
// ============================================================================
vec3 shadeBench(vec3 p, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 V = -rd;
// Subtle micro-brushed anodized slate bench texture
float grain = valueNoise3D(vec3(p.x * 26.0, 0.0, p.z * 6.5)) * 0.5 +
valueNoise3D(vec3(p.x * 78.0, 0.0, p.z * 18.0)) * 0.5;
vec3 baseAlb = isNeutral
? mix(vec3(0.022), vec3(0.034), grain)
: mix(vec3(0.026, 0.029, 0.036), vec3(0.038, 0.043, 0.054), grain);
float rough = mix(0.28, 0.38, grain);
vec3 L = getKeyLightDir();
float NdotL = max(dot(N, L), 0.0);
// Zero-bug rule (3): clean hero-only soft shadow, bias p + N*0.014, tMin 0.025
float sh = calcSoftShadow(p + N * 0.014, L, 0.025, 6.0, 10.0);
// Acrylic is transparent, so its shadow is softened by transmitted light
float shTrans = mix(0.42, 1.0, sh);
// Subtle ambient occlusion directly under the acrylic block footprint
vec2 footDist = abs(p.xz) - BLOCK_HALF.xz;
float dFoot = length(max(footDist, 0.0)) + min(max(footDist.x, footDist.y), 0.0);
float contactAO = mix(0.45, 1.0, smoothstep(-0.02, 0.22, dFoot));
// Diffuse key + studio ambient fill
vec3 keyCol = isNeutral ? vec3(0.95) : vec3(1.05, 1.08, 1.15);
if (uLightMode == 2) keyCol = vec3(1.35, 1.28, 1.18);
if (uLightMode == 3) keyCol = vec3(1.15, 1.28, 1.45);
vec3 diff = baseAlb * (keyCol * NdotL * shTrans + (isNeutral ? vec3(0.085) : vec3(0.065, 0.072, 0.090)) * contactAO);
// Satin specular reflection of studio environment
vec3 H = normalize(L + V);
float specPow = mix(48.0, 18.0, rough);
float spec = pow(max(dot(N, H), 0.0), specPow) * shTrans * 0.35;
float fres = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
vec3 col = diff + keyCol * spec + envRefl * fres * 0.42 * contactAO;
// Transmitted caustic & ionized cyan-violet glow spilled onto bench around block
float glowRad = length(vec2(p.x * 0.72, p.z * 1.15));
float causticBand = exp(-glowRad * glowRad * 1.15);
float nearEdge = exp(-max(dFoot, 0.0) * 3.8);
float pulse = 0.85 + 0.15 * sin(uTime * 2.4 - glowRad * 2.2);
if (isNeutral) {
// Pure neutral optical caustic brightening through the clear block
col += vec3(0.055) * causticBand * nearEdge;
} else {
vec3 plasmaSpill = mix(vec3(0.18, 0.09, 0.48), vec3(0.06, 0.42, 0.68), exp(-glowRad * 1.4));
float energy = mix(0.55, 1.35, openVal) * pulse;
col += plasmaSpill * causticBand * nearEdge * energy * (0.28 + 0.55 * fres);
}
// Subtle distance studio vignette fade into cyclorama horizon
float distFade = smoothstep(5.5, 18.0, length(p.xz));
col = mix(col, evalStudioSky(vec3(rd.x, max(rd.y, 0.04), rd.z)), distFade);
return col;
}
// ============================================================================
// Metallic Discharge Entry Pit Shading
// ============================================================================
vec3 shadeMetallicPit(vec3 p, vec3 N, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 V = -rd;
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
// Brushed tungsten / stainless steel discharge electrode
float micro = valueNoise3D(p * 85.0);
vec3 metalF0 = isNeutral ? vec3(0.68) : vec3(0.62, 0.65, 0.70);
// Subtle dark carbonized scorch right near the strike crater bottom
float scorch = smoothstep(-0.09, -0.01, p.y - PIT_POS.y);
metalF0 *= mix(0.45, 1.0, scorch);
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
vec3 F = metalF0 + (1.0 - metalF0) * pow(1.0 - NdotV, 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
float spec = pow(max(dot(N, H), 0.0), mix(90.0, 42.0, micro)) * 1.65;
vec3 col = envRefl * F * 0.95 + metalF0 * NdotL * 0.22 + F * spec;
// Electric discharge reflection on the metallic pit collar & pin tip
if (!isNeutral) {
float pinTip = exp(-length(p - (PIT_POS - vec3(0.0, 0.095, 0.0))) * 26.0);
float pulse = 0.8 + 0.2 * sin(uTime * 5.0);
col += vec3(0.55, 0.88, 1.0) * pinTip * mix(0.6, 1.5, openVal) * pulse;
}
return col;
}
// ============================================================================
// Internal Volumetric & Surface Raymarch through PMMA Acrylic Block
// ============================================================================
vec3 traceAcrylicInterior(vec3 entryPos, vec3 rayDir, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 L = getKeyLightDir();
// Step slightly inside the acrylic block
vec3 p = entryPos + rayDir * 0.008;
float s = 0.008;
float sMax = 2.85;
vec3 accumLight = vec3(0.0);
float transmittance = 1.0;
// March through the refractive acrylic volume, capturing hairline Lichtenberg channels
for (int i = 0; i < 96; i++) {
float dBlock = sdAcrylicBlock(p);
if (dBlock > 0.002 && s > 0.03) {
break;
}
vec4 tree = evalLichtenberg(p, openVal);
float dTree = tree.x;
float order = tree.y;
float arc = tree.z;
float fHash = tree.w;
// Fine adaptive step size to resolve razor-sharp hairline micro-channels
float dt = clamp(dTree * 0.45, 0.0028, 0.048);
dt = min(dt, max(-dBlock * 0.72, 0.007));
// 1. Physical fractured micro-channel wall (Silver TIR reflections!)
// Tight exponential falloff yields crisp etched frosted-silver filaments
float crackCore = exp(-max(dTree, 0.0) * mix(420.0, 740.0, order));
float crackSheen = exp(-max(dTree, 0.0) * mix(165.0, 290.0, order));
// Faceted micro-crystal sparkle orientation along fracture wall
float facetGlint = 0.48 + 0.52 * pow(0.5 + 0.5 * sin(fHash * 23.0 + dot(p, L) * 34.0 + dot(rayDir, vec3(13.0, 9.0, 17.0))), 3.0);
float lightAngle = 0.55 + 0.45 * clamp(dot(normalize(p - BLOCK_CENTER + vec3(0.001)), L), -0.3, 1.0);
if (isNeutral) {
// Pure neutral grayscale etched silver-white TIR fractures (no emission!)
float silverDensity = (crackCore * 2.25 + crackSheen * 0.35) * facetGlint * lightAngle;
vec3 silverCol = vec3(0.92) * silverDensity;
float alpha = clamp(crackCore * dt * 48.0 + crackSheen * dt * 5.0, 0.0, 0.88);
accumLight += transmittance * silverCol * dt * 36.0;
transmittance *= (1.0 - alpha * 0.65);
} else {
// Silver TIR reflection + razor-sharp ionized cyan-violet plasma discharge
vec3 silverTIR = vec3(0.80, 0.90, 0.98) * (crackCore * 1.95 + crackSheen * 0.30) * facetGlint * lightAngle;
// Traveling ionization pulse down branches from entry pit
float wave1 = exp(-pow(fract(arc * 1.35 - uTime * 0.52) * 4.5, 2.0));
float wave2 = exp(-pow(fract(arc * 2.40 - uTime * 1.08 + fHash * 0.35) * 5.8, 2.0));
float pulse = 0.48 + 0.62 * wave1 + 0.38 * wave2;
// Energized plasma boost from uOpen
float energy = mix(0.65, 1.75, openVal) * pulse;
// Tight electric core -> crisp cyan filament -> subtle ionized violet corona
float halo = exp(-max(dTree, 0.0) * mix(42.0, 74.0, order));
vec3 coreCol = vec3(0.88, 0.98, 1.00) * crackCore * 3.40;
vec3 midCol = vec3(0.10, 0.74, 1.00) * crackSheen * 1.45;
vec3 auraCol = vec3(0.46, 0.16, 0.98) * halo * 0.28;
// Trunk & primary branches burn hotter; fine outer tips lean electric violet
vec3 plasma = mix(coreCol + midCol, midCol * 0.85 + auraCol * 1.45, order * 0.48) + auraCol;
plasma *= energy;
// Focused ignition spark right beneath the tungsten strike pin
float pitGlow = exp(-length(p - (PIT_POS - vec3(0.0, 0.10, 0.0))) * 16.0) * 0.55 * energy;
plasma += vec3(0.50, 0.88, 1.0) * pitGlow;
accumLight += transmittance * (silverTIR * 18.0 + plasma * 13.5) * dt;
// Fractures partially occlude background light (frosted dielectric channels)
float alpha = clamp(crackCore * dt * 42.0 + crackSheen * dt * 4.5, 0.0, 0.78);
transmittance *= (1.0 - alpha * 0.48);
}
if (transmittance < 0.03) break;
s += dt;
if (s > sMax) break;
p = entryPos + rayDir * s;
}
// Optical Beer-Lambert absorption of thick crystal-clear PMMA
vec3 absorb = isNeutral
? exp(-s * vec3(0.055))
: exp(-s * vec3(0.068, 0.030, 0.024));
// Exit boundary refraction / Total Internal Reflection (TIR)
vec3 exitPos = p;
vec3 nOut = calcAcrylicNormal(exitPos);
vec3 nIn = -nOut; // pointing inward toward ray origin
vec3 exitDir = refract(rayDir, nIn, IOR_PMMA);
vec3 behindCol;
if (dot(exitDir, exitDir) < 0.001) {
// TRUE Total Internal Reflection inside acrylic bevels / side walls!
vec3 tirDir = reflect(rayDir, nIn);
vec3 tirEnv = evalStudioSky(tirDir);
behindCol = tirEnv * (isNeutral ? vec3(0.95) : vec3(0.88, 0.94, 1.02));
// Add subtle glowing bevel edge catchlight
if (!isNeutral) {
behindCol += vec3(0.10, 0.32, 0.58) * mix(0.22, 0.55, openVal);
}
} else {
// Compute exit Fresnel reflection + transmitted background (bench or sky)
float cosTheta = max(dot(-rayDir, nIn), 0.0);
float F0 = 0.039;
float fExit = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
vec3 bgCol;
if (exitDir.y < -0.001) {
float tBench = -exitPos.y / exitDir.y;
if (tBench > 0.0 && tBench < 25.0) {
vec3 bp = exitPos + exitDir * tBench;
bgCol = shadeBench(bp, exitDir, openVal);
} else {
bgCol = evalStudioSky(exitDir);
}
} else {
bgCol = evalStudioSky(exitDir);
}
vec3 internalRefl = evalStudioSky(reflect(rayDir, nIn));
behindCol = mix(bgCol, internalRefl, fExit * 0.72);
}
return accumLight * absorb + behindCol * absorb * transmittance;
}
// ============================================================================
// Primary Raymarch & Shading
// ============================================================================
float raymarchScene(vec3 ro, vec3 rd, float tMax) {
float t = 0.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0008) return t;
t += d;
if (t > tMax) break;
}
return -1.0;
}
vec3 renderPixel(vec2 uv) {
float openVal = clamp(uOpen, 0.0, 1.0);
bool isNeutral = (uLightMode == 1);
// Camera setup: uCamMode 0 = Hero establishing, 1 = ~2.5x Macro detail
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x macro focused on metallic entry pit, chamfered bevel, & upper Lichtenberg crown
ro = vec3(0.62, 1.46, 1.98);
ta = vec3(0.0, 0.94, 0.0);
focal = 2.25;
} else {
// Hero 3/4 elevated studio view framing full beveled block, pit, side faces, & bench shadow
ro = vec3(1.68, 2.05, 4.95);
ta = vec3(0.0, 0.68, 0.0);
focal = 2.05;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + focal * ww);
float t = raymarchScene(ro, rd, 22.0);
if (t < 0.0) {
return evalStudioSky(rd);
}
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-bug rule (2): exact bench classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
return shadeBench(p, rd, openVal);
}
// Hero hit: distinguish metallic discharge entry pit vs crystal PMMA acrylic block
float dPit = sdMetallicPit(p);
float dAcrylic = sdAcrylicBlock(p);
vec3 N = calcHeroNormal(p);
if (dPit < dAcrylic + 0.0005) {
return shadeMetallicPit(p, N, rd, openVal);
}
// Crystal-clear beveled PMMA acrylic surface
vec3 V = -rd;
float NdotV = max(dot(N, V), 0.0);
const float F0 = 0.039; // PMMA (n = 1.49)
float fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
// Crisp external studio reflection + specular glints on flat faces & 45-deg bevels
vec3 R = reflect(rd, N);
vec3 reflCol = evalStudioSky(R);
if (R.y < -0.001) {
float tb = -p.y / R.y;
if (tb > 0.0 && tb < 18.0) {
reflCol = mix(reflCol, shadeBench(p + R * tb, R, openVal), 0.85);
}
}
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
float specSharp = pow(max(dot(N, H), 0.0), 320.0) * 2.2;
float specSoft = pow(max(dot(N, H), 0.0), 48.0) * 0.25;
vec3 keySpecCol = isNeutral ? vec3(1.15) : vec3(1.10, 1.15, 1.25);
// Refract into the thick PMMA volume (with subtle chromatic dispersion on non-neutral modes)
vec3 refrDir = refract(rd, N, 1.0 / IOR_PMMA);
vec3 transCol = traceAcrylicInterior(p, refrDir, openVal);
// Subtle edge-bevel dispersion shimmer on chamfered edges (non-neutral only)
if (!isNeutral) {
float bevelMask = 1.0 - max(abs(N.x), max(abs(N.y), abs(N.z)));
float disp = smoothstep(0.12, 0.34, bevelMask);
transCol += disp * vec3(0.08, 0.22, 0.38) * (0.35 + 0.65 * fresnel);
}
vec3 col = mix(transCol, reflCol, clamp(fresnel * 1.08, 0.0, 0.94));
col += keySpecCol * (specSharp + specSoft);
// Subtle rim silhouette brightening in backlit mode
if (uLightMode == 3) {
col += vec3(0.28, 0.42, 0.62) * pow(1.0 - NdotV, 3.2) * 0.75;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping & Safe Luminance Calibration (10..248 / 255)
// ============================================================================
vec3 acesToneMap(vec3 x) {
// Gentle highlight shoulder compression so extreme plasma never hard-clips
x = x / (1.0 + x * 0.065);
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, 0.965);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / max(uResolution.y, 1.0);
vec3 col = renderPixel(uv);
// Enforce pure neutral grayscale when uLightMode == 1
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(lum);
}
// Subtle linear studio floor prevents pure-black crushing (<10/255)
col = max(col, vec3(0.0)) + vec3(0.0045);
// ACES Filmic Tone Mapping + sRGB gamma
col = acesToneMap(col * 1.05);
col = pow(col, vec3(1.0 / 2.2));
// Final safety clamp within studio gamut (no pure 0 or clipped 255)
col = clamp(col, vec3(0.045), vec3(0.968));
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Lichtenberg Fractal Lightning — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/03_lichtenberg_electrical_breakdown.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// ============================================================================
// Constants & Geometry Parameters
// ============================================================================
const float PI = 3.14159265359;
const float IOR_PMMA = 1.49;
// Acrylic block dimensions: rests flush on y = 0.0
const vec3 BLOCK_HALF = vec3(1.12, 0.74, 0.34);
const vec3 BLOCK_CENTER = vec3(0.0, 0.74, 0.0);
const vec3 PIT_POS = vec3(0.0, 1.48, 0.0);
// ============================================================================
// Math & Procedural Noise Utilities
// ============================================================================
float hash11(float p) {
p = fract(p * 0.1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float hash13(vec3 p3) {
p3 = fract(p3 * 0.1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
float valueNoise3D(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);
}
// Sharp-kinked piecewise-linear displacement for authentic jagged lightning
vec3 lightningJitter(vec3 p) {
vec3 a = abs(fract(p * 1.9 + vec3(0.13, 0.47, 0.81)) - 0.5) * 2.0 - 0.5;
vec3 b = abs(fract(p * 4.7 + vec3(0.61, 0.23, 0.37)) - 0.5) * 2.0 - 0.5;
vec3 c = abs(fract(p * 10.3 + vec3(0.29, 0.71, 0.19)) - 0.5) * 2.0 - 0.5;
return a * 0.55 + b * 0.30 + c * 0.15;
}
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
// ============================================================================
// SDF Primitives & Hero Geometry
// ============================================================================
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
// Chamfered & micro-rounded thick PMMA acrylic block resting strictly on y = 0.0
float sdAcrylicBlock(vec3 p) {
vec3 q = p - BLOCK_CENTER;
const float r = 0.016;
float dBox = sdRoundBox(q, BLOCK_HALF - vec3(r), r);
// 45-degree optical bevels along all 12 edges
vec3 aq = abs(q);
float bevel = 0.042;
float cXY = (aq.x + aq.y - (BLOCK_HALF.x + BLOCK_HALF.y - bevel)) * 0.70710678;
float cXZ = (aq.x + aq.z - (BLOCK_HALF.x + BLOCK_HALF.z - bevel)) * 0.70710678;
float cYZ = (aq.y + aq.z - (BLOCK_HALF.y + BLOCK_HALF.z - bevel)) * 0.70710678;
float dBevel = max(cXY, max(cXZ, cYZ));
// Smooth chamfer intersection preserving exact y = 0.0 contact plane
float k = 0.008;
float h = clamp(0.5 - 0.5 * (dBevel - dBox) / k, 0.0, 1.0);
float dBlock = mix(dBevel, dBox, h) + k * h * (1.0 - h);
// Conical discharge entry crater carved into top center
vec3 pq = p - PIT_POS;
float rPit = length(pq.xz);
float pitDepth = 0.11;
float pitRadius = mix(0.016, 0.095, clamp((pq.y + pitDepth) / pitDepth, 0.0, 1.0));
float dCrater = max(rPit - pitRadius, -(pq.y + pitDepth));
dCrater = max(dCrater, pq.y - 0.04);
// Smoothly carve the discharge crater from the acrylic block
float kc = 0.012;
float hc = clamp(0.5 - 0.5 * (dBlock + dCrater) / kc, 0.0, 1.0);
return mix(dBlock, -dCrater, hc) + kc * hc * (1.0 - hc);
}
// Machined metallic discharge entry pit collar & central tungsten strike pin
float sdMetallicPit(vec3 p) {
vec3 pq = p - PIT_POS;
float r = length(pq.xz);
// Recessed machined ring/ferrule seated inside top crater mouth
vec2 torusQ = vec2(r - 0.072, pq.y + 0.008);
float dRing = length(torusQ) - 0.016;
// Inner tapered metallic cup lining the crater wall
float cupR = mix(0.018, 0.076, clamp((pq.y + 0.095) / 0.095, 0.0, 1.0));
float dCup = abs(r - cupR) - 0.007;
dCup = max(dCup, abs(pq.y + 0.050) - 0.045);
// Sharp central tungsten discharge pin pointing downward into acrylic
float pinR = mix(0.003, 0.018, clamp((pq.y + 0.115) / 0.115, 0.0, 1.0));
float dPin = max(r - pinR, abs(pq.y + 0.058) - 0.060);
return min(dRing, min(dCup, dPin));
}
float mapHero(vec3 p) {
return min(sdAcrylicBlock(p), sdMetallicPit(p));
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// Zero-bug rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * mapHero(p + k.xyy * e) +
k.yyx * mapHero(p + k.yyx * e) +
k.yxy * mapHero(p + k.yxy * e) +
k.xxx * mapHero(p + k.xxx * e)
);
}
vec3 calcAcrylicNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * sdAcrylicBlock(p + k.xyy * e) +
k.yyx * sdAcrylicBlock(p + k.yyx * e) +
k.yxy * sdAcrylicBlock(p + k.yxy * e) +
k.xxx * sdAcrylicBlock(p + k.xxx * e)
);
}
// Zero-bug rule (3): Clean hero-only soft shadow (no ph)
float calcSoftShadow(vec3 ro, vec3 rd, float tMin, float tMax, float k) {
float res = 1.0;
float t = tMin;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.001) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
return clamp(res, 0.0, 1.0);
}
// ============================================================================
// 3D Fractal Branching Lichtenberg Dielectric Breakdown Field
// ============================================================================
float sdCapsule3D(vec3 p, vec3 a, vec3 b, float ra, float rb) {
vec3 pa = p - a;
vec3 ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// Evaluates one folded 3D fractal Lichtenberg branch tree
// Returns vec4(distance, branchOrder 0..1, arcFromPit 0..1, fractureHash 0..1)
vec4 evalBranchTree(vec3 q, float seed, float openVal) {
float dMin = 1e5;
float bestOrder = 1.0;
float bestArc = 1.0;
float bestHash = 0.5;
vec3 bp = q;
float sc = 1.0;
float arcAccum = 0.0;
float branchId = seed;
// 7 generations of razor-sharp 3D dendritic dielectric breakdown
for (int i = 0; i < 7; i++) {
float fi = float(i);
float order = fi / 6.0;
// Sharp angular lightning kinks along current limb
vec3 kink = lightningJitter(bp * (2.3 + fi * 0.65) + vec3(branchId * 3.7, fi * 1.9, branchId * 2.3));
vec3 jp = bp + vec3(kink.x, 0.0, kink.z * 0.65) * (0.068 / (1.0 + fi * 0.08));
// Limb length and needle-tapered hairline radius
float segLen = mix(0.36, 0.44, openVal) * (1.0 - fi * 0.025);
float rTop = mix(0.0052, 0.00085, order) * mix(0.92, 1.08, openVal);
float rBot = rTop * mix(0.45, 0.14, order);
// Distance to current segment from y=0 to y=segLen
float hy = clamp(jp.y / segLen, 0.0, 1.0);
vec3 segPt = vec3(0.0, hy * segLen, 0.0);
float rHere = mix(rTop, rBot, pow(hy, 0.85));
float dSeg = (length(jp - segPt) - rHere) / sc;
// Delicate lateral dendritic micro-spurs branching off each segment
if (i >= 1) {
vec3 sp = jp;
sp.y -= segLen * (0.35 + 0.35 * hash11(branchId * 9.3 + fi));
sp.x = abs(sp.x);
sp.xy = rot2(-0.78) * sp.xy;
float spurLen = segLen * 0.46;
float hs = clamp(sp.y / spurLen, 0.0, 1.0);
float rSpur = mix(rTop * 0.52, 0.00025, hs);
float dSpur = (length(sp - vec3(0.0, hs * spurLen, 0.0)) - rSpur) / sc;
dSeg = min(dSeg, dSpur);
}
// Deeper outer generations extend smoothly with uOpen
float genFade = smoothstep(4.4 + openVal * 2.8, 2.8 + openVal * 2.5, fi);
dSeg += (1.0 - genFade) * 0.04;
if (dSeg < dMin) {
dMin = dSeg;
bestOrder = order;
bestArc = clamp((arcAccum + hy * segLen / sc) * 0.72, 0.0, 1.0);
bestHash = hash11(branchId * 17.3 + floor(jp.y * 26.0));
}
// Advance into next child branch generation
float sgnX = (bp.x >= 0.0) ? 1.0 : -1.0;
float sgnZ = (bp.z >= 0.0) ? 1.0 : -1.0;
float hFork = hash11(branchId * 11.7 + sgnX * 2.31 + fi * 4.13);
float hForkZ = hash11(branchId * 7.9 + sgnZ * 3.17 + fi * 2.71);
float forkY = segLen * (0.54 + 0.32 * hFork);
arcAccum += forkY / sc;
bp.y -= forkY;
// Mirror-fold X and Z with organic angular asymmetry
bp.x = abs(bp.x) - 0.007 * (1.0 - order * 0.5);
float angXY = (0.48 + 0.24 * hFork) * (1.0 - 0.04 * fi);
bp.xy = rot2(-angXY) * bp.xy;
// Controlled 3D Z-branching keeps fractures inside thick acrylic slab
float angYZ = (0.16 + 0.14 * hForkZ) * sgnZ * (1.0 - order * 0.35);
bp.yz = rot2(angYZ) * bp.yz;
// Scale down child branch space
float scaleStep = 1.40 + 0.07 * hFork;
bp *= scaleStep;
sc *= scaleStep;
branchId = fract(branchId * 2.37 + sgnX * 0.41 + sgnZ * 0.19 + 0.13);
}
return vec4(dMin, bestOrder, bestArc, bestHash);
}
// Combined 3D Lichtenberg tree inside the PMMA block
vec4 evalLichtenberg(vec3 p, float openVal) {
// Anchor right below the tungsten strike pin in the metallic entry pit
vec3 q = vec3(p.x, (PIT_POS.y - 0.095) - p.y, p.z);
// Keep fractures strictly enclosed within acrylic volume with smooth margin
vec3 aq = abs(p - BLOCK_CENTER);
vec3 margin = BLOCK_HALF - vec3(0.095, 0.085, 0.075);
float wallMask = max(aq.x - margin.x, max(aq.y - margin.y, aq.z - margin.z));
if (wallMask > 0.14) {
return vec4(wallMask, 1.0, 1.0, 0.5);
}
// Central primary downward cascade
vec4 tMain = evalBranchTree(q, 0.37, openVal);
// Secondary sweeping lateral crown leader (tilted left-forward)
vec3 qL = q;
qL.y -= 0.05;
qL.xy = rot2(0.56) * qL.xy;
qL.yz = rot2(0.14) * qL.yz;
vec4 tLeft = evalBranchTree(qL * 1.16, 0.73, openVal);
tLeft.x /= 1.16;
// Tertiary sweeping lateral crown leader (tilted right-back)
vec3 qR = q;
qR.y -= 0.08;
qR.xy = rot2(-0.62) * qR.xy;
qR.yz = rot2(-0.16) * qR.yz;
vec4 tRight = evalBranchTree(qR * 1.20, 0.19, openVal);
tRight.x /= 1.20;
// Combine leaders
vec4 res = (tMain.x < tLeft.x) ? tMain : tLeft;
res = (res.x < tRight.x) ? res : tRight;
// Add crisp central entry-pit stem connecting tungsten pin to crown
vec3 stemJit = lightningJitter(q * 8.5) * 0.007;
float dStem = sdCapsule3D(q + stemJit, vec3(0.0, -0.02, 0.0), vec3(0.0, 0.16, 0.0), 0.0068, 0.0042);
if (dStem < res.x) {
res = vec4(dStem, 0.0, clamp(q.y * 0.5, 0.0, 0.15), 0.85);
}
// Softly terminate fractures before touching outer acrylic walls
if (wallMask > 0.0) {
res.x += wallMask * 1.65;
}
return res;
}
// ============================================================================
// Studio Lighting Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 getKeyLightDir() {
if (uLightMode == 2) {
// Grazing raking side light
return normalize(vec3(-0.92, 0.25, 0.30));
} else if (uLightMode == 3) {
// Backlit studio light
return normalize(vec3(0.12, 0.44, -0.89));
}
// Hero & Neutral studio key light
return normalize(vec3(-0.56, 0.72, 0.41));
}
vec3 evalStudioSky(vec3 rd) {
bool isNeutral = (uLightMode == 1);
float y = clamp(rd.y, -1.0, 1.0);
// Base dark cyclorama studio dome (calibrated so neutral mode has strong dark contrast ~25-55/255)
vec3 zenCol = isNeutral ? vec3(0.014) : vec3(0.032, 0.038, 0.052);
vec3 horCol = isNeutral ? vec3(0.030) : vec3(0.055, 0.062, 0.080);
vec3 flrCol = isNeutral ? vec3(0.011) : vec3(0.022, 0.025, 0.032);
vec3 env = (y >= 0.0)
? mix(horCol, zenCol, pow(y, 0.65))
: mix(horCol, flrCol, pow(-y, 0.55));
// Overhead diffused studio softbox (gives crisp top bevel reflections)
float topBox = smoothstep(0.68, 0.86, rd.y) *
smoothstep(0.58, 0.28, abs(rd.x)) *
smoothstep(0.55, 0.25, abs(rd.z));
vec3 topCol = isNeutral ? vec3(1.15) : vec3(1.05, 1.08, 1.16);
env += topBox * topCol * (uLightMode == 2 ? 0.55 : 0.95);
// Key softbox (left-front or grazing depending on uLightMode)
vec3 keyDir = getKeyLightDir();
float keyDot = dot(rd, keyDir);
float keyBox = smoothstep(0.84, 0.96, keyDot);
vec3 keyCol = isNeutral ? vec3(1.55) : vec3(1.45, 1.50, 1.62);
if (uLightMode == 2) keyCol = vec3(1.85, 1.78, 1.68);
if (uLightMode == 3) keyCol = vec3(1.90, 2.05, 2.35);
env += keyBox * keyCol;
// Cool rim strip softbox (right-rear edge catchlight for acrylic bevels)
vec3 rimDir = normalize(vec3(0.76, 0.42, -0.50));
float rimBox = smoothstep(0.86, 0.97, dot(rd, rimDir));
vec3 rimCol = isNeutral ? vec3(1.10) : vec3(0.78, 0.98, 1.35);
env += rimBox * rimCol * (uLightMode == 3 ? 1.35 : 0.85);
// Backlit studio cyclorama panel in uLightMode == 3
if (uLightMode == 3) {
float backPanel = smoothstep(0.52, 0.86, -rd.z) *
smoothstep(0.75, 0.15, abs(rd.x)) *
smoothstep(-0.15, 0.22, rd.y) *
smoothstep(0.82, 0.45, rd.y);
env += backPanel * vec3(1.35, 1.50, 1.75);
}
// Negative-fill flags (dark matte studio panels flanking sides for crystal edge contrast)
float negFlagL = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(-0.94, 0.08, -0.32))));
float negFlagR = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(0.95, 0.05, 0.28))));
float negMask = clamp(negFlagL + negFlagR, 0.0, 1.0);
env = mix(env, env * 0.24, negMask * 0.82);
return env;
}
// ============================================================================
// Bench Shading (with Hero Soft Shadow & Refracted Plasma Spill)
// ============================================================================
vec3 shadeBench(vec3 p, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 V = -rd;
// Subtle micro-brushed anodized slate bench texture
float grain = valueNoise3D(vec3(p.x * 26.0, 0.0, p.z * 6.5)) * 0.5 +
valueNoise3D(vec3(p.x * 78.0, 0.0, p.z * 18.0)) * 0.5;
vec3 baseAlb = isNeutral
? mix(vec3(0.022), vec3(0.034), grain)
: mix(vec3(0.026, 0.029, 0.036), vec3(0.038, 0.043, 0.054), grain);
float rough = mix(0.28, 0.38, grain);
vec3 L = getKeyLightDir();
float NdotL = max(dot(N, L), 0.0);
// Zero-bug rule (3): clean hero-only soft shadow, bias p + N*0.014, tMin 0.025
float sh = calcSoftShadow(p + N * 0.014, L, 0.025, 6.0, 10.0);
// Acrylic is transparent, so its shadow is softened by transmitted light
float shTrans = mix(0.42, 1.0, sh);
// Subtle ambient occlusion directly under the acrylic block footprint
vec2 footDist = abs(p.xz) - BLOCK_HALF.xz;
float dFoot = length(max(footDist, 0.0)) + min(max(footDist.x, footDist.y), 0.0);
float contactAO = mix(0.45, 1.0, smoothstep(-0.02, 0.22, dFoot));
// Diffuse key + studio ambient fill
vec3 keyCol = isNeutral ? vec3(0.95) : vec3(1.05, 1.08, 1.15);
if (uLightMode == 2) keyCol = vec3(1.35, 1.28, 1.18);
if (uLightMode == 3) keyCol = vec3(1.15, 1.28, 1.45);
vec3 diff = baseAlb * (keyCol * NdotL * shTrans + (isNeutral ? vec3(0.085) : vec3(0.065, 0.072, 0.090)) * contactAO);
// Satin specular reflection of studio environment
vec3 H = normalize(L + V);
float specPow = mix(48.0, 18.0, rough);
float spec = pow(max(dot(N, H), 0.0), specPow) * shTrans * 0.35;
float fres = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
vec3 col = diff + keyCol * spec + envRefl * fres * 0.42 * contactAO;
// Transmitted caustic & ionized cyan-violet glow spilled onto bench around block
float glowRad = length(vec2(p.x * 0.72, p.z * 1.15));
float causticBand = exp(-glowRad * glowRad * 1.15);
float nearEdge = exp(-max(dFoot, 0.0) * 3.8);
float pulse = 0.85 + 0.15 * sin(uTime * 2.4 - glowRad * 2.2);
if (isNeutral) {
// Pure neutral optical caustic brightening through the clear block
col += vec3(0.055) * causticBand * nearEdge;
} else {
vec3 plasmaSpill = mix(vec3(0.18, 0.09, 0.48), vec3(0.06, 0.42, 0.68), exp(-glowRad * 1.4));
float energy = mix(0.55, 1.35, openVal) * pulse;
col += plasmaSpill * causticBand * nearEdge * energy * (0.28 + 0.55 * fres);
}
// Subtle distance studio vignette fade into cyclorama horizon
float distFade = smoothstep(5.5, 18.0, length(p.xz));
col = mix(col, evalStudioSky(vec3(rd.x, max(rd.y, 0.04), rd.z)), distFade);
return col;
}
// ============================================================================
// Metallic Discharge Entry Pit Shading
// ============================================================================
vec3 shadeMetallicPit(vec3 p, vec3 N, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 V = -rd;
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
// Brushed tungsten / stainless steel discharge electrode
float micro = valueNoise3D(p * 85.0);
vec3 metalF0 = isNeutral ? vec3(0.68) : vec3(0.62, 0.65, 0.70);
// Subtle dark carbonized scorch right near the strike crater bottom
float scorch = smoothstep(-0.09, -0.01, p.y - PIT_POS.y);
metalF0 *= mix(0.45, 1.0, scorch);
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
vec3 F = metalF0 + (1.0 - metalF0) * pow(1.0 - NdotV, 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
float spec = pow(max(dot(N, H), 0.0), mix(90.0, 42.0, micro)) * 1.65;
vec3 col = envRefl * F * 0.95 + metalF0 * NdotL * 0.22 + F * spec;
// Electric discharge reflection on the metallic pit collar & pin tip
if (!isNeutral) {
float pinTip = exp(-length(p - (PIT_POS - vec3(0.0, 0.095, 0.0))) * 26.0);
float pulse = 0.8 + 0.2 * sin(uTime * 5.0);
col += vec3(0.55, 0.88, 1.0) * pinTip * mix(0.6, 1.5, openVal) * pulse;
}
return col;
}
// ============================================================================
// Internal Volumetric & Surface Raymarch through PMMA Acrylic Block
// ============================================================================
vec3 traceAcrylicInterior(vec3 entryPos, vec3 rayDir, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 L = getKeyLightDir();
// Step slightly inside the acrylic block
vec3 p = entryPos + rayDir * 0.008;
float s = 0.008;
float sMax = 2.85;
vec3 accumLight = vec3(0.0);
float transmittance = 1.0;
// March through the refractive acrylic volume, capturing hairline Lichtenberg channels
for (int i = 0; i < 96; i++) {
float dBlock = sdAcrylicBlock(p);
if (dBlock > 0.002 && s > 0.03) {
break;
}
vec4 tree = evalLichtenberg(p, openVal);
float dTree = tree.x;
float order = tree.y;
float arc = tree.z;
float fHash = tree.w;
// Fine adaptive step size to resolve razor-sharp hairline micro-channels
float dt = clamp(dTree * 0.45, 0.0028, 0.048);
dt = min(dt, max(-dBlock * 0.72, 0.007));
// 1. Physical fractured micro-channel wall (Silver TIR reflections!)
// Tight exponential falloff yields crisp etched frosted-silver filaments
float crackCore = exp(-max(dTree, 0.0) * mix(420.0, 740.0, order));
float crackSheen = exp(-max(dTree, 0.0) * mix(165.0, 290.0, order));
// Faceted micro-crystal sparkle orientation along fracture wall
float facetGlint = 0.48 + 0.52 * pow(0.5 + 0.5 * sin(fHash * 23.0 + dot(p, L) * 34.0 + dot(rayDir, vec3(13.0, 9.0, 17.0))), 3.0);
float lightAngle = 0.55 + 0.45 * clamp(dot(normalize(p - BLOCK_CENTER + vec3(0.001)), L), -0.3, 1.0);
if (isNeutral) {
// Pure neutral grayscale etched silver-white TIR fractures (no emission!)
float silverDensity = (crackCore * 2.25 + crackSheen * 0.35) * facetGlint * lightAngle;
vec3 silverCol = vec3(0.92) * silverDensity;
float alpha = clamp(crackCore * dt * 48.0 + crackSheen * dt * 5.0, 0.0, 0.88);
accumLight += transmittance * silverCol * dt * 36.0;
transmittance *= (1.0 - alpha * 0.65);
} else {
// Silver TIR reflection + razor-sharp ionized cyan-violet plasma discharge
vec3 silverTIR = vec3(0.80, 0.90, 0.98) * (crackCore * 1.95 + crackSheen * 0.30) * facetGlint * lightAngle;
// Traveling ionization pulse down branches from entry pit
float wave1 = exp(-pow(fract(arc * 1.35 - uTime * 0.52) * 4.5, 2.0));
float wave2 = exp(-pow(fract(arc * 2.40 - uTime * 1.08 + fHash * 0.35) * 5.8, 2.0));
float pulse = 0.48 + 0.62 * wave1 + 0.38 * wave2;
// Energized plasma boost from uOpen
float energy = mix(0.65, 1.75, openVal) * pulse;
// Tight electric core -> crisp cyan filament -> subtle ionized violet corona
float halo = exp(-max(dTree, 0.0) * mix(42.0, 74.0, order));
vec3 coreCol = vec3(0.88, 0.98, 1.00) * crackCore * 3.40;
vec3 midCol = vec3(0.10, 0.74, 1.00) * crackSheen * 1.45;
vec3 auraCol = vec3(0.46, 0.16, 0.98) * halo * 0.28;
// Trunk & primary branches burn hotter; fine outer tips lean electric violet
vec3 plasma = mix(coreCol + midCol, midCol * 0.85 + auraCol * 1.45, order * 0.48) + auraCol;
plasma *= energy;
// Focused ignition spark right beneath the tungsten strike pin
float pitGlow = exp(-length(p - (PIT_POS - vec3(0.0, 0.10, 0.0))) * 16.0) * 0.55 * energy;
plasma += vec3(0.50, 0.88, 1.0) * pitGlow;
accumLight += transmittance * (silverTIR * 18.0 + plasma * 13.5) * dt;
// Fractures partially occlude background light (frosted dielectric channels)
float alpha = clamp(crackCore * dt * 42.0 + crackSheen * dt * 4.5, 0.0, 0.78);
transmittance *= (1.0 - alpha * 0.48);
}
if (transmittance < 0.03) break;
s += dt;
if (s > sMax) break;
p = entryPos + rayDir * s;
}
// Optical Beer-Lambert absorption of thick crystal-clear PMMA
vec3 absorb = isNeutral
? exp(-s * vec3(0.055))
: exp(-s * vec3(0.068, 0.030, 0.024));
// Exit boundary refraction / Total Internal Reflection (TIR)
vec3 exitPos = p;
vec3 nOut = calcAcrylicNormal(exitPos);
vec3 nIn = -nOut; // pointing inward toward ray origin
vec3 exitDir = refract(rayDir, nIn, IOR_PMMA);
vec3 behindCol;
if (dot(exitDir, exitDir) < 0.001) {
// TRUE Total Internal Reflection inside acrylic bevels / side walls!
vec3 tirDir = reflect(rayDir, nIn);
vec3 tirEnv = evalStudioSky(tirDir);
behindCol = tirEnv * (isNeutral ? vec3(0.95) : vec3(0.88, 0.94, 1.02));
// Add subtle glowing bevel edge catchlight
if (!isNeutral) {
behindCol += vec3(0.10, 0.32, 0.58) * mix(0.22, 0.55, openVal);
}
} else {
// Compute exit Fresnel reflection + transmitted background (bench or sky)
float cosTheta = max(dot(-rayDir, nIn), 0.0);
float F0 = 0.039;
float fExit = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
vec3 bgCol;
if (exitDir.y < -0.001) {
float tBench = -exitPos.y / exitDir.y;
if (tBench > 0.0 && tBench < 25.0) {
vec3 bp = exitPos + exitDir * tBench;
bgCol = shadeBench(bp, exitDir, openVal);
} else {
bgCol = evalStudioSky(exitDir);
}
} else {
bgCol = evalStudioSky(exitDir);
}
vec3 internalRefl = evalStudioSky(reflect(rayDir, nIn));
behindCol = mix(bgCol, internalRefl, fExit * 0.72);
}
return accumLight * absorb + behindCol * absorb * transmittance;
}
// ============================================================================
// Primary Raymarch & Shading
// ============================================================================
float raymarchScene(vec3 ro, vec3 rd, float tMax) {
float t = 0.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0008) return t;
t += d;
if (t > tMax) break;
}
return -1.0;
}
vec3 renderPixel(vec2 uv) {
float openVal = clamp(uOpen, 0.0, 1.0);
bool isNeutral = (uLightMode == 1);
// Camera setup: uCamMode 0 = Hero establishing, 1 = ~2.5x Macro detail
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x macro focused on metallic entry pit, chamfered bevel, & upper Lichtenberg crown
ro = vec3(0.62, 1.46, 1.98);
ta = vec3(0.0, 0.94, 0.0);
focal = 2.25;
} else {
// Hero 3/4 elevated studio view framing full beveled block, pit, side faces, & bench shadow
ro = vec3(1.68, 2.05, 4.95);
ta = vec3(0.0, 0.68, 0.0);
focal = 2.05;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + focal * ww);
float t = raymarchScene(ro, rd, 22.0);
if (t < 0.0) {
return evalStudioSky(rd);
}
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-bug rule (2): exact bench classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
return shadeBench(p, rd, openVal);
}
// Hero hit: distinguish metallic discharge entry pit vs crystal PMMA acrylic block
float dPit = sdMetallicPit(p);
float dAcrylic = sdAcrylicBlock(p);
vec3 N = calcHeroNormal(p);
if (dPit < dAcrylic + 0.0005) {
return shadeMetallicPit(p, N, rd, openVal);
}
// Crystal-clear beveled PMMA acrylic surface
vec3 V = -rd;
float NdotV = max(dot(N, V), 0.0);
const float F0 = 0.039; // PMMA (n = 1.49)
float fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
// Crisp external studio reflection + specular glints on flat faces & 45-deg bevels
vec3 R = reflect(rd, N);
vec3 reflCol = evalStudioSky(R);
if (R.y < -0.001) {
float tb = -p.y / R.y;
if (tb > 0.0 && tb < 18.0) {
reflCol = mix(reflCol, shadeBench(p + R * tb, R, openVal), 0.85);
}
}
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
float specSharp = pow(max(dot(N, H), 0.0), 320.0) * 2.2;
float specSoft = pow(max(dot(N, H), 0.0), 48.0) * 0.25;
vec3 keySpecCol = isNeutral ? vec3(1.15) : vec3(1.10, 1.15, 1.25);
// Refract into the thick PMMA volume (with subtle chromatic dispersion on non-neutral modes)
vec3 refrDir = refract(rd, N, 1.0 / IOR_PMMA);
vec3 transCol = traceAcrylicInterior(p, refrDir, openVal);
// Subtle edge-bevel dispersion shimmer on chamfered edges (non-neutral only)
if (!isNeutral) {
float bevelMask = 1.0 - max(abs(N.x), max(abs(N.y), abs(N.z)));
float disp = smoothstep(0.12, 0.34, bevelMask);
transCol += disp * vec3(0.08, 0.22, 0.38) * (0.35 + 0.65 * fresnel);
}
vec3 col = mix(transCol, reflCol, clamp(fresnel * 1.08, 0.0, 0.94));
col += keySpecCol * (specSharp + specSoft);
// Subtle rim silhouette brightening in backlit mode
if (uLightMode == 3) {
col += vec3(0.28, 0.42, 0.62) * pow(1.0 - NdotV, 3.2) * 0.75;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping & Safe Luminance Calibration (10..248 / 255)
// ============================================================================
vec3 acesToneMap(vec3 x) {
// Gentle highlight shoulder compression so extreme plasma never hard-clips
x = x / (1.0 + x * 0.065);
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, 0.965);
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution) / max(uResolution.y, 1.0);
vec3 col = renderPixel(uv);
// Enforce pure neutral grayscale when uLightMode == 1
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(lum);
}
// Subtle linear studio floor prevents pure-black crushing (<10/255)
col = max(col, vec3(0.0)) + vec3(0.0045);
// ACES Filmic Tone Mapping + sRGB gamma
col = acesToneMap(col * 1.05);
col = pow(col, vec3(1.0 / 2.2));
// Final safety clamp within studio gamut (no pure 0 or clipped 255)
col = clamp(col, vec3(0.045), vec3(0.968));
fragColor = vec4(col, 1.0);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float out_alpha = study_col.a * COLOR.a;
if (u_luma_alpha_key) {
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
out_alpha *= smoothstep(0.035, 0.22, luma);
}
COLOR = vec4(tinted, out_alpha);
}
shader_type spatial;
render_mode cull_disabled;
// Lichtenberg Fractal Lightning — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/03_lichtenberg_electrical_breakdown.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// ============================================================================
// Constants & Geometry Parameters
// ============================================================================
const float PI = 3.14159265359;
const float IOR_PMMA = 1.49;
// Acrylic block dimensions: rests flush on y = 0.0
const vec3 BLOCK_HALF = vec3(1.12, 0.74, 0.34);
const vec3 BLOCK_CENTER = vec3(0.0, 0.74, 0.0);
const vec3 PIT_POS = vec3(0.0, 1.48, 0.0);
// ============================================================================
// Math & Procedural Noise Utilities
// ============================================================================
float hash11(float p) {
p = fract(p * 0.1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float hash13(vec3 p3) {
p3 = fract(p3 * 0.1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
float valueNoise3D(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);
}
// Sharp-kinked piecewise-linear displacement for authentic jagged lightning
vec3 lightningJitter(vec3 p) {
vec3 a = abs(fract(p * 1.9 + vec3(0.13, 0.47, 0.81)) - 0.5) * 2.0 - 0.5;
vec3 b = abs(fract(p * 4.7 + vec3(0.61, 0.23, 0.37)) - 0.5) * 2.0 - 0.5;
vec3 c = abs(fract(p * 10.3 + vec3(0.29, 0.71, 0.19)) - 0.5) * 2.0 - 0.5;
return a * 0.55 + b * 0.30 + c * 0.15;
}
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
// ============================================================================
// SDF Primitives & Hero Geometry
// ============================================================================
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
// Chamfered & micro-rounded thick PMMA acrylic block resting strictly on y = 0.0
float sdAcrylicBlock(vec3 p) {
vec3 q = p - BLOCK_CENTER;
const float r = 0.016;
float dBox = sdRoundBox(q, BLOCK_HALF - vec3(r), r);
// 45-degree optical bevels along all 12 edges
vec3 aq = abs(q);
float bevel = 0.042;
float cXY = (aq.x + aq.y - (BLOCK_HALF.x + BLOCK_HALF.y - bevel)) * 0.70710678;
float cXZ = (aq.x + aq.z - (BLOCK_HALF.x + BLOCK_HALF.z - bevel)) * 0.70710678;
float cYZ = (aq.y + aq.z - (BLOCK_HALF.y + BLOCK_HALF.z - bevel)) * 0.70710678;
float dBevel = max(cXY, max(cXZ, cYZ));
// Smooth chamfer intersection preserving exact y = 0.0 contact plane
float k = 0.008;
float h = clamp(0.5 - 0.5 * (dBevel - dBox) / k, 0.0, 1.0);
float dBlock = mix(dBevel, dBox, h) + k * h * (1.0 - h);
// Conical discharge entry crater carved into top center
vec3 pq = p - PIT_POS;
float rPit = length(pq.xz);
float pitDepth = 0.11;
float pitRadius = mix(0.016, 0.095, clamp((pq.y + pitDepth) / pitDepth, 0.0, 1.0));
float dCrater = max(rPit - pitRadius, -(pq.y + pitDepth));
dCrater = max(dCrater, pq.y - 0.04);
// Smoothly carve the discharge crater from the acrylic block
float kc = 0.012;
float hc = clamp(0.5 - 0.5 * (dBlock + dCrater) / kc, 0.0, 1.0);
return mix(dBlock, -dCrater, hc) + kc * hc * (1.0 - hc);
}
// Machined metallic discharge entry pit collar & central tungsten strike pin
float sdMetallicPit(vec3 p) {
vec3 pq = p - PIT_POS;
float r = length(pq.xz);
// Recessed machined ring/ferrule seated inside top crater mouth
vec2 torusQ = vec2(r - 0.072, pq.y + 0.008);
float dRing = length(torusQ) - 0.016;
// Inner tapered metallic cup lining the crater wall
float cupR = mix(0.018, 0.076, clamp((pq.y + 0.095) / 0.095, 0.0, 1.0));
float dCup = abs(r - cupR) - 0.007;
dCup = max(dCup, abs(pq.y + 0.050) - 0.045);
// Sharp central tungsten discharge pin pointing downward into acrylic
float pinR = mix(0.003, 0.018, clamp((pq.y + 0.115) / 0.115, 0.0, 1.0));
float dPin = max(r - pinR, abs(pq.y + 0.058) - 0.060);
return min(dRing, min(dCup, dPin));
}
float mapHero(vec3 p) {
return min(sdAcrylicBlock(p), sdMetallicPit(p));
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// Zero-bug rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * mapHero(p + k.xyy * e) +
k.yyx * mapHero(p + k.yyx * e) +
k.yxy * mapHero(p + k.yxy * e) +
k.xxx * mapHero(p + k.xxx * e)
);
}
vec3 calcAcrylicNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * sdAcrylicBlock(p + k.xyy * e) +
k.yyx * sdAcrylicBlock(p + k.yyx * e) +
k.yxy * sdAcrylicBlock(p + k.yxy * e) +
k.xxx * sdAcrylicBlock(p + k.xxx * e)
);
}
// Zero-bug rule (3): Clean hero-only soft shadow (no ph)
float calcSoftShadow(vec3 ro, vec3 rd, float tMin, float tMax, float k) {
float res = 1.0;
float t = tMin;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.001) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
return clamp(res, 0.0, 1.0);
}
// ============================================================================
// 3D Fractal Branching Lichtenberg Dielectric Breakdown Field
// ============================================================================
float sdCapsule3D(vec3 p, vec3 a, vec3 b, float ra, float rb) {
vec3 pa = p - a;
vec3 ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// Evaluates one folded 3D fractal Lichtenberg branch tree
// Returns vec4(distance, branchOrder 0..1, arcFromPit 0..1, fractureHash 0..1)
vec4 evalBranchTree(vec3 q, float seed, float openVal) {
float dMin = 1e5;
float bestOrder = 1.0;
float bestArc = 1.0;
float bestHash = 0.5;
vec3 bp = q;
float sc = 1.0;
float arcAccum = 0.0;
float branchId = seed;
// 7 generations of razor-sharp 3D dendritic dielectric breakdown
for (int i = 0; i < 7; i++) {
float fi = float(i);
float order = fi / 6.0;
// Sharp angular lightning kinks along current limb
vec3 kink = lightningJitter(bp * (2.3 + fi * 0.65) + vec3(branchId * 3.7, fi * 1.9, branchId * 2.3));
vec3 jp = bp + vec3(kink.x, 0.0, kink.z * 0.65) * (0.068 / (1.0 + fi * 0.08));
// Limb length and needle-tapered hairline radius
float segLen = mix(0.36, 0.44, openVal) * (1.0 - fi * 0.025);
float rTop = mix(0.0052, 0.00085, order) * mix(0.92, 1.08, openVal);
float rBot = rTop * mix(0.45, 0.14, order);
// Distance to current segment from y=0 to y=segLen
float hy = clamp(jp.y / segLen, 0.0, 1.0);
vec3 segPt = vec3(0.0, hy * segLen, 0.0);
float rHere = mix(rTop, rBot, pow(hy, 0.85));
float dSeg = (length(jp - segPt) - rHere) / sc;
// Delicate lateral dendritic micro-spurs branching off each segment
if (i >= 1) {
vec3 sp = jp;
sp.y -= segLen * (0.35 + 0.35 * hash11(branchId * 9.3 + fi));
sp.x = abs(sp.x);
sp.xy = rot2(-0.78) * sp.xy;
float spurLen = segLen * 0.46;
float hs = clamp(sp.y / spurLen, 0.0, 1.0);
float rSpur = mix(rTop * 0.52, 0.00025, hs);
float dSpur = (length(sp - vec3(0.0, hs * spurLen, 0.0)) - rSpur) / sc;
dSeg = min(dSeg, dSpur);
}
// Deeper outer generations extend smoothly with uOpen
float genFade = smoothstep(4.4 + openVal * 2.8, 2.8 + openVal * 2.5, fi);
dSeg += (1.0 - genFade) * 0.04;
if (dSeg < dMin) {
dMin = dSeg;
bestOrder = order;
bestArc = clamp((arcAccum + hy * segLen / sc) * 0.72, 0.0, 1.0);
bestHash = hash11(branchId * 17.3 + floor(jp.y * 26.0));
}
// Advance into next child branch generation
float sgnX = (bp.x >= 0.0) ? 1.0 : -1.0;
float sgnZ = (bp.z >= 0.0) ? 1.0 : -1.0;
float hFork = hash11(branchId * 11.7 + sgnX * 2.31 + fi * 4.13);
float hForkZ = hash11(branchId * 7.9 + sgnZ * 3.17 + fi * 2.71);
float forkY = segLen * (0.54 + 0.32 * hFork);
arcAccum += forkY / sc;
bp.y -= forkY;
// Mirror-fold X and Z with organic angular asymmetry
bp.x = abs(bp.x) - 0.007 * (1.0 - order * 0.5);
float angXY = (0.48 + 0.24 * hFork) * (1.0 - 0.04 * fi);
bp.xy = rot2(-angXY) * bp.xy;
// Controlled 3D Z-branching keeps fractures inside thick acrylic slab
float angYZ = (0.16 + 0.14 * hForkZ) * sgnZ * (1.0 - order * 0.35);
bp.yz = rot2(angYZ) * bp.yz;
// Scale down child branch space
float scaleStep = 1.40 + 0.07 * hFork;
bp *= scaleStep;
sc *= scaleStep;
branchId = fract(branchId * 2.37 + sgnX * 0.41 + sgnZ * 0.19 + 0.13);
}
return vec4(dMin, bestOrder, bestArc, bestHash);
}
// Combined 3D Lichtenberg tree inside the PMMA block
vec4 evalLichtenberg(vec3 p, float openVal) {
// Anchor right below the tungsten strike pin in the metallic entry pit
vec3 q = vec3(p.x, (PIT_POS.y - 0.095) - p.y, p.z);
// Keep fractures strictly enclosed within acrylic volume with smooth margin
vec3 aq = abs(p - BLOCK_CENTER);
vec3 margin = BLOCK_HALF - vec3(0.095, 0.085, 0.075);
float wallMask = max(aq.x - margin.x, max(aq.y - margin.y, aq.z - margin.z));
if (wallMask > 0.14) {
return vec4(wallMask, 1.0, 1.0, 0.5);
}
// Central primary downward cascade
vec4 tMain = evalBranchTree(q, 0.37, openVal);
// Secondary sweeping lateral crown leader (tilted left-forward)
vec3 qL = q;
qL.y -= 0.05;
qL.xy = rot2(0.56) * qL.xy;
qL.yz = rot2(0.14) * qL.yz;
vec4 tLeft = evalBranchTree(qL * 1.16, 0.73, openVal);
tLeft.x /= 1.16;
// Tertiary sweeping lateral crown leader (tilted right-back)
vec3 qR = q;
qR.y -= 0.08;
qR.xy = rot2(-0.62) * qR.xy;
qR.yz = rot2(-0.16) * qR.yz;
vec4 tRight = evalBranchTree(qR * 1.20, 0.19, openVal);
tRight.x /= 1.20;
// Combine leaders
vec4 res = (tMain.x < tLeft.x) ? tMain : tLeft;
res = (res.x < tRight.x) ? res : tRight;
// Add crisp central entry-pit stem connecting tungsten pin to crown
vec3 stemJit = lightningJitter(q * 8.5) * 0.007;
float dStem = sdCapsule3D(q + stemJit, vec3(0.0, -0.02, 0.0), vec3(0.0, 0.16, 0.0), 0.0068, 0.0042);
if (dStem < res.x) {
res = vec4(dStem, 0.0, clamp(q.y * 0.5, 0.0, 0.15), 0.85);
}
// Softly terminate fractures before touching outer acrylic walls
if (wallMask > 0.0) {
res.x += wallMask * 1.65;
}
return res;
}
// ============================================================================
// Studio Lighting Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 getKeyLightDir() {
if (uLightMode == 2) {
// Grazing raking side light
return normalize(vec3(-0.92, 0.25, 0.30));
} else if (uLightMode == 3) {
// Backlit studio light
return normalize(vec3(0.12, 0.44, -0.89));
}
// Hero & Neutral studio key light
return normalize(vec3(-0.56, 0.72, 0.41));
}
vec3 evalStudioSky(vec3 rd) {
bool isNeutral = (uLightMode == 1);
float y = clamp(rd.y, -1.0, 1.0);
// Base dark cyclorama studio dome (calibrated so neutral mode has strong dark contrast ~25-55/255)
vec3 zenCol = isNeutral ? vec3(0.014) : vec3(0.032, 0.038, 0.052);
vec3 horCol = isNeutral ? vec3(0.030) : vec3(0.055, 0.062, 0.080);
vec3 flrCol = isNeutral ? vec3(0.011) : vec3(0.022, 0.025, 0.032);
vec3 env = (y >= 0.0)
? mix(horCol, zenCol, pow(y, 0.65))
: mix(horCol, flrCol, pow(-y, 0.55));
// Overhead diffused studio softbox (gives crisp top bevel reflections)
float topBox = smoothstep(0.68, 0.86, rd.y) *
smoothstep(0.58, 0.28, abs(rd.x)) *
smoothstep(0.55, 0.25, abs(rd.z));
vec3 topCol = isNeutral ? vec3(1.15) : vec3(1.05, 1.08, 1.16);
env += topBox * topCol * (uLightMode == 2 ? 0.55 : 0.95);
// Key softbox (left-front or grazing depending on uLightMode)
vec3 keyDir = getKeyLightDir();
float keyDot = dot(rd, keyDir);
float keyBox = smoothstep(0.84, 0.96, keyDot);
vec3 keyCol = isNeutral ? vec3(1.55) : vec3(1.45, 1.50, 1.62);
if (uLightMode == 2) keyCol = vec3(1.85, 1.78, 1.68);
if (uLightMode == 3) keyCol = vec3(1.90, 2.05, 2.35);
env += keyBox * keyCol;
// Cool rim strip softbox (right-rear edge catchlight for acrylic bevels)
vec3 rimDir = normalize(vec3(0.76, 0.42, -0.50));
float rimBox = smoothstep(0.86, 0.97, dot(rd, rimDir));
vec3 rimCol = isNeutral ? vec3(1.10) : vec3(0.78, 0.98, 1.35);
env += rimBox * rimCol * (uLightMode == 3 ? 1.35 : 0.85);
// Backlit studio cyclorama panel in uLightMode == 3
if (uLightMode == 3) {
float backPanel = smoothstep(0.52, 0.86, -rd.z) *
smoothstep(0.75, 0.15, abs(rd.x)) *
smoothstep(-0.15, 0.22, rd.y) *
smoothstep(0.82, 0.45, rd.y);
env += backPanel * vec3(1.35, 1.50, 1.75);
}
// Negative-fill flags (dark matte studio panels flanking sides for crystal edge contrast)
float negFlagL = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(-0.94, 0.08, -0.32))));
float negFlagR = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(0.95, 0.05, 0.28))));
float negMask = clamp(negFlagL + negFlagR, 0.0, 1.0);
env = mix(env, env * 0.24, negMask * 0.82);
return env;
}
// ============================================================================
// Bench Shading (with Hero Soft Shadow & Refracted Plasma Spill)
// ============================================================================
vec3 shadeBench(vec3 p, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 V = -rd;
// Subtle micro-brushed anodized slate bench texture
float grain = valueNoise3D(vec3(p.x * 26.0, 0.0, p.z * 6.5)) * 0.5 +
valueNoise3D(vec3(p.x * 78.0, 0.0, p.z * 18.0)) * 0.5;
vec3 baseAlb = isNeutral
? mix(vec3(0.022), vec3(0.034), grain)
: mix(vec3(0.026, 0.029, 0.036), vec3(0.038, 0.043, 0.054), grain);
float rough = mix(0.28, 0.38, grain);
vec3 L = getKeyLightDir();
float NdotL = max(dot(N, L), 0.0);
// Zero-bug rule (3): clean hero-only soft shadow, bias p + N*0.014, tMin 0.025
float sh = calcSoftShadow(p + N * 0.014, L, 0.025, 6.0, 10.0);
// Acrylic is transparent, so its shadow is softened by transmitted light
float shTrans = mix(0.42, 1.0, sh);
// Subtle ambient occlusion directly under the acrylic block footprint
vec2 footDist = abs(p.xz) - BLOCK_HALF.xz;
float dFoot = length(max(footDist, 0.0)) + min(max(footDist.x, footDist.y), 0.0);
float contactAO = mix(0.45, 1.0, smoothstep(-0.02, 0.22, dFoot));
// Diffuse key + studio ambient fill
vec3 keyCol = isNeutral ? vec3(0.95) : vec3(1.05, 1.08, 1.15);
if (uLightMode == 2) keyCol = vec3(1.35, 1.28, 1.18);
if (uLightMode == 3) keyCol = vec3(1.15, 1.28, 1.45);
vec3 diff = baseAlb * (keyCol * NdotL * shTrans + (isNeutral ? vec3(0.085) : vec3(0.065, 0.072, 0.090)) * contactAO);
// Satin specular reflection of studio environment
vec3 H = normalize(L + V);
float specPow = mix(48.0, 18.0, rough);
float spec = pow(max(dot(N, H), 0.0), specPow) * shTrans * 0.35;
float fres = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
vec3 col = diff + keyCol * spec + envRefl * fres * 0.42 * contactAO;
// Transmitted caustic & ionized cyan-violet glow spilled onto bench around block
float glowRad = length(vec2(p.x * 0.72, p.z * 1.15));
float causticBand = exp(-glowRad * glowRad * 1.15);
float nearEdge = exp(-max(dFoot, 0.0) * 3.8);
float pulse = 0.85 + 0.15 * sin(uTime * 2.4 - glowRad * 2.2);
if (isNeutral) {
// Pure neutral optical caustic brightening through the clear block
col += vec3(0.055) * causticBand * nearEdge;
} else {
vec3 plasmaSpill = mix(vec3(0.18, 0.09, 0.48), vec3(0.06, 0.42, 0.68), exp(-glowRad * 1.4));
float energy = mix(0.55, 1.35, openVal) * pulse;
col += plasmaSpill * causticBand * nearEdge * energy * (0.28 + 0.55 * fres);
}
// Subtle distance studio vignette fade into cyclorama horizon
float distFade = smoothstep(5.5, 18.0, length(p.xz));
col = mix(col, evalStudioSky(vec3(rd.x, max(rd.y, 0.04), rd.z)), distFade);
return col;
}
// ============================================================================
// Metallic Discharge Entry Pit Shading
// ============================================================================
vec3 shadeMetallicPit(vec3 p, vec3 N, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 V = -rd;
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
// Brushed tungsten / stainless steel discharge electrode
float micro = valueNoise3D(p * 85.0);
vec3 metalF0 = isNeutral ? vec3(0.68) : vec3(0.62, 0.65, 0.70);
// Subtle dark carbonized scorch right near the strike crater bottom
float scorch = smoothstep(-0.09, -0.01, p.y - PIT_POS.y);
metalF0 *= mix(0.45, 1.0, scorch);
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
vec3 F = metalF0 + (1.0 - metalF0) * pow(1.0 - NdotV, 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
float spec = pow(max(dot(N, H), 0.0), mix(90.0, 42.0, micro)) * 1.65;
vec3 col = envRefl * F * 0.95 + metalF0 * NdotL * 0.22 + F * spec;
// Electric discharge reflection on the metallic pit collar & pin tip
if (!isNeutral) {
float pinTip = exp(-length(p - (PIT_POS - vec3(0.0, 0.095, 0.0))) * 26.0);
float pulse = 0.8 + 0.2 * sin(uTime * 5.0);
col += vec3(0.55, 0.88, 1.0) * pinTip * mix(0.6, 1.5, openVal) * pulse;
}
return col;
}
// ============================================================================
// Internal Volumetric & Surface Raymarch through PMMA Acrylic Block
// ============================================================================
vec3 traceAcrylicInterior(vec3 entryPos, vec3 rayDir, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 L = getKeyLightDir();
// Step slightly inside the acrylic block
vec3 p = entryPos + rayDir * 0.008;
float s = 0.008;
float sMax = 2.85;
vec3 accumLight = vec3(0.0);
float transmittance = 1.0;
// March through the refractive acrylic volume, capturing hairline Lichtenberg channels
for (int i = 0; i < 96; i++) {
float dBlock = sdAcrylicBlock(p);
if (dBlock > 0.002 && s > 0.03) {
break;
}
vec4 tree = evalLichtenberg(p, openVal);
float dTree = tree.x;
float order = tree.y;
float arc = tree.z;
float fHash = tree.w;
// Fine adaptive step size to resolve razor-sharp hairline micro-channels
float dt = clamp(dTree * 0.45, 0.0028, 0.048);
dt = min(dt, max(-dBlock * 0.72, 0.007));
// 1. Physical fractured micro-channel wall (Silver TIR reflections!)
// Tight exponential falloff yields crisp etched frosted-silver filaments
float crackCore = exp(-max(dTree, 0.0) * mix(420.0, 740.0, order));
float crackSheen = exp(-max(dTree, 0.0) * mix(165.0, 290.0, order));
// Faceted micro-crystal sparkle orientation along fracture wall
float facetGlint = 0.48 + 0.52 * pow(0.5 + 0.5 * sin(fHash * 23.0 + dot(p, L) * 34.0 + dot(rayDir, vec3(13.0, 9.0, 17.0))), 3.0);
float lightAngle = 0.55 + 0.45 * clamp(dot(normalize(p - BLOCK_CENTER + vec3(0.001)), L), -0.3, 1.0);
if (isNeutral) {
// Pure neutral grayscale etched silver-white TIR fractures (no emission!)
float silverDensity = (crackCore * 2.25 + crackSheen * 0.35) * facetGlint * lightAngle;
vec3 silverCol = vec3(0.92) * silverDensity;
float alpha = clamp(crackCore * dt * 48.0 + crackSheen * dt * 5.0, 0.0, 0.88);
accumLight += transmittance * silverCol * dt * 36.0;
transmittance *= (1.0 - alpha * 0.65);
} else {
// Silver TIR reflection + razor-sharp ionized cyan-violet plasma discharge
vec3 silverTIR = vec3(0.80, 0.90, 0.98) * (crackCore * 1.95 + crackSheen * 0.30) * facetGlint * lightAngle;
// Traveling ionization pulse down branches from entry pit
float wave1 = exp(-pow(fract(arc * 1.35 - uTime * 0.52) * 4.5, 2.0));
float wave2 = exp(-pow(fract(arc * 2.40 - uTime * 1.08 + fHash * 0.35) * 5.8, 2.0));
float pulse = 0.48 + 0.62 * wave1 + 0.38 * wave2;
// Energized plasma boost from uOpen
float energy = mix(0.65, 1.75, openVal) * pulse;
// Tight electric core -> crisp cyan filament -> subtle ionized violet corona
float halo = exp(-max(dTree, 0.0) * mix(42.0, 74.0, order));
vec3 coreCol = vec3(0.88, 0.98, 1.00) * crackCore * 3.40;
vec3 midCol = vec3(0.10, 0.74, 1.00) * crackSheen * 1.45;
vec3 auraCol = vec3(0.46, 0.16, 0.98) * halo * 0.28;
// Trunk & primary branches burn hotter; fine outer tips lean electric violet
vec3 plasma = mix(coreCol + midCol, midCol * 0.85 + auraCol * 1.45, order * 0.48) + auraCol;
plasma *= energy;
// Focused ignition spark right beneath the tungsten strike pin
float pitGlow = exp(-length(p - (PIT_POS - vec3(0.0, 0.10, 0.0))) * 16.0) * 0.55 * energy;
plasma += vec3(0.50, 0.88, 1.0) * pitGlow;
accumLight += transmittance * (silverTIR * 18.0 + plasma * 13.5) * dt;
// Fractures partially occlude background light (frosted dielectric channels)
float alpha = clamp(crackCore * dt * 42.0 + crackSheen * dt * 4.5, 0.0, 0.78);
transmittance *= (1.0 - alpha * 0.48);
}
if (transmittance < 0.03) break;
s += dt;
if (s > sMax) break;
p = entryPos + rayDir * s;
}
// Optical Beer-Lambert absorption of thick crystal-clear PMMA
vec3 absorb = isNeutral
? exp(-s * vec3(0.055))
: exp(-s * vec3(0.068, 0.030, 0.024));
// Exit boundary refraction / Total Internal Reflection (TIR)
vec3 exitPos = p;
vec3 nOut = calcAcrylicNormal(exitPos);
vec3 nIn = -nOut; // pointing inward toward ray origin
vec3 exitDir = refract(rayDir, nIn, IOR_PMMA);
vec3 behindCol;
if (dot(exitDir, exitDir) < 0.001) {
// TRUE Total Internal Reflection inside acrylic bevels / side walls!
vec3 tirDir = reflect(rayDir, nIn);
vec3 tirEnv = evalStudioSky(tirDir);
behindCol = tirEnv * (isNeutral ? vec3(0.95) : vec3(0.88, 0.94, 1.02));
// Add subtle glowing bevel edge catchlight
if (!isNeutral) {
behindCol += vec3(0.10, 0.32, 0.58) * mix(0.22, 0.55, openVal);
}
} else {
// Compute exit Fresnel reflection + transmitted background (bench or sky)
float cosTheta = max(dot(-rayDir, nIn), 0.0);
float F0 = 0.039;
float fExit = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
vec3 bgCol;
if (exitDir.y < -0.001) {
float tBench = -exitPos.y / exitDir.y;
if (tBench > 0.0 && tBench < 25.0) {
vec3 bp = exitPos + exitDir * tBench;
bgCol = shadeBench(bp, exitDir, openVal);
} else {
bgCol = evalStudioSky(exitDir);
}
} else {
bgCol = evalStudioSky(exitDir);
}
vec3 internalRefl = evalStudioSky(reflect(rayDir, nIn));
behindCol = mix(bgCol, internalRefl, fExit * 0.72);
}
return accumLight * absorb + behindCol * absorb * transmittance;
}
// ============================================================================
// Primary Raymarch & Shading
// ============================================================================
float raymarchScene(vec3 ro, vec3 rd, float tMax) {
float t = 0.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0008) return t;
t += d;
if (t > tMax) break;
}
return -1.0;
}
vec3 renderPixel(vec2 uv) {
float openVal = clamp(uOpen, 0.0, 1.0);
bool isNeutral = (uLightMode == 1);
// Camera setup: uCamMode 0 = Hero establishing, 1 = ~2.5x Macro detail
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x macro focused on metallic entry pit, chamfered bevel, & upper Lichtenberg crown
ro = vec3(0.62, 1.46, 1.98);
ta = vec3(0.0, 0.94, 0.0);
focal = 2.25;
} else {
// Hero 3/4 elevated studio view framing full beveled block, pit, side faces, & bench shadow
ro = vec3(1.68, 2.05, 4.95);
ta = vec3(0.0, 0.68, 0.0);
focal = 2.05;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + focal * ww);
float t = raymarchScene(ro, rd, 22.0);
if (t < 0.0) {
return evalStudioSky(rd);
}
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-bug rule (2): exact bench classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
return shadeBench(p, rd, openVal);
}
// Hero hit: distinguish metallic discharge entry pit vs crystal PMMA acrylic block
float dPit = sdMetallicPit(p);
float dAcrylic = sdAcrylicBlock(p);
vec3 N = calcHeroNormal(p);
if (dPit < dAcrylic + 0.0005) {
return shadeMetallicPit(p, N, rd, openVal);
}
// Crystal-clear beveled PMMA acrylic surface
vec3 V = -rd;
float NdotV = max(dot(N, V), 0.0);
const float F0 = 0.039; // PMMA (n = 1.49)
float fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
// Crisp external studio reflection + specular glints on flat faces & 45-deg bevels
vec3 R = reflect(rd, N);
vec3 reflCol = evalStudioSky(R);
if (R.y < -0.001) {
float tb = -p.y / R.y;
if (tb > 0.0 && tb < 18.0) {
reflCol = mix(reflCol, shadeBench(p + R * tb, R, openVal), 0.85);
}
}
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
float specSharp = pow(max(dot(N, H), 0.0), 320.0) * 2.2;
float specSoft = pow(max(dot(N, H), 0.0), 48.0) * 0.25;
vec3 keySpecCol = isNeutral ? vec3(1.15) : vec3(1.10, 1.15, 1.25);
// Refract into the thick PMMA volume (with subtle chromatic dispersion on non-neutral modes)
vec3 refrDir = refract(rd, N, 1.0 / IOR_PMMA);
vec3 transCol = traceAcrylicInterior(p, refrDir, openVal);
// Subtle edge-bevel dispersion shimmer on chamfered edges (non-neutral only)
if (!isNeutral) {
float bevelMask = 1.0 - max(abs(N.x), max(abs(N.y), abs(N.z)));
float disp = smoothstep(0.12, 0.34, bevelMask);
transCol += disp * vec3(0.08, 0.22, 0.38) * (0.35 + 0.65 * fresnel);
}
vec3 col = mix(transCol, reflCol, clamp(fresnel * 1.08, 0.0, 0.94));
col += keySpecCol * (specSharp + specSoft);
// Subtle rim silhouette brightening in backlit mode
if (uLightMode == 3) {
col += vec3(0.28, 0.42, 0.62) * pow(1.0 - NdotV, 3.2) * 0.75;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping & Safe Luminance Calibration (10..248 / 255)
// ============================================================================
vec3 acesToneMap(vec3 x) {
// Gentle highlight shoulder compression so extreme plasma never hard-clips
x = x / (1.0 + x * 0.065);
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, 0.965);
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution) / max(uResolution.y, 1.0);
vec3 col = renderPixel(uv);
// Enforce pure neutral grayscale when uLightMode == 1
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(lum);
}
// Subtle linear studio floor prevents pure-black crushing (<10/255)
col = max(col, vec3(0.0)) + vec3(0.0045);
// ACES Filmic Tone Mapping + sRGB gamma
col = acesToneMap(col * 1.05);
col = pow(col, vec3(1.0 / 2.2));
// Final safety clamp within studio gamut (no pure 0 or clipped 255)
col = clamp(col, vec3(0.045), vec3(0.968));
fragColor = vec4(col, 1.0);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
ALBEDO = tinted;
ROUGHNESS = clamp(0.85 - luma * 0.45, 0.08, 0.96);
METALLIC = 0.04;
EMISSION = tinted * 0.28;
}
// Lichtenberg Fractal Lightning — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/03_lichtenberg_electrical_breakdown.glsl (MIT License)
import * as THREE from 'three';
const vertexShader = /* glsl */ `
out vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = /* glsl */ `
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
// ============================================================================
// Constants & Geometry Parameters
// ============================================================================
const float PI = 3.14159265359;
const float IOR_PMMA = 1.49;
// Acrylic block dimensions: rests flush on y = 0.0
const vec3 BLOCK_HALF = vec3(1.12, 0.74, 0.34);
const vec3 BLOCK_CENTER = vec3(0.0, 0.74, 0.0);
const vec3 PIT_POS = vec3(0.0, 1.48, 0.0);
// ============================================================================
// Math & Procedural Noise Utilities
// ============================================================================
float hash11(float p) {
p = fract(p * 0.1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float hash13(vec3 p3) {
p3 = fract(p3 * 0.1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
float valueNoise3D(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);
}
// Sharp-kinked piecewise-linear displacement for authentic jagged lightning
vec3 lightningJitter(vec3 p) {
vec3 a = abs(fract(p * 1.9 + vec3(0.13, 0.47, 0.81)) - 0.5) * 2.0 - 0.5;
vec3 b = abs(fract(p * 4.7 + vec3(0.61, 0.23, 0.37)) - 0.5) * 2.0 - 0.5;
vec3 c = abs(fract(p * 10.3 + vec3(0.29, 0.71, 0.19)) - 0.5) * 2.0 - 0.5;
return a * 0.55 + b * 0.30 + c * 0.15;
}
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
// ============================================================================
// SDF Primitives & Hero Geometry
// ============================================================================
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
// Chamfered & micro-rounded thick PMMA acrylic block resting strictly on y = 0.0
float sdAcrylicBlock(vec3 p) {
vec3 q = p - BLOCK_CENTER;
const float r = 0.016;
float dBox = sdRoundBox(q, BLOCK_HALF - vec3(r), r);
// 45-degree optical bevels along all 12 edges
vec3 aq = abs(q);
float bevel = 0.042;
float cXY = (aq.x + aq.y - (BLOCK_HALF.x + BLOCK_HALF.y - bevel)) * 0.70710678;
float cXZ = (aq.x + aq.z - (BLOCK_HALF.x + BLOCK_HALF.z - bevel)) * 0.70710678;
float cYZ = (aq.y + aq.z - (BLOCK_HALF.y + BLOCK_HALF.z - bevel)) * 0.70710678;
float dBevel = max(cXY, max(cXZ, cYZ));
// Smooth chamfer intersection preserving exact y = 0.0 contact plane
float k = 0.008;
float h = clamp(0.5 - 0.5 * (dBevel - dBox) / k, 0.0, 1.0);
float dBlock = mix(dBevel, dBox, h) + k * h * (1.0 - h);
// Conical discharge entry crater carved into top center
vec3 pq = p - PIT_POS;
float rPit = length(pq.xz);
float pitDepth = 0.11;
float pitRadius = mix(0.016, 0.095, clamp((pq.y + pitDepth) / pitDepth, 0.0, 1.0));
float dCrater = max(rPit - pitRadius, -(pq.y + pitDepth));
dCrater = max(dCrater, pq.y - 0.04);
// Smoothly carve the discharge crater from the acrylic block
float kc = 0.012;
float hc = clamp(0.5 - 0.5 * (dBlock + dCrater) / kc, 0.0, 1.0);
return mix(dBlock, -dCrater, hc) + kc * hc * (1.0 - hc);
}
// Machined metallic discharge entry pit collar & central tungsten strike pin
float sdMetallicPit(vec3 p) {
vec3 pq = p - PIT_POS;
float r = length(pq.xz);
// Recessed machined ring/ferrule seated inside top crater mouth
vec2 torusQ = vec2(r - 0.072, pq.y + 0.008);
float dRing = length(torusQ) - 0.016;
// Inner tapered metallic cup lining the crater wall
float cupR = mix(0.018, 0.076, clamp((pq.y + 0.095) / 0.095, 0.0, 1.0));
float dCup = abs(r - cupR) - 0.007;
dCup = max(dCup, abs(pq.y + 0.050) - 0.045);
// Sharp central tungsten discharge pin pointing downward into acrylic
float pinR = mix(0.003, 0.018, clamp((pq.y + 0.115) / 0.115, 0.0, 1.0));
float dPin = max(r - pinR, abs(pq.y + 0.058) - 0.060);
return min(dRing, min(dCup, dPin));
}
float mapHero(vec3 p) {
return min(sdAcrylicBlock(p), sdMetallicPit(p));
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// Zero-bug rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * mapHero(p + k.xyy * e) +
k.yyx * mapHero(p + k.yyx * e) +
k.yxy * mapHero(p + k.yxy * e) +
k.xxx * mapHero(p + k.xxx * e)
);
}
vec3 calcAcrylicNormal(vec3 p) {
const float e = 0.0012;
const vec2 k = vec2(1., -1.);
return normalize(
k.xyy * sdAcrylicBlock(p + k.xyy * e) +
k.yyx * sdAcrylicBlock(p + k.yyx * e) +
k.yxy * sdAcrylicBlock(p + k.yxy * e) +
k.xxx * sdAcrylicBlock(p + k.xxx * e)
);
}
// Zero-bug rule (3): Clean hero-only soft shadow (no ph)
float calcSoftShadow(vec3 ro, vec3 rd, float tMin, float tMax, float k) {
float res = 1.0;
float t = tMin;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.001) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > tMax) break;
}
return clamp(res, 0.0, 1.0);
}
// ============================================================================
// 3D Fractal Branching Lichtenberg Dielectric Breakdown Field
// ============================================================================
float sdCapsule3D(vec3 p, vec3 a, vec3 b, float ra, float rb) {
vec3 pa = p - a;
vec3 ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
return length(pa - ba * h) - mix(ra, rb, h);
}
// Evaluates one folded 3D fractal Lichtenberg branch tree
// Returns vec4(distance, branchOrder 0..1, arcFromPit 0..1, fractureHash 0..1)
vec4 evalBranchTree(vec3 q, float seed, float openVal) {
float dMin = 1e5;
float bestOrder = 1.0;
float bestArc = 1.0;
float bestHash = 0.5;
vec3 bp = q;
float sc = 1.0;
float arcAccum = 0.0;
float branchId = seed;
// 7 generations of razor-sharp 3D dendritic dielectric breakdown
for (int i = 0; i < 7; i++) {
float fi = float(i);
float order = fi / 6.0;
// Sharp angular lightning kinks along current limb
vec3 kink = lightningJitter(bp * (2.3 + fi * 0.65) + vec3(branchId * 3.7, fi * 1.9, branchId * 2.3));
vec3 jp = bp + vec3(kink.x, 0.0, kink.z * 0.65) * (0.068 / (1.0 + fi * 0.08));
// Limb length and needle-tapered hairline radius
float segLen = mix(0.36, 0.44, openVal) * (1.0 - fi * 0.025);
float rTop = mix(0.0052, 0.00085, order) * mix(0.92, 1.08, openVal);
float rBot = rTop * mix(0.45, 0.14, order);
// Distance to current segment from y=0 to y=segLen
float hy = clamp(jp.y / segLen, 0.0, 1.0);
vec3 segPt = vec3(0.0, hy * segLen, 0.0);
float rHere = mix(rTop, rBot, pow(hy, 0.85));
float dSeg = (length(jp - segPt) - rHere) / sc;
// Delicate lateral dendritic micro-spurs branching off each segment
if (i >= 1) {
vec3 sp = jp;
sp.y -= segLen * (0.35 + 0.35 * hash11(branchId * 9.3 + fi));
sp.x = abs(sp.x);
sp.xy = rot2(-0.78) * sp.xy;
float spurLen = segLen * 0.46;
float hs = clamp(sp.y / spurLen, 0.0, 1.0);
float rSpur = mix(rTop * 0.52, 0.00025, hs);
float dSpur = (length(sp - vec3(0.0, hs * spurLen, 0.0)) - rSpur) / sc;
dSeg = min(dSeg, dSpur);
}
// Deeper outer generations extend smoothly with uOpen
float genFade = smoothstep(4.4 + openVal * 2.8, 2.8 + openVal * 2.5, fi);
dSeg += (1.0 - genFade) * 0.04;
if (dSeg < dMin) {
dMin = dSeg;
bestOrder = order;
bestArc = clamp((arcAccum + hy * segLen / sc) * 0.72, 0.0, 1.0);
bestHash = hash11(branchId * 17.3 + floor(jp.y * 26.0));
}
// Advance into next child branch generation
float sgnX = (bp.x >= 0.0) ? 1.0 : -1.0;
float sgnZ = (bp.z >= 0.0) ? 1.0 : -1.0;
float hFork = hash11(branchId * 11.7 + sgnX * 2.31 + fi * 4.13);
float hForkZ = hash11(branchId * 7.9 + sgnZ * 3.17 + fi * 2.71);
float forkY = segLen * (0.54 + 0.32 * hFork);
arcAccum += forkY / sc;
bp.y -= forkY;
// Mirror-fold X and Z with organic angular asymmetry
bp.x = abs(bp.x) - 0.007 * (1.0 - order * 0.5);
float angXY = (0.48 + 0.24 * hFork) * (1.0 - 0.04 * fi);
bp.xy = rot2(-angXY) * bp.xy;
// Controlled 3D Z-branching keeps fractures inside thick acrylic slab
float angYZ = (0.16 + 0.14 * hForkZ) * sgnZ * (1.0 - order * 0.35);
bp.yz = rot2(angYZ) * bp.yz;
// Scale down child branch space
float scaleStep = 1.40 + 0.07 * hFork;
bp *= scaleStep;
sc *= scaleStep;
branchId = fract(branchId * 2.37 + sgnX * 0.41 + sgnZ * 0.19 + 0.13);
}
return vec4(dMin, bestOrder, bestArc, bestHash);
}
// Combined 3D Lichtenberg tree inside the PMMA block
vec4 evalLichtenberg(vec3 p, float openVal) {
// Anchor right below the tungsten strike pin in the metallic entry pit
vec3 q = vec3(p.x, (PIT_POS.y - 0.095) - p.y, p.z);
// Keep fractures strictly enclosed within acrylic volume with smooth margin
vec3 aq = abs(p - BLOCK_CENTER);
vec3 margin = BLOCK_HALF - vec3(0.095, 0.085, 0.075);
float wallMask = max(aq.x - margin.x, max(aq.y - margin.y, aq.z - margin.z));
if (wallMask > 0.14) {
return vec4(wallMask, 1.0, 1.0, 0.5);
}
// Central primary downward cascade
vec4 tMain = evalBranchTree(q, 0.37, openVal);
// Secondary sweeping lateral crown leader (tilted left-forward)
vec3 qL = q;
qL.y -= 0.05;
qL.xy = rot2(0.56) * qL.xy;
qL.yz = rot2(0.14) * qL.yz;
vec4 tLeft = evalBranchTree(qL * 1.16, 0.73, openVal);
tLeft.x /= 1.16;
// Tertiary sweeping lateral crown leader (tilted right-back)
vec3 qR = q;
qR.y -= 0.08;
qR.xy = rot2(-0.62) * qR.xy;
qR.yz = rot2(-0.16) * qR.yz;
vec4 tRight = evalBranchTree(qR * 1.20, 0.19, openVal);
tRight.x /= 1.20;
// Combine leaders
vec4 res = (tMain.x < tLeft.x) ? tMain : tLeft;
res = (res.x < tRight.x) ? res : tRight;
// Add crisp central entry-pit stem connecting tungsten pin to crown
vec3 stemJit = lightningJitter(q * 8.5) * 0.007;
float dStem = sdCapsule3D(q + stemJit, vec3(0.0, -0.02, 0.0), vec3(0.0, 0.16, 0.0), 0.0068, 0.0042);
if (dStem < res.x) {
res = vec4(dStem, 0.0, clamp(q.y * 0.5, 0.0, 0.15), 0.85);
}
// Softly terminate fractures before touching outer acrylic walls
if (wallMask > 0.0) {
res.x += wallMask * 1.65;
}
return res;
}
// ============================================================================
// Studio Lighting Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 getKeyLightDir() {
if (uLightMode == 2) {
// Grazing raking side light
return normalize(vec3(-0.92, 0.25, 0.30));
} else if (uLightMode == 3) {
// Backlit studio light
return normalize(vec3(0.12, 0.44, -0.89));
}
// Hero & Neutral studio key light
return normalize(vec3(-0.56, 0.72, 0.41));
}
vec3 evalStudioSky(vec3 rd) {
bool isNeutral = (uLightMode == 1);
float y = clamp(rd.y, -1.0, 1.0);
// Base dark cyclorama studio dome (calibrated so neutral mode has strong dark contrast ~25-55/255)
vec3 zenCol = isNeutral ? vec3(0.014) : vec3(0.032, 0.038, 0.052);
vec3 horCol = isNeutral ? vec3(0.030) : vec3(0.055, 0.062, 0.080);
vec3 flrCol = isNeutral ? vec3(0.011) : vec3(0.022, 0.025, 0.032);
vec3 env = (y >= 0.0)
? mix(horCol, zenCol, pow(y, 0.65))
: mix(horCol, flrCol, pow(-y, 0.55));
// Overhead diffused studio softbox (gives crisp top bevel reflections)
float topBox = smoothstep(0.68, 0.86, rd.y) *
smoothstep(0.58, 0.28, abs(rd.x)) *
smoothstep(0.55, 0.25, abs(rd.z));
vec3 topCol = isNeutral ? vec3(1.15) : vec3(1.05, 1.08, 1.16);
env += topBox * topCol * (uLightMode == 2 ? 0.55 : 0.95);
// Key softbox (left-front or grazing depending on uLightMode)
vec3 keyDir = getKeyLightDir();
float keyDot = dot(rd, keyDir);
float keyBox = smoothstep(0.84, 0.96, keyDot);
vec3 keyCol = isNeutral ? vec3(1.55) : vec3(1.45, 1.50, 1.62);
if (uLightMode == 2) keyCol = vec3(1.85, 1.78, 1.68);
if (uLightMode == 3) keyCol = vec3(1.90, 2.05, 2.35);
env += keyBox * keyCol;
// Cool rim strip softbox (right-rear edge catchlight for acrylic bevels)
vec3 rimDir = normalize(vec3(0.76, 0.42, -0.50));
float rimBox = smoothstep(0.86, 0.97, dot(rd, rimDir));
vec3 rimCol = isNeutral ? vec3(1.10) : vec3(0.78, 0.98, 1.35);
env += rimBox * rimCol * (uLightMode == 3 ? 1.35 : 0.85);
// Backlit studio cyclorama panel in uLightMode == 3
if (uLightMode == 3) {
float backPanel = smoothstep(0.52, 0.86, -rd.z) *
smoothstep(0.75, 0.15, abs(rd.x)) *
smoothstep(-0.15, 0.22, rd.y) *
smoothstep(0.82, 0.45, rd.y);
env += backPanel * vec3(1.35, 1.50, 1.75);
}
// Negative-fill flags (dark matte studio panels flanking sides for crystal edge contrast)
float negFlagL = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(-0.94, 0.08, -0.32))));
float negFlagR = smoothstep(0.78, 0.93, dot(rd, normalize(vec3(0.95, 0.05, 0.28))));
float negMask = clamp(negFlagL + negFlagR, 0.0, 1.0);
env = mix(env, env * 0.24, negMask * 0.82);
return env;
}
// ============================================================================
// Bench Shading (with Hero Soft Shadow & Refracted Plasma Spill)
// ============================================================================
vec3 shadeBench(vec3 p, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 N = vec3(0.0, 1.0, 0.0);
vec3 V = -rd;
// Subtle micro-brushed anodized slate bench texture
float grain = valueNoise3D(vec3(p.x * 26.0, 0.0, p.z * 6.5)) * 0.5 +
valueNoise3D(vec3(p.x * 78.0, 0.0, p.z * 18.0)) * 0.5;
vec3 baseAlb = isNeutral
? mix(vec3(0.022), vec3(0.034), grain)
: mix(vec3(0.026, 0.029, 0.036), vec3(0.038, 0.043, 0.054), grain);
float rough = mix(0.28, 0.38, grain);
vec3 L = getKeyLightDir();
float NdotL = max(dot(N, L), 0.0);
// Zero-bug rule (3): clean hero-only soft shadow, bias p + N*0.014, tMin 0.025
float sh = calcSoftShadow(p + N * 0.014, L, 0.025, 6.0, 10.0);
// Acrylic is transparent, so its shadow is softened by transmitted light
float shTrans = mix(0.42, 1.0, sh);
// Subtle ambient occlusion directly under the acrylic block footprint
vec2 footDist = abs(p.xz) - BLOCK_HALF.xz;
float dFoot = length(max(footDist, 0.0)) + min(max(footDist.x, footDist.y), 0.0);
float contactAO = mix(0.45, 1.0, smoothstep(-0.02, 0.22, dFoot));
// Diffuse key + studio ambient fill
vec3 keyCol = isNeutral ? vec3(0.95) : vec3(1.05, 1.08, 1.15);
if (uLightMode == 2) keyCol = vec3(1.35, 1.28, 1.18);
if (uLightMode == 3) keyCol = vec3(1.15, 1.28, 1.45);
vec3 diff = baseAlb * (keyCol * NdotL * shTrans + (isNeutral ? vec3(0.085) : vec3(0.065, 0.072, 0.090)) * contactAO);
// Satin specular reflection of studio environment
vec3 H = normalize(L + V);
float specPow = mix(48.0, 18.0, rough);
float spec = pow(max(dot(N, H), 0.0), specPow) * shTrans * 0.35;
float fres = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
vec3 col = diff + keyCol * spec + envRefl * fres * 0.42 * contactAO;
// Transmitted caustic & ionized cyan-violet glow spilled onto bench around block
float glowRad = length(vec2(p.x * 0.72, p.z * 1.15));
float causticBand = exp(-glowRad * glowRad * 1.15);
float nearEdge = exp(-max(dFoot, 0.0) * 3.8);
float pulse = 0.85 + 0.15 * sin(uTime * 2.4 - glowRad * 2.2);
if (isNeutral) {
// Pure neutral optical caustic brightening through the clear block
col += vec3(0.055) * causticBand * nearEdge;
} else {
vec3 plasmaSpill = mix(vec3(0.18, 0.09, 0.48), vec3(0.06, 0.42, 0.68), exp(-glowRad * 1.4));
float energy = mix(0.55, 1.35, openVal) * pulse;
col += plasmaSpill * causticBand * nearEdge * energy * (0.28 + 0.55 * fres);
}
// Subtle distance studio vignette fade into cyclorama horizon
float distFade = smoothstep(5.5, 18.0, length(p.xz));
col = mix(col, evalStudioSky(vec3(rd.x, max(rd.y, 0.04), rd.z)), distFade);
return col;
}
// ============================================================================
// Metallic Discharge Entry Pit Shading
// ============================================================================
vec3 shadeMetallicPit(vec3 p, vec3 N, vec3 rd, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 V = -rd;
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
// Brushed tungsten / stainless steel discharge electrode
float micro = valueNoise3D(p * 85.0);
vec3 metalF0 = isNeutral ? vec3(0.68) : vec3(0.62, 0.65, 0.70);
// Subtle dark carbonized scorch right near the strike crater bottom
float scorch = smoothstep(-0.09, -0.01, p.y - PIT_POS.y);
metalF0 *= mix(0.45, 1.0, scorch);
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
vec3 F = metalF0 + (1.0 - metalF0) * pow(1.0 - NdotV, 5.0);
vec3 envRefl = evalStudioSky(reflect(rd, N));
float spec = pow(max(dot(N, H), 0.0), mix(90.0, 42.0, micro)) * 1.65;
vec3 col = envRefl * F * 0.95 + metalF0 * NdotL * 0.22 + F * spec;
// Electric discharge reflection on the metallic pit collar & pin tip
if (!isNeutral) {
float pinTip = exp(-length(p - (PIT_POS - vec3(0.0, 0.095, 0.0))) * 26.0);
float pulse = 0.8 + 0.2 * sin(uTime * 5.0);
col += vec3(0.55, 0.88, 1.0) * pinTip * mix(0.6, 1.5, openVal) * pulse;
}
return col;
}
// ============================================================================
// Internal Volumetric & Surface Raymarch through PMMA Acrylic Block
// ============================================================================
vec3 traceAcrylicInterior(vec3 entryPos, vec3 rayDir, float openVal) {
bool isNeutral = (uLightMode == 1);
vec3 L = getKeyLightDir();
// Step slightly inside the acrylic block
vec3 p = entryPos + rayDir * 0.008;
float s = 0.008;
float sMax = 2.85;
vec3 accumLight = vec3(0.0);
float transmittance = 1.0;
// March through the refractive acrylic volume, capturing hairline Lichtenberg channels
for (int i = 0; i < 96; i++) {
float dBlock = sdAcrylicBlock(p);
if (dBlock > 0.002 && s > 0.03) {
break;
}
vec4 tree = evalLichtenberg(p, openVal);
float dTree = tree.x;
float order = tree.y;
float arc = tree.z;
float fHash = tree.w;
// Fine adaptive step size to resolve razor-sharp hairline micro-channels
float dt = clamp(dTree * 0.45, 0.0028, 0.048);
dt = min(dt, max(-dBlock * 0.72, 0.007));
// 1. Physical fractured micro-channel wall (Silver TIR reflections!)
// Tight exponential falloff yields crisp etched frosted-silver filaments
float crackCore = exp(-max(dTree, 0.0) * mix(420.0, 740.0, order));
float crackSheen = exp(-max(dTree, 0.0) * mix(165.0, 290.0, order));
// Faceted micro-crystal sparkle orientation along fracture wall
float facetGlint = 0.48 + 0.52 * pow(0.5 + 0.5 * sin(fHash * 23.0 + dot(p, L) * 34.0 + dot(rayDir, vec3(13.0, 9.0, 17.0))), 3.0);
float lightAngle = 0.55 + 0.45 * clamp(dot(normalize(p - BLOCK_CENTER + vec3(0.001)), L), -0.3, 1.0);
if (isNeutral) {
// Pure neutral grayscale etched silver-white TIR fractures (no emission!)
float silverDensity = (crackCore * 2.25 + crackSheen * 0.35) * facetGlint * lightAngle;
vec3 silverCol = vec3(0.92) * silverDensity;
float alpha = clamp(crackCore * dt * 48.0 + crackSheen * dt * 5.0, 0.0, 0.88);
accumLight += transmittance * silverCol * dt * 36.0;
transmittance *= (1.0 - alpha * 0.65);
} else {
// Silver TIR reflection + razor-sharp ionized cyan-violet plasma discharge
vec3 silverTIR = vec3(0.80, 0.90, 0.98) * (crackCore * 1.95 + crackSheen * 0.30) * facetGlint * lightAngle;
// Traveling ionization pulse down branches from entry pit
float wave1 = exp(-pow(fract(arc * 1.35 - uTime * 0.52) * 4.5, 2.0));
float wave2 = exp(-pow(fract(arc * 2.40 - uTime * 1.08 + fHash * 0.35) * 5.8, 2.0));
float pulse = 0.48 + 0.62 * wave1 + 0.38 * wave2;
// Energized plasma boost from uOpen
float energy = mix(0.65, 1.75, openVal) * pulse;
// Tight electric core -> crisp cyan filament -> subtle ionized violet corona
float halo = exp(-max(dTree, 0.0) * mix(42.0, 74.0, order));
vec3 coreCol = vec3(0.88, 0.98, 1.00) * crackCore * 3.40;
vec3 midCol = vec3(0.10, 0.74, 1.00) * crackSheen * 1.45;
vec3 auraCol = vec3(0.46, 0.16, 0.98) * halo * 0.28;
// Trunk & primary branches burn hotter; fine outer tips lean electric violet
vec3 plasma = mix(coreCol + midCol, midCol * 0.85 + auraCol * 1.45, order * 0.48) + auraCol;
plasma *= energy;
// Focused ignition spark right beneath the tungsten strike pin
float pitGlow = exp(-length(p - (PIT_POS - vec3(0.0, 0.10, 0.0))) * 16.0) * 0.55 * energy;
plasma += vec3(0.50, 0.88, 1.0) * pitGlow;
accumLight += transmittance * (silverTIR * 18.0 + plasma * 13.5) * dt;
// Fractures partially occlude background light (frosted dielectric channels)
float alpha = clamp(crackCore * dt * 42.0 + crackSheen * dt * 4.5, 0.0, 0.78);
transmittance *= (1.0 - alpha * 0.48);
}
if (transmittance < 0.03) break;
s += dt;
if (s > sMax) break;
p = entryPos + rayDir * s;
}
// Optical Beer-Lambert absorption of thick crystal-clear PMMA
vec3 absorb = isNeutral
? exp(-s * vec3(0.055))
: exp(-s * vec3(0.068, 0.030, 0.024));
// Exit boundary refraction / Total Internal Reflection (TIR)
vec3 exitPos = p;
vec3 nOut = calcAcrylicNormal(exitPos);
vec3 nIn = -nOut; // pointing inward toward ray origin
vec3 exitDir = refract(rayDir, nIn, IOR_PMMA);
vec3 behindCol;
if (dot(exitDir, exitDir) < 0.001) {
// TRUE Total Internal Reflection inside acrylic bevels / side walls!
vec3 tirDir = reflect(rayDir, nIn);
vec3 tirEnv = evalStudioSky(tirDir);
behindCol = tirEnv * (isNeutral ? vec3(0.95) : vec3(0.88, 0.94, 1.02));
// Add subtle glowing bevel edge catchlight
if (!isNeutral) {
behindCol += vec3(0.10, 0.32, 0.58) * mix(0.22, 0.55, openVal);
}
} else {
// Compute exit Fresnel reflection + transmitted background (bench or sky)
float cosTheta = max(dot(-rayDir, nIn), 0.0);
float F0 = 0.039;
float fExit = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
vec3 bgCol;
if (exitDir.y < -0.001) {
float tBench = -exitPos.y / exitDir.y;
if (tBench > 0.0 && tBench < 25.0) {
vec3 bp = exitPos + exitDir * tBench;
bgCol = shadeBench(bp, exitDir, openVal);
} else {
bgCol = evalStudioSky(exitDir);
}
} else {
bgCol = evalStudioSky(exitDir);
}
vec3 internalRefl = evalStudioSky(reflect(rayDir, nIn));
behindCol = mix(bgCol, internalRefl, fExit * 0.72);
}
return accumLight * absorb + behindCol * absorb * transmittance;
}
// ============================================================================
// Primary Raymarch & Shading
// ============================================================================
float raymarchScene(vec3 ro, vec3 rd, float tMax) {
float t = 0.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0008) return t;
t += d;
if (t > tMax) break;
}
return -1.0;
}
vec3 renderPixel(vec2 uv) {
float openVal = clamp(uOpen, 0.0, 1.0);
bool isNeutral = (uLightMode == 1);
// Camera setup: uCamMode 0 = Hero establishing, 1 = ~2.5x Macro detail
vec3 ro, ta;
float focal;
if (uCamMode == 1) {
// ~2.5x macro focused on metallic entry pit, chamfered bevel, & upper Lichtenberg crown
ro = vec3(0.62, 1.46, 1.98);
ta = vec3(0.0, 0.94, 0.0);
focal = 2.25;
} else {
// Hero 3/4 elevated studio view framing full beveled block, pit, side faces, & bench shadow
ro = vec3(1.68, 2.05, 4.95);
ta = vec3(0.0, 0.68, 0.0);
focal = 2.05;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + focal * ww);
float t = raymarchScene(ro, rd, 22.0);
if (t < 0.0) {
return evalStudioSky(rd);
}
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-bug rule (2): exact bench classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
return shadeBench(p, rd, openVal);
}
// Hero hit: distinguish metallic discharge entry pit vs crystal PMMA acrylic block
float dPit = sdMetallicPit(p);
float dAcrylic = sdAcrylicBlock(p);
vec3 N = calcHeroNormal(p);
if (dPit < dAcrylic + 0.0005) {
return shadeMetallicPit(p, N, rd, openVal);
}
// Crystal-clear beveled PMMA acrylic surface
vec3 V = -rd;
float NdotV = max(dot(N, V), 0.0);
const float F0 = 0.039; // PMMA (n = 1.49)
float fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
// Crisp external studio reflection + specular glints on flat faces & 45-deg bevels
vec3 R = reflect(rd, N);
vec3 reflCol = evalStudioSky(R);
if (R.y < -0.001) {
float tb = -p.y / R.y;
if (tb > 0.0 && tb < 18.0) {
reflCol = mix(reflCol, shadeBench(p + R * tb, R, openVal), 0.85);
}
}
vec3 L = getKeyLightDir();
vec3 H = normalize(L + V);
float specSharp = pow(max(dot(N, H), 0.0), 320.0) * 2.2;
float specSoft = pow(max(dot(N, H), 0.0), 48.0) * 0.25;
vec3 keySpecCol = isNeutral ? vec3(1.15) : vec3(1.10, 1.15, 1.25);
// Refract into the thick PMMA volume (with subtle chromatic dispersion on non-neutral modes)
vec3 refrDir = refract(rd, N, 1.0 / IOR_PMMA);
vec3 transCol = traceAcrylicInterior(p, refrDir, openVal);
// Subtle edge-bevel dispersion shimmer on chamfered edges (non-neutral only)
if (!isNeutral) {
float bevelMask = 1.0 - max(abs(N.x), max(abs(N.y), abs(N.z)));
float disp = smoothstep(0.12, 0.34, bevelMask);
transCol += disp * vec3(0.08, 0.22, 0.38) * (0.35 + 0.65 * fresnel);
}
vec3 col = mix(transCol, reflCol, clamp(fresnel * 1.08, 0.0, 0.94));
col += keySpecCol * (specSharp + specSoft);
// Subtle rim silhouette brightening in backlit mode
if (uLightMode == 3) {
col += vec3(0.28, 0.42, 0.62) * pow(1.0 - NdotV, 3.2) * 0.75;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping & Safe Luminance Calibration (10..248 / 255)
// ============================================================================
vec3 acesToneMap(vec3 x) {
// Gentle highlight shoulder compression so extreme plasma never hard-clips
x = x / (1.0 + x * 0.065);
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, 0.965);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / max(uResolution.y, 1.0);
vec3 col = renderPixel(uv);
// Enforce pure neutral grayscale when uLightMode == 1
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(lum);
}
// Subtle linear studio floor prevents pure-black crushing (<10/255)
col = max(col, vec3(0.0)) + vec3(0.0045);
// ACES Filmic Tone Mapping + sRGB gamma
col = acesToneMap(col * 1.05);
col = pow(col, vec3(1.0 / 2.2));
// Final safety clamp within studio gamut (no pure 0 or clipped 255)
col = clamp(col, vec3(0.045), vec3(0.968));
fragColor = vec4(col, 1.0);
}
`;
export function createLichtenbergFractalLightningMaterial(width = 1024, height = 1024) {
const material = new THREE.ShaderMaterial({
glslVersion: THREE.GLSL3,
vertexShader,
fragmentShader,
uniforms: {
uResolution: { value: new THREE.Vector2(width, height) },
uTime: { value: 1.2 },
uOpen: { value: 0.0 },
uLightMode: { value: 0 },
uCamMode: { value: 1 },
},
});
return {
material,
update(elapsedSeconds) {
material.uniforms.uTime.value = elapsedSeconds * 1.0;
},
resize(nextW, nextH) {
material.uniforms.uResolution.value.set(nextW, nextH);
},
};
}
| Identifier | lichtenberg-fractal-lightning |
|---|---|
| Category | Sci-Fi, Plasma & Noise Shaders · Plasma, Lava & Fractures |
| Upstream study | thermal/lib/03_lichtenberg_electrical_breakdown.glsl (commit 095539c) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 790 lines · 29.5 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT lightning sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation
David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, tetrahedral SDF normal
AnimatedBuoyant high-voltage thermal plasma arc climbing twin electrodes with ionized violet aura.
AnimatedPolygonal charcoal and obsidian crust plates glowing Planck-accurate blackbody orange in deep fissures.
AnimatedIonized purple-blue St. Elmo’s brush filaments flickering from a metallic conductor tip.
AnimatedHierarchical primary and secondary crazing fracture network suspended inside translucent jade glaze.
AnimatedDark stoneware shards repaired with raised metallic gold-dusted urushi lacquer seams.
AnimatedAeolian desert sand ripples with bifurcating Y-junction defects and sparkling quartz grains.