AnimatedPolygonal charcoal and obsidian crust plates glowing Planck-accurate blackbody orange in deep fissures.
jacobs-ladder-plasma-arc.glsl · 917 lines · GLSL ES 3.00 · MIT
#version 300 es
// Jacob’s Ladder Plasma Arc (jacobs-ladder-plasma-arc.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/05_jacobs_ladder_plasma_arc.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 & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_BASEPLATE = 1;
const int MAT_CERAMIC = 2;
const int MAT_BRASS = 3;
const int MAT_COPPER = 4;
// ============================================================================
// Hash & Procedural Noise
// ============================================================================
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 noise2D(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i + vec2(0.0, 0.0));
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float noise3D(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
vec3 u = f * f * (3.0 - 2.0 * f);
float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
float n111 = hash13(i + vec3(1.0, 1.0, 1.0));
float nx00 = mix(n000, n100, u.x);
float nx10 = mix(n010, n110, u.x);
float nx01 = mix(n001, n101, u.x);
float nx11 = mix(n011, n111, u.x);
float nxy0 = mix(nx00, nx10, u.y);
float nxy1 = mix(nx01, nx11, u.y);
return mix(nxy0, nxy1, u.z);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * noise2D(p);
p = m * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// ============================================================================
// SDF Primitives
// ============================================================================
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;
}
float sdCylinder(vec3 p, float r, float h) {
vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCappedCone(vec3 p, float h, float r1, float r2) {
vec2 q = vec2(length(p.xz), p.y);
vec2 k1 = vec2(r2, h);
vec2 k2 = vec2(r2 - r1, 2.0 * h);
vec2 ca = vec2(q.x - min(q.x, (q.y < 0.0) ? r1 : r2), abs(q.y) - h);
vec2 cb = q - k1 + k2 * clamp(dot(k1 - q, k2) / dot(k2, k2), 0.0, 1.0);
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s * sqrt(min(dot(ca, ca), dot(cb, cb)));
}
float sdHexPrism(vec3 p, vec2 h) {
const vec3 k = vec3(-0.8660254, 0.5, 0.57735027);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0.0) * k.xy;
vec2 d = vec2(
length(p.xz - vec2(clamp(p.x, -k.z * h.x, k.z * h.x), h.x)) * sign(p.z - h.x),
p.y - h.y
);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - r;
}
float smin(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
// ============================================================================
// Jacob's Ladder Electrode & Plasma Arc Kinematics
// ============================================================================
// Returns horizontal offset (+x) of the right copper electrode centerline at height y
float electrodeX(float y) {
if (y <= 0.88) {
// S-bend inward from insulator terminal (x=0.19 at y=0.64) to narrow ignition throat (x=0.058 at y=0.88)
float t = clamp((y - 0.64) / 0.24, 0.0, 1.0);
float s = t * t * (3.0 - 2.0 * t);
return mix(0.19, 0.058, s);
} else {
// Classic diverging horn-gap climbing from y=0.88 to y=2.28
float t = clamp((y - 0.88) / 1.40, 0.0, 1.0);
// Smooth concave-to-flared horn divergence
return 0.058 + 0.26 * pow(t, 1.28) + 0.14 * t * t * t;
}
}
float getArcPhase() {
if (uOpen > 0.0001) {
return clamp(uOpen, 0.0, 1.0);
}
return fract(0.46 + uTime * 0.14);
}
float getArcY(float phase) {
// Climbs from ignition throat (0.91) to upper horn release (2.19)
return mix(0.91, 2.19, phase);
}
// Evaluates 3D point on the primary buoyant plasma arc for normalized parameter u in [-1, 1]
vec3 evalArcPoint(float u, float arcY, float phase, float tAnim) {
float halfSpan = electrodeX(arcY) - 0.021;
float env = max(0.0, 1.0 - u * u);
float bowHeight = (0.08 + 0.30 * pow(phase, 1.2)) * env;
// Multi-frequency ionized plasma writhe pinned to electrodes at u = +-1
float w1 = sin(u * 4.5 - tAnim * 6.5) * 0.018;
float w2 = sin(u * 11.3 + tAnim * 13.7 + phase * 9.0) * 0.008;
float w3 = sin(u * 23.0 - tAnim * 21.0) * 0.003;
float dy = bowHeight + env * (w1 + w2 + w3) * (0.55 + 0.85 * phase);
float z1 = sin(u * 3.8 + tAnim * 5.2) * 0.024;
float z2 = cos(u * 9.7 - tAnim * 11.4 + phase * 6.0) * 0.010;
float dz = env * (z1 + z2) * (0.45 + 0.85 * phase);
return vec3(u * halfSpan, arcY + dy, dz);
}
// Evaluates secondary braided filament offset slightly from main arc
vec3 evalArcSecondary(float u, float arcY, float phase, float tAnim) {
vec3 p = evalArcPoint(u, arcY, phase, tAnim);
float env = max(0.0, 1.0 - u * u);
p.y += env * (0.014 + 0.016 * sin(u * 7.5 + tAnim * 14.0));
p.z += env * (0.014 * cos(u * 8.2 - tAnim * 12.5));
return p;
}
// Distance from 3D point p to the primary plasma arc curve
float distToArc(vec3 p, float arcY, float phase, float tAnim, out float uOut) {
float halfSpan = max(electrodeX(arcY) - 0.021, 0.02);
float u = clamp(p.x / halfSpan, -1.0, 1.0);
// Two-step local projection refinement along the bowed curve
for (int i = 0; i < 2; i++) {
vec3 c = evalArcPoint(u, arcY, phase, tAnim);
vec3 c2 = evalArcPoint(clamp(u + 0.02, -1.0, 1.0), arcY, phase, tAnim);
vec3 tang = c2 - c;
float denom = dot(tang, tang) + 1e-6;
float du = dot(p - c, tang) / denom * 0.02;
u = clamp(u + du, -1.0, 1.0);
}
uOut = u;
return length(p - evalArcPoint(u, arcY, phase, tAnim));
}
// ============================================================================
// Hero Apparatus SDF
// ============================================================================
float sdInsulatorColumn(vec3 p) {
// Central cylindrical porcelain core (y = 0.09..0.58)
float core = sdCylinder(p - vec3(0.0, 0.335, 0.0), 0.072, 0.245);
// 5 overhanging glazed ceramic petticoat ribs/sheds
float sheds = 1e5;
for (int i = 0; i < 5; i++) {
float fi = float(i);
float cy = 0.165 + fi * 0.088;
float rOuter = 0.132 - fi * 0.004;
float rTop = 0.076;
// Asymmetric rain-shed profile: sloped top cone + undercut lower lip
float topCone = sdCappedCone(p - vec3(0.0, cy + 0.012, 0.0), 0.024, rOuter, rTop) - 0.008;
float dripLip = sdCappedCone(p - vec3(0.0, cy - 0.012, 0.0), 0.010, rOuter * 0.84, rOuter) - 0.006;
float shed = smin(topCone, dripLip, 0.010);
sheds = min(sheds, shed);
}
// Bottom ceramic mounting skirt
float skirt = sdCappedCone(p - vec3(0.0, 0.115, 0.0), 0.028, 0.118, 0.092) - 0.007;
float d = smin(core, sheds, 0.012);
d = smin(d, skirt, 0.010);
return d;
}
float sdCopperElectrodes(vec3 p) {
// Symmetric in X
vec3 q = vec3(abs(p.x), p.y, p.z);
float d = 1e5;
// 10 exact capsules along electrodeX(y) from y=0.62 to y=2.28
const float yStart = 0.62;
const float yEnd = 2.28;
const float dy = (yEnd - yStart) / 10.0;
const float rodR = 0.0235;
vec3 prevP = vec3(electrodeX(yStart), yStart, 0.0);
for (int i = 1; i <= 10; i++) {
float yn = yStart + float(i) * dy;
vec3 nextP = vec3(electrodeX(yn), yn, 0.0);
d = min(d, sdCapsule(q, prevP, nextP, rodR));
prevP = nextP;
}
// Subtle rounded tip bead at top of each rod
vec3 tipP = vec3(electrodeX(yEnd), yEnd, 0.0);
d = smin(d, length(q - tipP) - 0.0255, 0.008);
return d;
}
float mapHeroMat(vec3 p, out int matId) {
matId = MAT_BASEPLATE;
float d = 1e5;
// 1. Heavy laboratory cast-iron / anodized baseplate (y = 0.0..0.095)
float baseMain = sdRoundBox(p - vec3(0.0, 0.044, 0.0), vec3(0.50, 0.032, 0.25), 0.012);
float baseStep = sdRoundBox(p - vec3(0.0, 0.080, 0.0), vec3(0.45, 0.016, 0.205), 0.009);
float dBase = smin(baseMain, baseStep, 0.008);
if (dBase < d) { d = dBase; matId = MAT_BASEPLATE; }
// 2. Ribbed brown ceramic high-voltage insulator assembly (twin ribbed towers + central ceramic saddle)
vec3 pSym = vec3(abs(p.x), p.y, p.z);
float dTowers = sdInsulatorColumn(pSym - vec3(0.19, 0.0, 0.0));
// Glazed ceramic bridging saddle joining the lower base of the twin insulators
float dSaddle = sdRoundBox(p - vec3(0.0, 0.145, 0.0), vec3(0.16, 0.042, 0.085), 0.018);
float dCeramic = smin(dTowers, dSaddle, 0.018);
if (dCeramic < d) { d = dCeramic; matId = MAT_CERAMIC; }
// 3. Machined brass hardware: terminal collars, hex clamping nuts, set screws, corner bolts, nameplate
float dBrass = 1e5;
// Insulator top brass cap & electrode clamp collar at x = +-0.19, y = 0.58..0.67
vec3 pCol = pSym - vec3(0.19, 0.615, 0.0);
float capCyl = sdCylinder(pCol + vec3(0.0, 0.018, 0.0), 0.064, 0.022) - 0.004;
float hexNut = sdHexPrism(pCol - vec3(0.0, 0.024, 0.0), vec2(0.046, 0.024)) - 0.003;
float clampCol = sdCylinder(pCol - vec3(0.0, 0.052, 0.0), 0.036, 0.016) - 0.003;
// Lateral brass binding post thumbscrew facing outward
vec3 pScrew = pSym - vec3(0.245, 0.635, 0.0);
float screw = sdCylinder(pScrew.yxz, 0.015, 0.024) - 0.002;
dBrass = min(dBrass, min(min(capCyl, hexNut), min(clampCol, screw)));
// 4 brass corner mounting hex bolts on baseplate
vec3 pBolt = vec3(abs(p.x) - 0.425, p.y - 0.094, abs(p.z) - 0.175);
float bolt = sdHexPrism(pBolt, vec2(0.022, 0.014)) - 0.002;
float washer = sdCylinder(pBolt + vec3(0.0, 0.010, 0.0), 0.030, 0.004);
dBrass = min(dBrass, min(bolt, washer));
// Front brass voltage warning plaque on base bevel
float plaque = sdRoundBox(p - vec3(0.0, 0.048, 0.258), vec3(0.12, 0.022, 0.005), 0.002);
dBrass = min(dBrass, plaque);
if (dBrass < d) { d = dBrass; matId = MAT_BRASS; }
// 4. Diverging brushed copper rod electrodes
float dCopper = sdCopperElectrodes(p);
if (dCopper < d) { d = dCopper; matId = MAT_COPPER; }
return d;
}
float mapHero(vec3 p) {
int dummy;
return mapHeroMat(p, dummy);
}
// ============================================================================
// Normals, Soft Shadow, Ambient Occlusion (Zero-Bug Rules)
// ============================================================================
// Rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0008;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Rule (3): Clean hero-only soft shadow (no ph), bias p+N*0.014, tMin=0.025, step max(h,0.015)
float calcSoftShadow(vec3 p, vec3 N, vec3 L, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + L * t);
if (h < 0.0005) 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);
}
float calcAO(vec3 p, vec3 N) {
float occ = 0.0;
float sca = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.022 * float(i);
float d = min(p.y + N.y * h, mapHero(p + N * h));
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Studio Sky Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 rd, int lightMode) {
float up = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.008, 0.009, 0.013);
vec3 horCol = vec3(0.022, 0.025, 0.034);
vec3 topCol = vec3(0.011, 0.013, 0.020);
if (lightMode == 1) {
// Pure neutral grayscale studio
botCol = vec3(0.09);
horCol = vec3(0.18);
topCol = vec3(0.14);
} else if (lightMode == 2) {
// Grazing moody dark studio
botCol = vec3(0.007, 0.008, 0.011);
horCol = vec3(0.018, 0.020, 0.028);
topCol = vec3(0.010, 0.012, 0.017);
} else if (lightMode == 3) {
// Backlit dramatic studio
botCol = vec3(0.009, 0.010, 0.014);
horCol = vec3(0.024, 0.027, 0.036);
topCol = vec3(0.013, 0.015, 0.022);
}
vec3 col = mix(horCol, topCol, smoothstep(0.0, 0.75, up));
col = mix(col, botCol, smoothstep(0.0, -0.45, up));
// Overhead diffused studio softbox
float topBox = smoothstep(0.62, 0.86, rd.y) * smoothstep(0.55, 0.25, abs(rd.x)) * smoothstep(0.65, 0.30, abs(rd.z));
vec3 topBoxCol = (lightMode == 1) ? vec3(0.95) : vec3(0.75, 0.80, 0.90);
col += topBox * topBoxCol * ((lightMode == 0) ? 0.55 : (lightMode == 1 ? 0.85 : 0.30));
// Left key softbox
vec3 keyDir = normalize(vec3(-0.68, 0.52, 0.52));
float keySpot = smoothstep(0.76, 0.94, dot(rd, keyDir));
vec3 keyCol = (lightMode == 1) ? vec3(1.15) : vec3(1.10, 1.03, 0.95);
float keyGain = (lightMode == 0) ? 0.85 : (lightMode == 1 ? 0.95 : (lightMode == 2 ? 0.20 : 0.25));
col += keySpot * keyCol * keyGain;
// Right rim / grazing softbox
vec3 rimDir = (lightMode == 2)
? normalize(vec3(0.94, 0.18, 0.28))
: normalize(vec3(0.74, 0.38, -0.55));
float rimSpot = smoothstep(0.78, 0.96, dot(rd, rimDir));
vec3 rimCol = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.08);
float rimGain = (lightMode == 2) ? 1.65 : ((lightMode == 0) ? 0.75 : 0.55);
col += rimSpot * rimCol * rimGain;
// Backlight softbox (strong in mode 3)
vec3 backDir = normalize(vec3(-0.12, 0.34, -0.93));
float backSpot = smoothstep(0.70, 0.94, dot(rd, backDir));
vec3 backCol = (lightMode == 1) ? vec3(0.70) : vec3(0.92, 0.95, 1.06);
col += backSpot * backCol * ((lightMode == 3) ? 2.10 : 0.18);
// Negative-fill studio flags (crisp dark vertical strips on left/right flanks for specular contrast)
float flagR = smoothstep(0.68, 0.88, rd.x) * smoothstep(0.45, 0.10, abs(rd.y - 0.08)) * smoothstep(0.55, 0.15, abs(rd.z - 0.12));
float flagL = smoothstep(0.74, 0.92, -rd.x) * smoothstep(0.42, 0.10, abs(rd.y - 0.05)) * smoothstep(0.55, 0.15, abs(rd.z + 0.18));
float flagMask = (lightMode == 2) ? flagL : max(flagR, flagL * 0.65);
col *= (1.0 - 0.68 * flagMask);
return col;
}
// ============================================================================
// GGX Microfacet BRDF
// ============================================================================
float D_GGX(float NoH, float a) {
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d + 1e-6);
}
float G_SchlickGGX(float NoV, float NoL, float rough) {
float k = (rough + 1.0);
k = (k * k) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k + 1e-6);
float gl = NoL / (NoL * (1.0 - k) + k + 1e-6);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalPBR(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, float metallic) {
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
float a = max(rough * rough, 0.008);
float D = D_GGX(NoH, a);
float G = G_SchlickGGX(NoV, NoL, rough);
vec3 F = F_Schlick(VoH, F0);
vec3 spec = (D * G * F) / (4.0 * NoV * NoL + 1e-5);
vec3 kd = (1.0 - F) * (1.0 - metallic);
return (kd * albedo / PI + spec) * lightCol * NoL;
}
// ============================================================================
// Material Evaluation
// ============================================================================
void getMaterial(
vec3 p, vec3 N, int matId, float arcY, float phase,
out vec3 albedo, out float rough, out float metallic, out float clearcoat,
out vec3 emissive, out vec3 Nmod
) {
Nmod = N;
clearcoat = 0.0;
emissive = vec3(0.0);
if (matId == MAT_BENCH) {
// Dark satin laboratory phenolic resin / slate benchtop
float g1 = fbm2(p.xz * 3.5);
float g2 = noise2D(p.xz * 26.0);
float micro = noise2D(p.xz * 95.0);
albedo = mix(vec3(0.055, 0.060, 0.068), vec3(0.085, 0.092, 0.104), g1 * 0.7 + g2 * 0.3);
rough = clamp(0.32 + 0.14 * g1 + 0.06 * micro, 0.24, 0.55);
metallic = 0.0;
// Subtle bench micro-normal perturbation
vec2 eps = vec2(0.015, 0.0);
float h0 = fbm2(p.xz * 14.0);
float hx = fbm2((p.xz + eps.xy) * 14.0);
float hz = fbm2((p.xz + eps.yx) * 14.0);
Nmod = normalize(N + vec3(h0 - hx, 0.0, h0 - hz) * 0.28);
} else if (matId == MAT_BASEPLATE) {
// Crinkle-finish dark cast-iron laboratory plinth
float crinkle = noise3D(p * 65.0) * 0.6 + noise3D(p * 140.0) * 0.4;
albedo = mix(vec3(0.07, 0.075, 0.085), vec3(0.115, 0.122, 0.135), crinkle);
rough = 0.42 + 0.14 * crinkle;
metallic = 0.62;
} else if (matId == MAT_CERAMIC) {
// Glazed high-voltage chestnut/mahogany brown electrical porcelain
float glazeVar = noise3D(p * 18.0);
float ribBand = smoothstep(0.10, 0.56, p.y);
vec3 deepBrown = vec3(0.17, 0.058, 0.028);
vec3 warmChestnut = vec3(0.34, 0.125, 0.055);
// Subtle glaze pooling: slightly lighter amber-brown on convex shed tops, darker in undercuts
float shedTone = clamp(N.y * 0.5 + 0.5, 0.0, 1.0);
albedo = mix(deepBrown, warmChestnut, 0.35 + 0.45 * shedTone + 0.20 * glazeVar);
albedo *= mix(0.88, 1.05, ribBand);
rough = 0.14 + 0.06 * glazeVar;
metallic = 0.0;
clearcoat = 0.92;
} else if (matId == MAT_BRASS) {
// Turned brushed laboratory brass hardware
float brush = noise3D(vec3(p.x * 12.0, p.y * 160.0, p.z * 12.0));
albedo = mix(vec3(0.76, 0.58, 0.25), vec3(0.88, 0.71, 0.36), brush);
rough = 0.24 + 0.10 * brush;
metallic = 0.94;
} else {
// MAT_COPPER: Brushed copper rod electrodes with heat-tarnished tips & arc contact glow
vec3 q = vec3(abs(p.x), p.y, p.z);
float cx = electrodeX(p.y);
float ang = atan(q.z, q.x - cx);
// Fine circumferential + longitudinal brushed copper grain
float brushFine = noise2D(vec2(ang * 14.0, p.y * 110.0));
float brushLong = noise2D(vec2(ang * 48.0, p.y * 12.0));
float grain = 0.65 * brushFine + 0.35 * brushLong;
vec3 pureCopper = mix(vec3(0.88, 0.48, 0.32), vec3(0.97, 0.60, 0.42), grain);
// Heat-tarnished oxidation gradient toward the upper tips (thin-film thermal temper colors)
float heatZone = smoothstep(1.55, 2.26, p.y);
vec3 strawGold = vec3(0.74, 0.50, 0.26);
vec3 temperViolet = vec3(0.36, 0.22, 0.34);
vec3 gunmetalTip = vec3(0.20, 0.22, 0.28);
vec3 tarnished = mix(pureCopper, strawGold, smoothstep(0.0, 0.35, heatZone));
tarnished = mix(tarnished, temperViolet, smoothstep(0.30, 0.70, heatZone));
tarnished = mix(tarnished, gunmetalTip, smoothstep(0.68, 1.0, heatZone));
// Subtle arc-track darkening on the inner facing side of the rods
float innerFace = smoothstep(0.015, -0.015, q.x - cx);
float arcTrack = innerFace * smoothstep(0.84, 1.05, p.y) * (0.45 + 0.55 * noise2D(vec2(p.y * 28.0, q.z * 35.0)));
albedo = mix(tarnished, tarnished * vec3(0.55, 0.50, 0.58), arcTrack * 0.42);
rough = clamp(0.18 + 0.11 * grain + 0.18 * heatZone + 0.08 * arcTrack, 0.14, 0.54);
metallic = mix(0.96, 0.76, heatZone * 0.65);
// Incandescent cathode/anode contact spot where the plasma arc bridges the rod
if (uLightMode != 1) {
vec3 contactPt = vec3(electrodeX(arcY) - 0.022, arcY, 0.0);
float dContact = length(q - contactPt);
float spotCore = exp(-dContact * dContact / (0.014 * 0.014));
float spotGlow = exp(-dContact * dContact / (0.042 * 0.042));
emissive += vec3(1.0, 0.88, 1.0) * spotCore * 3.2;
emissive += vec3(0.95, 0.42, 0.16) * spotGlow * 0.9;
}
}
// LightMode 1: Pure neutral grayscale material override so geometry & silhouette read cleanly
if (uLightMode == 1) {
float lum = dot(albedo, vec3(0.299, 0.587, 0.114));
if (matId == MAT_BENCH) {
albedo = vec3(0.14);
} else if (matId == MAT_BASEPLATE) {
albedo = vec3(0.20);
} else if (matId == MAT_CERAMIC) {
albedo = vec3(mix(0.28, 0.42, clamp(N.y * 0.5 + 0.5, 0.0, 1.0)));
clearcoat = 0.55;
} else if (matId == MAT_BRASS) {
albedo = vec3(0.58);
metallic = 0.65;
} else {
albedo = vec3(mix(0.68, 0.46, smoothstep(1.65, 2.26, p.y)));
metallic = 0.75;
}
albedo = mix(vec3(lum), albedo, 0.65);
emissive = vec3(0.0);
}
}
// ============================================================================
// Volumetric Ionized Plasma Arc Integration
// ============================================================================
void marchPlasmaArc(
vec3 ro, vec3 rd, float tHit, float arcY, float phase, float tAnim,
out vec3 plasmaLight, out float plasmaAlpha
) {
plasmaLight = vec3(0.0);
plasmaAlpha = 0.0;
// Tight bounding sphere around the active arc for fast early-out
vec3 arcCenter = vec3(0.0, arcY + 0.14 * phase + 0.06, 0.0);
float arcBoundR = electrodeX(arcY) + 0.36;
vec3 oc = ro - arcCenter;
float b = dot(oc, rd);
float c = dot(oc, oc) - arcBoundR * arcBoundR;
float disc = b * b - c;
if (disc < 0.0) return;
float sq = sqrt(disc);
float t0 = max(0.05, -b - sq);
float t1 = min(tHit, -b + sq);
if (t1 <= t0) return;
const int STEPS = 44;
float dt = (t1 - t0) / float(STEPS);
// Subtle blue-noise-like jitter along ray to eliminate banding
float jitter = fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453);
float t = t0 + dt * jitter;
// Subtle high-frequency electrical flicker
float flicker = 0.92 + 0.08 * sin(tAnim * 37.0) * cos(tAnim * 19.0);
// Slight fade-out right at top release (phase > 0.94)
float releaseEnv = 1.0 - smoothstep(0.93, 1.0, phase) * 0.45;
float intensity = flicker * releaseEnv;
for (int i = 0; i < STEPS; i++) {
vec3 p = ro + rd * t;
float u;
float d1 = distToArc(p, arcY, phase, tAnim, u);
// Secondary faint braided filament
vec3 pSec = evalArcSecondary(u, arcY, phase, tAnim);
float d2 = length(p - pSec);
// Core width slightly tapers at the hottest apex
float rCore = 0.0085 + 0.003 * (1.0 - u * u);
float rInner = 0.026;
float rOuter = 0.095;
float core = exp(-d1 * d1 / (rCore * rCore));
float secCore = exp(-d2 * d2 / (0.0065 * 0.0065)) * 0.38;
float sheath = 1.0 / (1.0 + (d1 * d1) / (rInner * rInner));
float corona = exp(-d1 / rOuter) / (1.0 + d1 * 6.5);
// Rising ionized afterglow plume directly above the arc
vec3 cPt = evalArcPoint(u, arcY, phase, tAnim);
float above = max(0.0, p.y - cPt.y);
float plumeDist = length(vec2(p.x - cPt.x, p.z - cPt.z));
float plume = exp(-plumeDist * 14.0) * exp(-above * 6.5) * smoothstep(0.0, 0.03, above) * 0.16;
if (uLightMode == 1) {
// Muted clean neutral grayscale filament for silhouette/form QA
float neut = (core * 1.15 + secCore * 0.45 + sheath * 0.18);
plasmaLight += vec3(0.76) * neut * dt * 11.0;
plasmaAlpha += (core * 0.85 + sheath * 0.12) * dt * 12.0;
} else {
// Brilliant white-violet core + electric lavender-blue corona sheath
vec3 coreCol = vec3(1.00, 0.96, 1.00);
vec3 sheathCol = vec3(0.64, 0.36, 1.00);
vec3 coronaCol = vec3(0.22, 0.42, 1.00);
vec3 plumeCol = vec3(0.36, 0.26, 0.88);
vec3 stepEm =
coreCol * (core * 44.0 + secCore * 22.0) +
sheathCol * (sheath * 6.2) +
coronaCol * (corona * 2.15) +
plumeCol * (plume * 2.2);
plasmaLight += stepEm * intensity * dt;
plasmaAlpha += (core * 0.9 + sheath * 0.15) * dt * 8.0;
}
t += dt;
}
plasmaAlpha = clamp(plasmaAlpha, 0.0, 0.92);
}
// ============================================================================
// Camera Ray Setup
// ============================================================================
void getCamera(vec2 uv, float arcY, out vec3 ro, out vec3 rd) {
vec3 ta;
float fl;
if (uCamMode == 1) {
// ~2.5x macro framing centered on the active plasma arc & copper rod contact zone
float focusY = clamp(arcY + 0.08, 1.02, 2.08);
ta = vec3(0.0, focusY, 0.0);
ro = vec3(0.24, focusY + 0.08, 1.38);
fl = 2.65;
} else {
// Hero full-apparatus studio view
ta = vec3(0.0, 1.12, 0.0);
ro = vec3(0.75, 1.38, 4.95);
fl = 1.85;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
rd = normalize(uv.x * cu + uv.y * cv + fl * cw);
}
// ============================================================================
// ACES Filmic Tone Mapping (Zero-clipping, Zero-pure-black studio curve)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
vec3 mapped = (x * (a * x + b)) / (x * (c * x + d) + e);
// Gentle shoulder so peak plasma never hard-clips to 255 while preserving crisp brilliance
mapped = mapped / (1.0 + 0.12 * mapped);
return clamp(mapped, 0.0, 0.92);
}
// ============================================================================
// Main Shader Entry
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / min(uResolution.x, uResolution.y);
float phase = getArcPhase();
float arcY = getArcY(phase);
float tAnim = uTime + uOpen * 4.5;
vec3 ro, rd;
getCamera(uv, arcY, ro, rd);
// Raymarch scene (bench plane at y=0 + hero apparatus)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
float d = min(p.y, dHero);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
float tHit = hit ? t : tMax;
if (!hit) {
// Clean studio cyclorama background
col = evalStudioEnv(rd, uLightMode);
// Subtle atmospheric plasma scatter in background air around arc
if (uLightMode != 1) {
vec3 arcCenter = vec3(0.0, arcY + 0.12 * phase + 0.05, 0.0);
float tClosest = max(0.0, dot(arcCenter - ro, rd));
float dRay = length(ro + rd * tClosest - arcCenter);
col += vec3(0.14, 0.18, 0.45) * exp(-dRay * 2.4) * 0.24;
}
} else {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Rule (2): Exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
int matId = MAT_BENCH;
vec3 N;
if (isBench) {
matId = MAT_BENCH;
N = vec3(0.0, 1.0, 0.0);
} else {
mapHeroMat(p, matId);
N = calcHeroNormal(p);
}
vec3 V = -rd;
vec3 albedo, emissive, Nmod;
float rough, metallic, clearcoat;
getMaterial(p, N, matId, arcY, phase, albedo, rough, metallic, clearcoat, emissive, Nmod);
float ao = calcAO(p, N);
// Studio Key & Rim Lights per uLightMode
vec3 keyDir, keyCol;
vec3 rimDir, rimCol;
vec3 fillDir, fillCol;
if (uLightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.62, 0.72, 0.56));
keyCol = vec3(2.35);
rimDir = normalize(vec3(0.75, 0.45, -0.52));
rimCol = vec3(1.45);
fillDir = normalize(vec3(0.45, 0.35, 0.82));
fillCol = vec3(0.65);
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(0.92, 0.20, 0.33));
keyCol = vec3(2.85, 2.72, 2.55);
rimDir = normalize(vec3(-0.84, 0.26, -0.48));
rimCol = vec3(0.95, 1.12, 1.38);
fillDir = normalize(vec3(-0.25, 0.55, 0.80));
fillCol = vec3(0.28, 0.32, 0.40);
} else if (uLightMode == 3) {
// Backlit rim studio
keyDir = normalize(vec3(-0.14, 0.42, -0.90));
keyCol = vec3(3.40, 3.45, 3.70);
rimDir = normalize(vec3(0.78, 0.48, -0.40));
rimCol = vec3(1.60, 1.72, 1.95);
fillDir = normalize(vec3(-0.35, 0.55, 0.76));
fillCol = vec3(0.52, 0.54, 0.60);
} else {
// Hero laboratory lighting (mode 0)
keyDir = normalize(vec3(-0.60, 0.74, 0.55));
keyCol = vec3(2.15, 2.02, 1.88);
rimDir = normalize(vec3(0.76, 0.44, -0.50));
rimCol = vec3(1.25, 1.38, 1.62);
fillDir = normalize(vec3(0.38, 0.40, 0.84));
fillCol = vec3(0.36, 0.40, 0.48);
}
float shKey = calcSoftShadow(p, N, keyDir, 5.0, 14.0);
float shRim = calcSoftShadow(p, N, rimDir, 5.0, 10.0);
col = vec3(0.0);
col += evalPBR(Nmod, V, keyDir, keyCol * shKey, albedo, rough, metallic);
col += evalPBR(Nmod, V, rimDir, rimCol * shRim, albedo, rough, metallic);
col += evalPBR(Nmod, V, fillDir, fillCol * ao, albedo, rough, metallic);
// Ceramic glossy clearcoat specular lobe from studio lights
if (clearcoat > 0.01) {
col += evalPBR(N, V, keyDir, keyCol * shKey, vec3(0.04), 0.08, 0.0) * clearcoat;
col += evalPBR(N, V, rimDir, rimCol * shRim, vec3(0.04), 0.08, 0.0) * clearcoat;
}
// Studio IBL reflections & ambient fill
vec3 R = reflect(-V, Nmod);
vec3 envRefl = evalStudioEnv(R, uLightMode);
vec3 envDiff = evalStudioEnv(Nmod, uLightMode);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F_env = F_Schlick(max(dot(Nmod, V), 0.0), F0);
float specOcc = clamp(ao * 0.8 + 0.2, 0.0, 1.0);
vec3 iblDiff = (1.0 - F_env) * (1.0 - metallic) * albedo * envDiff * ao * 0.95;
vec3 iblSpec = F_env * envRefl * mix(1.0, 0.35, rough) * specOcc * 1.15;
col += iblDiff + iblSpec;
if (clearcoat > 0.01) {
vec3 Rcc = reflect(-V, N);
float Fcc = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
col += evalStudioEnv(Rcc, uLightMode) * Fcc * clearcoat * specOcc * 1.1;
}
// Dynamic vivid blue-violet illumination from the plasma arc (3-sample segment light)
if (uLightMode != 1) {
float plasmaGain = (uLightMode == 0) ? 1.0 : ((uLightMode == 2) ? 0.78 : 0.85);
vec3 arcPts[3];
arcPts[0] = evalArcPoint(-0.72, arcY, phase, tAnim);
arcPts[1] = evalArcPoint( 0.00, arcY, phase, tAnim);
arcPts[2] = evalArcPoint( 0.72, arcY, phase, tAnim);
vec3 arcCols[3];
arcCols[0] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
arcCols[1] = vec3(0.46, 0.56, 1.00) * 3.25 * plasmaGain;
arcCols[2] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
for (int li = 0; li < 3; li++) {
vec3 toL = arcPts[li] - p;
float dist = length(toL);
vec3 L = toL / (dist + 1e-5);
float atten = 1.0 / (0.14 + dist * dist * 1.35);
// Only shadow the bench & lower base from the arc (rods themselves self-receive cleanly)
float shArc = 1.0;
if (isBench || matId == MAT_BASEPLATE) {
shArc = calcSoftShadow(p, N, L, max(dist - 0.06, 0.05), 8.0);
}
// Slightly broaden specular on curved copper rods for vivid electric blue-violet streaks
float arcRough = (matId == MAT_COPPER) ? max(rough * 0.82, 0.14) : rough;
col += evalPBR(Nmod, V, L, arcCols[li] * atten * shArc, albedo, arcRough, metallic);
if (clearcoat > 0.01) {
col += evalPBR(N, V, L, arcCols[li] * atten * shArc, vec3(0.05), 0.10, 0.0) * clearcoat * 1.35;
}
}
}
col += emissive;
// Subtle studio bench radial vignette so bench blends seamlessly into studio horizon
if (isBench) {
float rBench = length(p.xz);
float fade = smoothstep(2.2, 7.5, rBench);
col = mix(col, evalStudioEnv(rd, uLightMode), fade);
}
}
// Volumetric ionized plasma arc pass (composite over scene up to tHit)
vec3 plasmaLight;
float plasmaAlpha;
marchPlasmaArc(ro, rd, tHit, arcY, phase, tAnim, plasmaLight, plasmaAlpha);
if (uLightMode == 1) {
// Muted neutral filament blend
col = mix(col, vec3(0.55), plasmaAlpha * 0.65) + plasmaLight * 0.35;
} else {
col = col * (1.0 - plasmaAlpha * 0.35) + plasmaLight;
}
// Ensure studio ambient floor avoids pure 0 black while keeping deep rich contrast
col = max(col, vec3(0.0036));
// ACES Filmic Tone Mapping + sRGB Gamma
col = acesToneMap(col);
col = pow(col, vec3(1.0 / 2.2));
// Subtle optical vignette
float vig = 1.0 - 0.14 * dot(uv, uv);
col *= clamp(vig, 0.80, 1.0);
// Pure achromatic guarantee in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.299, 0.587, 0.114));
col = vec3(g);
}
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Jacob’s Ladder Plasma Arc — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/05_jacobs_ladder_plasma_arc.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 & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_BASEPLATE = 1;
const int MAT_CERAMIC = 2;
const int MAT_BRASS = 3;
const int MAT_COPPER = 4;
// ============================================================================
// Hash & Procedural Noise
// ============================================================================
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 noise2D(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i + vec2(0.0, 0.0));
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float noise3D(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
vec3 u = f * f * (3.0 - 2.0 * f);
float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
float n111 = hash13(i + vec3(1.0, 1.0, 1.0));
float nx00 = mix(n000, n100, u.x);
float nx10 = mix(n010, n110, u.x);
float nx01 = mix(n001, n101, u.x);
float nx11 = mix(n011, n111, u.x);
float nxy0 = mix(nx00, nx10, u.y);
float nxy1 = mix(nx01, nx11, u.y);
return mix(nxy0, nxy1, u.z);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * noise2D(p);
p = m * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// ============================================================================
// SDF Primitives
// ============================================================================
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;
}
float sdCylinder(vec3 p, float r, float h) {
vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCappedCone(vec3 p, float h, float r1, float r2) {
vec2 q = vec2(length(p.xz), p.y);
vec2 k1 = vec2(r2, h);
vec2 k2 = vec2(r2 - r1, 2.0 * h);
vec2 ca = vec2(q.x - min(q.x, (q.y < 0.0) ? r1 : r2), abs(q.y) - h);
vec2 cb = q - k1 + k2 * clamp(dot(k1 - q, k2) / dot(k2, k2), 0.0, 1.0);
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s * sqrt(min(dot(ca, ca), dot(cb, cb)));
}
float sdHexPrism(vec3 p, vec2 h) {
const vec3 k = vec3(-0.8660254, 0.5, 0.57735027);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0.0) * k.xy;
vec2 d = vec2(
length(p.xz - vec2(clamp(p.x, -k.z * h.x, k.z * h.x), h.x)) * sign(p.z - h.x),
p.y - h.y
);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - r;
}
float smin(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
// ============================================================================
// Jacob's Ladder Electrode & Plasma Arc Kinematics
// ============================================================================
// Returns horizontal offset (+x) of the right copper electrode centerline at height y
float electrodeX(float y) {
if (y <= 0.88) {
// S-bend inward from insulator terminal (x=0.19 at y=0.64) to narrow ignition throat (x=0.058 at y=0.88)
float t = clamp((y - 0.64) / 0.24, 0.0, 1.0);
float s = t * t * (3.0 - 2.0 * t);
return mix(0.19, 0.058, s);
} else {
// Classic diverging horn-gap climbing from y=0.88 to y=2.28
float t = clamp((y - 0.88) / 1.40, 0.0, 1.0);
// Smooth concave-to-flared horn divergence
return 0.058 + 0.26 * pow(t, 1.28) + 0.14 * t * t * t;
}
}
float getArcPhase() {
if (uOpen > 0.0001) {
return clamp(uOpen, 0.0, 1.0);
}
return fract(0.46 + uTime * 0.14);
}
float getArcY(float phase) {
// Climbs from ignition throat (0.91) to upper horn release (2.19)
return mix(0.91, 2.19, phase);
}
// Evaluates 3D point on the primary buoyant plasma arc for normalized parameter u in [-1, 1]
vec3 evalArcPoint(float u, float arcY, float phase, float tAnim) {
float halfSpan = electrodeX(arcY) - 0.021;
float env = max(0.0, 1.0 - u * u);
float bowHeight = (0.08 + 0.30 * pow(phase, 1.2)) * env;
// Multi-frequency ionized plasma writhe pinned to electrodes at u = +-1
float w1 = sin(u * 4.5 - tAnim * 6.5) * 0.018;
float w2 = sin(u * 11.3 + tAnim * 13.7 + phase * 9.0) * 0.008;
float w3 = sin(u * 23.0 - tAnim * 21.0) * 0.003;
float dy = bowHeight + env * (w1 + w2 + w3) * (0.55 + 0.85 * phase);
float z1 = sin(u * 3.8 + tAnim * 5.2) * 0.024;
float z2 = cos(u * 9.7 - tAnim * 11.4 + phase * 6.0) * 0.010;
float dz = env * (z1 + z2) * (0.45 + 0.85 * phase);
return vec3(u * halfSpan, arcY + dy, dz);
}
// Evaluates secondary braided filament offset slightly from main arc
vec3 evalArcSecondary(float u, float arcY, float phase, float tAnim) {
vec3 p = evalArcPoint(u, arcY, phase, tAnim);
float env = max(0.0, 1.0 - u * u);
p.y += env * (0.014 + 0.016 * sin(u * 7.5 + tAnim * 14.0));
p.z += env * (0.014 * cos(u * 8.2 - tAnim * 12.5));
return p;
}
// Distance from 3D point p to the primary plasma arc curve
float distToArc(vec3 p, float arcY, float phase, float tAnim, out float uOut) {
float halfSpan = max(electrodeX(arcY) - 0.021, 0.02);
float u = clamp(p.x / halfSpan, -1.0, 1.0);
// Two-step local projection refinement along the bowed curve
for (int i = 0; i < 2; i++) {
vec3 c = evalArcPoint(u, arcY, phase, tAnim);
vec3 c2 = evalArcPoint(clamp(u + 0.02, -1.0, 1.0), arcY, phase, tAnim);
vec3 tang = c2 - c;
float denom = dot(tang, tang) + 1e-6;
float du = dot(p - c, tang) / denom * 0.02;
u = clamp(u + du, -1.0, 1.0);
}
uOut = u;
return length(p - evalArcPoint(u, arcY, phase, tAnim));
}
// ============================================================================
// Hero Apparatus SDF
// ============================================================================
float sdInsulatorColumn(vec3 p) {
// Central cylindrical porcelain core (y = 0.09..0.58)
float core = sdCylinder(p - vec3(0.0, 0.335, 0.0), 0.072, 0.245);
// 5 overhanging glazed ceramic petticoat ribs/sheds
float sheds = 1e5;
for (int i = 0; i < 5; i++) {
float fi = float(i);
float cy = 0.165 + fi * 0.088;
float rOuter = 0.132 - fi * 0.004;
float rTop = 0.076;
// Asymmetric rain-shed profile: sloped top cone + undercut lower lip
float topCone = sdCappedCone(p - vec3(0.0, cy + 0.012, 0.0), 0.024, rOuter, rTop) - 0.008;
float dripLip = sdCappedCone(p - vec3(0.0, cy - 0.012, 0.0), 0.010, rOuter * 0.84, rOuter) - 0.006;
float shed = smin(topCone, dripLip, 0.010);
sheds = min(sheds, shed);
}
// Bottom ceramic mounting skirt
float skirt = sdCappedCone(p - vec3(0.0, 0.115, 0.0), 0.028, 0.118, 0.092) - 0.007;
float d = smin(core, sheds, 0.012);
d = smin(d, skirt, 0.010);
return d;
}
float sdCopperElectrodes(vec3 p) {
// Symmetric in X
vec3 q = vec3(abs(p.x), p.y, p.z);
float d = 1e5;
// 10 exact capsules along electrodeX(y) from y=0.62 to y=2.28
const float yStart = 0.62;
const float yEnd = 2.28;
const float dy = (yEnd - yStart) / 10.0;
const float rodR = 0.0235;
vec3 prevP = vec3(electrodeX(yStart), yStart, 0.0);
for (int i = 1; i <= 10; i++) {
float yn = yStart + float(i) * dy;
vec3 nextP = vec3(electrodeX(yn), yn, 0.0);
d = min(d, sdCapsule(q, prevP, nextP, rodR));
prevP = nextP;
}
// Subtle rounded tip bead at top of each rod
vec3 tipP = vec3(electrodeX(yEnd), yEnd, 0.0);
d = smin(d, length(q - tipP) - 0.0255, 0.008);
return d;
}
float mapHeroMat(vec3 p, out int matId) {
matId = MAT_BASEPLATE;
float d = 1e5;
// 1. Heavy laboratory cast-iron / anodized baseplate (y = 0.0..0.095)
float baseMain = sdRoundBox(p - vec3(0.0, 0.044, 0.0), vec3(0.50, 0.032, 0.25), 0.012);
float baseStep = sdRoundBox(p - vec3(0.0, 0.080, 0.0), vec3(0.45, 0.016, 0.205), 0.009);
float dBase = smin(baseMain, baseStep, 0.008);
if (dBase < d) { d = dBase; matId = MAT_BASEPLATE; }
// 2. Ribbed brown ceramic high-voltage insulator assembly (twin ribbed towers + central ceramic saddle)
vec3 pSym = vec3(abs(p.x), p.y, p.z);
float dTowers = sdInsulatorColumn(pSym - vec3(0.19, 0.0, 0.0));
// Glazed ceramic bridging saddle joining the lower base of the twin insulators
float dSaddle = sdRoundBox(p - vec3(0.0, 0.145, 0.0), vec3(0.16, 0.042, 0.085), 0.018);
float dCeramic = smin(dTowers, dSaddle, 0.018);
if (dCeramic < d) { d = dCeramic; matId = MAT_CERAMIC; }
// 3. Machined brass hardware: terminal collars, hex clamping nuts, set screws, corner bolts, nameplate
float dBrass = 1e5;
// Insulator top brass cap & electrode clamp collar at x = +-0.19, y = 0.58..0.67
vec3 pCol = pSym - vec3(0.19, 0.615, 0.0);
float capCyl = sdCylinder(pCol + vec3(0.0, 0.018, 0.0), 0.064, 0.022) - 0.004;
float hexNut = sdHexPrism(pCol - vec3(0.0, 0.024, 0.0), vec2(0.046, 0.024)) - 0.003;
float clampCol = sdCylinder(pCol - vec3(0.0, 0.052, 0.0), 0.036, 0.016) - 0.003;
// Lateral brass binding post thumbscrew facing outward
vec3 pScrew = pSym - vec3(0.245, 0.635, 0.0);
float screw = sdCylinder(pScrew.yxz, 0.015, 0.024) - 0.002;
dBrass = min(dBrass, min(min(capCyl, hexNut), min(clampCol, screw)));
// 4 brass corner mounting hex bolts on baseplate
vec3 pBolt = vec3(abs(p.x) - 0.425, p.y - 0.094, abs(p.z) - 0.175);
float bolt = sdHexPrism(pBolt, vec2(0.022, 0.014)) - 0.002;
float washer = sdCylinder(pBolt + vec3(0.0, 0.010, 0.0), 0.030, 0.004);
dBrass = min(dBrass, min(bolt, washer));
// Front brass voltage warning plaque on base bevel
float plaque = sdRoundBox(p - vec3(0.0, 0.048, 0.258), vec3(0.12, 0.022, 0.005), 0.002);
dBrass = min(dBrass, plaque);
if (dBrass < d) { d = dBrass; matId = MAT_BRASS; }
// 4. Diverging brushed copper rod electrodes
float dCopper = sdCopperElectrodes(p);
if (dCopper < d) { d = dCopper; matId = MAT_COPPER; }
return d;
}
float mapHero(vec3 p) {
int dummy;
return mapHeroMat(p, dummy);
}
// ============================================================================
// Normals, Soft Shadow, Ambient Occlusion (Zero-Bug Rules)
// ============================================================================
// Rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0008;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Rule (3): Clean hero-only soft shadow (no ph), bias p+N*0.014, tMin=0.025, step max(h,0.015)
float calcSoftShadow(vec3 p, vec3 N, vec3 L, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + L * t);
if (h < 0.0005) 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);
}
float calcAO(vec3 p, vec3 N) {
float occ = 0.0;
float sca = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.022 * float(i);
float d = min(p.y + N.y * h, mapHero(p + N * h));
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Studio Sky Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 rd, int lightMode) {
float up = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.008, 0.009, 0.013);
vec3 horCol = vec3(0.022, 0.025, 0.034);
vec3 topCol = vec3(0.011, 0.013, 0.020);
if (lightMode == 1) {
// Pure neutral grayscale studio
botCol = vec3(0.09);
horCol = vec3(0.18);
topCol = vec3(0.14);
} else if (lightMode == 2) {
// Grazing moody dark studio
botCol = vec3(0.007, 0.008, 0.011);
horCol = vec3(0.018, 0.020, 0.028);
topCol = vec3(0.010, 0.012, 0.017);
} else if (lightMode == 3) {
// Backlit dramatic studio
botCol = vec3(0.009, 0.010, 0.014);
horCol = vec3(0.024, 0.027, 0.036);
topCol = vec3(0.013, 0.015, 0.022);
}
vec3 col = mix(horCol, topCol, smoothstep(0.0, 0.75, up));
col = mix(col, botCol, smoothstep(0.0, -0.45, up));
// Overhead diffused studio softbox
float topBox = smoothstep(0.62, 0.86, rd.y) * smoothstep(0.55, 0.25, abs(rd.x)) * smoothstep(0.65, 0.30, abs(rd.z));
vec3 topBoxCol = (lightMode == 1) ? vec3(0.95) : vec3(0.75, 0.80, 0.90);
col += topBox * topBoxCol * ((lightMode == 0) ? 0.55 : (lightMode == 1 ? 0.85 : 0.30));
// Left key softbox
vec3 keyDir = normalize(vec3(-0.68, 0.52, 0.52));
float keySpot = smoothstep(0.76, 0.94, dot(rd, keyDir));
vec3 keyCol = (lightMode == 1) ? vec3(1.15) : vec3(1.10, 1.03, 0.95);
float keyGain = (lightMode == 0) ? 0.85 : (lightMode == 1 ? 0.95 : (lightMode == 2 ? 0.20 : 0.25));
col += keySpot * keyCol * keyGain;
// Right rim / grazing softbox
vec3 rimDir = (lightMode == 2)
? normalize(vec3(0.94, 0.18, 0.28))
: normalize(vec3(0.74, 0.38, -0.55));
float rimSpot = smoothstep(0.78, 0.96, dot(rd, rimDir));
vec3 rimCol = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.08);
float rimGain = (lightMode == 2) ? 1.65 : ((lightMode == 0) ? 0.75 : 0.55);
col += rimSpot * rimCol * rimGain;
// Backlight softbox (strong in mode 3)
vec3 backDir = normalize(vec3(-0.12, 0.34, -0.93));
float backSpot = smoothstep(0.70, 0.94, dot(rd, backDir));
vec3 backCol = (lightMode == 1) ? vec3(0.70) : vec3(0.92, 0.95, 1.06);
col += backSpot * backCol * ((lightMode == 3) ? 2.10 : 0.18);
// Negative-fill studio flags (crisp dark vertical strips on left/right flanks for specular contrast)
float flagR = smoothstep(0.68, 0.88, rd.x) * smoothstep(0.45, 0.10, abs(rd.y - 0.08)) * smoothstep(0.55, 0.15, abs(rd.z - 0.12));
float flagL = smoothstep(0.74, 0.92, -rd.x) * smoothstep(0.42, 0.10, abs(rd.y - 0.05)) * smoothstep(0.55, 0.15, abs(rd.z + 0.18));
float flagMask = (lightMode == 2) ? flagL : max(flagR, flagL * 0.65);
col *= (1.0 - 0.68 * flagMask);
return col;
}
// ============================================================================
// GGX Microfacet BRDF
// ============================================================================
float D_GGX(float NoH, float a) {
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d + 1e-6);
}
float G_SchlickGGX(float NoV, float NoL, float rough) {
float k = (rough + 1.0);
k = (k * k) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k + 1e-6);
float gl = NoL / (NoL * (1.0 - k) + k + 1e-6);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalPBR(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, float metallic) {
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
float a = max(rough * rough, 0.008);
float D = D_GGX(NoH, a);
float G = G_SchlickGGX(NoV, NoL, rough);
vec3 F = F_Schlick(VoH, F0);
vec3 spec = (D * G * F) / (4.0 * NoV * NoL + 1e-5);
vec3 kd = (1.0 - F) * (1.0 - metallic);
return (kd * albedo / PI + spec) * lightCol * NoL;
}
// ============================================================================
// Material Evaluation
// ============================================================================
void getMaterial(
vec3 p, vec3 N, int matId, float arcY, float phase,
out vec3 albedo, out float rough, out float metallic, out float clearcoat,
out vec3 emissive, out vec3 Nmod
) {
Nmod = N;
clearcoat = 0.0;
emissive = vec3(0.0);
if (matId == MAT_BENCH) {
// Dark satin laboratory phenolic resin / slate benchtop
float g1 = fbm2(p.xz * 3.5);
float g2 = noise2D(p.xz * 26.0);
float micro = noise2D(p.xz * 95.0);
albedo = mix(vec3(0.055, 0.060, 0.068), vec3(0.085, 0.092, 0.104), g1 * 0.7 + g2 * 0.3);
rough = clamp(0.32 + 0.14 * g1 + 0.06 * micro, 0.24, 0.55);
metallic = 0.0;
// Subtle bench micro-normal perturbation
vec2 eps = vec2(0.015, 0.0);
float h0 = fbm2(p.xz * 14.0);
float hx = fbm2((p.xz + eps.xy) * 14.0);
float hz = fbm2((p.xz + eps.yx) * 14.0);
Nmod = normalize(N + vec3(h0 - hx, 0.0, h0 - hz) * 0.28);
} else if (matId == MAT_BASEPLATE) {
// Crinkle-finish dark cast-iron laboratory plinth
float crinkle = noise3D(p * 65.0) * 0.6 + noise3D(p * 140.0) * 0.4;
albedo = mix(vec3(0.07, 0.075, 0.085), vec3(0.115, 0.122, 0.135), crinkle);
rough = 0.42 + 0.14 * crinkle;
metallic = 0.62;
} else if (matId == MAT_CERAMIC) {
// Glazed high-voltage chestnut/mahogany brown electrical porcelain
float glazeVar = noise3D(p * 18.0);
float ribBand = smoothstep(0.10, 0.56, p.y);
vec3 deepBrown = vec3(0.17, 0.058, 0.028);
vec3 warmChestnut = vec3(0.34, 0.125, 0.055);
// Subtle glaze pooling: slightly lighter amber-brown on convex shed tops, darker in undercuts
float shedTone = clamp(N.y * 0.5 + 0.5, 0.0, 1.0);
albedo = mix(deepBrown, warmChestnut, 0.35 + 0.45 * shedTone + 0.20 * glazeVar);
albedo *= mix(0.88, 1.05, ribBand);
rough = 0.14 + 0.06 * glazeVar;
metallic = 0.0;
clearcoat = 0.92;
} else if (matId == MAT_BRASS) {
// Turned brushed laboratory brass hardware
float brush = noise3D(vec3(p.x * 12.0, p.y * 160.0, p.z * 12.0));
albedo = mix(vec3(0.76, 0.58, 0.25), vec3(0.88, 0.71, 0.36), brush);
rough = 0.24 + 0.10 * brush;
metallic = 0.94;
} else {
// MAT_COPPER: Brushed copper rod electrodes with heat-tarnished tips & arc contact glow
vec3 q = vec3(abs(p.x), p.y, p.z);
float cx = electrodeX(p.y);
float ang = atan(q.z, q.x - cx);
// Fine circumferential + longitudinal brushed copper grain
float brushFine = noise2D(vec2(ang * 14.0, p.y * 110.0));
float brushLong = noise2D(vec2(ang * 48.0, p.y * 12.0));
float grain = 0.65 * brushFine + 0.35 * brushLong;
vec3 pureCopper = mix(vec3(0.88, 0.48, 0.32), vec3(0.97, 0.60, 0.42), grain);
// Heat-tarnished oxidation gradient toward the upper tips (thin-film thermal temper colors)
float heatZone = smoothstep(1.55, 2.26, p.y);
vec3 strawGold = vec3(0.74, 0.50, 0.26);
vec3 temperViolet = vec3(0.36, 0.22, 0.34);
vec3 gunmetalTip = vec3(0.20, 0.22, 0.28);
vec3 tarnished = mix(pureCopper, strawGold, smoothstep(0.0, 0.35, heatZone));
tarnished = mix(tarnished, temperViolet, smoothstep(0.30, 0.70, heatZone));
tarnished = mix(tarnished, gunmetalTip, smoothstep(0.68, 1.0, heatZone));
// Subtle arc-track darkening on the inner facing side of the rods
float innerFace = smoothstep(0.015, -0.015, q.x - cx);
float arcTrack = innerFace * smoothstep(0.84, 1.05, p.y) * (0.45 + 0.55 * noise2D(vec2(p.y * 28.0, q.z * 35.0)));
albedo = mix(tarnished, tarnished * vec3(0.55, 0.50, 0.58), arcTrack * 0.42);
rough = clamp(0.18 + 0.11 * grain + 0.18 * heatZone + 0.08 * arcTrack, 0.14, 0.54);
metallic = mix(0.96, 0.76, heatZone * 0.65);
// Incandescent cathode/anode contact spot where the plasma arc bridges the rod
if (uLightMode != 1) {
vec3 contactPt = vec3(electrodeX(arcY) - 0.022, arcY, 0.0);
float dContact = length(q - contactPt);
float spotCore = exp(-dContact * dContact / (0.014 * 0.014));
float spotGlow = exp(-dContact * dContact / (0.042 * 0.042));
emissive += vec3(1.0, 0.88, 1.0) * spotCore * 3.2;
emissive += vec3(0.95, 0.42, 0.16) * spotGlow * 0.9;
}
}
// LightMode 1: Pure neutral grayscale material override so geometry & silhouette read cleanly
if (uLightMode == 1) {
float lum = dot(albedo, vec3(0.299, 0.587, 0.114));
if (matId == MAT_BENCH) {
albedo = vec3(0.14);
} else if (matId == MAT_BASEPLATE) {
albedo = vec3(0.20);
} else if (matId == MAT_CERAMIC) {
albedo = vec3(mix(0.28, 0.42, clamp(N.y * 0.5 + 0.5, 0.0, 1.0)));
clearcoat = 0.55;
} else if (matId == MAT_BRASS) {
albedo = vec3(0.58);
metallic = 0.65;
} else {
albedo = vec3(mix(0.68, 0.46, smoothstep(1.65, 2.26, p.y)));
metallic = 0.75;
}
albedo = mix(vec3(lum), albedo, 0.65);
emissive = vec3(0.0);
}
}
// ============================================================================
// Volumetric Ionized Plasma Arc Integration
// ============================================================================
void marchPlasmaArc(
vec3 ro, vec3 rd, float tHit, float arcY, float phase, float tAnim,
out vec3 plasmaLight, out float plasmaAlpha
) {
plasmaLight = vec3(0.0);
plasmaAlpha = 0.0;
// Tight bounding sphere around the active arc for fast early-out
vec3 arcCenter = vec3(0.0, arcY + 0.14 * phase + 0.06, 0.0);
float arcBoundR = electrodeX(arcY) + 0.36;
vec3 oc = ro - arcCenter;
float b = dot(oc, rd);
float c = dot(oc, oc) - arcBoundR * arcBoundR;
float disc = b * b - c;
if (disc < 0.0) return;
float sq = sqrt(disc);
float t0 = max(0.05, -b - sq);
float t1 = min(tHit, -b + sq);
if (t1 <= t0) return;
const int STEPS = 44;
float dt = (t1 - t0) / float(STEPS);
// Subtle blue-noise-like jitter along ray to eliminate banding
float jitter = fract(sin(dot(pfx_frag_xy, vec2(12.9898, 78.233))) * 43758.5453);
float t = t0 + dt * jitter;
// Subtle high-frequency electrical flicker
float flicker = 0.92 + 0.08 * sin(tAnim * 37.0) * cos(tAnim * 19.0);
// Slight fade-out right at top release (phase > 0.94)
float releaseEnv = 1.0 - smoothstep(0.93, 1.0, phase) * 0.45;
float intensity = flicker * releaseEnv;
for (int i = 0; i < STEPS; i++) {
vec3 p = ro + rd * t;
float u;
float d1 = distToArc(p, arcY, phase, tAnim, u);
// Secondary faint braided filament
vec3 pSec = evalArcSecondary(u, arcY, phase, tAnim);
float d2 = length(p - pSec);
// Core width slightly tapers at the hottest apex
float rCore = 0.0085 + 0.003 * (1.0 - u * u);
float rInner = 0.026;
float rOuter = 0.095;
float core = exp(-d1 * d1 / (rCore * rCore));
float secCore = exp(-d2 * d2 / (0.0065 * 0.0065)) * 0.38;
float sheath = 1.0 / (1.0 + (d1 * d1) / (rInner * rInner));
float corona = exp(-d1 / rOuter) / (1.0 + d1 * 6.5);
// Rising ionized afterglow plume directly above the arc
vec3 cPt = evalArcPoint(u, arcY, phase, tAnim);
float above = max(0.0, p.y - cPt.y);
float plumeDist = length(vec2(p.x - cPt.x, p.z - cPt.z));
float plume = exp(-plumeDist * 14.0) * exp(-above * 6.5) * smoothstep(0.0, 0.03, above) * 0.16;
if (uLightMode == 1) {
// Muted clean neutral grayscale filament for silhouette/form QA
float neut = (core * 1.15 + secCore * 0.45 + sheath * 0.18);
plasmaLight += vec3(0.76) * neut * dt * 11.0;
plasmaAlpha += (core * 0.85 + sheath * 0.12) * dt * 12.0;
} else {
// Brilliant white-violet core + electric lavender-blue corona sheath
vec3 coreCol = vec3(1.00, 0.96, 1.00);
vec3 sheathCol = vec3(0.64, 0.36, 1.00);
vec3 coronaCol = vec3(0.22, 0.42, 1.00);
vec3 plumeCol = vec3(0.36, 0.26, 0.88);
vec3 stepEm =
coreCol * (core * 44.0 + secCore * 22.0) +
sheathCol * (sheath * 6.2) +
coronaCol * (corona * 2.15) +
plumeCol * (plume * 2.2);
plasmaLight += stepEm * intensity * dt;
plasmaAlpha += (core * 0.9 + sheath * 0.15) * dt * 8.0;
}
t += dt;
}
plasmaAlpha = clamp(plasmaAlpha, 0.0, 0.92);
}
// ============================================================================
// Camera Ray Setup
// ============================================================================
void getCamera(vec2 uv, float arcY, out vec3 ro, out vec3 rd) {
vec3 ta;
float fl;
if (uCamMode == 1) {
// ~2.5x macro framing centered on the active plasma arc & copper rod contact zone
float focusY = clamp(arcY + 0.08, 1.02, 2.08);
ta = vec3(0.0, focusY, 0.0);
ro = vec3(0.24, focusY + 0.08, 1.38);
fl = 2.65;
} else {
// Hero full-apparatus studio view
ta = vec3(0.0, 1.12, 0.0);
ro = vec3(0.75, 1.38, 4.95);
fl = 1.85;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
rd = normalize(uv.x * cu + uv.y * cv + fl * cw);
}
// ============================================================================
// ACES Filmic Tone Mapping (Zero-clipping, Zero-pure-black studio curve)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
vec3 mapped = (x * (a * x + b)) / (x * (c * x + d) + e);
// Gentle shoulder so peak plasma never hard-clips to 255 while preserving crisp brilliance
mapped = mapped / (1.0 + 0.12 * mapped);
return clamp(mapped, 0.0, 0.92);
}
// ============================================================================
// Main Shader Entry
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution) / min(uResolution.x, uResolution.y);
float phase = getArcPhase();
float arcY = getArcY(phase);
float tAnim = uTime + uOpen * 4.5;
vec3 ro, rd;
getCamera(uv, arcY, ro, rd);
// Raymarch scene (bench plane at y=0 + hero apparatus)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
float d = min(p.y, dHero);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
float tHit = hit ? t : tMax;
if (!hit) {
// Clean studio cyclorama background
col = evalStudioEnv(rd, uLightMode);
// Subtle atmospheric plasma scatter in background air around arc
if (uLightMode != 1) {
vec3 arcCenter = vec3(0.0, arcY + 0.12 * phase + 0.05, 0.0);
float tClosest = max(0.0, dot(arcCenter - ro, rd));
float dRay = length(ro + rd * tClosest - arcCenter);
col += vec3(0.14, 0.18, 0.45) * exp(-dRay * 2.4) * 0.24;
}
} else {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Rule (2): Exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
int matId = MAT_BENCH;
vec3 N;
if (isBench) {
matId = MAT_BENCH;
N = vec3(0.0, 1.0, 0.0);
} else {
mapHeroMat(p, matId);
N = calcHeroNormal(p);
}
vec3 V = -rd;
vec3 albedo, emissive, Nmod;
float rough, metallic, clearcoat;
getMaterial(p, N, matId, arcY, phase, albedo, rough, metallic, clearcoat, emissive, Nmod);
float ao = calcAO(p, N);
// Studio Key & Rim Lights per uLightMode
vec3 keyDir, keyCol;
vec3 rimDir, rimCol;
vec3 fillDir, fillCol;
if (uLightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.62, 0.72, 0.56));
keyCol = vec3(2.35);
rimDir = normalize(vec3(0.75, 0.45, -0.52));
rimCol = vec3(1.45);
fillDir = normalize(vec3(0.45, 0.35, 0.82));
fillCol = vec3(0.65);
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(0.92, 0.20, 0.33));
keyCol = vec3(2.85, 2.72, 2.55);
rimDir = normalize(vec3(-0.84, 0.26, -0.48));
rimCol = vec3(0.95, 1.12, 1.38);
fillDir = normalize(vec3(-0.25, 0.55, 0.80));
fillCol = vec3(0.28, 0.32, 0.40);
} else if (uLightMode == 3) {
// Backlit rim studio
keyDir = normalize(vec3(-0.14, 0.42, -0.90));
keyCol = vec3(3.40, 3.45, 3.70);
rimDir = normalize(vec3(0.78, 0.48, -0.40));
rimCol = vec3(1.60, 1.72, 1.95);
fillDir = normalize(vec3(-0.35, 0.55, 0.76));
fillCol = vec3(0.52, 0.54, 0.60);
} else {
// Hero laboratory lighting (mode 0)
keyDir = normalize(vec3(-0.60, 0.74, 0.55));
keyCol = vec3(2.15, 2.02, 1.88);
rimDir = normalize(vec3(0.76, 0.44, -0.50));
rimCol = vec3(1.25, 1.38, 1.62);
fillDir = normalize(vec3(0.38, 0.40, 0.84));
fillCol = vec3(0.36, 0.40, 0.48);
}
float shKey = calcSoftShadow(p, N, keyDir, 5.0, 14.0);
float shRim = calcSoftShadow(p, N, rimDir, 5.0, 10.0);
col = vec3(0.0);
col += evalPBR(Nmod, V, keyDir, keyCol * shKey, albedo, rough, metallic);
col += evalPBR(Nmod, V, rimDir, rimCol * shRim, albedo, rough, metallic);
col += evalPBR(Nmod, V, fillDir, fillCol * ao, albedo, rough, metallic);
// Ceramic glossy clearcoat specular lobe from studio lights
if (clearcoat > 0.01) {
col += evalPBR(N, V, keyDir, keyCol * shKey, vec3(0.04), 0.08, 0.0) * clearcoat;
col += evalPBR(N, V, rimDir, rimCol * shRim, vec3(0.04), 0.08, 0.0) * clearcoat;
}
// Studio IBL reflections & ambient fill
vec3 R = reflect(-V, Nmod);
vec3 envRefl = evalStudioEnv(R, uLightMode);
vec3 envDiff = evalStudioEnv(Nmod, uLightMode);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F_env = F_Schlick(max(dot(Nmod, V), 0.0), F0);
float specOcc = clamp(ao * 0.8 + 0.2, 0.0, 1.0);
vec3 iblDiff = (1.0 - F_env) * (1.0 - metallic) * albedo * envDiff * ao * 0.95;
vec3 iblSpec = F_env * envRefl * mix(1.0, 0.35, rough) * specOcc * 1.15;
col += iblDiff + iblSpec;
if (clearcoat > 0.01) {
vec3 Rcc = reflect(-V, N);
float Fcc = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
col += evalStudioEnv(Rcc, uLightMode) * Fcc * clearcoat * specOcc * 1.1;
}
// Dynamic vivid blue-violet illumination from the plasma arc (3-sample segment light)
if (uLightMode != 1) {
float plasmaGain = (uLightMode == 0) ? 1.0 : ((uLightMode == 2) ? 0.78 : 0.85);
vec3 arcPts[3];
arcPts[0] = evalArcPoint(-0.72, arcY, phase, tAnim);
arcPts[1] = evalArcPoint( 0.00, arcY, phase, tAnim);
arcPts[2] = evalArcPoint( 0.72, arcY, phase, tAnim);
vec3 arcCols[3];
arcCols[0] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
arcCols[1] = vec3(0.46, 0.56, 1.00) * 3.25 * plasmaGain;
arcCols[2] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
for (int li = 0; li < 3; li++) {
vec3 toL = arcPts[li] - p;
float dist = length(toL);
vec3 L = toL / (dist + 1e-5);
float atten = 1.0 / (0.14 + dist * dist * 1.35);
// Only shadow the bench & lower base from the arc (rods themselves self-receive cleanly)
float shArc = 1.0;
if (isBench || matId == MAT_BASEPLATE) {
shArc = calcSoftShadow(p, N, L, max(dist - 0.06, 0.05), 8.0);
}
// Slightly broaden specular on curved copper rods for vivid electric blue-violet streaks
float arcRough = (matId == MAT_COPPER) ? max(rough * 0.82, 0.14) : rough;
col += evalPBR(Nmod, V, L, arcCols[li] * atten * shArc, albedo, arcRough, metallic);
if (clearcoat > 0.01) {
col += evalPBR(N, V, L, arcCols[li] * atten * shArc, vec3(0.05), 0.10, 0.0) * clearcoat * 1.35;
}
}
}
col += emissive;
// Subtle studio bench radial vignette so bench blends seamlessly into studio horizon
if (isBench) {
float rBench = length(p.xz);
float fade = smoothstep(2.2, 7.5, rBench);
col = mix(col, evalStudioEnv(rd, uLightMode), fade);
}
}
// Volumetric ionized plasma arc pass (composite over scene up to tHit)
vec3 plasmaLight;
float plasmaAlpha;
marchPlasmaArc(ro, rd, tHit, arcY, phase, tAnim, plasmaLight, plasmaAlpha);
if (uLightMode == 1) {
// Muted neutral filament blend
col = mix(col, vec3(0.55), plasmaAlpha * 0.65) + plasmaLight * 0.35;
} else {
col = col * (1.0 - plasmaAlpha * 0.35) + plasmaLight;
}
// Ensure studio ambient floor avoids pure 0 black while keeping deep rich contrast
col = max(col, vec3(0.0036));
// ACES Filmic Tone Mapping + sRGB Gamma
col = acesToneMap(col);
col = pow(col, vec3(1.0 / 2.2));
// Subtle optical vignette
float vig = 1.0 - 0.14 * dot(uv, uv);
col *= clamp(vig, 0.80, 1.0);
// Pure achromatic guarantee in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.299, 0.587, 0.114));
col = vec3(g);
}
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;
// Jacob’s Ladder Plasma Arc — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/05_jacobs_ladder_plasma_arc.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 & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_BASEPLATE = 1;
const int MAT_CERAMIC = 2;
const int MAT_BRASS = 3;
const int MAT_COPPER = 4;
// ============================================================================
// Hash & Procedural Noise
// ============================================================================
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 noise2D(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i + vec2(0.0, 0.0));
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float noise3D(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
vec3 u = f * f * (3.0 - 2.0 * f);
float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
float n111 = hash13(i + vec3(1.0, 1.0, 1.0));
float nx00 = mix(n000, n100, u.x);
float nx10 = mix(n010, n110, u.x);
float nx01 = mix(n001, n101, u.x);
float nx11 = mix(n011, n111, u.x);
float nxy0 = mix(nx00, nx10, u.y);
float nxy1 = mix(nx01, nx11, u.y);
return mix(nxy0, nxy1, u.z);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * noise2D(p);
p = m * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// ============================================================================
// SDF Primitives
// ============================================================================
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;
}
float sdCylinder(vec3 p, float r, float h) {
vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCappedCone(vec3 p, float h, float r1, float r2) {
vec2 q = vec2(length(p.xz), p.y);
vec2 k1 = vec2(r2, h);
vec2 k2 = vec2(r2 - r1, 2.0 * h);
vec2 ca = vec2(q.x - min(q.x, (q.y < 0.0) ? r1 : r2), abs(q.y) - h);
vec2 cb = q - k1 + k2 * clamp(dot(k1 - q, k2) / dot(k2, k2), 0.0, 1.0);
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s * sqrt(min(dot(ca, ca), dot(cb, cb)));
}
float sdHexPrism(vec3 p, vec2 h) {
const vec3 k = vec3(-0.8660254, 0.5, 0.57735027);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0.0) * k.xy;
vec2 d = vec2(
length(p.xz - vec2(clamp(p.x, -k.z * h.x, k.z * h.x), h.x)) * sign(p.z - h.x),
p.y - h.y
);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - r;
}
float smin(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
// ============================================================================
// Jacob's Ladder Electrode & Plasma Arc Kinematics
// ============================================================================
// Returns horizontal offset (+x) of the right copper electrode centerline at height y
float electrodeX(float y) {
if (y <= 0.88) {
// S-bend inward from insulator terminal (x=0.19 at y=0.64) to narrow ignition throat (x=0.058 at y=0.88)
float t = clamp((y - 0.64) / 0.24, 0.0, 1.0);
float s = t * t * (3.0 - 2.0 * t);
return mix(0.19, 0.058, s);
} else {
// Classic diverging horn-gap climbing from y=0.88 to y=2.28
float t = clamp((y - 0.88) / 1.40, 0.0, 1.0);
// Smooth concave-to-flared horn divergence
return 0.058 + 0.26 * pow(t, 1.28) + 0.14 * t * t * t;
}
}
float getArcPhase() {
if (uOpen > 0.0001) {
return clamp(uOpen, 0.0, 1.0);
}
return fract(0.46 + uTime * 0.14);
}
float getArcY(float phase) {
// Climbs from ignition throat (0.91) to upper horn release (2.19)
return mix(0.91, 2.19, phase);
}
// Evaluates 3D point on the primary buoyant plasma arc for normalized parameter u in [-1, 1]
vec3 evalArcPoint(float u, float arcY, float phase, float tAnim) {
float halfSpan = electrodeX(arcY) - 0.021;
float env = max(0.0, 1.0 - u * u);
float bowHeight = (0.08 + 0.30 * pow(phase, 1.2)) * env;
// Multi-frequency ionized plasma writhe pinned to electrodes at u = +-1
float w1 = sin(u * 4.5 - tAnim * 6.5) * 0.018;
float w2 = sin(u * 11.3 + tAnim * 13.7 + phase * 9.0) * 0.008;
float w3 = sin(u * 23.0 - tAnim * 21.0) * 0.003;
float dy = bowHeight + env * (w1 + w2 + w3) * (0.55 + 0.85 * phase);
float z1 = sin(u * 3.8 + tAnim * 5.2) * 0.024;
float z2 = cos(u * 9.7 - tAnim * 11.4 + phase * 6.0) * 0.010;
float dz = env * (z1 + z2) * (0.45 + 0.85 * phase);
return vec3(u * halfSpan, arcY + dy, dz);
}
// Evaluates secondary braided filament offset slightly from main arc
vec3 evalArcSecondary(float u, float arcY, float phase, float tAnim) {
vec3 p = evalArcPoint(u, arcY, phase, tAnim);
float env = max(0.0, 1.0 - u * u);
p.y += env * (0.014 + 0.016 * sin(u * 7.5 + tAnim * 14.0));
p.z += env * (0.014 * cos(u * 8.2 - tAnim * 12.5));
return p;
}
// Distance from 3D point p to the primary plasma arc curve
float distToArc(vec3 p, float arcY, float phase, float tAnim, out float uOut) {
float halfSpan = max(electrodeX(arcY) - 0.021, 0.02);
float u = clamp(p.x / halfSpan, -1.0, 1.0);
// Two-step local projection refinement along the bowed curve
for (int i = 0; i < 2; i++) {
vec3 c = evalArcPoint(u, arcY, phase, tAnim);
vec3 c2 = evalArcPoint(clamp(u + 0.02, -1.0, 1.0), arcY, phase, tAnim);
vec3 tang = c2 - c;
float denom = dot(tang, tang) + 1e-6;
float du = dot(p - c, tang) / denom * 0.02;
u = clamp(u + du, -1.0, 1.0);
}
uOut = u;
return length(p - evalArcPoint(u, arcY, phase, tAnim));
}
// ============================================================================
// Hero Apparatus SDF
// ============================================================================
float sdInsulatorColumn(vec3 p) {
// Central cylindrical porcelain core (y = 0.09..0.58)
float core = sdCylinder(p - vec3(0.0, 0.335, 0.0), 0.072, 0.245);
// 5 overhanging glazed ceramic petticoat ribs/sheds
float sheds = 1e5;
for (int i = 0; i < 5; i++) {
float fi = float(i);
float cy = 0.165 + fi * 0.088;
float rOuter = 0.132 - fi * 0.004;
float rTop = 0.076;
// Asymmetric rain-shed profile: sloped top cone + undercut lower lip
float topCone = sdCappedCone(p - vec3(0.0, cy + 0.012, 0.0), 0.024, rOuter, rTop) - 0.008;
float dripLip = sdCappedCone(p - vec3(0.0, cy - 0.012, 0.0), 0.010, rOuter * 0.84, rOuter) - 0.006;
float shed = smin(topCone, dripLip, 0.010);
sheds = min(sheds, shed);
}
// Bottom ceramic mounting skirt
float skirt = sdCappedCone(p - vec3(0.0, 0.115, 0.0), 0.028, 0.118, 0.092) - 0.007;
float d = smin(core, sheds, 0.012);
d = smin(d, skirt, 0.010);
return d;
}
float sdCopperElectrodes(vec3 p) {
// Symmetric in X
vec3 q = vec3(abs(p.x), p.y, p.z);
float d = 1e5;
// 10 exact capsules along electrodeX(y) from y=0.62 to y=2.28
const float yStart = 0.62;
const float yEnd = 2.28;
const float dy = (yEnd - yStart) / 10.0;
const float rodR = 0.0235;
vec3 prevP = vec3(electrodeX(yStart), yStart, 0.0);
for (int i = 1; i <= 10; i++) {
float yn = yStart + float(i) * dy;
vec3 nextP = vec3(electrodeX(yn), yn, 0.0);
d = min(d, sdCapsule(q, prevP, nextP, rodR));
prevP = nextP;
}
// Subtle rounded tip bead at top of each rod
vec3 tipP = vec3(electrodeX(yEnd), yEnd, 0.0);
d = smin(d, length(q - tipP) - 0.0255, 0.008);
return d;
}
float mapHeroMat(vec3 p, out int matId) {
matId = MAT_BASEPLATE;
float d = 1e5;
// 1. Heavy laboratory cast-iron / anodized baseplate (y = 0.0..0.095)
float baseMain = sdRoundBox(p - vec3(0.0, 0.044, 0.0), vec3(0.50, 0.032, 0.25), 0.012);
float baseStep = sdRoundBox(p - vec3(0.0, 0.080, 0.0), vec3(0.45, 0.016, 0.205), 0.009);
float dBase = smin(baseMain, baseStep, 0.008);
if (dBase < d) { d = dBase; matId = MAT_BASEPLATE; }
// 2. Ribbed brown ceramic high-voltage insulator assembly (twin ribbed towers + central ceramic saddle)
vec3 pSym = vec3(abs(p.x), p.y, p.z);
float dTowers = sdInsulatorColumn(pSym - vec3(0.19, 0.0, 0.0));
// Glazed ceramic bridging saddle joining the lower base of the twin insulators
float dSaddle = sdRoundBox(p - vec3(0.0, 0.145, 0.0), vec3(0.16, 0.042, 0.085), 0.018);
float dCeramic = smin(dTowers, dSaddle, 0.018);
if (dCeramic < d) { d = dCeramic; matId = MAT_CERAMIC; }
// 3. Machined brass hardware: terminal collars, hex clamping nuts, set screws, corner bolts, nameplate
float dBrass = 1e5;
// Insulator top brass cap & electrode clamp collar at x = +-0.19, y = 0.58..0.67
vec3 pCol = pSym - vec3(0.19, 0.615, 0.0);
float capCyl = sdCylinder(pCol + vec3(0.0, 0.018, 0.0), 0.064, 0.022) - 0.004;
float hexNut = sdHexPrism(pCol - vec3(0.0, 0.024, 0.0), vec2(0.046, 0.024)) - 0.003;
float clampCol = sdCylinder(pCol - vec3(0.0, 0.052, 0.0), 0.036, 0.016) - 0.003;
// Lateral brass binding post thumbscrew facing outward
vec3 pScrew = pSym - vec3(0.245, 0.635, 0.0);
float screw = sdCylinder(pScrew.yxz, 0.015, 0.024) - 0.002;
dBrass = min(dBrass, min(min(capCyl, hexNut), min(clampCol, screw)));
// 4 brass corner mounting hex bolts on baseplate
vec3 pBolt = vec3(abs(p.x) - 0.425, p.y - 0.094, abs(p.z) - 0.175);
float bolt = sdHexPrism(pBolt, vec2(0.022, 0.014)) - 0.002;
float washer = sdCylinder(pBolt + vec3(0.0, 0.010, 0.0), 0.030, 0.004);
dBrass = min(dBrass, min(bolt, washer));
// Front brass voltage warning plaque on base bevel
float plaque = sdRoundBox(p - vec3(0.0, 0.048, 0.258), vec3(0.12, 0.022, 0.005), 0.002);
dBrass = min(dBrass, plaque);
if (dBrass < d) { d = dBrass; matId = MAT_BRASS; }
// 4. Diverging brushed copper rod electrodes
float dCopper = sdCopperElectrodes(p);
if (dCopper < d) { d = dCopper; matId = MAT_COPPER; }
return d;
}
float mapHero(vec3 p) {
int dummy;
return mapHeroMat(p, dummy);
}
// ============================================================================
// Normals, Soft Shadow, Ambient Occlusion (Zero-Bug Rules)
// ============================================================================
// Rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0008;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Rule (3): Clean hero-only soft shadow (no ph), bias p+N*0.014, tMin=0.025, step max(h,0.015)
float calcSoftShadow(vec3 p, vec3 N, vec3 L, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + L * t);
if (h < 0.0005) 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);
}
float calcAO(vec3 p, vec3 N) {
float occ = 0.0;
float sca = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.022 * float(i);
float d = min(p.y + N.y * h, mapHero(p + N * h));
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Studio Sky Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 rd, int lightMode) {
float up = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.008, 0.009, 0.013);
vec3 horCol = vec3(0.022, 0.025, 0.034);
vec3 topCol = vec3(0.011, 0.013, 0.020);
if (lightMode == 1) {
// Pure neutral grayscale studio
botCol = vec3(0.09);
horCol = vec3(0.18);
topCol = vec3(0.14);
} else if (lightMode == 2) {
// Grazing moody dark studio
botCol = vec3(0.007, 0.008, 0.011);
horCol = vec3(0.018, 0.020, 0.028);
topCol = vec3(0.010, 0.012, 0.017);
} else if (lightMode == 3) {
// Backlit dramatic studio
botCol = vec3(0.009, 0.010, 0.014);
horCol = vec3(0.024, 0.027, 0.036);
topCol = vec3(0.013, 0.015, 0.022);
}
vec3 col = mix(horCol, topCol, smoothstep(0.0, 0.75, up));
col = mix(col, botCol, smoothstep(0.0, -0.45, up));
// Overhead diffused studio softbox
float topBox = smoothstep(0.62, 0.86, rd.y) * smoothstep(0.55, 0.25, abs(rd.x)) * smoothstep(0.65, 0.30, abs(rd.z));
vec3 topBoxCol = (lightMode == 1) ? vec3(0.95) : vec3(0.75, 0.80, 0.90);
col += topBox * topBoxCol * ((lightMode == 0) ? 0.55 : (lightMode == 1 ? 0.85 : 0.30));
// Left key softbox
vec3 keyDir = normalize(vec3(-0.68, 0.52, 0.52));
float keySpot = smoothstep(0.76, 0.94, dot(rd, keyDir));
vec3 keyCol = (lightMode == 1) ? vec3(1.15) : vec3(1.10, 1.03, 0.95);
float keyGain = (lightMode == 0) ? 0.85 : (lightMode == 1 ? 0.95 : (lightMode == 2 ? 0.20 : 0.25));
col += keySpot * keyCol * keyGain;
// Right rim / grazing softbox
vec3 rimDir = (lightMode == 2)
? normalize(vec3(0.94, 0.18, 0.28))
: normalize(vec3(0.74, 0.38, -0.55));
float rimSpot = smoothstep(0.78, 0.96, dot(rd, rimDir));
vec3 rimCol = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.08);
float rimGain = (lightMode == 2) ? 1.65 : ((lightMode == 0) ? 0.75 : 0.55);
col += rimSpot * rimCol * rimGain;
// Backlight softbox (strong in mode 3)
vec3 backDir = normalize(vec3(-0.12, 0.34, -0.93));
float backSpot = smoothstep(0.70, 0.94, dot(rd, backDir));
vec3 backCol = (lightMode == 1) ? vec3(0.70) : vec3(0.92, 0.95, 1.06);
col += backSpot * backCol * ((lightMode == 3) ? 2.10 : 0.18);
// Negative-fill studio flags (crisp dark vertical strips on left/right flanks for specular contrast)
float flagR = smoothstep(0.68, 0.88, rd.x) * smoothstep(0.45, 0.10, abs(rd.y - 0.08)) * smoothstep(0.55, 0.15, abs(rd.z - 0.12));
float flagL = smoothstep(0.74, 0.92, -rd.x) * smoothstep(0.42, 0.10, abs(rd.y - 0.05)) * smoothstep(0.55, 0.15, abs(rd.z + 0.18));
float flagMask = (lightMode == 2) ? flagL : max(flagR, flagL * 0.65);
col *= (1.0 - 0.68 * flagMask);
return col;
}
// ============================================================================
// GGX Microfacet BRDF
// ============================================================================
float D_GGX(float NoH, float a) {
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d + 1e-6);
}
float G_SchlickGGX(float NoV, float NoL, float rough) {
float k = (rough + 1.0);
k = (k * k) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k + 1e-6);
float gl = NoL / (NoL * (1.0 - k) + k + 1e-6);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalPBR(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, float metallic) {
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
float a = max(rough * rough, 0.008);
float D = D_GGX(NoH, a);
float G = G_SchlickGGX(NoV, NoL, rough);
vec3 F = F_Schlick(VoH, F0);
vec3 spec = (D * G * F) / (4.0 * NoV * NoL + 1e-5);
vec3 kd = (1.0 - F) * (1.0 - metallic);
return (kd * albedo / PI + spec) * lightCol * NoL;
}
// ============================================================================
// Material Evaluation
// ============================================================================
void getMaterial(
vec3 p, vec3 N, int matId, float arcY, float phase,
out vec3 albedo, out float rough, out float metallic, out float clearcoat,
out vec3 emissive, out vec3 Nmod
) {
Nmod = N;
clearcoat = 0.0;
emissive = vec3(0.0);
if (matId == MAT_BENCH) {
// Dark satin laboratory phenolic resin / slate benchtop
float g1 = fbm2(p.xz * 3.5);
float g2 = noise2D(p.xz * 26.0);
float micro = noise2D(p.xz * 95.0);
albedo = mix(vec3(0.055, 0.060, 0.068), vec3(0.085, 0.092, 0.104), g1 * 0.7 + g2 * 0.3);
rough = clamp(0.32 + 0.14 * g1 + 0.06 * micro, 0.24, 0.55);
metallic = 0.0;
// Subtle bench micro-normal perturbation
vec2 eps = vec2(0.015, 0.0);
float h0 = fbm2(p.xz * 14.0);
float hx = fbm2((p.xz + eps.xy) * 14.0);
float hz = fbm2((p.xz + eps.yx) * 14.0);
Nmod = normalize(N + vec3(h0 - hx, 0.0, h0 - hz) * 0.28);
} else if (matId == MAT_BASEPLATE) {
// Crinkle-finish dark cast-iron laboratory plinth
float crinkle = noise3D(p * 65.0) * 0.6 + noise3D(p * 140.0) * 0.4;
albedo = mix(vec3(0.07, 0.075, 0.085), vec3(0.115, 0.122, 0.135), crinkle);
rough = 0.42 + 0.14 * crinkle;
metallic = 0.62;
} else if (matId == MAT_CERAMIC) {
// Glazed high-voltage chestnut/mahogany brown electrical porcelain
float glazeVar = noise3D(p * 18.0);
float ribBand = smoothstep(0.10, 0.56, p.y);
vec3 deepBrown = vec3(0.17, 0.058, 0.028);
vec3 warmChestnut = vec3(0.34, 0.125, 0.055);
// Subtle glaze pooling: slightly lighter amber-brown on convex shed tops, darker in undercuts
float shedTone = clamp(N.y * 0.5 + 0.5, 0.0, 1.0);
albedo = mix(deepBrown, warmChestnut, 0.35 + 0.45 * shedTone + 0.20 * glazeVar);
albedo *= mix(0.88, 1.05, ribBand);
rough = 0.14 + 0.06 * glazeVar;
metallic = 0.0;
clearcoat = 0.92;
} else if (matId == MAT_BRASS) {
// Turned brushed laboratory brass hardware
float brush = noise3D(vec3(p.x * 12.0, p.y * 160.0, p.z * 12.0));
albedo = mix(vec3(0.76, 0.58, 0.25), vec3(0.88, 0.71, 0.36), brush);
rough = 0.24 + 0.10 * brush;
metallic = 0.94;
} else {
// MAT_COPPER: Brushed copper rod electrodes with heat-tarnished tips & arc contact glow
vec3 q = vec3(abs(p.x), p.y, p.z);
float cx = electrodeX(p.y);
float ang = atan(q.z, q.x - cx);
// Fine circumferential + longitudinal brushed copper grain
float brushFine = noise2D(vec2(ang * 14.0, p.y * 110.0));
float brushLong = noise2D(vec2(ang * 48.0, p.y * 12.0));
float grain = 0.65 * brushFine + 0.35 * brushLong;
vec3 pureCopper = mix(vec3(0.88, 0.48, 0.32), vec3(0.97, 0.60, 0.42), grain);
// Heat-tarnished oxidation gradient toward the upper tips (thin-film thermal temper colors)
float heatZone = smoothstep(1.55, 2.26, p.y);
vec3 strawGold = vec3(0.74, 0.50, 0.26);
vec3 temperViolet = vec3(0.36, 0.22, 0.34);
vec3 gunmetalTip = vec3(0.20, 0.22, 0.28);
vec3 tarnished = mix(pureCopper, strawGold, smoothstep(0.0, 0.35, heatZone));
tarnished = mix(tarnished, temperViolet, smoothstep(0.30, 0.70, heatZone));
tarnished = mix(tarnished, gunmetalTip, smoothstep(0.68, 1.0, heatZone));
// Subtle arc-track darkening on the inner facing side of the rods
float innerFace = smoothstep(0.015, -0.015, q.x - cx);
float arcTrack = innerFace * smoothstep(0.84, 1.05, p.y) * (0.45 + 0.55 * noise2D(vec2(p.y * 28.0, q.z * 35.0)));
albedo = mix(tarnished, tarnished * vec3(0.55, 0.50, 0.58), arcTrack * 0.42);
rough = clamp(0.18 + 0.11 * grain + 0.18 * heatZone + 0.08 * arcTrack, 0.14, 0.54);
metallic = mix(0.96, 0.76, heatZone * 0.65);
// Incandescent cathode/anode contact spot where the plasma arc bridges the rod
if (uLightMode != 1) {
vec3 contactPt = vec3(electrodeX(arcY) - 0.022, arcY, 0.0);
float dContact = length(q - contactPt);
float spotCore = exp(-dContact * dContact / (0.014 * 0.014));
float spotGlow = exp(-dContact * dContact / (0.042 * 0.042));
emissive += vec3(1.0, 0.88, 1.0) * spotCore * 3.2;
emissive += vec3(0.95, 0.42, 0.16) * spotGlow * 0.9;
}
}
// LightMode 1: Pure neutral grayscale material override so geometry & silhouette read cleanly
if (uLightMode == 1) {
float lum = dot(albedo, vec3(0.299, 0.587, 0.114));
if (matId == MAT_BENCH) {
albedo = vec3(0.14);
} else if (matId == MAT_BASEPLATE) {
albedo = vec3(0.20);
} else if (matId == MAT_CERAMIC) {
albedo = vec3(mix(0.28, 0.42, clamp(N.y * 0.5 + 0.5, 0.0, 1.0)));
clearcoat = 0.55;
} else if (matId == MAT_BRASS) {
albedo = vec3(0.58);
metallic = 0.65;
} else {
albedo = vec3(mix(0.68, 0.46, smoothstep(1.65, 2.26, p.y)));
metallic = 0.75;
}
albedo = mix(vec3(lum), albedo, 0.65);
emissive = vec3(0.0);
}
}
// ============================================================================
// Volumetric Ionized Plasma Arc Integration
// ============================================================================
void marchPlasmaArc(
vec3 ro, vec3 rd, float tHit, float arcY, float phase, float tAnim,
out vec3 plasmaLight, out float plasmaAlpha
) {
plasmaLight = vec3(0.0);
plasmaAlpha = 0.0;
// Tight bounding sphere around the active arc for fast early-out
vec3 arcCenter = vec3(0.0, arcY + 0.14 * phase + 0.06, 0.0);
float arcBoundR = electrodeX(arcY) + 0.36;
vec3 oc = ro - arcCenter;
float b = dot(oc, rd);
float c = dot(oc, oc) - arcBoundR * arcBoundR;
float disc = b * b - c;
if (disc < 0.0) return;
float sq = sqrt(disc);
float t0 = max(0.05, -b - sq);
float t1 = min(tHit, -b + sq);
if (t1 <= t0) return;
const int STEPS = 44;
float dt = (t1 - t0) / float(STEPS);
// Subtle blue-noise-like jitter along ray to eliminate banding
float jitter = fract(sin(dot(pfx_frag_xy, vec2(12.9898, 78.233))) * 43758.5453);
float t = t0 + dt * jitter;
// Subtle high-frequency electrical flicker
float flicker = 0.92 + 0.08 * sin(tAnim * 37.0) * cos(tAnim * 19.0);
// Slight fade-out right at top release (phase > 0.94)
float releaseEnv = 1.0 - smoothstep(0.93, 1.0, phase) * 0.45;
float intensity = flicker * releaseEnv;
for (int i = 0; i < STEPS; i++) {
vec3 p = ro + rd * t;
float u;
float d1 = distToArc(p, arcY, phase, tAnim, u);
// Secondary faint braided filament
vec3 pSec = evalArcSecondary(u, arcY, phase, tAnim);
float d2 = length(p - pSec);
// Core width slightly tapers at the hottest apex
float rCore = 0.0085 + 0.003 * (1.0 - u * u);
float rInner = 0.026;
float rOuter = 0.095;
float core = exp(-d1 * d1 / (rCore * rCore));
float secCore = exp(-d2 * d2 / (0.0065 * 0.0065)) * 0.38;
float sheath = 1.0 / (1.0 + (d1 * d1) / (rInner * rInner));
float corona = exp(-d1 / rOuter) / (1.0 + d1 * 6.5);
// Rising ionized afterglow plume directly above the arc
vec3 cPt = evalArcPoint(u, arcY, phase, tAnim);
float above = max(0.0, p.y - cPt.y);
float plumeDist = length(vec2(p.x - cPt.x, p.z - cPt.z));
float plume = exp(-plumeDist * 14.0) * exp(-above * 6.5) * smoothstep(0.0, 0.03, above) * 0.16;
if (uLightMode == 1) {
// Muted clean neutral grayscale filament for silhouette/form QA
float neut = (core * 1.15 + secCore * 0.45 + sheath * 0.18);
plasmaLight += vec3(0.76) * neut * dt * 11.0;
plasmaAlpha += (core * 0.85 + sheath * 0.12) * dt * 12.0;
} else {
// Brilliant white-violet core + electric lavender-blue corona sheath
vec3 coreCol = vec3(1.00, 0.96, 1.00);
vec3 sheathCol = vec3(0.64, 0.36, 1.00);
vec3 coronaCol = vec3(0.22, 0.42, 1.00);
vec3 plumeCol = vec3(0.36, 0.26, 0.88);
vec3 stepEm =
coreCol * (core * 44.0 + secCore * 22.0) +
sheathCol * (sheath * 6.2) +
coronaCol * (corona * 2.15) +
plumeCol * (plume * 2.2);
plasmaLight += stepEm * intensity * dt;
plasmaAlpha += (core * 0.9 + sheath * 0.15) * dt * 8.0;
}
t += dt;
}
plasmaAlpha = clamp(plasmaAlpha, 0.0, 0.92);
}
// ============================================================================
// Camera Ray Setup
// ============================================================================
void getCamera(vec2 uv, float arcY, out vec3 ro, out vec3 rd) {
vec3 ta;
float fl;
if (uCamMode == 1) {
// ~2.5x macro framing centered on the active plasma arc & copper rod contact zone
float focusY = clamp(arcY + 0.08, 1.02, 2.08);
ta = vec3(0.0, focusY, 0.0);
ro = vec3(0.24, focusY + 0.08, 1.38);
fl = 2.65;
} else {
// Hero full-apparatus studio view
ta = vec3(0.0, 1.12, 0.0);
ro = vec3(0.75, 1.38, 4.95);
fl = 1.85;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
rd = normalize(uv.x * cu + uv.y * cv + fl * cw);
}
// ============================================================================
// ACES Filmic Tone Mapping (Zero-clipping, Zero-pure-black studio curve)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
vec3 mapped = (x * (a * x + b)) / (x * (c * x + d) + e);
// Gentle shoulder so peak plasma never hard-clips to 255 while preserving crisp brilliance
mapped = mapped / (1.0 + 0.12 * mapped);
return clamp(mapped, 0.0, 0.92);
}
// ============================================================================
// Main Shader Entry
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution) / min(uResolution.x, uResolution.y);
float phase = getArcPhase();
float arcY = getArcY(phase);
float tAnim = uTime + uOpen * 4.5;
vec3 ro, rd;
getCamera(uv, arcY, ro, rd);
// Raymarch scene (bench plane at y=0 + hero apparatus)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
float d = min(p.y, dHero);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
float tHit = hit ? t : tMax;
if (!hit) {
// Clean studio cyclorama background
col = evalStudioEnv(rd, uLightMode);
// Subtle atmospheric plasma scatter in background air around arc
if (uLightMode != 1) {
vec3 arcCenter = vec3(0.0, arcY + 0.12 * phase + 0.05, 0.0);
float tClosest = max(0.0, dot(arcCenter - ro, rd));
float dRay = length(ro + rd * tClosest - arcCenter);
col += vec3(0.14, 0.18, 0.45) * exp(-dRay * 2.4) * 0.24;
}
} else {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Rule (2): Exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
int matId = MAT_BENCH;
vec3 N;
if (isBench) {
matId = MAT_BENCH;
N = vec3(0.0, 1.0, 0.0);
} else {
mapHeroMat(p, matId);
N = calcHeroNormal(p);
}
vec3 V = -rd;
vec3 albedo, emissive, Nmod;
float rough, metallic, clearcoat;
getMaterial(p, N, matId, arcY, phase, albedo, rough, metallic, clearcoat, emissive, Nmod);
float ao = calcAO(p, N);
// Studio Key & Rim Lights per uLightMode
vec3 keyDir, keyCol;
vec3 rimDir, rimCol;
vec3 fillDir, fillCol;
if (uLightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.62, 0.72, 0.56));
keyCol = vec3(2.35);
rimDir = normalize(vec3(0.75, 0.45, -0.52));
rimCol = vec3(1.45);
fillDir = normalize(vec3(0.45, 0.35, 0.82));
fillCol = vec3(0.65);
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(0.92, 0.20, 0.33));
keyCol = vec3(2.85, 2.72, 2.55);
rimDir = normalize(vec3(-0.84, 0.26, -0.48));
rimCol = vec3(0.95, 1.12, 1.38);
fillDir = normalize(vec3(-0.25, 0.55, 0.80));
fillCol = vec3(0.28, 0.32, 0.40);
} else if (uLightMode == 3) {
// Backlit rim studio
keyDir = normalize(vec3(-0.14, 0.42, -0.90));
keyCol = vec3(3.40, 3.45, 3.70);
rimDir = normalize(vec3(0.78, 0.48, -0.40));
rimCol = vec3(1.60, 1.72, 1.95);
fillDir = normalize(vec3(-0.35, 0.55, 0.76));
fillCol = vec3(0.52, 0.54, 0.60);
} else {
// Hero laboratory lighting (mode 0)
keyDir = normalize(vec3(-0.60, 0.74, 0.55));
keyCol = vec3(2.15, 2.02, 1.88);
rimDir = normalize(vec3(0.76, 0.44, -0.50));
rimCol = vec3(1.25, 1.38, 1.62);
fillDir = normalize(vec3(0.38, 0.40, 0.84));
fillCol = vec3(0.36, 0.40, 0.48);
}
float shKey = calcSoftShadow(p, N, keyDir, 5.0, 14.0);
float shRim = calcSoftShadow(p, N, rimDir, 5.0, 10.0);
col = vec3(0.0);
col += evalPBR(Nmod, V, keyDir, keyCol * shKey, albedo, rough, metallic);
col += evalPBR(Nmod, V, rimDir, rimCol * shRim, albedo, rough, metallic);
col += evalPBR(Nmod, V, fillDir, fillCol * ao, albedo, rough, metallic);
// Ceramic glossy clearcoat specular lobe from studio lights
if (clearcoat > 0.01) {
col += evalPBR(N, V, keyDir, keyCol * shKey, vec3(0.04), 0.08, 0.0) * clearcoat;
col += evalPBR(N, V, rimDir, rimCol * shRim, vec3(0.04), 0.08, 0.0) * clearcoat;
}
// Studio IBL reflections & ambient fill
vec3 R = reflect(-V, Nmod);
vec3 envRefl = evalStudioEnv(R, uLightMode);
vec3 envDiff = evalStudioEnv(Nmod, uLightMode);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F_env = F_Schlick(max(dot(Nmod, V), 0.0), F0);
float specOcc = clamp(ao * 0.8 + 0.2, 0.0, 1.0);
vec3 iblDiff = (1.0 - F_env) * (1.0 - metallic) * albedo * envDiff * ao * 0.95;
vec3 iblSpec = F_env * envRefl * mix(1.0, 0.35, rough) * specOcc * 1.15;
col += iblDiff + iblSpec;
if (clearcoat > 0.01) {
vec3 Rcc = reflect(-V, N);
float Fcc = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
col += evalStudioEnv(Rcc, uLightMode) * Fcc * clearcoat * specOcc * 1.1;
}
// Dynamic vivid blue-violet illumination from the plasma arc (3-sample segment light)
if (uLightMode != 1) {
float plasmaGain = (uLightMode == 0) ? 1.0 : ((uLightMode == 2) ? 0.78 : 0.85);
vec3 arcPts[3];
arcPts[0] = evalArcPoint(-0.72, arcY, phase, tAnim);
arcPts[1] = evalArcPoint( 0.00, arcY, phase, tAnim);
arcPts[2] = evalArcPoint( 0.72, arcY, phase, tAnim);
vec3 arcCols[3];
arcCols[0] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
arcCols[1] = vec3(0.46, 0.56, 1.00) * 3.25 * plasmaGain;
arcCols[2] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
for (int li = 0; li < 3; li++) {
vec3 toL = arcPts[li] - p;
float dist = length(toL);
vec3 L = toL / (dist + 1e-5);
float atten = 1.0 / (0.14 + dist * dist * 1.35);
// Only shadow the bench & lower base from the arc (rods themselves self-receive cleanly)
float shArc = 1.0;
if (isBench || matId == MAT_BASEPLATE) {
shArc = calcSoftShadow(p, N, L, max(dist - 0.06, 0.05), 8.0);
}
// Slightly broaden specular on curved copper rods for vivid electric blue-violet streaks
float arcRough = (matId == MAT_COPPER) ? max(rough * 0.82, 0.14) : rough;
col += evalPBR(Nmod, V, L, arcCols[li] * atten * shArc, albedo, arcRough, metallic);
if (clearcoat > 0.01) {
col += evalPBR(N, V, L, arcCols[li] * atten * shArc, vec3(0.05), 0.10, 0.0) * clearcoat * 1.35;
}
}
}
col += emissive;
// Subtle studio bench radial vignette so bench blends seamlessly into studio horizon
if (isBench) {
float rBench = length(p.xz);
float fade = smoothstep(2.2, 7.5, rBench);
col = mix(col, evalStudioEnv(rd, uLightMode), fade);
}
}
// Volumetric ionized plasma arc pass (composite over scene up to tHit)
vec3 plasmaLight;
float plasmaAlpha;
marchPlasmaArc(ro, rd, tHit, arcY, phase, tAnim, plasmaLight, plasmaAlpha);
if (uLightMode == 1) {
// Muted neutral filament blend
col = mix(col, vec3(0.55), plasmaAlpha * 0.65) + plasmaLight * 0.35;
} else {
col = col * (1.0 - plasmaAlpha * 0.35) + plasmaLight;
}
// Ensure studio ambient floor avoids pure 0 black while keeping deep rich contrast
col = max(col, vec3(0.0036));
// ACES Filmic Tone Mapping + sRGB Gamma
col = acesToneMap(col);
col = pow(col, vec3(1.0 / 2.2));
// Subtle optical vignette
float vig = 1.0 - 0.14 * dot(uv, uv);
col *= clamp(vig, 0.80, 1.0);
// Pure achromatic guarantee in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.299, 0.587, 0.114));
col = vec3(g);
}
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;
}
// Jacob’s Ladder Plasma Arc — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/thermal/lib/05_jacobs_ladder_plasma_arc.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 & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_BASEPLATE = 1;
const int MAT_CERAMIC = 2;
const int MAT_BRASS = 3;
const int MAT_COPPER = 4;
// ============================================================================
// Hash & Procedural Noise
// ============================================================================
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 noise2D(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i + vec2(0.0, 0.0));
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float noise3D(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
vec3 u = f * f * (3.0 - 2.0 * f);
float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
float n111 = hash13(i + vec3(1.0, 1.0, 1.0));
float nx00 = mix(n000, n100, u.x);
float nx10 = mix(n010, n110, u.x);
float nx01 = mix(n001, n101, u.x);
float nx11 = mix(n011, n111, u.x);
float nxy0 = mix(nx00, nx10, u.y);
float nxy1 = mix(nx01, nx11, u.y);
return mix(nxy0, nxy1, u.z);
}
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
for (int i = 0; i < 4; i++) {
v += a * noise2D(p);
p = m * p * 2.03 + vec2(17.1, 31.7);
a *= 0.5;
}
return v;
}
// ============================================================================
// SDF Primitives
// ============================================================================
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;
}
float sdCylinder(vec3 p, float r, float h) {
vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCappedCone(vec3 p, float h, float r1, float r2) {
vec2 q = vec2(length(p.xz), p.y);
vec2 k1 = vec2(r2, h);
vec2 k2 = vec2(r2 - r1, 2.0 * h);
vec2 ca = vec2(q.x - min(q.x, (q.y < 0.0) ? r1 : r2), abs(q.y) - h);
vec2 cb = q - k1 + k2 * clamp(dot(k1 - q, k2) / dot(k2, k2), 0.0, 1.0);
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s * sqrt(min(dot(ca, ca), dot(cb, cb)));
}
float sdHexPrism(vec3 p, vec2 h) {
const vec3 k = vec3(-0.8660254, 0.5, 0.57735027);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0.0) * k.xy;
vec2 d = vec2(
length(p.xz - vec2(clamp(p.x, -k.z * h.x, k.z * h.x), h.x)) * sign(p.z - h.x),
p.y - h.y
);
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h) - r;
}
float smin(float a, float b, float k) {
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return mix(b, a, h) - k * h * (1.0 - h);
}
// ============================================================================
// Jacob's Ladder Electrode & Plasma Arc Kinematics
// ============================================================================
// Returns horizontal offset (+x) of the right copper electrode centerline at height y
float electrodeX(float y) {
if (y <= 0.88) {
// S-bend inward from insulator terminal (x=0.19 at y=0.64) to narrow ignition throat (x=0.058 at y=0.88)
float t = clamp((y - 0.64) / 0.24, 0.0, 1.0);
float s = t * t * (3.0 - 2.0 * t);
return mix(0.19, 0.058, s);
} else {
// Classic diverging horn-gap climbing from y=0.88 to y=2.28
float t = clamp((y - 0.88) / 1.40, 0.0, 1.0);
// Smooth concave-to-flared horn divergence
return 0.058 + 0.26 * pow(t, 1.28) + 0.14 * t * t * t;
}
}
float getArcPhase() {
if (uOpen > 0.0001) {
return clamp(uOpen, 0.0, 1.0);
}
return fract(0.46 + uTime * 0.14);
}
float getArcY(float phase) {
// Climbs from ignition throat (0.91) to upper horn release (2.19)
return mix(0.91, 2.19, phase);
}
// Evaluates 3D point on the primary buoyant plasma arc for normalized parameter u in [-1, 1]
vec3 evalArcPoint(float u, float arcY, float phase, float tAnim) {
float halfSpan = electrodeX(arcY) - 0.021;
float env = max(0.0, 1.0 - u * u);
float bowHeight = (0.08 + 0.30 * pow(phase, 1.2)) * env;
// Multi-frequency ionized plasma writhe pinned to electrodes at u = +-1
float w1 = sin(u * 4.5 - tAnim * 6.5) * 0.018;
float w2 = sin(u * 11.3 + tAnim * 13.7 + phase * 9.0) * 0.008;
float w3 = sin(u * 23.0 - tAnim * 21.0) * 0.003;
float dy = bowHeight + env * (w1 + w2 + w3) * (0.55 + 0.85 * phase);
float z1 = sin(u * 3.8 + tAnim * 5.2) * 0.024;
float z2 = cos(u * 9.7 - tAnim * 11.4 + phase * 6.0) * 0.010;
float dz = env * (z1 + z2) * (0.45 + 0.85 * phase);
return vec3(u * halfSpan, arcY + dy, dz);
}
// Evaluates secondary braided filament offset slightly from main arc
vec3 evalArcSecondary(float u, float arcY, float phase, float tAnim) {
vec3 p = evalArcPoint(u, arcY, phase, tAnim);
float env = max(0.0, 1.0 - u * u);
p.y += env * (0.014 + 0.016 * sin(u * 7.5 + tAnim * 14.0));
p.z += env * (0.014 * cos(u * 8.2 - tAnim * 12.5));
return p;
}
// Distance from 3D point p to the primary plasma arc curve
float distToArc(vec3 p, float arcY, float phase, float tAnim, out float uOut) {
float halfSpan = max(electrodeX(arcY) - 0.021, 0.02);
float u = clamp(p.x / halfSpan, -1.0, 1.0);
// Two-step local projection refinement along the bowed curve
for (int i = 0; i < 2; i++) {
vec3 c = evalArcPoint(u, arcY, phase, tAnim);
vec3 c2 = evalArcPoint(clamp(u + 0.02, -1.0, 1.0), arcY, phase, tAnim);
vec3 tang = c2 - c;
float denom = dot(tang, tang) + 1e-6;
float du = dot(p - c, tang) / denom * 0.02;
u = clamp(u + du, -1.0, 1.0);
}
uOut = u;
return length(p - evalArcPoint(u, arcY, phase, tAnim));
}
// ============================================================================
// Hero Apparatus SDF
// ============================================================================
float sdInsulatorColumn(vec3 p) {
// Central cylindrical porcelain core (y = 0.09..0.58)
float core = sdCylinder(p - vec3(0.0, 0.335, 0.0), 0.072, 0.245);
// 5 overhanging glazed ceramic petticoat ribs/sheds
float sheds = 1e5;
for (int i = 0; i < 5; i++) {
float fi = float(i);
float cy = 0.165 + fi * 0.088;
float rOuter = 0.132 - fi * 0.004;
float rTop = 0.076;
// Asymmetric rain-shed profile: sloped top cone + undercut lower lip
float topCone = sdCappedCone(p - vec3(0.0, cy + 0.012, 0.0), 0.024, rOuter, rTop) - 0.008;
float dripLip = sdCappedCone(p - vec3(0.0, cy - 0.012, 0.0), 0.010, rOuter * 0.84, rOuter) - 0.006;
float shed = smin(topCone, dripLip, 0.010);
sheds = min(sheds, shed);
}
// Bottom ceramic mounting skirt
float skirt = sdCappedCone(p - vec3(0.0, 0.115, 0.0), 0.028, 0.118, 0.092) - 0.007;
float d = smin(core, sheds, 0.012);
d = smin(d, skirt, 0.010);
return d;
}
float sdCopperElectrodes(vec3 p) {
// Symmetric in X
vec3 q = vec3(abs(p.x), p.y, p.z);
float d = 1e5;
// 10 exact capsules along electrodeX(y) from y=0.62 to y=2.28
const float yStart = 0.62;
const float yEnd = 2.28;
const float dy = (yEnd - yStart) / 10.0;
const float rodR = 0.0235;
vec3 prevP = vec3(electrodeX(yStart), yStart, 0.0);
for (int i = 1; i <= 10; i++) {
float yn = yStart + float(i) * dy;
vec3 nextP = vec3(electrodeX(yn), yn, 0.0);
d = min(d, sdCapsule(q, prevP, nextP, rodR));
prevP = nextP;
}
// Subtle rounded tip bead at top of each rod
vec3 tipP = vec3(electrodeX(yEnd), yEnd, 0.0);
d = smin(d, length(q - tipP) - 0.0255, 0.008);
return d;
}
float mapHeroMat(vec3 p, out int matId) {
matId = MAT_BASEPLATE;
float d = 1e5;
// 1. Heavy laboratory cast-iron / anodized baseplate (y = 0.0..0.095)
float baseMain = sdRoundBox(p - vec3(0.0, 0.044, 0.0), vec3(0.50, 0.032, 0.25), 0.012);
float baseStep = sdRoundBox(p - vec3(0.0, 0.080, 0.0), vec3(0.45, 0.016, 0.205), 0.009);
float dBase = smin(baseMain, baseStep, 0.008);
if (dBase < d) { d = dBase; matId = MAT_BASEPLATE; }
// 2. Ribbed brown ceramic high-voltage insulator assembly (twin ribbed towers + central ceramic saddle)
vec3 pSym = vec3(abs(p.x), p.y, p.z);
float dTowers = sdInsulatorColumn(pSym - vec3(0.19, 0.0, 0.0));
// Glazed ceramic bridging saddle joining the lower base of the twin insulators
float dSaddle = sdRoundBox(p - vec3(0.0, 0.145, 0.0), vec3(0.16, 0.042, 0.085), 0.018);
float dCeramic = smin(dTowers, dSaddle, 0.018);
if (dCeramic < d) { d = dCeramic; matId = MAT_CERAMIC; }
// 3. Machined brass hardware: terminal collars, hex clamping nuts, set screws, corner bolts, nameplate
float dBrass = 1e5;
// Insulator top brass cap & electrode clamp collar at x = +-0.19, y = 0.58..0.67
vec3 pCol = pSym - vec3(0.19, 0.615, 0.0);
float capCyl = sdCylinder(pCol + vec3(0.0, 0.018, 0.0), 0.064, 0.022) - 0.004;
float hexNut = sdHexPrism(pCol - vec3(0.0, 0.024, 0.0), vec2(0.046, 0.024)) - 0.003;
float clampCol = sdCylinder(pCol - vec3(0.0, 0.052, 0.0), 0.036, 0.016) - 0.003;
// Lateral brass binding post thumbscrew facing outward
vec3 pScrew = pSym - vec3(0.245, 0.635, 0.0);
float screw = sdCylinder(pScrew.yxz, 0.015, 0.024) - 0.002;
dBrass = min(dBrass, min(min(capCyl, hexNut), min(clampCol, screw)));
// 4 brass corner mounting hex bolts on baseplate
vec3 pBolt = vec3(abs(p.x) - 0.425, p.y - 0.094, abs(p.z) - 0.175);
float bolt = sdHexPrism(pBolt, vec2(0.022, 0.014)) - 0.002;
float washer = sdCylinder(pBolt + vec3(0.0, 0.010, 0.0), 0.030, 0.004);
dBrass = min(dBrass, min(bolt, washer));
// Front brass voltage warning plaque on base bevel
float plaque = sdRoundBox(p - vec3(0.0, 0.048, 0.258), vec3(0.12, 0.022, 0.005), 0.002);
dBrass = min(dBrass, plaque);
if (dBrass < d) { d = dBrass; matId = MAT_BRASS; }
// 4. Diverging brushed copper rod electrodes
float dCopper = sdCopperElectrodes(p);
if (dCopper < d) { d = dCopper; matId = MAT_COPPER; }
return d;
}
float mapHero(vec3 p) {
int dummy;
return mapHeroMat(p, dummy);
}
// ============================================================================
// Normals, Soft Shadow, Ambient Occlusion (Zero-Bug Rules)
// ============================================================================
// Rule (1): Unit-weight tetrahedral normals
vec3 calcHeroNormal(vec3 p) {
const float e = 0.0008;
const vec2 k = vec2(1.0, -1.0);
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)
);
}
// Rule (3): Clean hero-only soft shadow (no ph), bias p+N*0.014, tMin=0.025, step max(h,0.015)
float calcSoftShadow(vec3 p, vec3 N, vec3 L, float tMax, float k) {
vec3 ro = p + N * 0.014;
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + L * t);
if (h < 0.0005) 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);
}
float calcAO(vec3 p, vec3 N) {
float occ = 0.0;
float sca = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.022 * float(i);
float d = min(p.y + N.y * h, mapHero(p + N * h));
occ += (h - d) * sca;
sca *= 0.72;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Studio Sky Environment (Softboxes + Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 rd, int lightMode) {
float up = clamp(rd.y, -1.0, 1.0);
// Base studio cyclorama gradient
vec3 botCol = vec3(0.008, 0.009, 0.013);
vec3 horCol = vec3(0.022, 0.025, 0.034);
vec3 topCol = vec3(0.011, 0.013, 0.020);
if (lightMode == 1) {
// Pure neutral grayscale studio
botCol = vec3(0.09);
horCol = vec3(0.18);
topCol = vec3(0.14);
} else if (lightMode == 2) {
// Grazing moody dark studio
botCol = vec3(0.007, 0.008, 0.011);
horCol = vec3(0.018, 0.020, 0.028);
topCol = vec3(0.010, 0.012, 0.017);
} else if (lightMode == 3) {
// Backlit dramatic studio
botCol = vec3(0.009, 0.010, 0.014);
horCol = vec3(0.024, 0.027, 0.036);
topCol = vec3(0.013, 0.015, 0.022);
}
vec3 col = mix(horCol, topCol, smoothstep(0.0, 0.75, up));
col = mix(col, botCol, smoothstep(0.0, -0.45, up));
// Overhead diffused studio softbox
float topBox = smoothstep(0.62, 0.86, rd.y) * smoothstep(0.55, 0.25, abs(rd.x)) * smoothstep(0.65, 0.30, abs(rd.z));
vec3 topBoxCol = (lightMode == 1) ? vec3(0.95) : vec3(0.75, 0.80, 0.90);
col += topBox * topBoxCol * ((lightMode == 0) ? 0.55 : (lightMode == 1 ? 0.85 : 0.30));
// Left key softbox
vec3 keyDir = normalize(vec3(-0.68, 0.52, 0.52));
float keySpot = smoothstep(0.76, 0.94, dot(rd, keyDir));
vec3 keyCol = (lightMode == 1) ? vec3(1.15) : vec3(1.10, 1.03, 0.95);
float keyGain = (lightMode == 0) ? 0.85 : (lightMode == 1 ? 0.95 : (lightMode == 2 ? 0.20 : 0.25));
col += keySpot * keyCol * keyGain;
// Right rim / grazing softbox
vec3 rimDir = (lightMode == 2)
? normalize(vec3(0.94, 0.18, 0.28))
: normalize(vec3(0.74, 0.38, -0.55));
float rimSpot = smoothstep(0.78, 0.96, dot(rd, rimDir));
vec3 rimCol = (lightMode == 1) ? vec3(0.85) : vec3(0.78, 0.88, 1.08);
float rimGain = (lightMode == 2) ? 1.65 : ((lightMode == 0) ? 0.75 : 0.55);
col += rimSpot * rimCol * rimGain;
// Backlight softbox (strong in mode 3)
vec3 backDir = normalize(vec3(-0.12, 0.34, -0.93));
float backSpot = smoothstep(0.70, 0.94, dot(rd, backDir));
vec3 backCol = (lightMode == 1) ? vec3(0.70) : vec3(0.92, 0.95, 1.06);
col += backSpot * backCol * ((lightMode == 3) ? 2.10 : 0.18);
// Negative-fill studio flags (crisp dark vertical strips on left/right flanks for specular contrast)
float flagR = smoothstep(0.68, 0.88, rd.x) * smoothstep(0.45, 0.10, abs(rd.y - 0.08)) * smoothstep(0.55, 0.15, abs(rd.z - 0.12));
float flagL = smoothstep(0.74, 0.92, -rd.x) * smoothstep(0.42, 0.10, abs(rd.y - 0.05)) * smoothstep(0.55, 0.15, abs(rd.z + 0.18));
float flagMask = (lightMode == 2) ? flagL : max(flagR, flagL * 0.65);
col *= (1.0 - 0.68 * flagMask);
return col;
}
// ============================================================================
// GGX Microfacet BRDF
// ============================================================================
float D_GGX(float NoH, float a) {
float a2 = a * a;
float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
return a2 / (PI * d * d + 1e-6);
}
float G_SchlickGGX(float NoV, float NoL, float rough) {
float k = (rough + 1.0);
k = (k * k) * 0.125;
float gv = NoV / (NoV * (1.0 - k) + k + 1e-6);
float gl = NoL / (NoL * (1.0 - k) + k + 1e-6);
return gv * gl;
}
vec3 F_Schlick(float VoH, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - VoH, 0.0, 1.0), 5.0);
}
vec3 evalPBR(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float rough, float metallic) {
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoV = max(dot(N, V), 0.001);
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
float a = max(rough * rough, 0.008);
float D = D_GGX(NoH, a);
float G = G_SchlickGGX(NoV, NoL, rough);
vec3 F = F_Schlick(VoH, F0);
vec3 spec = (D * G * F) / (4.0 * NoV * NoL + 1e-5);
vec3 kd = (1.0 - F) * (1.0 - metallic);
return (kd * albedo / PI + spec) * lightCol * NoL;
}
// ============================================================================
// Material Evaluation
// ============================================================================
void getMaterial(
vec3 p, vec3 N, int matId, float arcY, float phase,
out vec3 albedo, out float rough, out float metallic, out float clearcoat,
out vec3 emissive, out vec3 Nmod
) {
Nmod = N;
clearcoat = 0.0;
emissive = vec3(0.0);
if (matId == MAT_BENCH) {
// Dark satin laboratory phenolic resin / slate benchtop
float g1 = fbm2(p.xz * 3.5);
float g2 = noise2D(p.xz * 26.0);
float micro = noise2D(p.xz * 95.0);
albedo = mix(vec3(0.055, 0.060, 0.068), vec3(0.085, 0.092, 0.104), g1 * 0.7 + g2 * 0.3);
rough = clamp(0.32 + 0.14 * g1 + 0.06 * micro, 0.24, 0.55);
metallic = 0.0;
// Subtle bench micro-normal perturbation
vec2 eps = vec2(0.015, 0.0);
float h0 = fbm2(p.xz * 14.0);
float hx = fbm2((p.xz + eps.xy) * 14.0);
float hz = fbm2((p.xz + eps.yx) * 14.0);
Nmod = normalize(N + vec3(h0 - hx, 0.0, h0 - hz) * 0.28);
} else if (matId == MAT_BASEPLATE) {
// Crinkle-finish dark cast-iron laboratory plinth
float crinkle = noise3D(p * 65.0) * 0.6 + noise3D(p * 140.0) * 0.4;
albedo = mix(vec3(0.07, 0.075, 0.085), vec3(0.115, 0.122, 0.135), crinkle);
rough = 0.42 + 0.14 * crinkle;
metallic = 0.62;
} else if (matId == MAT_CERAMIC) {
// Glazed high-voltage chestnut/mahogany brown electrical porcelain
float glazeVar = noise3D(p * 18.0);
float ribBand = smoothstep(0.10, 0.56, p.y);
vec3 deepBrown = vec3(0.17, 0.058, 0.028);
vec3 warmChestnut = vec3(0.34, 0.125, 0.055);
// Subtle glaze pooling: slightly lighter amber-brown on convex shed tops, darker in undercuts
float shedTone = clamp(N.y * 0.5 + 0.5, 0.0, 1.0);
albedo = mix(deepBrown, warmChestnut, 0.35 + 0.45 * shedTone + 0.20 * glazeVar);
albedo *= mix(0.88, 1.05, ribBand);
rough = 0.14 + 0.06 * glazeVar;
metallic = 0.0;
clearcoat = 0.92;
} else if (matId == MAT_BRASS) {
// Turned brushed laboratory brass hardware
float brush = noise3D(vec3(p.x * 12.0, p.y * 160.0, p.z * 12.0));
albedo = mix(vec3(0.76, 0.58, 0.25), vec3(0.88, 0.71, 0.36), brush);
rough = 0.24 + 0.10 * brush;
metallic = 0.94;
} else {
// MAT_COPPER: Brushed copper rod electrodes with heat-tarnished tips & arc contact glow
vec3 q = vec3(abs(p.x), p.y, p.z);
float cx = electrodeX(p.y);
float ang = atan(q.z, q.x - cx);
// Fine circumferential + longitudinal brushed copper grain
float brushFine = noise2D(vec2(ang * 14.0, p.y * 110.0));
float brushLong = noise2D(vec2(ang * 48.0, p.y * 12.0));
float grain = 0.65 * brushFine + 0.35 * brushLong;
vec3 pureCopper = mix(vec3(0.88, 0.48, 0.32), vec3(0.97, 0.60, 0.42), grain);
// Heat-tarnished oxidation gradient toward the upper tips (thin-film thermal temper colors)
float heatZone = smoothstep(1.55, 2.26, p.y);
vec3 strawGold = vec3(0.74, 0.50, 0.26);
vec3 temperViolet = vec3(0.36, 0.22, 0.34);
vec3 gunmetalTip = vec3(0.20, 0.22, 0.28);
vec3 tarnished = mix(pureCopper, strawGold, smoothstep(0.0, 0.35, heatZone));
tarnished = mix(tarnished, temperViolet, smoothstep(0.30, 0.70, heatZone));
tarnished = mix(tarnished, gunmetalTip, smoothstep(0.68, 1.0, heatZone));
// Subtle arc-track darkening on the inner facing side of the rods
float innerFace = smoothstep(0.015, -0.015, q.x - cx);
float arcTrack = innerFace * smoothstep(0.84, 1.05, p.y) * (0.45 + 0.55 * noise2D(vec2(p.y * 28.0, q.z * 35.0)));
albedo = mix(tarnished, tarnished * vec3(0.55, 0.50, 0.58), arcTrack * 0.42);
rough = clamp(0.18 + 0.11 * grain + 0.18 * heatZone + 0.08 * arcTrack, 0.14, 0.54);
metallic = mix(0.96, 0.76, heatZone * 0.65);
// Incandescent cathode/anode contact spot where the plasma arc bridges the rod
if (uLightMode != 1) {
vec3 contactPt = vec3(electrodeX(arcY) - 0.022, arcY, 0.0);
float dContact = length(q - contactPt);
float spotCore = exp(-dContact * dContact / (0.014 * 0.014));
float spotGlow = exp(-dContact * dContact / (0.042 * 0.042));
emissive += vec3(1.0, 0.88, 1.0) * spotCore * 3.2;
emissive += vec3(0.95, 0.42, 0.16) * spotGlow * 0.9;
}
}
// LightMode 1: Pure neutral grayscale material override so geometry & silhouette read cleanly
if (uLightMode == 1) {
float lum = dot(albedo, vec3(0.299, 0.587, 0.114));
if (matId == MAT_BENCH) {
albedo = vec3(0.14);
} else if (matId == MAT_BASEPLATE) {
albedo = vec3(0.20);
} else if (matId == MAT_CERAMIC) {
albedo = vec3(mix(0.28, 0.42, clamp(N.y * 0.5 + 0.5, 0.0, 1.0)));
clearcoat = 0.55;
} else if (matId == MAT_BRASS) {
albedo = vec3(0.58);
metallic = 0.65;
} else {
albedo = vec3(mix(0.68, 0.46, smoothstep(1.65, 2.26, p.y)));
metallic = 0.75;
}
albedo = mix(vec3(lum), albedo, 0.65);
emissive = vec3(0.0);
}
}
// ============================================================================
// Volumetric Ionized Plasma Arc Integration
// ============================================================================
void marchPlasmaArc(
vec3 ro, vec3 rd, float tHit, float arcY, float phase, float tAnim,
out vec3 plasmaLight, out float plasmaAlpha
) {
plasmaLight = vec3(0.0);
plasmaAlpha = 0.0;
// Tight bounding sphere around the active arc for fast early-out
vec3 arcCenter = vec3(0.0, arcY + 0.14 * phase + 0.06, 0.0);
float arcBoundR = electrodeX(arcY) + 0.36;
vec3 oc = ro - arcCenter;
float b = dot(oc, rd);
float c = dot(oc, oc) - arcBoundR * arcBoundR;
float disc = b * b - c;
if (disc < 0.0) return;
float sq = sqrt(disc);
float t0 = max(0.05, -b - sq);
float t1 = min(tHit, -b + sq);
if (t1 <= t0) return;
const int STEPS = 44;
float dt = (t1 - t0) / float(STEPS);
// Subtle blue-noise-like jitter along ray to eliminate banding
float jitter = fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453);
float t = t0 + dt * jitter;
// Subtle high-frequency electrical flicker
float flicker = 0.92 + 0.08 * sin(tAnim * 37.0) * cos(tAnim * 19.0);
// Slight fade-out right at top release (phase > 0.94)
float releaseEnv = 1.0 - smoothstep(0.93, 1.0, phase) * 0.45;
float intensity = flicker * releaseEnv;
for (int i = 0; i < STEPS; i++) {
vec3 p = ro + rd * t;
float u;
float d1 = distToArc(p, arcY, phase, tAnim, u);
// Secondary faint braided filament
vec3 pSec = evalArcSecondary(u, arcY, phase, tAnim);
float d2 = length(p - pSec);
// Core width slightly tapers at the hottest apex
float rCore = 0.0085 + 0.003 * (1.0 - u * u);
float rInner = 0.026;
float rOuter = 0.095;
float core = exp(-d1 * d1 / (rCore * rCore));
float secCore = exp(-d2 * d2 / (0.0065 * 0.0065)) * 0.38;
float sheath = 1.0 / (1.0 + (d1 * d1) / (rInner * rInner));
float corona = exp(-d1 / rOuter) / (1.0 + d1 * 6.5);
// Rising ionized afterglow plume directly above the arc
vec3 cPt = evalArcPoint(u, arcY, phase, tAnim);
float above = max(0.0, p.y - cPt.y);
float plumeDist = length(vec2(p.x - cPt.x, p.z - cPt.z));
float plume = exp(-plumeDist * 14.0) * exp(-above * 6.5) * smoothstep(0.0, 0.03, above) * 0.16;
if (uLightMode == 1) {
// Muted clean neutral grayscale filament for silhouette/form QA
float neut = (core * 1.15 + secCore * 0.45 + sheath * 0.18);
plasmaLight += vec3(0.76) * neut * dt * 11.0;
plasmaAlpha += (core * 0.85 + sheath * 0.12) * dt * 12.0;
} else {
// Brilliant white-violet core + electric lavender-blue corona sheath
vec3 coreCol = vec3(1.00, 0.96, 1.00);
vec3 sheathCol = vec3(0.64, 0.36, 1.00);
vec3 coronaCol = vec3(0.22, 0.42, 1.00);
vec3 plumeCol = vec3(0.36, 0.26, 0.88);
vec3 stepEm =
coreCol * (core * 44.0 + secCore * 22.0) +
sheathCol * (sheath * 6.2) +
coronaCol * (corona * 2.15) +
plumeCol * (plume * 2.2);
plasmaLight += stepEm * intensity * dt;
plasmaAlpha += (core * 0.9 + sheath * 0.15) * dt * 8.0;
}
t += dt;
}
plasmaAlpha = clamp(plasmaAlpha, 0.0, 0.92);
}
// ============================================================================
// Camera Ray Setup
// ============================================================================
void getCamera(vec2 uv, float arcY, out vec3 ro, out vec3 rd) {
vec3 ta;
float fl;
if (uCamMode == 1) {
// ~2.5x macro framing centered on the active plasma arc & copper rod contact zone
float focusY = clamp(arcY + 0.08, 1.02, 2.08);
ta = vec3(0.0, focusY, 0.0);
ro = vec3(0.24, focusY + 0.08, 1.38);
fl = 2.65;
} else {
// Hero full-apparatus studio view
ta = vec3(0.0, 1.12, 0.0);
ro = vec3(0.75, 1.38, 4.95);
fl = 1.85;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
rd = normalize(uv.x * cu + uv.y * cv + fl * cw);
}
// ============================================================================
// ACES Filmic Tone Mapping (Zero-clipping, Zero-pure-black studio curve)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
vec3 mapped = (x * (a * x + b)) / (x * (c * x + d) + e);
// Gentle shoulder so peak plasma never hard-clips to 255 while preserving crisp brilliance
mapped = mapped / (1.0 + 0.12 * mapped);
return clamp(mapped, 0.0, 0.92);
}
// ============================================================================
// Main Shader Entry
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / min(uResolution.x, uResolution.y);
float phase = getArcPhase();
float arcY = getArcY(phase);
float tAnim = uTime + uOpen * 4.5;
vec3 ro, rd;
getCamera(uv, arcY, ro, rd);
// Raymarch scene (bench plane at y=0 + hero apparatus)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 128; i++) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
float d = min(p.y, dHero);
if (d < 0.0007) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
float tHit = hit ? t : tMax;
if (!hit) {
// Clean studio cyclorama background
col = evalStudioEnv(rd, uLightMode);
// Subtle atmospheric plasma scatter in background air around arc
if (uLightMode != 1) {
vec3 arcCenter = vec3(0.0, arcY + 0.12 * phase + 0.05, 0.0);
float tClosest = max(0.0, dot(arcCenter - ro, rd));
float dRay = length(ro + rd * tClosest - arcCenter);
col += vec3(0.14, 0.18, 0.45) * exp(-dRay * 2.4) * 0.24;
}
} else {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Rule (2): Exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
int matId = MAT_BENCH;
vec3 N;
if (isBench) {
matId = MAT_BENCH;
N = vec3(0.0, 1.0, 0.0);
} else {
mapHeroMat(p, matId);
N = calcHeroNormal(p);
}
vec3 V = -rd;
vec3 albedo, emissive, Nmod;
float rough, metallic, clearcoat;
getMaterial(p, N, matId, arcY, phase, albedo, rough, metallic, clearcoat, emissive, Nmod);
float ao = calcAO(p, N);
// Studio Key & Rim Lights per uLightMode
vec3 keyDir, keyCol;
vec3 rimDir, rimCol;
vec3 fillDir, fillCol;
if (uLightMode == 1) {
// Pure neutral grayscale studio lighting
keyDir = normalize(vec3(-0.62, 0.72, 0.56));
keyCol = vec3(2.35);
rimDir = normalize(vec3(0.75, 0.45, -0.52));
rimCol = vec3(1.45);
fillDir = normalize(vec3(0.45, 0.35, 0.82));
fillCol = vec3(0.65);
} else if (uLightMode == 2) {
// Grazing raking light
keyDir = normalize(vec3(0.92, 0.20, 0.33));
keyCol = vec3(2.85, 2.72, 2.55);
rimDir = normalize(vec3(-0.84, 0.26, -0.48));
rimCol = vec3(0.95, 1.12, 1.38);
fillDir = normalize(vec3(-0.25, 0.55, 0.80));
fillCol = vec3(0.28, 0.32, 0.40);
} else if (uLightMode == 3) {
// Backlit rim studio
keyDir = normalize(vec3(-0.14, 0.42, -0.90));
keyCol = vec3(3.40, 3.45, 3.70);
rimDir = normalize(vec3(0.78, 0.48, -0.40));
rimCol = vec3(1.60, 1.72, 1.95);
fillDir = normalize(vec3(-0.35, 0.55, 0.76));
fillCol = vec3(0.52, 0.54, 0.60);
} else {
// Hero laboratory lighting (mode 0)
keyDir = normalize(vec3(-0.60, 0.74, 0.55));
keyCol = vec3(2.15, 2.02, 1.88);
rimDir = normalize(vec3(0.76, 0.44, -0.50));
rimCol = vec3(1.25, 1.38, 1.62);
fillDir = normalize(vec3(0.38, 0.40, 0.84));
fillCol = vec3(0.36, 0.40, 0.48);
}
float shKey = calcSoftShadow(p, N, keyDir, 5.0, 14.0);
float shRim = calcSoftShadow(p, N, rimDir, 5.0, 10.0);
col = vec3(0.0);
col += evalPBR(Nmod, V, keyDir, keyCol * shKey, albedo, rough, metallic);
col += evalPBR(Nmod, V, rimDir, rimCol * shRim, albedo, rough, metallic);
col += evalPBR(Nmod, V, fillDir, fillCol * ao, albedo, rough, metallic);
// Ceramic glossy clearcoat specular lobe from studio lights
if (clearcoat > 0.01) {
col += evalPBR(N, V, keyDir, keyCol * shKey, vec3(0.04), 0.08, 0.0) * clearcoat;
col += evalPBR(N, V, rimDir, rimCol * shRim, vec3(0.04), 0.08, 0.0) * clearcoat;
}
// Studio IBL reflections & ambient fill
vec3 R = reflect(-V, Nmod);
vec3 envRefl = evalStudioEnv(R, uLightMode);
vec3 envDiff = evalStudioEnv(Nmod, uLightMode);
vec3 F0 = mix(vec3(0.04), albedo, metallic);
vec3 F_env = F_Schlick(max(dot(Nmod, V), 0.0), F0);
float specOcc = clamp(ao * 0.8 + 0.2, 0.0, 1.0);
vec3 iblDiff = (1.0 - F_env) * (1.0 - metallic) * albedo * envDiff * ao * 0.95;
vec3 iblSpec = F_env * envRefl * mix(1.0, 0.35, rough) * specOcc * 1.15;
col += iblDiff + iblSpec;
if (clearcoat > 0.01) {
vec3 Rcc = reflect(-V, N);
float Fcc = 0.04 + 0.96 * pow(1.0 - max(dot(N, V), 0.0), 5.0);
col += evalStudioEnv(Rcc, uLightMode) * Fcc * clearcoat * specOcc * 1.1;
}
// Dynamic vivid blue-violet illumination from the plasma arc (3-sample segment light)
if (uLightMode != 1) {
float plasmaGain = (uLightMode == 0) ? 1.0 : ((uLightMode == 2) ? 0.78 : 0.85);
vec3 arcPts[3];
arcPts[0] = evalArcPoint(-0.72, arcY, phase, tAnim);
arcPts[1] = evalArcPoint( 0.00, arcY, phase, tAnim);
arcPts[2] = evalArcPoint( 0.72, arcY, phase, tAnim);
vec3 arcCols[3];
arcCols[0] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
arcCols[1] = vec3(0.46, 0.56, 1.00) * 3.25 * plasmaGain;
arcCols[2] = vec3(0.60, 0.44, 1.00) * 2.15 * plasmaGain;
for (int li = 0; li < 3; li++) {
vec3 toL = arcPts[li] - p;
float dist = length(toL);
vec3 L = toL / (dist + 1e-5);
float atten = 1.0 / (0.14 + dist * dist * 1.35);
// Only shadow the bench & lower base from the arc (rods themselves self-receive cleanly)
float shArc = 1.0;
if (isBench || matId == MAT_BASEPLATE) {
shArc = calcSoftShadow(p, N, L, max(dist - 0.06, 0.05), 8.0);
}
// Slightly broaden specular on curved copper rods for vivid electric blue-violet streaks
float arcRough = (matId == MAT_COPPER) ? max(rough * 0.82, 0.14) : rough;
col += evalPBR(Nmod, V, L, arcCols[li] * atten * shArc, albedo, arcRough, metallic);
if (clearcoat > 0.01) {
col += evalPBR(N, V, L, arcCols[li] * atten * shArc, vec3(0.05), 0.10, 0.0) * clearcoat * 1.35;
}
}
}
col += emissive;
// Subtle studio bench radial vignette so bench blends seamlessly into studio horizon
if (isBench) {
float rBench = length(p.xz);
float fade = smoothstep(2.2, 7.5, rBench);
col = mix(col, evalStudioEnv(rd, uLightMode), fade);
}
}
// Volumetric ionized plasma arc pass (composite over scene up to tHit)
vec3 plasmaLight;
float plasmaAlpha;
marchPlasmaArc(ro, rd, tHit, arcY, phase, tAnim, plasmaLight, plasmaAlpha);
if (uLightMode == 1) {
// Muted neutral filament blend
col = mix(col, vec3(0.55), plasmaAlpha * 0.65) + plasmaLight * 0.35;
} else {
col = col * (1.0 - plasmaAlpha * 0.35) + plasmaLight;
}
// Ensure studio ambient floor avoids pure 0 black while keeping deep rich contrast
col = max(col, vec3(0.0036));
// ACES Filmic Tone Mapping + sRGB Gamma
col = acesToneMap(col);
col = pow(col, vec3(1.0 / 2.2));
// Subtle optical vignette
float vig = 1.0 - 0.14 * dot(uv, uv);
col *= clamp(vig, 0.80, 1.0);
// Pure achromatic guarantee in uLightMode == 1
if (uLightMode == 1) {
float g = dot(col, vec3(0.299, 0.587, 0.114));
col = vec3(g);
}
fragColor = vec4(col, 1.0);
}
`;
export function createJacobsLadderPlasmaArcMaterial(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 | jacobs-ladder-plasma-arc |
|---|---|
| Category | Sci-Fi, Plasma & Noise Shaders · Plasma, Lava & Fractures |
| Upstream study | thermal/lib/05_jacobs_ladder_plasma_arc.glsl (commit 095539c) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 917 lines · 35.1 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 |
GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation
David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal
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.
Diamond PenTile RGB emitter array for close-up cybernetic visors, HUD screens, and holo-panels.