AnimatedHeavy foundry cast iron with mold-sand stipple micro-relief, graphite dark oxide, and parting seam.
anodized-titanium-finish.glsl · 839 lines · GLSL ES 3.00 · MIT
#version 300 es
// Anodized Titanium Finish (anodized-titanium-finish.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/04_anodized_titanium_interference_finish.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// ============================================================================
// Hashing & Procedural Micro-Surface 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);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float s = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
for (int i = 0; i < 4; i++) {
s += a * valueNoise(p);
p = m * p * 2.04 + 13.7;
a *= 0.5;
}
return s;
}
// ============================================================================
// SDF Primitives & Boolean Ops
// ============================================================================
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - (b - r);
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
float sdChamferBox(vec3 p, vec3 b, float ch) {
vec3 q = abs(p) - b;
float box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
// 45-degree chamfer on top/side edges
float c1 = (q.x + q.y + ch) * 0.70710678;
float c2 = (q.z + q.y + ch) * 0.70710678;
float c3 = (q.x + q.z + ch) * 0.70710678;
return max(box, max(c1, max(c2, c3)));
}
float sdCylinderY(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 smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
// ============================================================================
// Coupon Geometry Parameters (5 Stepped Voltage Zones)
// ============================================================================
// Total footprint: X in [-1.30, 1.30], Z in [-0.66, 0.66], grounded at Y = 0.0
// 5 steps of width 0.52 each: centers at -1.04, -0.52, 0.0, +0.52, +1.04
// Step heights: 0.16, 0.23, 0.30, 0.37, 0.44
float stepHeightForIdx(int idx) {
return 0.16 + float(idx) * 0.07;
}
int zoneIndexFromX(float x) {
float u = (x + 1.30) / 0.52;
return clamp(int(floor(u)), 0, 4);
}
// Hero SDF: Stepped aerospace machined titanium test coupon
float mapHero(vec3 p) {
// Quick bounding box rejection
float dBound = sdBox(p - vec3(0.0, 0.24, 0.0), vec3(1.42, 0.34, 0.78));
if (dBound > 0.25) return dBound;
// 5 stepped aerospace terraces fused cleanly with crisp machined chamfers
float dSteps = 1e5;
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Overlap adjacent steps slightly in X so internal seams are solid
float halfW = (i == 0 || i == 4) ? 0.262 : 0.266;
vec3 q = p - vec3(cx, h * 0.5, 0.0);
// Chamfered outer & top edges (0.016 bevel)
float dStep = sdChamferBox(q, vec3(halfW, h * 0.5, 0.66), 0.015);
dSteps = min(dSteps, dStep);
}
// Precision outer perimeter fillet trim so corners look CNC-milled
float dOuter = sdRoundBox(p - vec3(0.0, 0.30, 0.0), vec3(1.30, 0.34, 0.66), 0.028);
float d = max(dSteps, dOuter);
// Subtle undercut relief along bottom edge (0.008 mm datum foot chamfer)
float dBottomBevel = -(p.y) + max(abs(p.x) - 1.288, abs(p.z) - 0.648) * 0.70;
d = max(d, min(dBottomBevel, -p.y + 0.012));
// Ensure grounded cleanly at y = 0.0
d = max(d, -p.y);
// Strip divider V-groove along Z = 0.02 separating satin-machined (back) and bead-blasted (front)
// Runs across the top face of every step
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float zDiv = abs(p.z - 0.02);
float vGroove = max((topY - p.y) - 0.012, zDiv - (0.013 - max(topY - p.y, 0.0) * 0.95));
// Carve clean 90-degree V-groove along Z = 0.02
float dV = max(abs(p.z - 0.02) - 0.011, (topY - 0.010) - p.y);
float dGroove = max((abs(p.z - 0.02) + (topY - p.y)) * 0.70710678 - 0.0095, -((topY - p.y) + 0.02));
// Keep groove shallow and clean
float grooveCut = max(abs(p.z - 0.02) - (0.010 - clamp(topY - p.y, 0.0, 0.010)), (topY - 0.010) - p.y);
d = smax(d, -grooveCut, 0.0025);
// Chamfered aerospace weight-reduction pockets in each of the 5 steps
// Front pocket (in bead-blasted zone, z centered at +0.34)
// Back shallow inspection pocket (in satin-machined zone, z centered at -0.33)
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Front deep chamfered pocket
vec3 pf = p - vec3(cx, h, 0.34);
float pDepthF = 0.056;
// Main pocket cavity with corner radius
float cavF = sdRoundBox(pf - vec3(0.0, -pDepthF * 0.5 + 0.04, 0.0), vec3(0.165, pDepthF * 0.5 + 0.04, 0.205), 0.032);
// 45-deg chamfered rim flare near top surface (pf.y near 0)
float flareF = sdRoundBox(pf - vec3(0.0, 0.018, 0.0), vec3(0.184, 0.036, 0.224), 0.038);
float bevelF = (flareF + (-pf.y) * 0.75) * 0.70710678;
float pocketF = min(cavF, max(flareF, -pf.y - 0.022));
// Clean 45-degree top chamfer on pocket lip
vec2 qf2 = abs(pf.xz) - vec2(0.165 - 0.032, 0.205 - 0.032);
float d2dF = length(max(qf2, 0.0)) + min(max(qf2.x, qf2.y), 0.0) - 0.032;
float pocketLipF = max(d2dF - max(pf.y + 0.018, 0.0), -pf.y - pDepthF);
d = smax(d, -pocketLipF, 0.003);
// Back shallower satin-machined telemetry slot / pocket
vec3 pb = p - vec3(cx, h, -0.33);
float pDepthB = 0.026;
vec2 qb2 = abs(pb.xz) - vec2(0.155 - 0.026, 0.185 - 0.026);
float d2dB = length(max(qb2, 0.0)) + min(max(qb2.x, qb2.y), 0.0) - 0.026;
float pocketLipB = max(d2dB - max(pb.y + 0.012, 0.0), -pb.y - pDepthB);
d = smax(d, -pocketLipB, 0.0025);
}
// Aerospace datum tooling bores (countersunk) on the 2 outer ends of the satin strip
for (int j = 0; j < 2; j++) {
float bx = (j == 0) ? -1.175 : 1.175;
float bh = (j == 0) ? stepHeightForIdx(0) : stepHeightForIdx(4);
vec3 pb = p - vec3(bx, bh, -0.545);
float rCyl = length(pb.xz);
float bore = max(rCyl - 0.038, -pb.y - 0.14);
float csk = max(rCyl - (0.038 + max(pb.y + 0.016, 0.0) * 1.0), -pb.y - 0.03);
d = smax(d, -min(bore, csk), 0.002);
}
return d;
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormalHero(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.00075;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
// ============================================================================
// Clean Hero-Only Soft Shadow (Zero-Bug Rule #3: no ph!)
// ============================================================================
float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
t += max(h, 0.015);
if (res < 0.004 || 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.018 * float(i) * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.1 * occ, 0.28, 1.0);
}
// ============================================================================
// Physical TiO2 Thin-Film Airy Optical Interference on Titanium Substrate
// ============================================================================
// Complex arithmetic helpers
vec2 cMul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
vec2 cDiv(vec2 a, vec2 b) { return vec2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / max(dot(b, b), 1e-6); }
// Single-wavelength Airy thin-film reflectance for Air -> TiO2(n1, d) -> Ti(n2 + i*k2)
float airyReflectance(float lambdaNm, float dNm, float cosTheta0, float n1, float n2, float k2) {
float sin2Theta0 = max(0.0, 1.0 - cosTheta0 * cosTheta0);
float cosTheta1 = sqrt(max(0.001, 1.0 - sin2Theta0 / (n1 * n1)));
// Air -> TiO2 Fresnel (s and p polarizations)
float r01s = (cosTheta0 - n1 * cosTheta1) / (cosTheta0 + n1 * cosTheta1);
float r01p = (n1 * cosTheta0 - cosTheta1) / (n1 * cosTheta0 + cosTheta1);
// TiO2 -> Ti complex Fresnel (effective s and p)
vec2 nTi = vec2(n2, k2);
vec2 nFilmS = vec2(n1 * cosTheta1, 0.0);
vec2 nFilmP = vec2(n1 / cosTheta1, 0.0);
// Approximate transmitted angle in dense metal (~cosTheta2 near 1)
vec2 r12s = cDiv(nFilmS - nTi, nFilmS + nTi);
vec2 r12p = cDiv(nTi - nFilmP, nTi + nFilmP);
// Optical phase thickness across TiO2 film
float delta = (4.0 * PI * n1 * dNm * cosTheta1) / lambdaNm;
vec2 expI = vec2(cos(delta), sin(delta));
// Airy summation for s-pol
vec2 r12sE = cMul(r12s, expI);
vec2 rs = cDiv(vec2(r01s, 0.0) + r12sE, vec2(1.0, 0.0) + r01s * r12sE);
// Airy summation for p-pol
vec2 r12pE = cMul(r12p, expI);
vec2 rp = cDiv(vec2(r01p, 0.0) + r12pE, vec2(1.0, 0.0) + r01p * r12pE);
return 0.5 * (dot(rs, rs) + dot(rp, rp));
}
// 6-band spectral integration of TiO2 thin-film interference -> linear sRGB reflectance
vec3 evalTiO2Interference(float dNm, float cosTheta) {
// 6 wavelengths spanning visible spectrum (nm)
// TiO2 anodic dispersion n1(lambda), Ti substrate n2(lambda), k2(lambda)
float r640 = airyReflectance(640.0, dNm, cosTheta, 2.34, 2.72, 3.80);
float r595 = airyReflectance(595.0, dNm, cosTheta, 2.37, 2.60, 3.58);
float r550 = airyReflectance(550.0, dNm, cosTheta, 2.41, 2.48, 3.36);
float r510 = airyReflectance(510.0, dNm, cosTheta, 2.45, 2.38, 3.18);
float r475 = airyReflectance(475.0, dNm, cosTheta, 2.50, 2.29, 3.04);
float r445 = airyReflectance(445.0, dNm, cosTheta, 2.56, 2.22, 2.94);
// Smooth CIE-inspired spectral-to-linear-sRGB weights (normalized so flat 1.0 -> white)
vec3 col =
r640 * vec3(0.58, 0.04, 0.00) +
r595 * vec3(0.42, 0.24, 0.00) +
r550 * vec3(0.08, 0.46, 0.02) +
r510 * vec3(-0.08, 0.28, 0.16) +
r475 * vec3(0.00, 0.04, 0.46) +
r445 * vec3(0.00, -0.06, 0.36);
// Clamp tiny negative gamut excursions and boost subtle physical chroma clarity
col = max(col, vec3(0.035));
// Mode 1: pure neutral metallic grayscale (strip interference chroma)
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(mix(0.42, 0.78, lum));
}
// Preserve metallic Titanium reflectivity envelope
return clamp(col, vec3(0.035), vec3(0.96));
}
// Compute TiO2 oxide thickness (nm) for a point on the coupon
// 5 voltage zones at uOpen = 0:
// Step 0: 15V Bronze (~28 nm)
// Step 1: 25V Royal Blue (~53 nm)
// Step 2: 55V Gold (~120 nm)
// Step 3: 75V Magenta-Violet(~149 nm)
// Step 4: 95V Teal-Green (~186 nm)
// uOpen (0..1) sweeps anodizing voltage / oxide thickness smoothly across the coupon.
float computeOxideThickness(vec3 p) {
// Smooth transitions at step risers so vertical walls show slight anodizing meniscus
float u = (p.x + 1.30) / 0.52;
float idxF = clamp(u, 0.0, 4.999);
int i0 = int(floor(idxF));
float f = fract(idxF);
float dTable[5];
dTable[0] = 28.0; // 15V bronze
dTable[1] = 53.0; // 25V royal blue
dTable[2] = 120.0; // 55V gold
dTable[3] = 149.0; // 75V magenta-violet
dTable[4] = 186.0; // 95V teal-green
float dBase = dTable[i0];
// Subtle transition right at step riser edge (2% of step width)
if (i0 < 4 && f > 0.96) {
float t = smoothstep(0.96, 1.0, f);
dBase = mix(dTable[i0], dTable[i0 + 1], t);
} else if (i0 > 0 && f < 0.04) {
float t = smoothstep(0.04, 0.0, f);
dBase = mix(dTable[i0], dTable[i0 - 1], t);
}
// Subtle natural anodic thickness micro-variation (+/- 2.2 nm) from electrolyte bath flow
float bathVar = (fbm2(p.xz * 3.2 + vec2(1.7, -2.3)) - 0.5) * 4.2;
// Slight edge current-density thickening on sharp chamfers (+3 nm)
float edgeBoost = smoothstep(0.55, 0.66, abs(p.z)) * 3.5;
// uOpen sweeps anodizing voltage across the coupon (+0..+120 nm)
float sweep = uOpen * 120.0;
return max(12.0, dBase + sweep + bathVar + edgeBoost);
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base dark cyclorama studio ambient (gives strong negative-fill contrast on metal)
vec3 col = mix(vec3(0.035, 0.038, 0.044), vec3(0.065, 0.072, 0.084), smoothstep(-0.2, 0.8, y));
if (lightMode == 1) {
// Mode 1: Pure neutral grayscale studio
col = mix(vec3(0.045), vec3(0.085), smoothstep(-0.2, 0.8, y));
// Overhead main neutral softbox
float sbTop = smoothstep(0.62, 0.90, dot(d, normalize(vec3(0.15, 0.95, 0.25))));
// Key softbox right-front
float sbKey = smoothstep(0.68, 0.92, dot(d, normalize(vec3(0.65, 0.58, 0.48))));
// Cool-neutral strip rim left-back
float sbRim = smoothstep(0.76, 0.95, dot(d, normalize(vec3(-0.72, 0.42, -0.55))));
// Negative-fill dark flag right-side
float flag1 = smoothstep(0.70, 0.88, dot(d, normalize(vec3(0.92, 0.18, -0.35))));
col += vec3(1.85) * sbTop;
col += vec3(2.40) * sbKey;
col += vec3(1.50) * sbRim;
col *= (1.0 - 0.72 * flag1);
return col;
}
if (lightMode == 2) {
// Mode 2: Grazing raking light
col = vec3(0.025, 0.028, 0.034);
float sbRaking = smoothstep(0.74, 0.96, dot(d, normalize(vec3(0.92, 0.24, 0.30))));
float sbCounter = smoothstep(0.72, 0.94, dot(d, normalize(vec3(-0.88, 0.26, -0.38))));
float sbTopStrip = smoothstep(0.84, 0.97, dot(d, normalize(vec3(0.0, 0.98, 0.15))));
col += vec3(3.40, 3.15, 2.80) * sbRaking;
col += vec3(1.25, 1.45, 1.75) * sbCounter;
col += vec3(0.85, 0.90, 0.98) * sbTopStrip;
return col;
}
if (lightMode == 3) {
// Mode 3: Backlit studio rim
col = vec3(0.028, 0.032, 0.040);
float sbBack = smoothstep(0.55, 0.90, dot(d, normalize(vec3(-0.18, 0.45, -0.87))));
float sbRimL = smoothstep(0.74, 0.94, dot(d, normalize(vec3(-0.75, 0.50, -0.42))));
float sbFrontFill = smoothstep(0.65, 0.92, dot(d, normalize(vec3(0.35, 0.52, 0.78))));
col += vec3(3.20, 3.25, 3.45) * sbBack;
col += vec3(2.10, 2.25, 2.50) * sbRimL;
col += vec3(0.62, 0.64, 0.68) * sbFrontFill;
return col;
}
// Mode 0: Hero aerospace product studio lighting
// Large overhead strip softbox (crisp specular bands across stepped planes)
vec3 topDir = normalize(vec3(-0.12, 0.94, 0.32));
float sbTop = smoothstep(0.68, 0.91, dot(d, topDir));
// Warm-white key softbox (front-right-high)
vec3 keyDir = normalize(vec3(0.62, 0.56, 0.54));
float sbKey = smoothstep(0.72, 0.94, dot(d, keyDir));
// Cool rim strip softbox (back-left)
vec3 rimDir = normalize(vec3(-0.70, 0.44, -0.56));
float sbRim = smoothstep(0.76, 0.95, dot(d, rimDir));
// Crisp horizon accent line for chamfer glints
float horizonStrip = exp(-abs(y - 0.16) * 14.0) * smoothstep(-0.2, 0.8, -d.z) * 0.45;
// Dark negative-fill flags to sculpt metallic contrast
float negFlagR = smoothstep(0.65, 0.86, dot(d, normalize(vec3(0.94, 0.16, -0.30))));
float negFlagL = smoothstep(0.68, 0.88, dot(d, normalize(vec3(-0.88, 0.14, 0.44))));
col += vec3(1.95, 1.98, 2.08) * sbTop;
col += vec3(2.65, 2.52, 2.34) * sbKey;
col += vec3(1.45, 1.65, 2.05) * sbRim;
col += vec3(0.95, 1.02, 1.15) * horizonStrip;
col *= (1.0 - 0.78 * negFlagR);
col *= (1.0 - 0.65 * negFlagL);
return col;
}
// ============================================================================
// GGX Microfacet BRDF with Anisotropic Satin & Thin-Film Fresnel
// ============================================================================
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float d = (ToH * ToH) / (ax * ax) + (BoH * BoH) / (ay * ay) + NoH * NoH;
return 1.0 / max(PI * ax * ay * d * d, 1e-5);
}
float G_SmithGGX(float NoV, float NoL, float alpha) {
float a2 = alpha * alpha;
float gv = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2);
float gl = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// Micro-blasted metallic sparkle glints (high-frequency crystalline Ti grain facets)
float microSparkle(vec3 p, vec3 N, vec3 V, vec3 L, float scale) {
vec2 uv = p.xz * scale + p.y * scale * 0.37;
vec2 cell = floor(uv);
vec2 f = fract(uv);
vec3 rnd = hash32(cell);
// Random micro-facet normal tilted within bead-blast cone
vec2 tilt = (rnd.xy - 0.5) * 0.52;
vec3 T = normalize(cross(abs(N.y) > 0.9 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0), N));
vec3 B = cross(N, T);
vec3 mN = normalize(N + T * tilt.x + B * tilt.y);
vec3 H = normalize(V + L);
float spec = pow(max(dot(mN, H), 0.0), 90.0);
// Soft circular grain mask so sparkles never look blocky
float grainMask = smoothstep(0.55, 0.10, length(f - 0.5));
return spec * grainMask * (0.45 + 0.55 * rnd.z);
}
// ============================================================================
// Surface Micro-Detail & Finish Classification
// ============================================================================
// Returns: x = roughness, y = anisotropy (-1..1), z = isBeadBlasted (0..1), w = chamferMask
vec4 sampleCouponFinish(vec3 p, vec3 N, out vec3 N_perturbed) {
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float cx = -1.04 + float(zi) * 0.52;
// Classify front bead-blasted strip (z > 0.02) vs back satin-machined strip (z < 0.02)
float isBlasted = smoothstep(0.014, 0.028, p.z);
// Detect angled chamfered edges & bevels (bright machined specular)
float chamferMask = smoothstep(0.35, 0.78, 1.0 - abs(N.y)) * smoothstep(0.15, 0.55, abs(N.y));
// Also keep vertical step risers satin-machined
float isSideWall = smoothstep(0.45, 0.15, abs(N.y));
// Inside front pocket floor: fine satin concentric milled finish vs blasted rim
vec2 pf = p.xz - vec2(cx, 0.34);
float inFrontPocketFloor = smoothstep(topY - 0.042, topY - 0.052, p.y) * smoothstep(0.75, 0.95, N.y);
float blastWeight = isBlasted * (1.0 - chamferMask) * (1.0 - isSideWall) * (1.0 - inFrontPocketFloor * 0.85);
// Construct tangent frame
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
// 1. Satin CNC-machined micro-grooves (parallel toolpath + subtle fly-cutter arcs)
float toolGrooveX = sin(p.z * 320.0 + fbm2(p.xz * 18.0) * 1.4) * 0.5;
float flyArc = sin(length((p.xz - vec2(cx, -0.33)) * vec2(1.0, 1.0)) * 240.0) * 0.35;
float millLine = mix(toolGrooveX, flyArc, smoothstep(0.24, 0.14, length(p.xz - vec2(cx, -0.33))));
// Side walls have fine horizontal end-mill passes
float sideMill = sin(p.y * 380.0) * 0.45;
float satinBump = mix(millLine, sideMill, isSideWall);
// 2. Glass-bead-blasted isotropic micro-dimple perturbation
vec2 bp = p.xz * 165.0 + p.y * 90.0;
float b1 = valueNoise(bp) - 0.5;
float b2 = valueNoise(bp * 2.15 + 17.3) - 0.5;
float bx = (valueNoise(bp + vec2(0.35, 0.0)) - valueNoise(bp - vec2(0.35, 0.0)));
float bz = (valueNoise(bp + vec2(0.0, 0.35)) - valueNoise(bp - vec2(0.0, 0.35)));
vec3 nSatin = normalize(N + B * (satinBump * 0.022) + T * ((valueNoise(p.xz * 95.0) - 0.5) * 0.010));
vec3 nBlast = normalize(N + T * (bx * 0.14 + b2 * 0.05) + B * (bz * 0.14 + b1 * 0.05));
N_perturbed = normalize(mix(nSatin, nBlast, blastWeight));
// Roughness & anisotropy
float roughSatin = 0.155 + 0.025 * valueNoise(p.xz * 40.0);
float roughBlast = 0.355 + 0.045 * valueNoise(p.xz * 90.0);
float rough = mix(roughSatin, roughBlast, blastWeight);
// Chamfers are freshly cut and slightly glossier
rough = mix(rough, 0.115, chamferMask * 0.85);
float aniso = mix(0.58, 0.0, blastWeight);
aniso = mix(aniso, 0.15, chamferMask);
return vec4(rough, aniso, blastWeight, chamferMask);
}
// ============================================================================
// Lighting Setup (4 Light Modes)
// ============================================================================
void getLights(int lightMode, out vec3 lDir[3], out vec3 lCol[3]) {
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
lDir[0] = normalize(vec3(0.58, 0.68, 0.45));
lCol[0] = vec3(2.35);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.30);
lDir[2] = normalize(vec3(-0.15, 0.92, 0.36));
lCol[2] = vec3(1.10);
} else if (lightMode == 2) {
// Grazing raking light
lDir[0] = normalize(vec3(0.92, 0.25, 0.30));
lCol[0] = vec3(3.10, 2.90, 2.65);
lDir[1] = normalize(vec3(-0.88, 0.26, -0.40));
lCol[1] = vec3(1.15, 1.32, 1.62);
lDir[2] = normalize(vec3(0.05, 0.86, 0.50));
lCol[2] = vec3(0.55, 0.58, 0.64);
} else if (lightMode == 3) {
// Backlit studio rim
lDir[0] = normalize(vec3(-0.22, 0.46, -0.86));
lCol[0] = vec3(3.05, 3.12, 3.32);
lDir[1] = normalize(vec3(-0.74, 0.52, -0.42));
lCol[1] = vec3(1.85, 1.98, 2.22);
lDir[2] = normalize(vec3(0.38, 0.58, 0.72));
lCol[2] = vec3(0.78, 0.80, 0.84);
} else {
// Mode 0: Hero studio lighting (warm-white key, cool rim, crisp overhead)
lDir[0] = normalize(vec3(0.58, 0.62, 0.52));
lCol[0] = vec3(2.55, 2.44, 2.30);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.38, 1.56, 1.92);
lDir[2] = normalize(vec3(-0.12, 0.94, 0.32));
lCol[2] = vec3(1.18, 1.20, 1.26);
}
}
// ============================================================================
// Shade Hero (Anodized Titanium Coupon)
// ============================================================================
vec3 shadeHero(vec3 p, vec3 Ngeom, vec3 V) {
vec3 N;
vec4 fin = sampleCouponFinish(p, Ngeom, N);
float rough = fin.x;
float aniso = fin.y;
float blastWeight = fin.z;
float dOxide = computeOxideThickness(p);
float ao = calcAO(p, Ngeom);
// Tangent frame for anisotropic satin highlights
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
float ax = max(0.04, rough * (1.0 + aniso * 0.65));
float ay = max(0.04, rough * (1.0 - aniso * 0.55));
float NoV = max(dot(N, V), 0.02);
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 color = vec3(0.0);
// Direct studio lights
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + Ngeom * 0.014, L, 6.0, 14.0)
: (i == 1) ? softShadow(p + Ngeom * 0.014, L, 6.0, 10.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float ToH = dot(T, H);
float BoH = dot(B, H);
// View-angle & microfacet-angle TiO2 thin-film Airy interference reflectance
vec3 F_film = evalTiO2Interference(dOxide, VoH);
float D = D_GGX_Aniso(NoH, ToH, BoH, ax, ay);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 spec = D * G * F_film;
// Micro-blasted iridescent sparkle glints on bead-blasted strip
float spk = 0.0;
if (blastWeight > 0.05) {
float s1 = microSparkle(p, Ngeom, V, L, 210.0);
float s2 = microSparkle(p + vec3(0.13, 0.0, 0.27), Ngeom, V, L, 340.0);
spk = (s1 * 0.75 + s2 * 0.55) * blastWeight;
}
// Subtle multi-scatter metallic lobe for bead-blasted Ti (keeps matte strip luminous & vivid)
vec3 multiScatter = F_film * F_film * (0.14 + 0.22 * blastWeight) / PI;
color += (spec + multiScatter + F_film * spk * 1.45) * lCol[i] * NoL * sh;
}
// Studio IBL environment reflection with view-angle thin-film iridescence
vec3 R = reflect(-V, N);
vec3 F_env = evalTiO2Interference(dOxide, NoV);
// Multi-lobe filtered studio environment lookup (sharp + broad roughness blend)
vec3 envSharp = evalStudioEnv(R, uLightMode);
vec3 envMid = evalStudioEnv(normalize(mix(R, N, 0.35)), uLightMode);
vec3 envBroad = evalStudioEnv(normalize(mix(R, N, 0.75)), uLightMode);
vec3 envSpec = mix(envSharp, mix(envMid, envBroad, smoothstep(0.18, 0.45, rough)), smoothstep(0.08, 0.38, rough));
// Ground bounce tint (subtle dark bench reflection for downward-facing bevels)
float downFade = smoothstep(-0.15, 0.15, R.y);
envSpec = mix(vec3(0.04, 0.042, 0.048), envSpec, 0.35 + 0.65 * downFade);
color += envSpec * F_env * (0.82 + 0.16 * (1.0 - rough)) * ao;
// Lift deepest pocket ambient slightly so metallic crevices stay readable
color += F_env * 0.042 * (0.45 + 0.55 * ao);
// Subtle secondary iridescent edge sheen at grazing view angles
float fresnelRim = pow(1.0 - NoV, 4.0);
vec3 F_grazing = evalTiO2Interference(dOxide, 0.18);
color += F_grazing * fresnelRim * 0.13 * ao;
return color;
}
// ============================================================================
// Shade Grounded Studio Bench
// ============================================================================
vec3 shadeBench(vec3 p, vec3 V) {
// Subtle dark anodized slate / machined granite inspection bench
vec2 gUV = p.xz * 24.0;
float grain = fbm2(gUV) - 0.5;
float fine = valueNoise(p.xz * 140.0) - 0.5;
vec3 N = normalize(vec3(grain * 0.016 + fine * 0.008, 1.0, (fbm2(gUV + 9.1) - 0.5) * 0.016));
float dist = length(p.xz);
// Subtle radial falloff into studio cyclorama (darkened ~0.68x for rich metallic contrast)
float benchFade = smoothstep(6.5, 1.8, dist);
vec3 baseCol = mix(vec3(0.028, 0.030, 0.035), vec3(0.046, 0.049, 0.055), benchFade);
if (uLightMode == 1) {
baseCol = vec3(dot(baseCol, vec3(0.2126, 0.7152, 0.0722)));
}
baseCol *= (0.94 + 0.12 * grain);
// Subtle calibrated inspection bench hairline grid (very faint, museum feel)
vec2 grid = abs(fract(p.xz * 2.0 - 0.5) - 0.5) / 0.015;
float line = 1.0 - min(min(grid.x, grid.y), 1.0);
baseCol += vec3(0.007) * line * smoothstep(3.8, 1.2, dist);
float ao = calcAO(p, vec3(0.0, 1.0, 0.0));
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 col = vec3(0.0);
float NoV = max(dot(N, V), 0.02);
float rough = 0.36 + 0.04 * grain;
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 11.0)
: (i == 1) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 8.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float F = 0.04 + 0.96 * pow(1.0 - VoH, 5.0);
float D = D_GGX_Aniso(NoH, 0.0, 0.0, rough, rough);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 diff = baseCol * (1.0 - F) / PI;
vec3 spec = vec3(D * G * F) * 0.72;
col += (diff + spec) * lCol[i] * NoL * sh;
}
// Soft studio reflection + subtle colored bounce from anodized coupon near base
vec3 R = reflect(-V, N);
float F_env = 0.04 + 0.96 * pow(1.0 - NoV, 5.0);
vec3 envRefl = evalStudioEnv(normalize(mix(R, N, 0.38)), uLightMode);
col += envRefl * F_env * 0.22 * ao;
col += baseCol * evalStudioEnv(vec3(0.0, 1.0, 0.0), uLightMode) * 0.16 * ao;
// Subtle colorful iridescent reflection of the coupon onto the nearby bench
float dHero = mapHero(p);
if (dHero < 0.75) {
float bounceMask = smoothstep(0.75, 0.02, dHero) * (0.25 + 0.75 * F_env);
float dOx = computeOxideThickness(vec3(clamp(p.x, -1.28, 1.28), 0.10, clamp(p.z, -0.64, 0.64)));
vec3 bounceCol = evalTiO2Interference(dOx, 0.45);
col += bounceCol * bounceMask * 0.11 * ao;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping (Calibrated: 0% Black Crush, 0% Hard Clip)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// ============================================================================
// Camera Rig & Main Raymarcher
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;
// Camera modes: 0 = Hero 3/4 studio framing, 1 = ~2.5x Macro inspection
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x macro focused on gold/blue/magenta stepped chamfer + satin/blasted seam
ro = vec3(0.32, 0.92, 1.18);
ta = vec3(0.06, 0.28, 0.04);
fov = 1.65;
} else {
// Hero 3/4 framing showing all 5 stepped voltage zones cleanly
ro = vec3(-0.72, 1.88, 2.75);
ta = vec3(0.02, 0.24, -0.02);
fov = 1.48;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + fov * cw);
// Raymarch scene (hero + bench y=0)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0005) {
hit = true;
break;
}
t += d * 0.92;
if (t > tMax) break;
}
// Studio cyclorama background (darkened ~0.7x so coupon pops with richer metallic contrast)
vec3 bg = evalStudioEnv(rd, uLightMode) * 0.18;
// Soft vertical studio backdrop gradient
vec3 col = mix(vec3(0.038, 0.041, 0.048), vec3(0.066, 0.071, 0.080), smoothstep(-0.25, 0.55, uv.y));
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
col = mix(col, bg, 0.32);
if (hit) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-Bug Rule #2: exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
vec3 benchCol = shadeBench(p, -rd);
// Smooth atmospheric blend into studio horizon
float fog = smoothstep(5.0, 12.5, t);
col = mix(benchCol, col, fog);
} else {
vec3 N = calcNormalHero(p);
col = shadeHero(p, N, -rd);
}
}
// Pure neutral luminance grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Calibrated exposure & ACES filmic tone mapping (keeps hero lum in [14, 242], 0% black/clipped)
col = max(col * 0.90, vec3(0.007));
col = acesToneMap(col) * 0.89;
// Subtle studio lens vignette (keeps corners soft without crushing below lumMin=12)
float vig = 1.0 - 0.11 * dot(uv, uv);
col *= vig;
// Linear -> sRGB gamma
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2));
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Anodized Titanium Finish — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/04_anodized_titanium_interference_finish.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;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// ============================================================================
// Hashing & Procedural Micro-Surface 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);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float s = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
for (int i = 0; i < 4; i++) {
s += a * valueNoise(p);
p = m * p * 2.04 + 13.7;
a *= 0.5;
}
return s;
}
// ============================================================================
// SDF Primitives & Boolean Ops
// ============================================================================
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - (b - r);
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
float sdChamferBox(vec3 p, vec3 b, float ch) {
vec3 q = abs(p) - b;
float box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
// 45-degree chamfer on top/side edges
float c1 = (q.x + q.y + ch) * 0.70710678;
float c2 = (q.z + q.y + ch) * 0.70710678;
float c3 = (q.x + q.z + ch) * 0.70710678;
return max(box, max(c1, max(c2, c3)));
}
float sdCylinderY(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 smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
// ============================================================================
// Coupon Geometry Parameters (5 Stepped Voltage Zones)
// ============================================================================
// Total footprint: X in [-1.30, 1.30], Z in [-0.66, 0.66], grounded at Y = 0.0
// 5 steps of width 0.52 each: centers at -1.04, -0.52, 0.0, +0.52, +1.04
// Step heights: 0.16, 0.23, 0.30, 0.37, 0.44
float stepHeightForIdx(int idx) {
return 0.16 + float(idx) * 0.07;
}
int zoneIndexFromX(float x) {
float u = (x + 1.30) / 0.52;
return clamp(int(floor(u)), 0, 4);
}
// Hero SDF: Stepped aerospace machined titanium test coupon
float mapHero(vec3 p) {
// Quick bounding box rejection
float dBound = sdBox(p - vec3(0.0, 0.24, 0.0), vec3(1.42, 0.34, 0.78));
if (dBound > 0.25) return dBound;
// 5 stepped aerospace terraces fused cleanly with crisp machined chamfers
float dSteps = 1e5;
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Overlap adjacent steps slightly in X so internal seams are solid
float halfW = (i == 0 || i == 4) ? 0.262 : 0.266;
vec3 q = p - vec3(cx, h * 0.5, 0.0);
// Chamfered outer & top edges (0.016 bevel)
float dStep = sdChamferBox(q, vec3(halfW, h * 0.5, 0.66), 0.015);
dSteps = min(dSteps, dStep);
}
// Precision outer perimeter fillet trim so corners look CNC-milled
float dOuter = sdRoundBox(p - vec3(0.0, 0.30, 0.0), vec3(1.30, 0.34, 0.66), 0.028);
float d = max(dSteps, dOuter);
// Subtle undercut relief along bottom edge (0.008 mm datum foot chamfer)
float dBottomBevel = -(p.y) + max(abs(p.x) - 1.288, abs(p.z) - 0.648) * 0.70;
d = max(d, min(dBottomBevel, -p.y + 0.012));
// Ensure grounded cleanly at y = 0.0
d = max(d, -p.y);
// Strip divider V-groove along Z = 0.02 separating satin-machined (back) and bead-blasted (front)
// Runs across the top face of every step
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float zDiv = abs(p.z - 0.02);
float vGroove = max((topY - p.y) - 0.012, zDiv - (0.013 - max(topY - p.y, 0.0) * 0.95));
// Carve clean 90-degree V-groove along Z = 0.02
float dV = max(abs(p.z - 0.02) - 0.011, (topY - 0.010) - p.y);
float dGroove = max((abs(p.z - 0.02) + (topY - p.y)) * 0.70710678 - 0.0095, -((topY - p.y) + 0.02));
// Keep groove shallow and clean
float grooveCut = max(abs(p.z - 0.02) - (0.010 - clamp(topY - p.y, 0.0, 0.010)), (topY - 0.010) - p.y);
d = smax(d, -grooveCut, 0.0025);
// Chamfered aerospace weight-reduction pockets in each of the 5 steps
// Front pocket (in bead-blasted zone, z centered at +0.34)
// Back shallow inspection pocket (in satin-machined zone, z centered at -0.33)
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Front deep chamfered pocket
vec3 pf = p - vec3(cx, h, 0.34);
float pDepthF = 0.056;
// Main pocket cavity with corner radius
float cavF = sdRoundBox(pf - vec3(0.0, -pDepthF * 0.5 + 0.04, 0.0), vec3(0.165, pDepthF * 0.5 + 0.04, 0.205), 0.032);
// 45-deg chamfered rim flare near top surface (pf.y near 0)
float flareF = sdRoundBox(pf - vec3(0.0, 0.018, 0.0), vec3(0.184, 0.036, 0.224), 0.038);
float bevelF = (flareF + (-pf.y) * 0.75) * 0.70710678;
float pocketF = min(cavF, max(flareF, -pf.y - 0.022));
// Clean 45-degree top chamfer on pocket lip
vec2 qf2 = abs(pf.xz) - vec2(0.165 - 0.032, 0.205 - 0.032);
float d2dF = length(max(qf2, 0.0)) + min(max(qf2.x, qf2.y), 0.0) - 0.032;
float pocketLipF = max(d2dF - max(pf.y + 0.018, 0.0), -pf.y - pDepthF);
d = smax(d, -pocketLipF, 0.003);
// Back shallower satin-machined telemetry slot / pocket
vec3 pb = p - vec3(cx, h, -0.33);
float pDepthB = 0.026;
vec2 qb2 = abs(pb.xz) - vec2(0.155 - 0.026, 0.185 - 0.026);
float d2dB = length(max(qb2, 0.0)) + min(max(qb2.x, qb2.y), 0.0) - 0.026;
float pocketLipB = max(d2dB - max(pb.y + 0.012, 0.0), -pb.y - pDepthB);
d = smax(d, -pocketLipB, 0.0025);
}
// Aerospace datum tooling bores (countersunk) on the 2 outer ends of the satin strip
for (int j = 0; j < 2; j++) {
float bx = (j == 0) ? -1.175 : 1.175;
float bh = (j == 0) ? stepHeightForIdx(0) : stepHeightForIdx(4);
vec3 pb = p - vec3(bx, bh, -0.545);
float rCyl = length(pb.xz);
float bore = max(rCyl - 0.038, -pb.y - 0.14);
float csk = max(rCyl - (0.038 + max(pb.y + 0.016, 0.0) * 1.0), -pb.y - 0.03);
d = smax(d, -min(bore, csk), 0.002);
}
return d;
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormalHero(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.00075;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
// ============================================================================
// Clean Hero-Only Soft Shadow (Zero-Bug Rule #3: no ph!)
// ============================================================================
float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
t += max(h, 0.015);
if (res < 0.004 || 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.018 * float(i) * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.1 * occ, 0.28, 1.0);
}
// ============================================================================
// Physical TiO2 Thin-Film Airy Optical Interference on Titanium Substrate
// ============================================================================
// Complex arithmetic helpers
vec2 cMul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
vec2 cDiv(vec2 a, vec2 b) { return vec2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / max(dot(b, b), 1e-6); }
// Single-wavelength Airy thin-film reflectance for Air -> TiO2(n1, d) -> Ti(n2 + i*k2)
float airyReflectance(float lambdaNm, float dNm, float cosTheta0, float n1, float n2, float k2) {
float sin2Theta0 = max(0.0, 1.0 - cosTheta0 * cosTheta0);
float cosTheta1 = sqrt(max(0.001, 1.0 - sin2Theta0 / (n1 * n1)));
// Air -> TiO2 Fresnel (s and p polarizations)
float r01s = (cosTheta0 - n1 * cosTheta1) / (cosTheta0 + n1 * cosTheta1);
float r01p = (n1 * cosTheta0 - cosTheta1) / (n1 * cosTheta0 + cosTheta1);
// TiO2 -> Ti complex Fresnel (effective s and p)
vec2 nTi = vec2(n2, k2);
vec2 nFilmS = vec2(n1 * cosTheta1, 0.0);
vec2 nFilmP = vec2(n1 / cosTheta1, 0.0);
// Approximate transmitted angle in dense metal (~cosTheta2 near 1)
vec2 r12s = cDiv(nFilmS - nTi, nFilmS + nTi);
vec2 r12p = cDiv(nTi - nFilmP, nTi + nFilmP);
// Optical phase thickness across TiO2 film
float delta = (4.0 * PI * n1 * dNm * cosTheta1) / lambdaNm;
vec2 expI = vec2(cos(delta), sin(delta));
// Airy summation for s-pol
vec2 r12sE = cMul(r12s, expI);
vec2 rs = cDiv(vec2(r01s, 0.0) + r12sE, vec2(1.0, 0.0) + r01s * r12sE);
// Airy summation for p-pol
vec2 r12pE = cMul(r12p, expI);
vec2 rp = cDiv(vec2(r01p, 0.0) + r12pE, vec2(1.0, 0.0) + r01p * r12pE);
return 0.5 * (dot(rs, rs) + dot(rp, rp));
}
// 6-band spectral integration of TiO2 thin-film interference -> linear sRGB reflectance
vec3 evalTiO2Interference(float dNm, float cosTheta) {
// 6 wavelengths spanning visible spectrum (nm)
// TiO2 anodic dispersion n1(lambda), Ti substrate n2(lambda), k2(lambda)
float r640 = airyReflectance(640.0, dNm, cosTheta, 2.34, 2.72, 3.80);
float r595 = airyReflectance(595.0, dNm, cosTheta, 2.37, 2.60, 3.58);
float r550 = airyReflectance(550.0, dNm, cosTheta, 2.41, 2.48, 3.36);
float r510 = airyReflectance(510.0, dNm, cosTheta, 2.45, 2.38, 3.18);
float r475 = airyReflectance(475.0, dNm, cosTheta, 2.50, 2.29, 3.04);
float r445 = airyReflectance(445.0, dNm, cosTheta, 2.56, 2.22, 2.94);
// Smooth CIE-inspired spectral-to-linear-sRGB weights (normalized so flat 1.0 -> white)
vec3 col =
r640 * vec3(0.58, 0.04, 0.00) +
r595 * vec3(0.42, 0.24, 0.00) +
r550 * vec3(0.08, 0.46, 0.02) +
r510 * vec3(-0.08, 0.28, 0.16) +
r475 * vec3(0.00, 0.04, 0.46) +
r445 * vec3(0.00, -0.06, 0.36);
// Clamp tiny negative gamut excursions and boost subtle physical chroma clarity
col = max(col, vec3(0.035));
// Mode 1: pure neutral metallic grayscale (strip interference chroma)
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(mix(0.42, 0.78, lum));
}
// Preserve metallic Titanium reflectivity envelope
return clamp(col, vec3(0.035), vec3(0.96));
}
// Compute TiO2 oxide thickness (nm) for a point on the coupon
// 5 voltage zones at uOpen = 0:
// Step 0: 15V Bronze (~28 nm)
// Step 1: 25V Royal Blue (~53 nm)
// Step 2: 55V Gold (~120 nm)
// Step 3: 75V Magenta-Violet(~149 nm)
// Step 4: 95V Teal-Green (~186 nm)
// uOpen (0..1) sweeps anodizing voltage / oxide thickness smoothly across the coupon.
float computeOxideThickness(vec3 p) {
// Smooth transitions at step risers so vertical walls show slight anodizing meniscus
float u = (p.x + 1.30) / 0.52;
float idxF = clamp(u, 0.0, 4.999);
int i0 = int(floor(idxF));
float f = fract(idxF);
float dTable[5];
dTable[0] = 28.0; // 15V bronze
dTable[1] = 53.0; // 25V royal blue
dTable[2] = 120.0; // 55V gold
dTable[3] = 149.0; // 75V magenta-violet
dTable[4] = 186.0; // 95V teal-green
float dBase = dTable[i0];
// Subtle transition right at step riser edge (2% of step width)
if (i0 < 4 && f > 0.96) {
float t = smoothstep(0.96, 1.0, f);
dBase = mix(dTable[i0], dTable[i0 + 1], t);
} else if (i0 > 0 && f < 0.04) {
float t = smoothstep(0.04, 0.0, f);
dBase = mix(dTable[i0], dTable[i0 - 1], t);
}
// Subtle natural anodic thickness micro-variation (+/- 2.2 nm) from electrolyte bath flow
float bathVar = (fbm2(p.xz * 3.2 + vec2(1.7, -2.3)) - 0.5) * 4.2;
// Slight edge current-density thickening on sharp chamfers (+3 nm)
float edgeBoost = smoothstep(0.55, 0.66, abs(p.z)) * 3.5;
// uOpen sweeps anodizing voltage across the coupon (+0..+120 nm)
float sweep = uOpen * 120.0;
return max(12.0, dBase + sweep + bathVar + edgeBoost);
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base dark cyclorama studio ambient (gives strong negative-fill contrast on metal)
vec3 col = mix(vec3(0.035, 0.038, 0.044), vec3(0.065, 0.072, 0.084), smoothstep(-0.2, 0.8, y));
if (lightMode == 1) {
// Mode 1: Pure neutral grayscale studio
col = mix(vec3(0.045), vec3(0.085), smoothstep(-0.2, 0.8, y));
// Overhead main neutral softbox
float sbTop = smoothstep(0.62, 0.90, dot(d, normalize(vec3(0.15, 0.95, 0.25))));
// Key softbox right-front
float sbKey = smoothstep(0.68, 0.92, dot(d, normalize(vec3(0.65, 0.58, 0.48))));
// Cool-neutral strip rim left-back
float sbRim = smoothstep(0.76, 0.95, dot(d, normalize(vec3(-0.72, 0.42, -0.55))));
// Negative-fill dark flag right-side
float flag1 = smoothstep(0.70, 0.88, dot(d, normalize(vec3(0.92, 0.18, -0.35))));
col += vec3(1.85) * sbTop;
col += vec3(2.40) * sbKey;
col += vec3(1.50) * sbRim;
col *= (1.0 - 0.72 * flag1);
return col;
}
if (lightMode == 2) {
// Mode 2: Grazing raking light
col = vec3(0.025, 0.028, 0.034);
float sbRaking = smoothstep(0.74, 0.96, dot(d, normalize(vec3(0.92, 0.24, 0.30))));
float sbCounter = smoothstep(0.72, 0.94, dot(d, normalize(vec3(-0.88, 0.26, -0.38))));
float sbTopStrip = smoothstep(0.84, 0.97, dot(d, normalize(vec3(0.0, 0.98, 0.15))));
col += vec3(3.40, 3.15, 2.80) * sbRaking;
col += vec3(1.25, 1.45, 1.75) * sbCounter;
col += vec3(0.85, 0.90, 0.98) * sbTopStrip;
return col;
}
if (lightMode == 3) {
// Mode 3: Backlit studio rim
col = vec3(0.028, 0.032, 0.040);
float sbBack = smoothstep(0.55, 0.90, dot(d, normalize(vec3(-0.18, 0.45, -0.87))));
float sbRimL = smoothstep(0.74, 0.94, dot(d, normalize(vec3(-0.75, 0.50, -0.42))));
float sbFrontFill = smoothstep(0.65, 0.92, dot(d, normalize(vec3(0.35, 0.52, 0.78))));
col += vec3(3.20, 3.25, 3.45) * sbBack;
col += vec3(2.10, 2.25, 2.50) * sbRimL;
col += vec3(0.62, 0.64, 0.68) * sbFrontFill;
return col;
}
// Mode 0: Hero aerospace product studio lighting
// Large overhead strip softbox (crisp specular bands across stepped planes)
vec3 topDir = normalize(vec3(-0.12, 0.94, 0.32));
float sbTop = smoothstep(0.68, 0.91, dot(d, topDir));
// Warm-white key softbox (front-right-high)
vec3 keyDir = normalize(vec3(0.62, 0.56, 0.54));
float sbKey = smoothstep(0.72, 0.94, dot(d, keyDir));
// Cool rim strip softbox (back-left)
vec3 rimDir = normalize(vec3(-0.70, 0.44, -0.56));
float sbRim = smoothstep(0.76, 0.95, dot(d, rimDir));
// Crisp horizon accent line for chamfer glints
float horizonStrip = exp(-abs(y - 0.16) * 14.0) * smoothstep(-0.2, 0.8, -d.z) * 0.45;
// Dark negative-fill flags to sculpt metallic contrast
float negFlagR = smoothstep(0.65, 0.86, dot(d, normalize(vec3(0.94, 0.16, -0.30))));
float negFlagL = smoothstep(0.68, 0.88, dot(d, normalize(vec3(-0.88, 0.14, 0.44))));
col += vec3(1.95, 1.98, 2.08) * sbTop;
col += vec3(2.65, 2.52, 2.34) * sbKey;
col += vec3(1.45, 1.65, 2.05) * sbRim;
col += vec3(0.95, 1.02, 1.15) * horizonStrip;
col *= (1.0 - 0.78 * negFlagR);
col *= (1.0 - 0.65 * negFlagL);
return col;
}
// ============================================================================
// GGX Microfacet BRDF with Anisotropic Satin & Thin-Film Fresnel
// ============================================================================
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float d = (ToH * ToH) / (ax * ax) + (BoH * BoH) / (ay * ay) + NoH * NoH;
return 1.0 / max(PI * ax * ay * d * d, 1e-5);
}
float G_SmithGGX(float NoV, float NoL, float alpha) {
float a2 = alpha * alpha;
float gv = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2);
float gl = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// Micro-blasted metallic sparkle glints (high-frequency crystalline Ti grain facets)
float microSparkle(vec3 p, vec3 N, vec3 V, vec3 L, float scale) {
vec2 uv = p.xz * scale + p.y * scale * 0.37;
vec2 cell = floor(uv);
vec2 f = fract(uv);
vec3 rnd = hash32(cell);
// Random micro-facet normal tilted within bead-blast cone
vec2 tilt = (rnd.xy - 0.5) * 0.52;
vec3 T = normalize(cross(abs(N.y) > 0.9 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0), N));
vec3 B = cross(N, T);
vec3 mN = normalize(N + T * tilt.x + B * tilt.y);
vec3 H = normalize(V + L);
float spec = pow(max(dot(mN, H), 0.0), 90.0);
// Soft circular grain mask so sparkles never look blocky
float grainMask = smoothstep(0.55, 0.10, length(f - 0.5));
return spec * grainMask * (0.45 + 0.55 * rnd.z);
}
// ============================================================================
// Surface Micro-Detail & Finish Classification
// ============================================================================
// Returns: x = roughness, y = anisotropy (-1..1), z = isBeadBlasted (0..1), w = chamferMask
vec4 sampleCouponFinish(vec3 p, vec3 N, out vec3 N_perturbed) {
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float cx = -1.04 + float(zi) * 0.52;
// Classify front bead-blasted strip (z > 0.02) vs back satin-machined strip (z < 0.02)
float isBlasted = smoothstep(0.014, 0.028, p.z);
// Detect angled chamfered edges & bevels (bright machined specular)
float chamferMask = smoothstep(0.35, 0.78, 1.0 - abs(N.y)) * smoothstep(0.15, 0.55, abs(N.y));
// Also keep vertical step risers satin-machined
float isSideWall = smoothstep(0.45, 0.15, abs(N.y));
// Inside front pocket floor: fine satin concentric milled finish vs blasted rim
vec2 pf = p.xz - vec2(cx, 0.34);
float inFrontPocketFloor = smoothstep(topY - 0.042, topY - 0.052, p.y) * smoothstep(0.75, 0.95, N.y);
float blastWeight = isBlasted * (1.0 - chamferMask) * (1.0 - isSideWall) * (1.0 - inFrontPocketFloor * 0.85);
// Construct tangent frame
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
// 1. Satin CNC-machined micro-grooves (parallel toolpath + subtle fly-cutter arcs)
float toolGrooveX = sin(p.z * 320.0 + fbm2(p.xz * 18.0) * 1.4) * 0.5;
float flyArc = sin(length((p.xz - vec2(cx, -0.33)) * vec2(1.0, 1.0)) * 240.0) * 0.35;
float millLine = mix(toolGrooveX, flyArc, smoothstep(0.24, 0.14, length(p.xz - vec2(cx, -0.33))));
// Side walls have fine horizontal end-mill passes
float sideMill = sin(p.y * 380.0) * 0.45;
float satinBump = mix(millLine, sideMill, isSideWall);
// 2. Glass-bead-blasted isotropic micro-dimple perturbation
vec2 bp = p.xz * 165.0 + p.y * 90.0;
float b1 = valueNoise(bp) - 0.5;
float b2 = valueNoise(bp * 2.15 + 17.3) - 0.5;
float bx = (valueNoise(bp + vec2(0.35, 0.0)) - valueNoise(bp - vec2(0.35, 0.0)));
float bz = (valueNoise(bp + vec2(0.0, 0.35)) - valueNoise(bp - vec2(0.0, 0.35)));
vec3 nSatin = normalize(N + B * (satinBump * 0.022) + T * ((valueNoise(p.xz * 95.0) - 0.5) * 0.010));
vec3 nBlast = normalize(N + T * (bx * 0.14 + b2 * 0.05) + B * (bz * 0.14 + b1 * 0.05));
N_perturbed = normalize(mix(nSatin, nBlast, blastWeight));
// Roughness & anisotropy
float roughSatin = 0.155 + 0.025 * valueNoise(p.xz * 40.0);
float roughBlast = 0.355 + 0.045 * valueNoise(p.xz * 90.0);
float rough = mix(roughSatin, roughBlast, blastWeight);
// Chamfers are freshly cut and slightly glossier
rough = mix(rough, 0.115, chamferMask * 0.85);
float aniso = mix(0.58, 0.0, blastWeight);
aniso = mix(aniso, 0.15, chamferMask);
return vec4(rough, aniso, blastWeight, chamferMask);
}
// ============================================================================
// Lighting Setup (4 Light Modes)
// ============================================================================
void getLights(int lightMode, out vec3 lDir[3], out vec3 lCol[3]) {
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
lDir[0] = normalize(vec3(0.58, 0.68, 0.45));
lCol[0] = vec3(2.35);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.30);
lDir[2] = normalize(vec3(-0.15, 0.92, 0.36));
lCol[2] = vec3(1.10);
} else if (lightMode == 2) {
// Grazing raking light
lDir[0] = normalize(vec3(0.92, 0.25, 0.30));
lCol[0] = vec3(3.10, 2.90, 2.65);
lDir[1] = normalize(vec3(-0.88, 0.26, -0.40));
lCol[1] = vec3(1.15, 1.32, 1.62);
lDir[2] = normalize(vec3(0.05, 0.86, 0.50));
lCol[2] = vec3(0.55, 0.58, 0.64);
} else if (lightMode == 3) {
// Backlit studio rim
lDir[0] = normalize(vec3(-0.22, 0.46, -0.86));
lCol[0] = vec3(3.05, 3.12, 3.32);
lDir[1] = normalize(vec3(-0.74, 0.52, -0.42));
lCol[1] = vec3(1.85, 1.98, 2.22);
lDir[2] = normalize(vec3(0.38, 0.58, 0.72));
lCol[2] = vec3(0.78, 0.80, 0.84);
} else {
// Mode 0: Hero studio lighting (warm-white key, cool rim, crisp overhead)
lDir[0] = normalize(vec3(0.58, 0.62, 0.52));
lCol[0] = vec3(2.55, 2.44, 2.30);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.38, 1.56, 1.92);
lDir[2] = normalize(vec3(-0.12, 0.94, 0.32));
lCol[2] = vec3(1.18, 1.20, 1.26);
}
}
// ============================================================================
// Shade Hero (Anodized Titanium Coupon)
// ============================================================================
vec3 shadeHero(vec3 p, vec3 Ngeom, vec3 V) {
vec3 N;
vec4 fin = sampleCouponFinish(p, Ngeom, N);
float rough = fin.x;
float aniso = fin.y;
float blastWeight = fin.z;
float dOxide = computeOxideThickness(p);
float ao = calcAO(p, Ngeom);
// Tangent frame for anisotropic satin highlights
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
float ax = max(0.04, rough * (1.0 + aniso * 0.65));
float ay = max(0.04, rough * (1.0 - aniso * 0.55));
float NoV = max(dot(N, V), 0.02);
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 color = vec3(0.0);
// Direct studio lights
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + Ngeom * 0.014, L, 6.0, 14.0)
: (i == 1) ? softShadow(p + Ngeom * 0.014, L, 6.0, 10.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float ToH = dot(T, H);
float BoH = dot(B, H);
// View-angle & microfacet-angle TiO2 thin-film Airy interference reflectance
vec3 F_film = evalTiO2Interference(dOxide, VoH);
float D = D_GGX_Aniso(NoH, ToH, BoH, ax, ay);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 spec = D * G * F_film;
// Micro-blasted iridescent sparkle glints on bead-blasted strip
float spk = 0.0;
if (blastWeight > 0.05) {
float s1 = microSparkle(p, Ngeom, V, L, 210.0);
float s2 = microSparkle(p + vec3(0.13, 0.0, 0.27), Ngeom, V, L, 340.0);
spk = (s1 * 0.75 + s2 * 0.55) * blastWeight;
}
// Subtle multi-scatter metallic lobe for bead-blasted Ti (keeps matte strip luminous & vivid)
vec3 multiScatter = F_film * F_film * (0.14 + 0.22 * blastWeight) / PI;
color += (spec + multiScatter + F_film * spk * 1.45) * lCol[i] * NoL * sh;
}
// Studio IBL environment reflection with view-angle thin-film iridescence
vec3 R = reflect(-V, N);
vec3 F_env = evalTiO2Interference(dOxide, NoV);
// Multi-lobe filtered studio environment lookup (sharp + broad roughness blend)
vec3 envSharp = evalStudioEnv(R, uLightMode);
vec3 envMid = evalStudioEnv(normalize(mix(R, N, 0.35)), uLightMode);
vec3 envBroad = evalStudioEnv(normalize(mix(R, N, 0.75)), uLightMode);
vec3 envSpec = mix(envSharp, mix(envMid, envBroad, smoothstep(0.18, 0.45, rough)), smoothstep(0.08, 0.38, rough));
// Ground bounce tint (subtle dark bench reflection for downward-facing bevels)
float downFade = smoothstep(-0.15, 0.15, R.y);
envSpec = mix(vec3(0.04, 0.042, 0.048), envSpec, 0.35 + 0.65 * downFade);
color += envSpec * F_env * (0.82 + 0.16 * (1.0 - rough)) * ao;
// Lift deepest pocket ambient slightly so metallic crevices stay readable
color += F_env * 0.042 * (0.45 + 0.55 * ao);
// Subtle secondary iridescent edge sheen at grazing view angles
float fresnelRim = pow(1.0 - NoV, 4.0);
vec3 F_grazing = evalTiO2Interference(dOxide, 0.18);
color += F_grazing * fresnelRim * 0.13 * ao;
return color;
}
// ============================================================================
// Shade Grounded Studio Bench
// ============================================================================
vec3 shadeBench(vec3 p, vec3 V) {
// Subtle dark anodized slate / machined granite inspection bench
vec2 gUV = p.xz * 24.0;
float grain = fbm2(gUV) - 0.5;
float fine = valueNoise(p.xz * 140.0) - 0.5;
vec3 N = normalize(vec3(grain * 0.016 + fine * 0.008, 1.0, (fbm2(gUV + 9.1) - 0.5) * 0.016));
float dist = length(p.xz);
// Subtle radial falloff into studio cyclorama (darkened ~0.68x for rich metallic contrast)
float benchFade = smoothstep(6.5, 1.8, dist);
vec3 baseCol = mix(vec3(0.028, 0.030, 0.035), vec3(0.046, 0.049, 0.055), benchFade);
if (uLightMode == 1) {
baseCol = vec3(dot(baseCol, vec3(0.2126, 0.7152, 0.0722)));
}
baseCol *= (0.94 + 0.12 * grain);
// Subtle calibrated inspection bench hairline grid (very faint, museum feel)
vec2 grid = abs(fract(p.xz * 2.0 - 0.5) - 0.5) / 0.015;
float line = 1.0 - min(min(grid.x, grid.y), 1.0);
baseCol += vec3(0.007) * line * smoothstep(3.8, 1.2, dist);
float ao = calcAO(p, vec3(0.0, 1.0, 0.0));
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 col = vec3(0.0);
float NoV = max(dot(N, V), 0.02);
float rough = 0.36 + 0.04 * grain;
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 11.0)
: (i == 1) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 8.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float F = 0.04 + 0.96 * pow(1.0 - VoH, 5.0);
float D = D_GGX_Aniso(NoH, 0.0, 0.0, rough, rough);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 diff = baseCol * (1.0 - F) / PI;
vec3 spec = vec3(D * G * F) * 0.72;
col += (diff + spec) * lCol[i] * NoL * sh;
}
// Soft studio reflection + subtle colored bounce from anodized coupon near base
vec3 R = reflect(-V, N);
float F_env = 0.04 + 0.96 * pow(1.0 - NoV, 5.0);
vec3 envRefl = evalStudioEnv(normalize(mix(R, N, 0.38)), uLightMode);
col += envRefl * F_env * 0.22 * ao;
col += baseCol * evalStudioEnv(vec3(0.0, 1.0, 0.0), uLightMode) * 0.16 * ao;
// Subtle colorful iridescent reflection of the coupon onto the nearby bench
float dHero = mapHero(p);
if (dHero < 0.75) {
float bounceMask = smoothstep(0.75, 0.02, dHero) * (0.25 + 0.75 * F_env);
float dOx = computeOxideThickness(vec3(clamp(p.x, -1.28, 1.28), 0.10, clamp(p.z, -0.64, 0.64)));
vec3 bounceCol = evalTiO2Interference(dOx, 0.45);
col += bounceCol * bounceMask * 0.11 * ao;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping (Calibrated: 0% Black Crush, 0% Hard Clip)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// ============================================================================
// Camera Rig & Main Raymarcher
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / uResolution.y;
// Camera modes: 0 = Hero 3/4 studio framing, 1 = ~2.5x Macro inspection
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x macro focused on gold/blue/magenta stepped chamfer + satin/blasted seam
ro = vec3(0.32, 0.92, 1.18);
ta = vec3(0.06, 0.28, 0.04);
fov = 1.65;
} else {
// Hero 3/4 framing showing all 5 stepped voltage zones cleanly
ro = vec3(-0.72, 1.88, 2.75);
ta = vec3(0.02, 0.24, -0.02);
fov = 1.48;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + fov * cw);
// Raymarch scene (hero + bench y=0)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0005) {
hit = true;
break;
}
t += d * 0.92;
if (t > tMax) break;
}
// Studio cyclorama background (darkened ~0.7x so coupon pops with richer metallic contrast)
vec3 bg = evalStudioEnv(rd, uLightMode) * 0.18;
// Soft vertical studio backdrop gradient
vec3 col = mix(vec3(0.038, 0.041, 0.048), vec3(0.066, 0.071, 0.080), smoothstep(-0.25, 0.55, uv.y));
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
col = mix(col, bg, 0.32);
if (hit) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-Bug Rule #2: exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
vec3 benchCol = shadeBench(p, -rd);
// Smooth atmospheric blend into studio horizon
float fog = smoothstep(5.0, 12.5, t);
col = mix(benchCol, col, fog);
} else {
vec3 N = calcNormalHero(p);
col = shadeHero(p, N, -rd);
}
}
// Pure neutral luminance grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Calibrated exposure & ACES filmic tone mapping (keeps hero lum in [14, 242], 0% black/clipped)
col = max(col * 0.90, vec3(0.007));
col = acesToneMap(col) * 0.89;
// Subtle studio lens vignette (keeps corners soft without crushing below lumMin=12)
float vig = 1.0 - 0.11 * dot(uv, uv);
col *= vig;
// Linear -> sRGB gamma
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2));
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;
// Anodized Titanium Finish — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/04_anodized_titanium_interference_finish.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;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// ============================================================================
// Hashing & Procedural Micro-Surface 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);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float s = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
for (int i = 0; i < 4; i++) {
s += a * valueNoise(p);
p = m * p * 2.04 + 13.7;
a *= 0.5;
}
return s;
}
// ============================================================================
// SDF Primitives & Boolean Ops
// ============================================================================
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - (b - r);
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
float sdChamferBox(vec3 p, vec3 b, float ch) {
vec3 q = abs(p) - b;
float box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
// 45-degree chamfer on top/side edges
float c1 = (q.x + q.y + ch) * 0.70710678;
float c2 = (q.z + q.y + ch) * 0.70710678;
float c3 = (q.x + q.z + ch) * 0.70710678;
return max(box, max(c1, max(c2, c3)));
}
float sdCylinderY(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 smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
// ============================================================================
// Coupon Geometry Parameters (5 Stepped Voltage Zones)
// ============================================================================
// Total footprint: X in [-1.30, 1.30], Z in [-0.66, 0.66], grounded at Y = 0.0
// 5 steps of width 0.52 each: centers at -1.04, -0.52, 0.0, +0.52, +1.04
// Step heights: 0.16, 0.23, 0.30, 0.37, 0.44
float stepHeightForIdx(int idx) {
return 0.16 + float(idx) * 0.07;
}
int zoneIndexFromX(float x) {
float u = (x + 1.30) / 0.52;
return clamp(int(floor(u)), 0, 4);
}
// Hero SDF: Stepped aerospace machined titanium test coupon
float mapHero(vec3 p) {
// Quick bounding box rejection
float dBound = sdBox(p - vec3(0.0, 0.24, 0.0), vec3(1.42, 0.34, 0.78));
if (dBound > 0.25) return dBound;
// 5 stepped aerospace terraces fused cleanly with crisp machined chamfers
float dSteps = 1e5;
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Overlap adjacent steps slightly in X so internal seams are solid
float halfW = (i == 0 || i == 4) ? 0.262 : 0.266;
vec3 q = p - vec3(cx, h * 0.5, 0.0);
// Chamfered outer & top edges (0.016 bevel)
float dStep = sdChamferBox(q, vec3(halfW, h * 0.5, 0.66), 0.015);
dSteps = min(dSteps, dStep);
}
// Precision outer perimeter fillet trim so corners look CNC-milled
float dOuter = sdRoundBox(p - vec3(0.0, 0.30, 0.0), vec3(1.30, 0.34, 0.66), 0.028);
float d = max(dSteps, dOuter);
// Subtle undercut relief along bottom edge (0.008 mm datum foot chamfer)
float dBottomBevel = -(p.y) + max(abs(p.x) - 1.288, abs(p.z) - 0.648) * 0.70;
d = max(d, min(dBottomBevel, -p.y + 0.012));
// Ensure grounded cleanly at y = 0.0
d = max(d, -p.y);
// Strip divider V-groove along Z = 0.02 separating satin-machined (back) and bead-blasted (front)
// Runs across the top face of every step
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float zDiv = abs(p.z - 0.02);
float vGroove = max((topY - p.y) - 0.012, zDiv - (0.013 - max(topY - p.y, 0.0) * 0.95));
// Carve clean 90-degree V-groove along Z = 0.02
float dV = max(abs(p.z - 0.02) - 0.011, (topY - 0.010) - p.y);
float dGroove = max((abs(p.z - 0.02) + (topY - p.y)) * 0.70710678 - 0.0095, -((topY - p.y) + 0.02));
// Keep groove shallow and clean
float grooveCut = max(abs(p.z - 0.02) - (0.010 - clamp(topY - p.y, 0.0, 0.010)), (topY - 0.010) - p.y);
d = smax(d, -grooveCut, 0.0025);
// Chamfered aerospace weight-reduction pockets in each of the 5 steps
// Front pocket (in bead-blasted zone, z centered at +0.34)
// Back shallow inspection pocket (in satin-machined zone, z centered at -0.33)
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Front deep chamfered pocket
vec3 pf = p - vec3(cx, h, 0.34);
float pDepthF = 0.056;
// Main pocket cavity with corner radius
float cavF = sdRoundBox(pf - vec3(0.0, -pDepthF * 0.5 + 0.04, 0.0), vec3(0.165, pDepthF * 0.5 + 0.04, 0.205), 0.032);
// 45-deg chamfered rim flare near top surface (pf.y near 0)
float flareF = sdRoundBox(pf - vec3(0.0, 0.018, 0.0), vec3(0.184, 0.036, 0.224), 0.038);
float bevelF = (flareF + (-pf.y) * 0.75) * 0.70710678;
float pocketF = min(cavF, max(flareF, -pf.y - 0.022));
// Clean 45-degree top chamfer on pocket lip
vec2 qf2 = abs(pf.xz) - vec2(0.165 - 0.032, 0.205 - 0.032);
float d2dF = length(max(qf2, 0.0)) + min(max(qf2.x, qf2.y), 0.0) - 0.032;
float pocketLipF = max(d2dF - max(pf.y + 0.018, 0.0), -pf.y - pDepthF);
d = smax(d, -pocketLipF, 0.003);
// Back shallower satin-machined telemetry slot / pocket
vec3 pb = p - vec3(cx, h, -0.33);
float pDepthB = 0.026;
vec2 qb2 = abs(pb.xz) - vec2(0.155 - 0.026, 0.185 - 0.026);
float d2dB = length(max(qb2, 0.0)) + min(max(qb2.x, qb2.y), 0.0) - 0.026;
float pocketLipB = max(d2dB - max(pb.y + 0.012, 0.0), -pb.y - pDepthB);
d = smax(d, -pocketLipB, 0.0025);
}
// Aerospace datum tooling bores (countersunk) on the 2 outer ends of the satin strip
for (int j = 0; j < 2; j++) {
float bx = (j == 0) ? -1.175 : 1.175;
float bh = (j == 0) ? stepHeightForIdx(0) : stepHeightForIdx(4);
vec3 pb = p - vec3(bx, bh, -0.545);
float rCyl = length(pb.xz);
float bore = max(rCyl - 0.038, -pb.y - 0.14);
float csk = max(rCyl - (0.038 + max(pb.y + 0.016, 0.0) * 1.0), -pb.y - 0.03);
d = smax(d, -min(bore, csk), 0.002);
}
return d;
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormalHero(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.00075;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
// ============================================================================
// Clean Hero-Only Soft Shadow (Zero-Bug Rule #3: no ph!)
// ============================================================================
float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
t += max(h, 0.015);
if (res < 0.004 || 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.018 * float(i) * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.1 * occ, 0.28, 1.0);
}
// ============================================================================
// Physical TiO2 Thin-Film Airy Optical Interference on Titanium Substrate
// ============================================================================
// Complex arithmetic helpers
vec2 cMul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
vec2 cDiv(vec2 a, vec2 b) { return vec2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / max(dot(b, b), 1e-6); }
// Single-wavelength Airy thin-film reflectance for Air -> TiO2(n1, d) -> Ti(n2 + i*k2)
float airyReflectance(float lambdaNm, float dNm, float cosTheta0, float n1, float n2, float k2) {
float sin2Theta0 = max(0.0, 1.0 - cosTheta0 * cosTheta0);
float cosTheta1 = sqrt(max(0.001, 1.0 - sin2Theta0 / (n1 * n1)));
// Air -> TiO2 Fresnel (s and p polarizations)
float r01s = (cosTheta0 - n1 * cosTheta1) / (cosTheta0 + n1 * cosTheta1);
float r01p = (n1 * cosTheta0 - cosTheta1) / (n1 * cosTheta0 + cosTheta1);
// TiO2 -> Ti complex Fresnel (effective s and p)
vec2 nTi = vec2(n2, k2);
vec2 nFilmS = vec2(n1 * cosTheta1, 0.0);
vec2 nFilmP = vec2(n1 / cosTheta1, 0.0);
// Approximate transmitted angle in dense metal (~cosTheta2 near 1)
vec2 r12s = cDiv(nFilmS - nTi, nFilmS + nTi);
vec2 r12p = cDiv(nTi - nFilmP, nTi + nFilmP);
// Optical phase thickness across TiO2 film
float delta = (4.0 * PI * n1 * dNm * cosTheta1) / lambdaNm;
vec2 expI = vec2(cos(delta), sin(delta));
// Airy summation for s-pol
vec2 r12sE = cMul(r12s, expI);
vec2 rs = cDiv(vec2(r01s, 0.0) + r12sE, vec2(1.0, 0.0) + r01s * r12sE);
// Airy summation for p-pol
vec2 r12pE = cMul(r12p, expI);
vec2 rp = cDiv(vec2(r01p, 0.0) + r12pE, vec2(1.0, 0.0) + r01p * r12pE);
return 0.5 * (dot(rs, rs) + dot(rp, rp));
}
// 6-band spectral integration of TiO2 thin-film interference -> linear sRGB reflectance
vec3 evalTiO2Interference(float dNm, float cosTheta) {
// 6 wavelengths spanning visible spectrum (nm)
// TiO2 anodic dispersion n1(lambda), Ti substrate n2(lambda), k2(lambda)
float r640 = airyReflectance(640.0, dNm, cosTheta, 2.34, 2.72, 3.80);
float r595 = airyReflectance(595.0, dNm, cosTheta, 2.37, 2.60, 3.58);
float r550 = airyReflectance(550.0, dNm, cosTheta, 2.41, 2.48, 3.36);
float r510 = airyReflectance(510.0, dNm, cosTheta, 2.45, 2.38, 3.18);
float r475 = airyReflectance(475.0, dNm, cosTheta, 2.50, 2.29, 3.04);
float r445 = airyReflectance(445.0, dNm, cosTheta, 2.56, 2.22, 2.94);
// Smooth CIE-inspired spectral-to-linear-sRGB weights (normalized so flat 1.0 -> white)
vec3 col =
r640 * vec3(0.58, 0.04, 0.00) +
r595 * vec3(0.42, 0.24, 0.00) +
r550 * vec3(0.08, 0.46, 0.02) +
r510 * vec3(-0.08, 0.28, 0.16) +
r475 * vec3(0.00, 0.04, 0.46) +
r445 * vec3(0.00, -0.06, 0.36);
// Clamp tiny negative gamut excursions and boost subtle physical chroma clarity
col = max(col, vec3(0.035));
// Mode 1: pure neutral metallic grayscale (strip interference chroma)
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(mix(0.42, 0.78, lum));
}
// Preserve metallic Titanium reflectivity envelope
return clamp(col, vec3(0.035), vec3(0.96));
}
// Compute TiO2 oxide thickness (nm) for a point on the coupon
// 5 voltage zones at uOpen = 0:
// Step 0: 15V Bronze (~28 nm)
// Step 1: 25V Royal Blue (~53 nm)
// Step 2: 55V Gold (~120 nm)
// Step 3: 75V Magenta-Violet(~149 nm)
// Step 4: 95V Teal-Green (~186 nm)
// uOpen (0..1) sweeps anodizing voltage / oxide thickness smoothly across the coupon.
float computeOxideThickness(vec3 p) {
// Smooth transitions at step risers so vertical walls show slight anodizing meniscus
float u = (p.x + 1.30) / 0.52;
float idxF = clamp(u, 0.0, 4.999);
int i0 = int(floor(idxF));
float f = fract(idxF);
float dTable[5];
dTable[0] = 28.0; // 15V bronze
dTable[1] = 53.0; // 25V royal blue
dTable[2] = 120.0; // 55V gold
dTable[3] = 149.0; // 75V magenta-violet
dTable[4] = 186.0; // 95V teal-green
float dBase = dTable[i0];
// Subtle transition right at step riser edge (2% of step width)
if (i0 < 4 && f > 0.96) {
float t = smoothstep(0.96, 1.0, f);
dBase = mix(dTable[i0], dTable[i0 + 1], t);
} else if (i0 > 0 && f < 0.04) {
float t = smoothstep(0.04, 0.0, f);
dBase = mix(dTable[i0], dTable[i0 - 1], t);
}
// Subtle natural anodic thickness micro-variation (+/- 2.2 nm) from electrolyte bath flow
float bathVar = (fbm2(p.xz * 3.2 + vec2(1.7, -2.3)) - 0.5) * 4.2;
// Slight edge current-density thickening on sharp chamfers (+3 nm)
float edgeBoost = smoothstep(0.55, 0.66, abs(p.z)) * 3.5;
// uOpen sweeps anodizing voltage across the coupon (+0..+120 nm)
float sweep = uOpen * 120.0;
return max(12.0, dBase + sweep + bathVar + edgeBoost);
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base dark cyclorama studio ambient (gives strong negative-fill contrast on metal)
vec3 col = mix(vec3(0.035, 0.038, 0.044), vec3(0.065, 0.072, 0.084), smoothstep(-0.2, 0.8, y));
if (lightMode == 1) {
// Mode 1: Pure neutral grayscale studio
col = mix(vec3(0.045), vec3(0.085), smoothstep(-0.2, 0.8, y));
// Overhead main neutral softbox
float sbTop = smoothstep(0.62, 0.90, dot(d, normalize(vec3(0.15, 0.95, 0.25))));
// Key softbox right-front
float sbKey = smoothstep(0.68, 0.92, dot(d, normalize(vec3(0.65, 0.58, 0.48))));
// Cool-neutral strip rim left-back
float sbRim = smoothstep(0.76, 0.95, dot(d, normalize(vec3(-0.72, 0.42, -0.55))));
// Negative-fill dark flag right-side
float flag1 = smoothstep(0.70, 0.88, dot(d, normalize(vec3(0.92, 0.18, -0.35))));
col += vec3(1.85) * sbTop;
col += vec3(2.40) * sbKey;
col += vec3(1.50) * sbRim;
col *= (1.0 - 0.72 * flag1);
return col;
}
if (lightMode == 2) {
// Mode 2: Grazing raking light
col = vec3(0.025, 0.028, 0.034);
float sbRaking = smoothstep(0.74, 0.96, dot(d, normalize(vec3(0.92, 0.24, 0.30))));
float sbCounter = smoothstep(0.72, 0.94, dot(d, normalize(vec3(-0.88, 0.26, -0.38))));
float sbTopStrip = smoothstep(0.84, 0.97, dot(d, normalize(vec3(0.0, 0.98, 0.15))));
col += vec3(3.40, 3.15, 2.80) * sbRaking;
col += vec3(1.25, 1.45, 1.75) * sbCounter;
col += vec3(0.85, 0.90, 0.98) * sbTopStrip;
return col;
}
if (lightMode == 3) {
// Mode 3: Backlit studio rim
col = vec3(0.028, 0.032, 0.040);
float sbBack = smoothstep(0.55, 0.90, dot(d, normalize(vec3(-0.18, 0.45, -0.87))));
float sbRimL = smoothstep(0.74, 0.94, dot(d, normalize(vec3(-0.75, 0.50, -0.42))));
float sbFrontFill = smoothstep(0.65, 0.92, dot(d, normalize(vec3(0.35, 0.52, 0.78))));
col += vec3(3.20, 3.25, 3.45) * sbBack;
col += vec3(2.10, 2.25, 2.50) * sbRimL;
col += vec3(0.62, 0.64, 0.68) * sbFrontFill;
return col;
}
// Mode 0: Hero aerospace product studio lighting
// Large overhead strip softbox (crisp specular bands across stepped planes)
vec3 topDir = normalize(vec3(-0.12, 0.94, 0.32));
float sbTop = smoothstep(0.68, 0.91, dot(d, topDir));
// Warm-white key softbox (front-right-high)
vec3 keyDir = normalize(vec3(0.62, 0.56, 0.54));
float sbKey = smoothstep(0.72, 0.94, dot(d, keyDir));
// Cool rim strip softbox (back-left)
vec3 rimDir = normalize(vec3(-0.70, 0.44, -0.56));
float sbRim = smoothstep(0.76, 0.95, dot(d, rimDir));
// Crisp horizon accent line for chamfer glints
float horizonStrip = exp(-abs(y - 0.16) * 14.0) * smoothstep(-0.2, 0.8, -d.z) * 0.45;
// Dark negative-fill flags to sculpt metallic contrast
float negFlagR = smoothstep(0.65, 0.86, dot(d, normalize(vec3(0.94, 0.16, -0.30))));
float negFlagL = smoothstep(0.68, 0.88, dot(d, normalize(vec3(-0.88, 0.14, 0.44))));
col += vec3(1.95, 1.98, 2.08) * sbTop;
col += vec3(2.65, 2.52, 2.34) * sbKey;
col += vec3(1.45, 1.65, 2.05) * sbRim;
col += vec3(0.95, 1.02, 1.15) * horizonStrip;
col *= (1.0 - 0.78 * negFlagR);
col *= (1.0 - 0.65 * negFlagL);
return col;
}
// ============================================================================
// GGX Microfacet BRDF with Anisotropic Satin & Thin-Film Fresnel
// ============================================================================
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float d = (ToH * ToH) / (ax * ax) + (BoH * BoH) / (ay * ay) + NoH * NoH;
return 1.0 / max(PI * ax * ay * d * d, 1e-5);
}
float G_SmithGGX(float NoV, float NoL, float alpha) {
float a2 = alpha * alpha;
float gv = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2);
float gl = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// Micro-blasted metallic sparkle glints (high-frequency crystalline Ti grain facets)
float microSparkle(vec3 p, vec3 N, vec3 V, vec3 L, float scale) {
vec2 uv = p.xz * scale + p.y * scale * 0.37;
vec2 cell = floor(uv);
vec2 f = fract(uv);
vec3 rnd = hash32(cell);
// Random micro-facet normal tilted within bead-blast cone
vec2 tilt = (rnd.xy - 0.5) * 0.52;
vec3 T = normalize(cross(abs(N.y) > 0.9 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0), N));
vec3 B = cross(N, T);
vec3 mN = normalize(N + T * tilt.x + B * tilt.y);
vec3 H = normalize(V + L);
float spec = pow(max(dot(mN, H), 0.0), 90.0);
// Soft circular grain mask so sparkles never look blocky
float grainMask = smoothstep(0.55, 0.10, length(f - 0.5));
return spec * grainMask * (0.45 + 0.55 * rnd.z);
}
// ============================================================================
// Surface Micro-Detail & Finish Classification
// ============================================================================
// Returns: x = roughness, y = anisotropy (-1..1), z = isBeadBlasted (0..1), w = chamferMask
vec4 sampleCouponFinish(vec3 p, vec3 N, out vec3 N_perturbed) {
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float cx = -1.04 + float(zi) * 0.52;
// Classify front bead-blasted strip (z > 0.02) vs back satin-machined strip (z < 0.02)
float isBlasted = smoothstep(0.014, 0.028, p.z);
// Detect angled chamfered edges & bevels (bright machined specular)
float chamferMask = smoothstep(0.35, 0.78, 1.0 - abs(N.y)) * smoothstep(0.15, 0.55, abs(N.y));
// Also keep vertical step risers satin-machined
float isSideWall = smoothstep(0.45, 0.15, abs(N.y));
// Inside front pocket floor: fine satin concentric milled finish vs blasted rim
vec2 pf = p.xz - vec2(cx, 0.34);
float inFrontPocketFloor = smoothstep(topY - 0.042, topY - 0.052, p.y) * smoothstep(0.75, 0.95, N.y);
float blastWeight = isBlasted * (1.0 - chamferMask) * (1.0 - isSideWall) * (1.0 - inFrontPocketFloor * 0.85);
// Construct tangent frame
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
// 1. Satin CNC-machined micro-grooves (parallel toolpath + subtle fly-cutter arcs)
float toolGrooveX = sin(p.z * 320.0 + fbm2(p.xz * 18.0) * 1.4) * 0.5;
float flyArc = sin(length((p.xz - vec2(cx, -0.33)) * vec2(1.0, 1.0)) * 240.0) * 0.35;
float millLine = mix(toolGrooveX, flyArc, smoothstep(0.24, 0.14, length(p.xz - vec2(cx, -0.33))));
// Side walls have fine horizontal end-mill passes
float sideMill = sin(p.y * 380.0) * 0.45;
float satinBump = mix(millLine, sideMill, isSideWall);
// 2. Glass-bead-blasted isotropic micro-dimple perturbation
vec2 bp = p.xz * 165.0 + p.y * 90.0;
float b1 = valueNoise(bp) - 0.5;
float b2 = valueNoise(bp * 2.15 + 17.3) - 0.5;
float bx = (valueNoise(bp + vec2(0.35, 0.0)) - valueNoise(bp - vec2(0.35, 0.0)));
float bz = (valueNoise(bp + vec2(0.0, 0.35)) - valueNoise(bp - vec2(0.0, 0.35)));
vec3 nSatin = normalize(N + B * (satinBump * 0.022) + T * ((valueNoise(p.xz * 95.0) - 0.5) * 0.010));
vec3 nBlast = normalize(N + T * (bx * 0.14 + b2 * 0.05) + B * (bz * 0.14 + b1 * 0.05));
N_perturbed = normalize(mix(nSatin, nBlast, blastWeight));
// Roughness & anisotropy
float roughSatin = 0.155 + 0.025 * valueNoise(p.xz * 40.0);
float roughBlast = 0.355 + 0.045 * valueNoise(p.xz * 90.0);
float rough = mix(roughSatin, roughBlast, blastWeight);
// Chamfers are freshly cut and slightly glossier
rough = mix(rough, 0.115, chamferMask * 0.85);
float aniso = mix(0.58, 0.0, blastWeight);
aniso = mix(aniso, 0.15, chamferMask);
return vec4(rough, aniso, blastWeight, chamferMask);
}
// ============================================================================
// Lighting Setup (4 Light Modes)
// ============================================================================
void getLights(int lightMode, out vec3 lDir[3], out vec3 lCol[3]) {
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
lDir[0] = normalize(vec3(0.58, 0.68, 0.45));
lCol[0] = vec3(2.35);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.30);
lDir[2] = normalize(vec3(-0.15, 0.92, 0.36));
lCol[2] = vec3(1.10);
} else if (lightMode == 2) {
// Grazing raking light
lDir[0] = normalize(vec3(0.92, 0.25, 0.30));
lCol[0] = vec3(3.10, 2.90, 2.65);
lDir[1] = normalize(vec3(-0.88, 0.26, -0.40));
lCol[1] = vec3(1.15, 1.32, 1.62);
lDir[2] = normalize(vec3(0.05, 0.86, 0.50));
lCol[2] = vec3(0.55, 0.58, 0.64);
} else if (lightMode == 3) {
// Backlit studio rim
lDir[0] = normalize(vec3(-0.22, 0.46, -0.86));
lCol[0] = vec3(3.05, 3.12, 3.32);
lDir[1] = normalize(vec3(-0.74, 0.52, -0.42));
lCol[1] = vec3(1.85, 1.98, 2.22);
lDir[2] = normalize(vec3(0.38, 0.58, 0.72));
lCol[2] = vec3(0.78, 0.80, 0.84);
} else {
// Mode 0: Hero studio lighting (warm-white key, cool rim, crisp overhead)
lDir[0] = normalize(vec3(0.58, 0.62, 0.52));
lCol[0] = vec3(2.55, 2.44, 2.30);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.38, 1.56, 1.92);
lDir[2] = normalize(vec3(-0.12, 0.94, 0.32));
lCol[2] = vec3(1.18, 1.20, 1.26);
}
}
// ============================================================================
// Shade Hero (Anodized Titanium Coupon)
// ============================================================================
vec3 shadeHero(vec3 p, vec3 Ngeom, vec3 V) {
vec3 N;
vec4 fin = sampleCouponFinish(p, Ngeom, N);
float rough = fin.x;
float aniso = fin.y;
float blastWeight = fin.z;
float dOxide = computeOxideThickness(p);
float ao = calcAO(p, Ngeom);
// Tangent frame for anisotropic satin highlights
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
float ax = max(0.04, rough * (1.0 + aniso * 0.65));
float ay = max(0.04, rough * (1.0 - aniso * 0.55));
float NoV = max(dot(N, V), 0.02);
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 color = vec3(0.0);
// Direct studio lights
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + Ngeom * 0.014, L, 6.0, 14.0)
: (i == 1) ? softShadow(p + Ngeom * 0.014, L, 6.0, 10.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float ToH = dot(T, H);
float BoH = dot(B, H);
// View-angle & microfacet-angle TiO2 thin-film Airy interference reflectance
vec3 F_film = evalTiO2Interference(dOxide, VoH);
float D = D_GGX_Aniso(NoH, ToH, BoH, ax, ay);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 spec = D * G * F_film;
// Micro-blasted iridescent sparkle glints on bead-blasted strip
float spk = 0.0;
if (blastWeight > 0.05) {
float s1 = microSparkle(p, Ngeom, V, L, 210.0);
float s2 = microSparkle(p + vec3(0.13, 0.0, 0.27), Ngeom, V, L, 340.0);
spk = (s1 * 0.75 + s2 * 0.55) * blastWeight;
}
// Subtle multi-scatter metallic lobe for bead-blasted Ti (keeps matte strip luminous & vivid)
vec3 multiScatter = F_film * F_film * (0.14 + 0.22 * blastWeight) / PI;
color += (spec + multiScatter + F_film * spk * 1.45) * lCol[i] * NoL * sh;
}
// Studio IBL environment reflection with view-angle thin-film iridescence
vec3 R = reflect(-V, N);
vec3 F_env = evalTiO2Interference(dOxide, NoV);
// Multi-lobe filtered studio environment lookup (sharp + broad roughness blend)
vec3 envSharp = evalStudioEnv(R, uLightMode);
vec3 envMid = evalStudioEnv(normalize(mix(R, N, 0.35)), uLightMode);
vec3 envBroad = evalStudioEnv(normalize(mix(R, N, 0.75)), uLightMode);
vec3 envSpec = mix(envSharp, mix(envMid, envBroad, smoothstep(0.18, 0.45, rough)), smoothstep(0.08, 0.38, rough));
// Ground bounce tint (subtle dark bench reflection for downward-facing bevels)
float downFade = smoothstep(-0.15, 0.15, R.y);
envSpec = mix(vec3(0.04, 0.042, 0.048), envSpec, 0.35 + 0.65 * downFade);
color += envSpec * F_env * (0.82 + 0.16 * (1.0 - rough)) * ao;
// Lift deepest pocket ambient slightly so metallic crevices stay readable
color += F_env * 0.042 * (0.45 + 0.55 * ao);
// Subtle secondary iridescent edge sheen at grazing view angles
float fresnelRim = pow(1.0 - NoV, 4.0);
vec3 F_grazing = evalTiO2Interference(dOxide, 0.18);
color += F_grazing * fresnelRim * 0.13 * ao;
return color;
}
// ============================================================================
// Shade Grounded Studio Bench
// ============================================================================
vec3 shadeBench(vec3 p, vec3 V) {
// Subtle dark anodized slate / machined granite inspection bench
vec2 gUV = p.xz * 24.0;
float grain = fbm2(gUV) - 0.5;
float fine = valueNoise(p.xz * 140.0) - 0.5;
vec3 N = normalize(vec3(grain * 0.016 + fine * 0.008, 1.0, (fbm2(gUV + 9.1) - 0.5) * 0.016));
float dist = length(p.xz);
// Subtle radial falloff into studio cyclorama (darkened ~0.68x for rich metallic contrast)
float benchFade = smoothstep(6.5, 1.8, dist);
vec3 baseCol = mix(vec3(0.028, 0.030, 0.035), vec3(0.046, 0.049, 0.055), benchFade);
if (uLightMode == 1) {
baseCol = vec3(dot(baseCol, vec3(0.2126, 0.7152, 0.0722)));
}
baseCol *= (0.94 + 0.12 * grain);
// Subtle calibrated inspection bench hairline grid (very faint, museum feel)
vec2 grid = abs(fract(p.xz * 2.0 - 0.5) - 0.5) / 0.015;
float line = 1.0 - min(min(grid.x, grid.y), 1.0);
baseCol += vec3(0.007) * line * smoothstep(3.8, 1.2, dist);
float ao = calcAO(p, vec3(0.0, 1.0, 0.0));
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 col = vec3(0.0);
float NoV = max(dot(N, V), 0.02);
float rough = 0.36 + 0.04 * grain;
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 11.0)
: (i == 1) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 8.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float F = 0.04 + 0.96 * pow(1.0 - VoH, 5.0);
float D = D_GGX_Aniso(NoH, 0.0, 0.0, rough, rough);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 diff = baseCol * (1.0 - F) / PI;
vec3 spec = vec3(D * G * F) * 0.72;
col += (diff + spec) * lCol[i] * NoL * sh;
}
// Soft studio reflection + subtle colored bounce from anodized coupon near base
vec3 R = reflect(-V, N);
float F_env = 0.04 + 0.96 * pow(1.0 - NoV, 5.0);
vec3 envRefl = evalStudioEnv(normalize(mix(R, N, 0.38)), uLightMode);
col += envRefl * F_env * 0.22 * ao;
col += baseCol * evalStudioEnv(vec3(0.0, 1.0, 0.0), uLightMode) * 0.16 * ao;
// Subtle colorful iridescent reflection of the coupon onto the nearby bench
float dHero = mapHero(p);
if (dHero < 0.75) {
float bounceMask = smoothstep(0.75, 0.02, dHero) * (0.25 + 0.75 * F_env);
float dOx = computeOxideThickness(vec3(clamp(p.x, -1.28, 1.28), 0.10, clamp(p.z, -0.64, 0.64)));
vec3 bounceCol = evalTiO2Interference(dOx, 0.45);
col += bounceCol * bounceMask * 0.11 * ao;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping (Calibrated: 0% Black Crush, 0% Hard Clip)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// ============================================================================
// Camera Rig & Main Raymarcher
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / uResolution.y;
// Camera modes: 0 = Hero 3/4 studio framing, 1 = ~2.5x Macro inspection
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x macro focused on gold/blue/magenta stepped chamfer + satin/blasted seam
ro = vec3(0.32, 0.92, 1.18);
ta = vec3(0.06, 0.28, 0.04);
fov = 1.65;
} else {
// Hero 3/4 framing showing all 5 stepped voltage zones cleanly
ro = vec3(-0.72, 1.88, 2.75);
ta = vec3(0.02, 0.24, -0.02);
fov = 1.48;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + fov * cw);
// Raymarch scene (hero + bench y=0)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0005) {
hit = true;
break;
}
t += d * 0.92;
if (t > tMax) break;
}
// Studio cyclorama background (darkened ~0.7x so coupon pops with richer metallic contrast)
vec3 bg = evalStudioEnv(rd, uLightMode) * 0.18;
// Soft vertical studio backdrop gradient
vec3 col = mix(vec3(0.038, 0.041, 0.048), vec3(0.066, 0.071, 0.080), smoothstep(-0.25, 0.55, uv.y));
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
col = mix(col, bg, 0.32);
if (hit) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-Bug Rule #2: exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
vec3 benchCol = shadeBench(p, -rd);
// Smooth atmospheric blend into studio horizon
float fog = smoothstep(5.0, 12.5, t);
col = mix(benchCol, col, fog);
} else {
vec3 N = calcNormalHero(p);
col = shadeHero(p, N, -rd);
}
}
// Pure neutral luminance grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Calibrated exposure & ACES filmic tone mapping (keeps hero lum in [14, 242], 0% black/clipped)
col = max(col * 0.90, vec3(0.007));
col = acesToneMap(col) * 0.89;
// Subtle studio lens vignette (keeps corners soft without crushing below lumMin=12)
float vig = 1.0 - 0.11 * dot(uv, uv);
col *= vig;
// Linear -> sRGB gamma
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2));
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.85;
EMISSION = tinted * 0.0;
}
// Anodized Titanium Finish — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/04_anodized_titanium_interference_finish.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;
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
// ============================================================================
// Hashing & Procedural Micro-Surface 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);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
float a = hash12(i);
float b = hash12(i + vec2(1.0, 0.0));
float c = hash12(i + vec2(0.0, 1.0));
float d = hash12(i + vec2(1.0, 1.0));
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm2(vec2 p) {
float s = 0.0;
float a = 0.5;
mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
for (int i = 0; i < 4; i++) {
s += a * valueNoise(p);
p = m * p * 2.04 + 13.7;
a *= 0.5;
}
return s;
}
// ============================================================================
// SDF Primitives & Boolean Ops
// ============================================================================
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - (b - r);
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
float sdChamferBox(vec3 p, vec3 b, float ch) {
vec3 q = abs(p) - b;
float box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
// 45-degree chamfer on top/side edges
float c1 = (q.x + q.y + ch) * 0.70710678;
float c2 = (q.z + q.y + ch) * 0.70710678;
float c3 = (q.x + q.z + ch) * 0.70710678;
return max(box, max(c1, max(c2, c3)));
}
float sdCylinderY(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 smin(float a, float b, float k) {
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
return -smin(-a, -b, k);
}
// ============================================================================
// Coupon Geometry Parameters (5 Stepped Voltage Zones)
// ============================================================================
// Total footprint: X in [-1.30, 1.30], Z in [-0.66, 0.66], grounded at Y = 0.0
// 5 steps of width 0.52 each: centers at -1.04, -0.52, 0.0, +0.52, +1.04
// Step heights: 0.16, 0.23, 0.30, 0.37, 0.44
float stepHeightForIdx(int idx) {
return 0.16 + float(idx) * 0.07;
}
int zoneIndexFromX(float x) {
float u = (x + 1.30) / 0.52;
return clamp(int(floor(u)), 0, 4);
}
// Hero SDF: Stepped aerospace machined titanium test coupon
float mapHero(vec3 p) {
// Quick bounding box rejection
float dBound = sdBox(p - vec3(0.0, 0.24, 0.0), vec3(1.42, 0.34, 0.78));
if (dBound > 0.25) return dBound;
// 5 stepped aerospace terraces fused cleanly with crisp machined chamfers
float dSteps = 1e5;
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Overlap adjacent steps slightly in X so internal seams are solid
float halfW = (i == 0 || i == 4) ? 0.262 : 0.266;
vec3 q = p - vec3(cx, h * 0.5, 0.0);
// Chamfered outer & top edges (0.016 bevel)
float dStep = sdChamferBox(q, vec3(halfW, h * 0.5, 0.66), 0.015);
dSteps = min(dSteps, dStep);
}
// Precision outer perimeter fillet trim so corners look CNC-milled
float dOuter = sdRoundBox(p - vec3(0.0, 0.30, 0.0), vec3(1.30, 0.34, 0.66), 0.028);
float d = max(dSteps, dOuter);
// Subtle undercut relief along bottom edge (0.008 mm datum foot chamfer)
float dBottomBevel = -(p.y) + max(abs(p.x) - 1.288, abs(p.z) - 0.648) * 0.70;
d = max(d, min(dBottomBevel, -p.y + 0.012));
// Ensure grounded cleanly at y = 0.0
d = max(d, -p.y);
// Strip divider V-groove along Z = 0.02 separating satin-machined (back) and bead-blasted (front)
// Runs across the top face of every step
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float zDiv = abs(p.z - 0.02);
float vGroove = max((topY - p.y) - 0.012, zDiv - (0.013 - max(topY - p.y, 0.0) * 0.95));
// Carve clean 90-degree V-groove along Z = 0.02
float dV = max(abs(p.z - 0.02) - 0.011, (topY - 0.010) - p.y);
float dGroove = max((abs(p.z - 0.02) + (topY - p.y)) * 0.70710678 - 0.0095, -((topY - p.y) + 0.02));
// Keep groove shallow and clean
float grooveCut = max(abs(p.z - 0.02) - (0.010 - clamp(topY - p.y, 0.0, 0.010)), (topY - 0.010) - p.y);
d = smax(d, -grooveCut, 0.0025);
// Chamfered aerospace weight-reduction pockets in each of the 5 steps
// Front pocket (in bead-blasted zone, z centered at +0.34)
// Back shallow inspection pocket (in satin-machined zone, z centered at -0.33)
for (int i = 0; i < 5; i++) {
float cx = -1.04 + float(i) * 0.52;
float h = stepHeightForIdx(i);
// Front deep chamfered pocket
vec3 pf = p - vec3(cx, h, 0.34);
float pDepthF = 0.056;
// Main pocket cavity with corner radius
float cavF = sdRoundBox(pf - vec3(0.0, -pDepthF * 0.5 + 0.04, 0.0), vec3(0.165, pDepthF * 0.5 + 0.04, 0.205), 0.032);
// 45-deg chamfered rim flare near top surface (pf.y near 0)
float flareF = sdRoundBox(pf - vec3(0.0, 0.018, 0.0), vec3(0.184, 0.036, 0.224), 0.038);
float bevelF = (flareF + (-pf.y) * 0.75) * 0.70710678;
float pocketF = min(cavF, max(flareF, -pf.y - 0.022));
// Clean 45-degree top chamfer on pocket lip
vec2 qf2 = abs(pf.xz) - vec2(0.165 - 0.032, 0.205 - 0.032);
float d2dF = length(max(qf2, 0.0)) + min(max(qf2.x, qf2.y), 0.0) - 0.032;
float pocketLipF = max(d2dF - max(pf.y + 0.018, 0.0), -pf.y - pDepthF);
d = smax(d, -pocketLipF, 0.003);
// Back shallower satin-machined telemetry slot / pocket
vec3 pb = p - vec3(cx, h, -0.33);
float pDepthB = 0.026;
vec2 qb2 = abs(pb.xz) - vec2(0.155 - 0.026, 0.185 - 0.026);
float d2dB = length(max(qb2, 0.0)) + min(max(qb2.x, qb2.y), 0.0) - 0.026;
float pocketLipB = max(d2dB - max(pb.y + 0.012, 0.0), -pb.y - pDepthB);
d = smax(d, -pocketLipB, 0.0025);
}
// Aerospace datum tooling bores (countersunk) on the 2 outer ends of the satin strip
for (int j = 0; j < 2; j++) {
float bx = (j == 0) ? -1.175 : 1.175;
float bh = (j == 0) ? stepHeightForIdx(0) : stepHeightForIdx(4);
vec3 pb = p - vec3(bx, bh, -0.545);
float rCyl = length(pb.xz);
float bore = max(rCyl - 0.038, -pb.y - 0.14);
float csk = max(rCyl - (0.038 + max(pb.y + 0.016, 0.0) * 1.0), -pb.y - 0.03);
d = smax(d, -min(bore, csk), 0.002);
}
return d;
}
float mapScene(vec3 p) {
return min(p.y, mapHero(p));
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormalHero(vec3 p) {
const vec2 k = vec2(1.0, -1.0);
const float h = 0.00075;
return normalize(
k.xyy * mapHero(p + k.xyy * h) +
k.yyx * mapHero(p + k.yyx * h) +
k.yxy * mapHero(p + k.yxy * h) +
k.xxx * mapHero(p + k.xxx * h)
);
}
// ============================================================================
// Clean Hero-Only Soft Shadow (Zero-Bug Rule #3: no ph!)
// ============================================================================
float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
res = min(res, k * h / t);
t += max(h, 0.015);
if (res < 0.004 || 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.018 * float(i) * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.1 * occ, 0.28, 1.0);
}
// ============================================================================
// Physical TiO2 Thin-Film Airy Optical Interference on Titanium Substrate
// ============================================================================
// Complex arithmetic helpers
vec2 cMul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
vec2 cDiv(vec2 a, vec2 b) { return vec2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / max(dot(b, b), 1e-6); }
// Single-wavelength Airy thin-film reflectance for Air -> TiO2(n1, d) -> Ti(n2 + i*k2)
float airyReflectance(float lambdaNm, float dNm, float cosTheta0, float n1, float n2, float k2) {
float sin2Theta0 = max(0.0, 1.0 - cosTheta0 * cosTheta0);
float cosTheta1 = sqrt(max(0.001, 1.0 - sin2Theta0 / (n1 * n1)));
// Air -> TiO2 Fresnel (s and p polarizations)
float r01s = (cosTheta0 - n1 * cosTheta1) / (cosTheta0 + n1 * cosTheta1);
float r01p = (n1 * cosTheta0 - cosTheta1) / (n1 * cosTheta0 + cosTheta1);
// TiO2 -> Ti complex Fresnel (effective s and p)
vec2 nTi = vec2(n2, k2);
vec2 nFilmS = vec2(n1 * cosTheta1, 0.0);
vec2 nFilmP = vec2(n1 / cosTheta1, 0.0);
// Approximate transmitted angle in dense metal (~cosTheta2 near 1)
vec2 r12s = cDiv(nFilmS - nTi, nFilmS + nTi);
vec2 r12p = cDiv(nTi - nFilmP, nTi + nFilmP);
// Optical phase thickness across TiO2 film
float delta = (4.0 * PI * n1 * dNm * cosTheta1) / lambdaNm;
vec2 expI = vec2(cos(delta), sin(delta));
// Airy summation for s-pol
vec2 r12sE = cMul(r12s, expI);
vec2 rs = cDiv(vec2(r01s, 0.0) + r12sE, vec2(1.0, 0.0) + r01s * r12sE);
// Airy summation for p-pol
vec2 r12pE = cMul(r12p, expI);
vec2 rp = cDiv(vec2(r01p, 0.0) + r12pE, vec2(1.0, 0.0) + r01p * r12pE);
return 0.5 * (dot(rs, rs) + dot(rp, rp));
}
// 6-band spectral integration of TiO2 thin-film interference -> linear sRGB reflectance
vec3 evalTiO2Interference(float dNm, float cosTheta) {
// 6 wavelengths spanning visible spectrum (nm)
// TiO2 anodic dispersion n1(lambda), Ti substrate n2(lambda), k2(lambda)
float r640 = airyReflectance(640.0, dNm, cosTheta, 2.34, 2.72, 3.80);
float r595 = airyReflectance(595.0, dNm, cosTheta, 2.37, 2.60, 3.58);
float r550 = airyReflectance(550.0, dNm, cosTheta, 2.41, 2.48, 3.36);
float r510 = airyReflectance(510.0, dNm, cosTheta, 2.45, 2.38, 3.18);
float r475 = airyReflectance(475.0, dNm, cosTheta, 2.50, 2.29, 3.04);
float r445 = airyReflectance(445.0, dNm, cosTheta, 2.56, 2.22, 2.94);
// Smooth CIE-inspired spectral-to-linear-sRGB weights (normalized so flat 1.0 -> white)
vec3 col =
r640 * vec3(0.58, 0.04, 0.00) +
r595 * vec3(0.42, 0.24, 0.00) +
r550 * vec3(0.08, 0.46, 0.02) +
r510 * vec3(-0.08, 0.28, 0.16) +
r475 * vec3(0.00, 0.04, 0.46) +
r445 * vec3(0.00, -0.06, 0.36);
// Clamp tiny negative gamut excursions and boost subtle physical chroma clarity
col = max(col, vec3(0.035));
// Mode 1: pure neutral metallic grayscale (strip interference chroma)
if (uLightMode == 1) {
float lum = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = vec3(mix(0.42, 0.78, lum));
}
// Preserve metallic Titanium reflectivity envelope
return clamp(col, vec3(0.035), vec3(0.96));
}
// Compute TiO2 oxide thickness (nm) for a point on the coupon
// 5 voltage zones at uOpen = 0:
// Step 0: 15V Bronze (~28 nm)
// Step 1: 25V Royal Blue (~53 nm)
// Step 2: 55V Gold (~120 nm)
// Step 3: 75V Magenta-Violet(~149 nm)
// Step 4: 95V Teal-Green (~186 nm)
// uOpen (0..1) sweeps anodizing voltage / oxide thickness smoothly across the coupon.
float computeOxideThickness(vec3 p) {
// Smooth transitions at step risers so vertical walls show slight anodizing meniscus
float u = (p.x + 1.30) / 0.52;
float idxF = clamp(u, 0.0, 4.999);
int i0 = int(floor(idxF));
float f = fract(idxF);
float dTable[5];
dTable[0] = 28.0; // 15V bronze
dTable[1] = 53.0; // 25V royal blue
dTable[2] = 120.0; // 55V gold
dTable[3] = 149.0; // 75V magenta-violet
dTable[4] = 186.0; // 95V teal-green
float dBase = dTable[i0];
// Subtle transition right at step riser edge (2% of step width)
if (i0 < 4 && f > 0.96) {
float t = smoothstep(0.96, 1.0, f);
dBase = mix(dTable[i0], dTable[i0 + 1], t);
} else if (i0 > 0 && f < 0.04) {
float t = smoothstep(0.04, 0.0, f);
dBase = mix(dTable[i0], dTable[i0 - 1], t);
}
// Subtle natural anodic thickness micro-variation (+/- 2.2 nm) from electrolyte bath flow
float bathVar = (fbm2(p.xz * 3.2 + vec2(1.7, -2.3)) - 0.5) * 4.2;
// Slight edge current-density thickening on sharp chamfers (+3 nm)
float edgeBoost = smoothstep(0.55, 0.66, abs(p.z)) * 3.5;
// uOpen sweeps anodizing voltage across the coupon (+0..+120 nm)
float sweep = uOpen * 120.0;
return max(12.0, dBase + sweep + bathVar + edgeBoost);
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 evalStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base dark cyclorama studio ambient (gives strong negative-fill contrast on metal)
vec3 col = mix(vec3(0.035, 0.038, 0.044), vec3(0.065, 0.072, 0.084), smoothstep(-0.2, 0.8, y));
if (lightMode == 1) {
// Mode 1: Pure neutral grayscale studio
col = mix(vec3(0.045), vec3(0.085), smoothstep(-0.2, 0.8, y));
// Overhead main neutral softbox
float sbTop = smoothstep(0.62, 0.90, dot(d, normalize(vec3(0.15, 0.95, 0.25))));
// Key softbox right-front
float sbKey = smoothstep(0.68, 0.92, dot(d, normalize(vec3(0.65, 0.58, 0.48))));
// Cool-neutral strip rim left-back
float sbRim = smoothstep(0.76, 0.95, dot(d, normalize(vec3(-0.72, 0.42, -0.55))));
// Negative-fill dark flag right-side
float flag1 = smoothstep(0.70, 0.88, dot(d, normalize(vec3(0.92, 0.18, -0.35))));
col += vec3(1.85) * sbTop;
col += vec3(2.40) * sbKey;
col += vec3(1.50) * sbRim;
col *= (1.0 - 0.72 * flag1);
return col;
}
if (lightMode == 2) {
// Mode 2: Grazing raking light
col = vec3(0.025, 0.028, 0.034);
float sbRaking = smoothstep(0.74, 0.96, dot(d, normalize(vec3(0.92, 0.24, 0.30))));
float sbCounter = smoothstep(0.72, 0.94, dot(d, normalize(vec3(-0.88, 0.26, -0.38))));
float sbTopStrip = smoothstep(0.84, 0.97, dot(d, normalize(vec3(0.0, 0.98, 0.15))));
col += vec3(3.40, 3.15, 2.80) * sbRaking;
col += vec3(1.25, 1.45, 1.75) * sbCounter;
col += vec3(0.85, 0.90, 0.98) * sbTopStrip;
return col;
}
if (lightMode == 3) {
// Mode 3: Backlit studio rim
col = vec3(0.028, 0.032, 0.040);
float sbBack = smoothstep(0.55, 0.90, dot(d, normalize(vec3(-0.18, 0.45, -0.87))));
float sbRimL = smoothstep(0.74, 0.94, dot(d, normalize(vec3(-0.75, 0.50, -0.42))));
float sbFrontFill = smoothstep(0.65, 0.92, dot(d, normalize(vec3(0.35, 0.52, 0.78))));
col += vec3(3.20, 3.25, 3.45) * sbBack;
col += vec3(2.10, 2.25, 2.50) * sbRimL;
col += vec3(0.62, 0.64, 0.68) * sbFrontFill;
return col;
}
// Mode 0: Hero aerospace product studio lighting
// Large overhead strip softbox (crisp specular bands across stepped planes)
vec3 topDir = normalize(vec3(-0.12, 0.94, 0.32));
float sbTop = smoothstep(0.68, 0.91, dot(d, topDir));
// Warm-white key softbox (front-right-high)
vec3 keyDir = normalize(vec3(0.62, 0.56, 0.54));
float sbKey = smoothstep(0.72, 0.94, dot(d, keyDir));
// Cool rim strip softbox (back-left)
vec3 rimDir = normalize(vec3(-0.70, 0.44, -0.56));
float sbRim = smoothstep(0.76, 0.95, dot(d, rimDir));
// Crisp horizon accent line for chamfer glints
float horizonStrip = exp(-abs(y - 0.16) * 14.0) * smoothstep(-0.2, 0.8, -d.z) * 0.45;
// Dark negative-fill flags to sculpt metallic contrast
float negFlagR = smoothstep(0.65, 0.86, dot(d, normalize(vec3(0.94, 0.16, -0.30))));
float negFlagL = smoothstep(0.68, 0.88, dot(d, normalize(vec3(-0.88, 0.14, 0.44))));
col += vec3(1.95, 1.98, 2.08) * sbTop;
col += vec3(2.65, 2.52, 2.34) * sbKey;
col += vec3(1.45, 1.65, 2.05) * sbRim;
col += vec3(0.95, 1.02, 1.15) * horizonStrip;
col *= (1.0 - 0.78 * negFlagR);
col *= (1.0 - 0.65 * negFlagL);
return col;
}
// ============================================================================
// GGX Microfacet BRDF with Anisotropic Satin & Thin-Film Fresnel
// ============================================================================
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float d = (ToH * ToH) / (ax * ax) + (BoH * BoH) / (ay * ay) + NoH * NoH;
return 1.0 / max(PI * ax * ay * d * d, 1e-5);
}
float G_SmithGGX(float NoV, float NoL, float alpha) {
float a2 = alpha * alpha;
float gv = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2);
float gl = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1e-5);
}
// Micro-blasted metallic sparkle glints (high-frequency crystalline Ti grain facets)
float microSparkle(vec3 p, vec3 N, vec3 V, vec3 L, float scale) {
vec2 uv = p.xz * scale + p.y * scale * 0.37;
vec2 cell = floor(uv);
vec2 f = fract(uv);
vec3 rnd = hash32(cell);
// Random micro-facet normal tilted within bead-blast cone
vec2 tilt = (rnd.xy - 0.5) * 0.52;
vec3 T = normalize(cross(abs(N.y) > 0.9 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0), N));
vec3 B = cross(N, T);
vec3 mN = normalize(N + T * tilt.x + B * tilt.y);
vec3 H = normalize(V + L);
float spec = pow(max(dot(mN, H), 0.0), 90.0);
// Soft circular grain mask so sparkles never look blocky
float grainMask = smoothstep(0.55, 0.10, length(f - 0.5));
return spec * grainMask * (0.45 + 0.55 * rnd.z);
}
// ============================================================================
// Surface Micro-Detail & Finish Classification
// ============================================================================
// Returns: x = roughness, y = anisotropy (-1..1), z = isBeadBlasted (0..1), w = chamferMask
vec4 sampleCouponFinish(vec3 p, vec3 N, out vec3 N_perturbed) {
int zi = zoneIndexFromX(p.x);
float topY = stepHeightForIdx(zi);
float cx = -1.04 + float(zi) * 0.52;
// Classify front bead-blasted strip (z > 0.02) vs back satin-machined strip (z < 0.02)
float isBlasted = smoothstep(0.014, 0.028, p.z);
// Detect angled chamfered edges & bevels (bright machined specular)
float chamferMask = smoothstep(0.35, 0.78, 1.0 - abs(N.y)) * smoothstep(0.15, 0.55, abs(N.y));
// Also keep vertical step risers satin-machined
float isSideWall = smoothstep(0.45, 0.15, abs(N.y));
// Inside front pocket floor: fine satin concentric milled finish vs blasted rim
vec2 pf = p.xz - vec2(cx, 0.34);
float inFrontPocketFloor = smoothstep(topY - 0.042, topY - 0.052, p.y) * smoothstep(0.75, 0.95, N.y);
float blastWeight = isBlasted * (1.0 - chamferMask) * (1.0 - isSideWall) * (1.0 - inFrontPocketFloor * 0.85);
// Construct tangent frame
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
// 1. Satin CNC-machined micro-grooves (parallel toolpath + subtle fly-cutter arcs)
float toolGrooveX = sin(p.z * 320.0 + fbm2(p.xz * 18.0) * 1.4) * 0.5;
float flyArc = sin(length((p.xz - vec2(cx, -0.33)) * vec2(1.0, 1.0)) * 240.0) * 0.35;
float millLine = mix(toolGrooveX, flyArc, smoothstep(0.24, 0.14, length(p.xz - vec2(cx, -0.33))));
// Side walls have fine horizontal end-mill passes
float sideMill = sin(p.y * 380.0) * 0.45;
float satinBump = mix(millLine, sideMill, isSideWall);
// 2. Glass-bead-blasted isotropic micro-dimple perturbation
vec2 bp = p.xz * 165.0 + p.y * 90.0;
float b1 = valueNoise(bp) - 0.5;
float b2 = valueNoise(bp * 2.15 + 17.3) - 0.5;
float bx = (valueNoise(bp + vec2(0.35, 0.0)) - valueNoise(bp - vec2(0.35, 0.0)));
float bz = (valueNoise(bp + vec2(0.0, 0.35)) - valueNoise(bp - vec2(0.0, 0.35)));
vec3 nSatin = normalize(N + B * (satinBump * 0.022) + T * ((valueNoise(p.xz * 95.0) - 0.5) * 0.010));
vec3 nBlast = normalize(N + T * (bx * 0.14 + b2 * 0.05) + B * (bz * 0.14 + b1 * 0.05));
N_perturbed = normalize(mix(nSatin, nBlast, blastWeight));
// Roughness & anisotropy
float roughSatin = 0.155 + 0.025 * valueNoise(p.xz * 40.0);
float roughBlast = 0.355 + 0.045 * valueNoise(p.xz * 90.0);
float rough = mix(roughSatin, roughBlast, blastWeight);
// Chamfers are freshly cut and slightly glossier
rough = mix(rough, 0.115, chamferMask * 0.85);
float aniso = mix(0.58, 0.0, blastWeight);
aniso = mix(aniso, 0.15, chamferMask);
return vec4(rough, aniso, blastWeight, chamferMask);
}
// ============================================================================
// Lighting Setup (4 Light Modes)
// ============================================================================
void getLights(int lightMode, out vec3 lDir[3], out vec3 lCol[3]) {
if (lightMode == 1) {
// Pure neutral grayscale studio lighting
lDir[0] = normalize(vec3(0.58, 0.68, 0.45));
lCol[0] = vec3(2.35);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.30);
lDir[2] = normalize(vec3(-0.15, 0.92, 0.36));
lCol[2] = vec3(1.10);
} else if (lightMode == 2) {
// Grazing raking light
lDir[0] = normalize(vec3(0.92, 0.25, 0.30));
lCol[0] = vec3(3.10, 2.90, 2.65);
lDir[1] = normalize(vec3(-0.88, 0.26, -0.40));
lCol[1] = vec3(1.15, 1.32, 1.62);
lDir[2] = normalize(vec3(0.05, 0.86, 0.50));
lCol[2] = vec3(0.55, 0.58, 0.64);
} else if (lightMode == 3) {
// Backlit studio rim
lDir[0] = normalize(vec3(-0.22, 0.46, -0.86));
lCol[0] = vec3(3.05, 3.12, 3.32);
lDir[1] = normalize(vec3(-0.74, 0.52, -0.42));
lCol[1] = vec3(1.85, 1.98, 2.22);
lDir[2] = normalize(vec3(0.38, 0.58, 0.72));
lCol[2] = vec3(0.78, 0.80, 0.84);
} else {
// Mode 0: Hero studio lighting (warm-white key, cool rim, crisp overhead)
lDir[0] = normalize(vec3(0.58, 0.62, 0.52));
lCol[0] = vec3(2.55, 2.44, 2.30);
lDir[1] = normalize(vec3(-0.68, 0.46, -0.56));
lCol[1] = vec3(1.38, 1.56, 1.92);
lDir[2] = normalize(vec3(-0.12, 0.94, 0.32));
lCol[2] = vec3(1.18, 1.20, 1.26);
}
}
// ============================================================================
// Shade Hero (Anodized Titanium Coupon)
// ============================================================================
vec3 shadeHero(vec3 p, vec3 Ngeom, vec3 V) {
vec3 N;
vec4 fin = sampleCouponFinish(p, Ngeom, N);
float rough = fin.x;
float aniso = fin.y;
float blastWeight = fin.z;
float dOxide = computeOxideThickness(p);
float ao = calcAO(p, Ngeom);
// Tangent frame for anisotropic satin highlights
vec3 refAxis = abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 T = normalize(cross(N, refAxis));
vec3 B = cross(N, T);
float ax = max(0.04, rough * (1.0 + aniso * 0.65));
float ay = max(0.04, rough * (1.0 - aniso * 0.55));
float NoV = max(dot(N, V), 0.02);
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 color = vec3(0.0);
// Direct studio lights
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + Ngeom * 0.014, L, 6.0, 14.0)
: (i == 1) ? softShadow(p + Ngeom * 0.014, L, 6.0, 10.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float ToH = dot(T, H);
float BoH = dot(B, H);
// View-angle & microfacet-angle TiO2 thin-film Airy interference reflectance
vec3 F_film = evalTiO2Interference(dOxide, VoH);
float D = D_GGX_Aniso(NoH, ToH, BoH, ax, ay);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 spec = D * G * F_film;
// Micro-blasted iridescent sparkle glints on bead-blasted strip
float spk = 0.0;
if (blastWeight > 0.05) {
float s1 = microSparkle(p, Ngeom, V, L, 210.0);
float s2 = microSparkle(p + vec3(0.13, 0.0, 0.27), Ngeom, V, L, 340.0);
spk = (s1 * 0.75 + s2 * 0.55) * blastWeight;
}
// Subtle multi-scatter metallic lobe for bead-blasted Ti (keeps matte strip luminous & vivid)
vec3 multiScatter = F_film * F_film * (0.14 + 0.22 * blastWeight) / PI;
color += (spec + multiScatter + F_film * spk * 1.45) * lCol[i] * NoL * sh;
}
// Studio IBL environment reflection with view-angle thin-film iridescence
vec3 R = reflect(-V, N);
vec3 F_env = evalTiO2Interference(dOxide, NoV);
// Multi-lobe filtered studio environment lookup (sharp + broad roughness blend)
vec3 envSharp = evalStudioEnv(R, uLightMode);
vec3 envMid = evalStudioEnv(normalize(mix(R, N, 0.35)), uLightMode);
vec3 envBroad = evalStudioEnv(normalize(mix(R, N, 0.75)), uLightMode);
vec3 envSpec = mix(envSharp, mix(envMid, envBroad, smoothstep(0.18, 0.45, rough)), smoothstep(0.08, 0.38, rough));
// Ground bounce tint (subtle dark bench reflection for downward-facing bevels)
float downFade = smoothstep(-0.15, 0.15, R.y);
envSpec = mix(vec3(0.04, 0.042, 0.048), envSpec, 0.35 + 0.65 * downFade);
color += envSpec * F_env * (0.82 + 0.16 * (1.0 - rough)) * ao;
// Lift deepest pocket ambient slightly so metallic crevices stay readable
color += F_env * 0.042 * (0.45 + 0.55 * ao);
// Subtle secondary iridescent edge sheen at grazing view angles
float fresnelRim = pow(1.0 - NoV, 4.0);
vec3 F_grazing = evalTiO2Interference(dOxide, 0.18);
color += F_grazing * fresnelRim * 0.13 * ao;
return color;
}
// ============================================================================
// Shade Grounded Studio Bench
// ============================================================================
vec3 shadeBench(vec3 p, vec3 V) {
// Subtle dark anodized slate / machined granite inspection bench
vec2 gUV = p.xz * 24.0;
float grain = fbm2(gUV) - 0.5;
float fine = valueNoise(p.xz * 140.0) - 0.5;
vec3 N = normalize(vec3(grain * 0.016 + fine * 0.008, 1.0, (fbm2(gUV + 9.1) - 0.5) * 0.016));
float dist = length(p.xz);
// Subtle radial falloff into studio cyclorama (darkened ~0.68x for rich metallic contrast)
float benchFade = smoothstep(6.5, 1.8, dist);
vec3 baseCol = mix(vec3(0.028, 0.030, 0.035), vec3(0.046, 0.049, 0.055), benchFade);
if (uLightMode == 1) {
baseCol = vec3(dot(baseCol, vec3(0.2126, 0.7152, 0.0722)));
}
baseCol *= (0.94 + 0.12 * grain);
// Subtle calibrated inspection bench hairline grid (very faint, museum feel)
vec2 grid = abs(fract(p.xz * 2.0 - 0.5) - 0.5) / 0.015;
float line = 1.0 - min(min(grid.x, grid.y), 1.0);
baseCol += vec3(0.007) * line * smoothstep(3.8, 1.2, dist);
float ao = calcAO(p, vec3(0.0, 1.0, 0.0));
vec3 lDir[3];
vec3 lCol[3];
getLights(uLightMode, lDir, lCol);
vec3 col = vec3(0.0);
float NoV = max(dot(N, V), 0.02);
float rough = 0.36 + 0.04 * grain;
for (int i = 0; i < 3; i++) {
vec3 L = lDir[i];
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.001) continue;
float sh = (i == 0) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 11.0)
: (i == 1) ? softShadow(p + vec3(0.0, 0.014, 0.0), L, 6.0, 8.0)
: 1.0;
vec3 H = normalize(V + L);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.02);
float F = 0.04 + 0.96 * pow(1.0 - VoH, 5.0);
float D = D_GGX_Aniso(NoH, 0.0, 0.0, rough, rough);
float G = G_SmithGGX(NoV, NoL, rough);
vec3 diff = baseCol * (1.0 - F) / PI;
vec3 spec = vec3(D * G * F) * 0.72;
col += (diff + spec) * lCol[i] * NoL * sh;
}
// Soft studio reflection + subtle colored bounce from anodized coupon near base
vec3 R = reflect(-V, N);
float F_env = 0.04 + 0.96 * pow(1.0 - NoV, 5.0);
vec3 envRefl = evalStudioEnv(normalize(mix(R, N, 0.38)), uLightMode);
col += envRefl * F_env * 0.22 * ao;
col += baseCol * evalStudioEnv(vec3(0.0, 1.0, 0.0), uLightMode) * 0.16 * ao;
// Subtle colorful iridescent reflection of the coupon onto the nearby bench
float dHero = mapHero(p);
if (dHero < 0.75) {
float bounceMask = smoothstep(0.75, 0.02, dHero) * (0.25 + 0.75 * F_env);
float dOx = computeOxideThickness(vec3(clamp(p.x, -1.28, 1.28), 0.10, clamp(p.z, -0.64, 0.64)));
vec3 bounceCol = evalTiO2Interference(dOx, 0.45);
col += bounceCol * bounceMask * 0.11 * ao;
}
return col;
}
// ============================================================================
// ACES Filmic Tone Mapping (Calibrated: 0% Black Crush, 0% Hard Clip)
// ============================================================================
vec3 acesToneMap(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}
// ============================================================================
// Camera Rig & Main Raymarcher
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;
// Camera modes: 0 = Hero 3/4 studio framing, 1 = ~2.5x Macro inspection
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x macro focused on gold/blue/magenta stepped chamfer + satin/blasted seam
ro = vec3(0.32, 0.92, 1.18);
ta = vec3(0.06, 0.28, 0.04);
fov = 1.65;
} else {
// Hero 3/4 framing showing all 5 stepped voltage zones cleanly
ro = vec3(-0.72, 1.88, 2.75);
ta = vec3(0.02, 0.24, -0.02);
fov = 1.48;
}
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + fov * cw);
// Raymarch scene (hero + bench y=0)
float t = 0.0;
float tMax = 14.0;
bool hit = false;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.0005) {
hit = true;
break;
}
t += d * 0.92;
if (t > tMax) break;
}
// Studio cyclorama background (darkened ~0.7x so coupon pops with richer metallic contrast)
vec3 bg = evalStudioEnv(rd, uLightMode) * 0.18;
// Soft vertical studio backdrop gradient
vec3 col = mix(vec3(0.038, 0.041, 0.048), vec3(0.066, 0.071, 0.080), smoothstep(-0.25, 0.55, uv.y));
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
col = mix(col, bg, 0.32);
if (hit) {
vec3 p = ro + rd * t;
float dHero = mapHero(p);
// Zero-Bug Rule #2: exact bench vs hero classification
bool isBench = (p.y <= dHero + 0.001);
if (isBench) {
vec3 benchCol = shadeBench(p, -rd);
// Smooth atmospheric blend into studio horizon
float fog = smoothstep(5.0, 12.5, t);
col = mix(benchCol, col, fog);
} else {
vec3 N = calcNormalHero(p);
col = shadeHero(p, N, -rd);
}
}
// Pure neutral luminance grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Calibrated exposure & ACES filmic tone mapping (keeps hero lum in [14, 242], 0% black/clipped)
col = max(col * 0.90, vec3(0.007));
col = acesToneMap(col) * 0.89;
// Subtle studio lens vignette (keeps corners soft without crushing below lumMin=12)
float vig = 1.0 - 0.11 * dot(uv, uv);
col *= vig;
// Linear -> sRGB gamma
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2));
fragColor = vec4(col, 1.0);
}
`;
export function createAnodizedTitaniumFinishMaterial(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 | anodized-titanium-finish |
|---|---|
| Category | Metals & Armor Shaders · Industrial Metals |
| Upstream study | metals/lib/04_anodized_titanium_interference_finish.glsl (commit 4778681) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 839 lines · 33.0 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT scifi 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
AnimatedHeavy foundry cast iron with mold-sand stipple micro-relief, graphite dark oxide, and parting seam.
AnimatedBaked polymer powder coating with subtle low-frequency orange-peel waviness and satin specular.
AnimatedOverlapping circular perlage abrasive swirls catching directional metallic sheen across steel plate.
AnimatedMirror electroplated chrome bubbling and flaking away over oxidized substrate metal.
AnimatedMottled bone-charcoal case-hardened weapon steel with straw-amber, violet, and peacock-blue oxide.
AnimatedThermal gradient oxide bands transitioning from bare silver steel through gold, purple, and blue.