AnimatedMirror electroplated chrome bubbling and flaking away over oxidized substrate metal.
engine-turned-guilloche.glsl · 936 lines · GLSL ES 3.00 · MIT
#version 300 es
// Engine-Turned Guilloche (engine-turned-guilloche.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/07_engine_turned_perlon_guilloche.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
// ============================================================================
// Constants & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_MAINPLATE = 1; // Lower rhodium perlage deck
const int MAT_BRIDGE_TOP = 2; // Upper rhodium perlage bridge top
const int MAT_ANGLAGE = 3; // Mirror-polished 45-deg rhodium bevels & walls
const int MAT_GOLD_CHATON = 4; // 18k yellow gold jewel setting
const int MAT_RUBY = 5; // Synthetic corundum ruby jewel bearing
const int MAT_BLUED_STEEL = 6; // Heat-blued steel screw
const int MAT_BRASS_WHEEL = 7; // Subtle gilt escapement wheel glimpse
// Key horological feature centers in XZ
const vec2 C_JEWEL = vec2(-0.16, 0.08);
const vec2 C_SCREW1 = vec2( 0.40, -0.14);
const vec2 C_SCREW2 = vec2(-0.56, -0.34);
const vec2 C_WELL = vec2( 0.44, 0.46);
// ============================================================================
// Math & Hashing Helpers
// ============================================================================
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i);
float b = hash21(i + vec2(1.0, 0.0));
float c = hash21(i + vec2(0.0, 1.0));
float d = hash21(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float 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);
}
// ============================================================================
// 2D & 3D Horological SDF Primitives
// ============================================================================
float sdExtrudeChamfer(float d2d, float y, float yMin, float yMax, float bevel) {
float h = 0.5 * (yMax - yMin);
float yc = y - 0.5 * (yMin + yMax);
vec2 w = vec2(d2d, abs(yc) - h);
float dBox = min(max(w.x, w.y), 0.0) + length(max(w, 0.0));
// 45-degree chamfer on top edge
float dChamfer = (d2d + (yc - h)) * 0.70710678 + bevel * 0.70710678;
return max(dBox, dChamfer);
}
float sdCylinder(vec3 p, vec2 c, float r, float yMin, float yMax, float bevel) {
float d2d = length(p.xz - c) - r;
return sdExtrudeChamfer(d2d, p.y, yMin, yMax, bevel);
}
// Curved Swiss watch movement bridge silhouette in XZ
float sdBridge2D(vec2 xz) {
float dOuter = length(xz) - 1.10;
// Sweeping horological balance-cock cutaway on upper-right quadrant
float dCut1 = length(xz - vec2(0.58, 0.54)) - 0.62;
// Secondary graceful waist curve on upper-left
float dCut2 = length(xz - vec2(-0.88, 0.72)) - 0.42;
// Combine outer disc minus smooth cutaways
float d = smax(dOuter, -dCut1, 0.09);
d = smax(d, -dCut2, 0.08);
// Subtle decorative curved extension lobe around primary screw
float dLobe = length(xz - vec2(0.42, -0.14)) - 0.34;
d = smin(d, dLobe, 0.14);
return d;
}
// Mainplate outer silhouette in XZ
float sdMainplate2D(vec2 xz) {
float d = length(xz) - 1.24;
// Subtle movement casing notch on outer rim
float dNotch = length(xz - vec2(-1.26, 0.0)) - 0.12;
d = smax(d, -dNotch, 0.03);
return d;
}
// ============================================================================
// Hero Assembly SDF (Watch Movement Mainplate, Bridge, Jewel, Chaton, Screws)
// ============================================================================
float mapMainplate(vec3 p) {
float d2d = sdMainplate2D(p.xz);
float d = sdExtrudeChamfer(d2d, p.y, 0.0, 0.105, 0.014);
// Recessed escapement bearing well on exposed lower mainplate
float rWell = length(p.xz - C_WELL);
float dWell = max(rWell - 0.23, -(p.y - 0.055));
float dWellChamfer = (-(rWell - 0.23) + (p.y - 0.105)) * 0.70710678;
dWell = min(dWell, -dWellChamfer + 0.012);
d = max(d, -dWell);
return d;
}
float mapBridge(vec3 p) {
float d2d = sdBridge2D(p.xz);
// Bridge sits from y = 0.095 to y = 0.225 with prominent 45-deg mirror anglage
float d = sdExtrudeChamfer(d2d, p.y, 0.095, 0.225, 0.030);
// Conical jewel countersink (oil sink + chaton bore)
float rJ = length(p.xz - C_JEWEL);
float dJ = max(rJ - 0.188, -(p.y - 0.150));
float dJBevel = ((rJ - 0.188) - (p.y - 0.225)) * 0.70710678 - 0.018;
d = max(d, -max(dJ, dJBevel));
// Primary screw counterbore
float rS1 = length(p.xz - C_SCREW1);
float dS1 = max(rS1 - 0.158, -(p.y - 0.155));
float dS1Bevel = ((rS1 - 0.158) - (p.y - 0.225)) * 0.70710678 - 0.012;
d = max(d, -max(dS1, dS1Bevel));
// Secondary screw counterbore
float rS2 = length(p.xz - C_SCREW2);
float dS2 = max(rS2 - 0.105, -(p.y - 0.165));
float dS2Bevel = ((rS2 - 0.105) - (p.y - 0.225)) * 0.70710678 - 0.010;
d = max(d, -max(dS2, dS2Bevel));
return d;
}
float mapGoldChaton(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Gold setting ring seated in the countersink
float dOuter = r - 0.174;
float dInner = 0.114 - r;
float d2d = max(dOuter, dInner);
float dRing = sdExtrudeChamfer(d2d, p.y, 0.148, 0.214, 0.011);
// Subtle torus lip on top of gold chaton
vec2 tor = vec2(r - 0.144, p.y - 0.207);
float dLip = length(tor) - 0.014;
return smin(dRing, dLip, 0.008);
}
float mapRuby(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Synthetic corundum ruby dome with central pivot hole & oil sink
float dCyl = sdExtrudeChamfer(r - 0.116, p.y, 0.148, 0.204, 0.016);
// Gentle concave oil-sink bowl on top of the ruby
float dBowl = length(vec3(q.x, (p.y - 0.260) * 1.15, q.y)) - 0.088;
float dRuby = max(dCyl, -dBowl);
// Central pivot bore
float dBore = 0.030 - r;
dRuby = max(dRuby, dBore);
return dRuby;
}
float mapPivot(vec3 p) {
// Polished steel pivot pin inside the ruby center
vec2 q = p.xz - C_JEWEL;
float r = length(q);
return sdExtrudeChamfer(r - 0.025, p.y, 0.140, 0.194, 0.006);
}
float mapScrewSingle(vec3 p, vec2 c, float radius, float yBase, float yTop, float angle) {
vec2 q = (p.xz - c) * rot2(angle);
float r = length(q);
float dHead = sdExtrudeChamfer(r - radius, p.y, yBase, yTop, radius * 0.14);
// Subtle dome curvature on screw head
float dDome = length(vec3(q.x, (p.y - (yTop - radius * 2.2)) * 1.0, q.y)) - radius * 2.24;
dHead = max(dHead, dDome);
// Precision horological screwdriver slot with beveled mouth
float slotHalfW = radius * 0.145;
float slotDepth = 0.030;
float dSlotBox = max(abs(q.y) - slotHalfW, -(p.y - (yTop - slotDepth)));
float dSlotBevel = (abs(q.y) - slotHalfW - (p.y - yTop)) * 0.70710678 - 0.005;
float dSlot = max(dSlotBox, dSlotBevel);
return max(dHead, -dSlot);
}
float mapBluedScrews(vec3 p) {
float s1 = mapScrewSingle(p, C_SCREW1, 0.142, 0.150, 0.232, 0.58);
float s2 = mapScrewSingle(p, C_SCREW2, 0.092, 0.160, 0.228, -0.82);
return min(s1, s2);
}
float mapBrassWheel(vec3 p) {
// Escapement wheel ring + hub visible inside the mainplate bearing well
vec2 q = p.xz - C_WELL;
float r = length(q);
float ang = atan(q.y, q.x) + uTime * 0.18;
float teeth = 0.009 * smoothstep(-0.2, 0.6, cos(ang * 18.0));
float dRim = max((r - (0.195 + teeth)), 0.132 - r);
float dHub = r - 0.056;
// 4 spokes
vec2 qs = q * rot2(uTime * 0.18);
float dSpoke = min(abs(qs.x), abs(qs.y)) - 0.016;
dSpoke = max(dSpoke, r - 0.16);
float d2d = min(min(dRim, dHub), dSpoke);
return sdExtrudeChamfer(d2d, p.y, 0.052, 0.088, 0.005);
}
float mapHero(vec3 p) {
float d = mapMainplate(p);
d = min(d, mapBridge(p));
d = min(d, mapGoldChaton(p));
d = min(d, mapRuby(p));
d = min(d, mapPivot(p));
d = min(d, mapBluedScrews(p));
d = min(d, mapBrassWheel(p));
return d;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormal(vec3 p) {
const float e = 0.00045;
const vec2 k = vec2(1.0, -1.0);
return normalize(
k.xyy * mapScene(p + k.xyy * e) +
k.yyx * mapScene(p + k.yyx * e) +
k.yxy * mapScene(p + k.yxy * e) +
k.xxx * mapScene(p + k.xxx * e)
);
}
// ============================================================================
// Hero-Only Soft Shadow (Zero-Bug Rule #3: clean res = min(res, k*h/t), no ph!)
// ============================================================================
float calcSoftShadow(vec3 ro, vec3 rd, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.0005) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > 4.5) 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 = 0; i < 5; i++) {
float h = 0.012 + 0.045 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Material Identification
// ============================================================================
int identifyMaterial(vec3 p, vec3 N, bool isBench) {
if (isBench) return MAT_BENCH;
float dMain = mapMainplate(p);
float dBrdg = mapBridge(p);
float dChat = mapGoldChaton(p);
float dRuby = mapRuby(p);
float dPiv = mapPivot(p);
float dScrew = mapBluedScrews(p);
float dWheel = mapBrassWheel(p);
float dMin = dMain;
int mat = MAT_MAINPLATE;
if (dBrdg < dMin) { dMin = dBrdg; mat = MAT_BRIDGE_TOP; }
if (dChat < dMin) { dMin = dChat; mat = MAT_GOLD_CHATON; }
if (dRuby < dMin) { dMin = dRuby; mat = MAT_RUBY; }
if (dPiv < dMin) { dMin = dPiv; mat = MAT_ANGLAGE; }
if (dScrew < dMin) { dMin = dScrew; mat = MAT_BLUED_STEEL; }
if (dWheel < dMin) { dMin = dWheel; mat = MAT_BRASS_WHEEL; }
// Separate flat perlage top surfaces from mirror-polished 45-deg anglage bevels & walls
if (mat == MAT_BRIDGE_TOP) {
if (N.y < 0.965 || p.y < 0.219) {
mat = MAT_ANGLAGE;
}
} else if (mat == MAT_MAINPLATE) {
if (N.y < 0.965 || p.y < 0.099) {
mat = MAT_ANGLAGE;
}
}
return mat;
}
// ============================================================================
// Shingled Engine-Turned Perlage (Circular Abrasive-Swirl Rosettes)
// ============================================================================
struct PerlageData {
vec2 tangent2D; // Concentric groove tangent in XZ (unit vector)
vec2 radial2D; // Radial direction from winning rosette center
float rNorm; // Normalized radius [0..1] inside winning rosette
float grooveSlope; // Band-limited concentric micro-groove radial derivative
float edgeLip; // Crisp outer abrasive ring highlight/seam
float centerDimple;// Soft zero-velocity center dimple
float cellTone; // Subtle rosette-to-rosette burnishing variation
float rosetteMask; // Coverage mask
};
PerlageData evalPerlage(vec2 uv, float openAmount, float scaleMul) {
// uOpen = 0.0 -> fine Swiss watchmaking perlage (small, delicate overlapping circles)
// uOpen = 1.0 -> bold vintage automotive dashboard engine-turning (large, punchy rosettes)
float pitch = mix(0.145, 0.340, clamp(openAmount, 0.0, 1.0)) * scaleMul;
float dx = pitch;
float dy = pitch * 0.72;
float rosetteR = pitch * mix(0.74, 0.78, openAmount);
// Analytic screen-space pixel footprint for fwidth antialiasing
float fwUV = max(length(fwidth(uv)), 0.00055);
float aaEdge = max(fwUV * 1.35, 0.0007);
// Base grid cell
float rowF = floor(uv.y / dy);
int baseY = int(rowF);
// Initialize with fallback
PerlageData outP;
outP.tangent2D = vec2(1.0, 0.0);
outP.radial2D = vec2(0.0, 1.0);
outP.rNorm = 0.5;
outP.grooveSlope = 0.0;
outP.edgeLip = 0.0;
outP.centerDimple = 0.0;
outP.cellTone = 0.5;
outP.rosetteMask = 0.0;
// Groove frequency: finer grooves on watchmaking perlage, bolder on dashboard engine-turning
float grooveCycles = mix(15.0, 22.0, openAmount);
// Paint-order shingled iteration: lower rows & columns first, later stamps overwrite earlier!
for (int iy = -1; iy <= 2; iy++) {
int gy = baseY + iy;
// Brick stagger every odd row
float rowShift = (mod(float(gy), 2.0) > 0.5) ? (0.5 * dx) : 0.0;
int baseX = int(floor((uv.x - rowShift) / dx));
for (int ix = -1; ix <= 2; ix++) {
int gx = baseX + ix;
vec2 gid = vec2(float(gx), float(gy));
vec2 rnd = hash22(gid + vec2(17.3, 41.7));
// Tiny hand-indexed engine-turning micro-jitter (1.2% of pitch)
vec2 jitter = (rnd - 0.5) * pitch * 0.022;
vec2 center = vec2((float(gx) + 0.5) * dx + rowShift, (float(gy) + 0.5) * dy) + jitter;
vec2 rel = uv - center;
float dist = length(rel);
float rNorm = dist / rosetteR;
if (rNorm < 1.12) {
// Crisp fwidth-antialiased circular stamp boundary
float cov = 1.0 - smoothstep(rosetteR - aaEdge, rosetteR + aaEdge, dist);
if (cov > 0.001) {
vec2 radDir = (dist > 1e-5) ? (rel / dist) : vec2(1.0, 0.0);
vec2 tanDir = vec2(-radDir.y, radDir.x);
// Concentric abrasive swirl micro-grooves (band-limited via fwUV)
float phase = rNorm * grooveCycles * TAU + rnd.x * TAU;
float cyclesPerPx = (fwUV / rosetteR) * grooveCycles;
float grooveFade = 1.0 - smoothstep(0.28, 0.95, cyclesPerPx);
float fineFade = 1.0 - smoothstep(0.18, 0.65, cyclesPerPx * 2.1);
float g1 = cos(phase) * grooveFade;
float g2 = cos(phase * 2.13 + rnd.y * 5.0) * 0.42 * fineFade;
// Subtle angular scratch break-up (abrasive peg grit isn't mathematically uniform)
float ang = atan(rel.y, rel.x);
float gritMod = 0.72 + 0.28 * sin(ang * 3.0 + rnd.x * 12.0 + rNorm * 7.0);
float gSlope = (g1 + g2) * gritMod;
// Crisp outer rim ring left by the spinning peg perimeter
float rimBand = exp(-pow((rNorm - 0.955) / max(0.032, aaEdge / rosetteR * 1.4), 2.0));
// Subtle zero-velocity burnish dimple at exact center
float dimple = exp(-pow(rNorm / 0.11, 2.0));
// Subtle per-rosette pressure/tone variation
float tone = 0.88 + 0.24 * rnd.y;
outP.tangent2D = normalize(mix(outP.tangent2D, tanDir, cov));
outP.radial2D = normalize(mix(outP.radial2D, radDir, cov));
outP.rNorm = mix(outP.rNorm, clamp(rNorm, 0.0, 1.0), cov);
outP.grooveSlope = mix(outP.grooveSlope, gSlope, cov);
outP.edgeLip = mix(outP.edgeLip, rimBand, cov);
outP.centerDimple = mix(outP.centerDimple, dimple, cov);
outP.cellTone = mix(outP.cellTone, tone, cov);
outP.rosetteMask = max(outP.rosetteMask, cov);
}
}
}
}
return outP;
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 sampleStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base studio cyclorama (dark negative-fill charcoal for rich metallic contrast)
vec3 env = mix(vec3(0.035, 0.038, 0.044), vec3(0.085, 0.090, 0.100), smoothstep(-0.2, 0.65, y));
if (lightMode == 1) {
// Pure neutral grayscale studio
env = vec3(mix(0.040, 0.095, smoothstep(-0.2, 0.65, y)));
}
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.28, 0.82, 0.42));
if (lightMode == 2) sb1Dir = normalize(vec3(0.86, 0.28, 0.38)); // Grazing raking strip
if (lightMode == 3) sb1Dir = normalize(vec3(-0.18, 0.46, -0.86)); // Strong backlit rim box
float sb1Dot = dot(d, sb1Dir);
float sb1 = smoothstep(0.68, 0.94, sb1Dot);
float sb1Core = smoothstep(0.88, 0.985, sb1Dot);
// Secondary crisp rim/accent strip light
vec3 sb2Dir = normalize(vec3(-0.76, 0.46, -0.45));
if (lightMode == 2) sb2Dir = normalize(vec3(-0.82, 0.22, -0.52));
if (lightMode == 3) sb2Dir = normalize(vec3(0.32, 0.72, 0.60));
float sb2 = smoothstep(0.72, 0.95, dot(d, sb2Dir));
// Warm/cool studio color grading by lightMode
vec3 cKey = vec3(1.04, 0.99, 0.93);
vec3 cRim = vec3(0.86, 0.94, 1.06);
if (lightMode == 1) {
cKey = vec3(1.0);
cRim = vec3(1.0);
} else if (lightMode == 2) {
cKey = vec3(1.08, 0.96, 0.84);
cRim = vec3(0.78, 0.88, 1.05);
} else if (lightMode == 3) {
cKey = vec3(1.05, 1.02, 0.98);
cRim = vec3(0.92, 0.95, 1.02);
}
float keyGain = (lightMode == 3) ? 2.5 : 2.6;
float rimGain = (lightMode == 3) ? 1.15 : 1.65;
env += cKey * (sb1 * 1.15 + sb1Core * keyGain);
env += cRim * (sb2 * rimGain);
// Crisp horizontal studio light-ribbon around horizon for anglage edge glints
float horizonStrip = exp(-pow((y - 0.14) * 9.5, 2.0)) * (0.45 + 0.55 * smoothstep(-0.2, 0.8, sin(atan(d.z, d.x) * 2.0 + 0.8)));
vec3 stripCol = (lightMode == 1) ? vec3(0.55) : vec3(0.52, 0.56, 0.62);
env += stripCol * horizonStrip;
// Dark negative-fill flags (absorptive matte black studio cards for high-contrast metal sculpting)
float flag1 = smoothstep(0.74, 0.90, dot(d, normalize(vec3(-0.35, 0.52, 0.78))));
float flag2 = smoothstep(0.78, 0.92, dot(d, normalize(vec3(0.78, 0.42, -0.45))));
float negFill = max(flag1, flag2);
env *= mix(1.0, 0.14, negFill);
return env;
}
// ============================================================================
// Physically-Based Anisotropic GGX & Isotropic GGX BRDF
// ============================================================================
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float a2 = ax * ay;
vec3 v = vec3(ay * ToH, ax * BoH, a2 * NoH);
float v2 = dot(v, v);
float w2 = a2 / max(v2, 1e-6);
return a2 * w2 * w2 / PI;
}
float V_SmithGGX_Aniso(float NoV, float NoL, float ToV, float BoV, float ToL, float BoL, float ax, float ay) {
float lambdaV = NoL * length(vec3(ax * ToV, ay * BoV, NoV));
float lambdaL = NoV * length(vec3(ax * ToL, ay * BoL, NoL));
return 0.5 / max(lambdaV + lambdaL, 1e-5);
}
vec3 evalAnisoSpecular(vec3 N, vec3 V, vec3 L, vec3 T, vec3 B, vec3 F0, float alphaT, float alphaB) {
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float ToH = dot(T, H);
float BoH = dot(B, H);
float ToV = dot(T, V);
float BoV = dot(B, V);
float ToL = dot(T, L);
float BoL = dot(B, L);
float D = D_GGX_Aniso(NoH, ToH, BoH, alphaT, alphaB);
float G = V_SmithGGX_Aniso(NoV, NoL, ToV, BoV, ToL, BoL, alphaT, alphaB);
vec3 F = fresnelSchlick(VoH, F0);
return D * G * F * NoL;
}
vec3 evalIsoSpecular(vec3 N, vec3 V, vec3 L, vec3 F0, float rough) {
float a = max(rough * rough, 0.0018);
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float a2 = a * a;
float dDen = NoH * NoH * (a2 - 1.0) + 1.0;
float D = a2 / max(PI * dDen * dDen, 1e-6);
float k = (rough + 1.0) * (rough + 1.0) * 0.125;
float gV = NoV / (NoV * (1.0 - k) + k);
float gL = NoL / (NoL * (1.0 - k) + k);
vec3 F = fresnelSchlick(VoH, F0);
return (D * gV * gL * F) / max(4.0 * NoV, 1e-4);
}
// ============================================================================
// ACES Filmic Tone Mapping
// ============================================================================
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);
}
// ============================================================================
// Lighting Setup by uLightMode
// ============================================================================
void getStudioLights(
int lightMode,
out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol
) {
if (lightMode == 0) {
// Hero luxury watch photography rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.85, 2.74, 2.60);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(0.95, 1.02, 1.14);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.65, 1.72, 1.85);
} else if (lightMode == 1) {
// Pure neutral grayscale rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.80);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(1.02);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.70);
} else if (lightMode == 2) {
// Grazing raking light to accentuate perlage grooves & anglage
keyDir = normalize(vec3(0.88, 0.25, 0.40));
keyCol = vec3(3.45, 3.10, 2.72);
fillDir = normalize(vec3(-0.82, 0.26, -0.50));
fillCol = vec3(0.88, 0.98, 1.15);
rimDir = normalize(vec3(-0.20, 0.62, -0.76));
rimCol = vec3(1.25, 1.30, 1.40);
} else {
// Backlit dramatic rim lighting
keyDir = normalize(vec3(-0.22, 0.42, -0.88));
keyCol = vec3(2.35, 2.30, 2.22);
fillDir = normalize(vec3(0.55, 0.64, 0.54));
fillCol = vec3(0.88, 0.90, 0.96);
rimDir = normalize(vec3(-0.78, 0.36, -0.50));
rimCol = vec3(1.25, 1.30, 1.40);
}
}
// ============================================================================
// Main Shading Function
// ============================================================================
vec3 shadeSurface(vec3 p, vec3 N, vec3 V, bool isBench) {
int mat = identifyMaterial(p, N, isBench);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getStudioLights(uLightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Hero-only soft shadows with required bias p + N * 0.014
float shKey = calcSoftShadow(p + N * 0.014, keyDir, 10.0);
float shRim = calcSoftShadow(p + N * 0.014, rimDir, 8.0);
float ao = calcAO(p, N);
// ------------------------------------------------------------------------
// Dark Horologist's Studio Bench (Micro-grained anodized slate)
// ------------------------------------------------------------------------
if (mat == MAT_BENCH) {
float grain1 = valueNoise(p.xz * 26.0);
float grain2 = valueNoise(p.xz * 82.0 + 7.3);
float micro = (grain1 * 0.6 + grain2 * 0.4);
vec3 baseCol = (uLightMode == 1)
? vec3(0.042 + 0.012 * micro)
: vec3(0.038, 0.041, 0.048) + vec3(0.010, 0.011, 0.013) * micro;
// Subtle radial studio pool-of-light vignette on bench
float distC = length(p.xz);
float spot = exp(-distC * distC * 0.14);
vec3 benchN = normalize(N + vec3((grain1 - 0.5) * 0.025, 0.0, (grain2 - 0.5) * 0.025));
float rough = 0.48 + 0.12 * micro;
vec3 F0 = vec3(0.045);
vec3 col = vec3(0.0);
col += (baseCol / PI * max(dot(benchN, keyDir), 0.0) + evalIsoSpecular(benchN, V, keyDir, F0, rough)) * keyCol * shKey;
col += (baseCol / PI * max(dot(benchN, fillDir), 0.0) + evalIsoSpecular(benchN, V, fillDir, F0, rough) * 0.6) * fillCol;
col += (baseCol / PI * max(dot(benchN, rimDir), 0.0) + evalIsoSpecular(benchN, V, rimDir, F0, rough)) * rimCol * shRim;
vec3 R = reflect(-V, benchN);
vec3 envRefl = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(benchN, V), 0.0), F0);
col += envRefl * Fenv * 0.35 * ao;
col += baseCol * (0.28 + 0.25 * spot) * ao;
// Soft grounding contact shadow right under the watch movement
float contact = smoothstep(0.0, 0.18, max(length(p.xz) - 1.20, 0.0));
col *= mix(0.45, 1.0, contact);
return col;
}
// ------------------------------------------------------------------------
// Engine-Turned Perlage Rhodium (Mainplate & Bridge Top)
// ------------------------------------------------------------------------
if (mat == MAT_BRIDGE_TOP || mat == MAT_MAINPLATE) {
float scaleMul = (mat == MAT_MAINPLATE) ? 0.78 : 1.0;
PerlageData perl = evalPerlage(p.xz, uOpen, scaleMul);
// Rhodium-plated brass F0 (cool, lustrous silvery-white precious metal)
vec3 F0 = (uLightMode == 1)
? vec3(0.77, 0.77, 0.77)
: vec3(0.76, 0.775, 0.805);
F0 *= perl.cellTone;
// Slight contrast dip right at the outer circular abrasive seam & central dimple
F0 *= (1.0 - 0.07 * perl.edgeLip - 0.05 * perl.centerDimple);
// Build tangent frame aligned with concentric abrasive grooves
vec3 T = normalize(vec3(perl.tangent2D.x, 0.0, perl.tangent2D.y));
T = normalize(T - N * dot(T, N));
vec3 B = normalize(cross(N, T));
// Perturb surface normal radially by the band-limited concentric micro-grooves
float grooveAmp = mix(0.065, 0.115, uOpen);
vec3 Np = normalize(N + B * (perl.grooveSlope * grooveAmp + perl.edgeLip * 0.085));
// Re-orthogonalize T and B to perturbed normal
T = normalize(T - Np * dot(T, Np));
B = normalize(cross(Np, T));
// Anisotropic roughness: tight along grooves (T), broad across grooves (B)
// This creates the crisp rotating specular ring / bowtie inside every rosette!
float alphaT = mix(0.065, 0.052, uOpen) + perl.centerDimple * 0.08;
float alphaB = mix(0.36, 0.44, uOpen) - perl.centerDimple * 0.14;
vec3 col = vec3(0.0);
// Direct anisotropic specular from studio softboxes
col += evalAnisoSpecular(Np, V, keyDir, T, B, F0, alphaT, alphaB) * keyCol * shKey * 0.92;
col += evalAnisoSpecular(Np, V, fillDir, T, B, F0, alphaT, alphaB) * fillCol * 0.88;
col += evalAnisoSpecular(Np, V, rimDir, T, B, F0, alphaT, alphaB) * rimCol * shRim * 0.90;
// Anisotropic studio environment sampling (bend reflection normal toward radial bitangent)
vec3 anisoDir = cross(T, V);
vec3 anisoN = normalize(cross(anisoDir, T));
float bendAmt = clamp(alphaB * 0.85, 0.0, 0.55);
vec3 bentN = normalize(mix(Np, anisoN, bendAmt));
vec3 R0 = reflect(-V, bentN);
// Sample 3 spread rays along the anisotropic radial fan for rich rotating ring glints
vec3 R1 = normalize(R0 + B * 0.16);
vec3 R2 = normalize(R0 - B * 0.16);
vec3 envCol = (sampleStudioEnv(R0, uLightMode) * 0.50 +
sampleStudioEnv(R1, uLightMode) * 0.25 +
sampleStudioEnv(R2, uLightMode) * 0.25);
vec3 Fenv = fresnelSchlick(max(dot(Np, V), 0.0), F0);
// Crisp rotating highlight ring accent where view/key half-vector aligns radially
vec3 Hkey = normalize(V + keyDir);
float ringAlign = 1.0 - abs(dot(T, Hkey));
float ringGlow = pow(clamp(ringAlign, 0.0, 1.0), mix(10.0, 14.0, uOpen));
float radialProfile = smoothstep(0.06, 0.25, perl.rNorm) * (1.0 - 0.25 * smoothstep(0.82, 1.0, perl.rNorm));
col += envCol * Fenv * (0.64 + 0.28 * ringGlow * radialProfile) * ao;
// Subtle crisp glint along the circular rosette rim
col += F0 * perl.edgeLip * (0.12 * shKey + 0.06) * ao;
// Subtle satin metallic floor so dark negative-fill areas stay rich (never crushed to 0)
col += F0 * 0.055 * ao;
return col;
}
// ------------------------------------------------------------------------
// Mirror-Polished 45-deg Rhodium Anglage Bevels & Walls
// ------------------------------------------------------------------------
if (mat == MAT_ANGLAGE) {
vec3 F0 = (uLightMode == 1) ? vec3(0.84) : vec3(0.82, 0.84, 0.87);
float rough = 0.055;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.92 * ao;
col += F0 * 0.035 * ao;
return col;
}
// ------------------------------------------------------------------------
// 18k Yellow Gold Chaton
// ------------------------------------------------------------------------
if (mat == MAT_GOLD_CHATON) {
vec3 F0 = vec3(1.00, 0.78, 0.34);
// Fine circular lathe-turning micro-brushing on the gold chaton
vec2 q = p.xz - C_JEWEL;
float r = length(q);
float lathe = sin(r * 420.0) * (1.0 - smoothstep(0.002, 0.008, length(fwidth(q))));
vec3 radDir = (r > 1e-5) ? normalize(vec3(q.x, 0.0, q.y)) : vec3(1.0, 0.0, 0.0);
vec3 Ng = normalize(N + radDir * lathe * 0.025);
float rough = 0.11;
vec3 R = reflect(-V, Ng);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(Ng, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(Ng, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(Ng, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(Ng, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += F0 * 0.05 * ao;
return col;
}
// ------------------------------------------------------------------------
// Synthetic Corundum Ruby Jewel Bearing (IOR 1.77, Deep Crimson-Magenta Glow)
// ------------------------------------------------------------------------
if (mat == MAT_RUBY) {
vec3 F0 = vec3(0.077); // Corundum IOR = 1.77
float rough = 0.035;
float NoV = max(dot(N, V), 0.0);
vec3 F = fresnelSchlick(NoV, F0);
// Refracted internal ray & Beer-Lambert ruby absorption + internal caustic bounce
vec3 refr = refract(-V, N, 1.0 / 1.77);
vec2 q = p.xz - C_JEWEL;
float rNorm = clamp(length(q) / 0.115, 0.0, 1.0);
// Deep pigeon-blood / Swiss watchmaking synthetic ruby color
vec3 rubyDeep = vec3(0.32, 0.012, 0.065);
vec3 rubyBright = vec3(0.95, 0.055, 0.240);
// Internal light scatter & caustic opposite the key light
float caustic = pow(clamp(dot(normalize(-refr), keyDir) * 0.5 + 0.5, 0.0, 1.0), 3.0);
float rimGlow = smoothstep(0.25, 0.92, rNorm);
vec3 internal = mix(rubyDeep, rubyBright, 0.35 + 0.45 * caustic * (0.4 + 0.6 * shKey) + 0.28 * rimGlow);
// Subtle warm golden reflection from the surrounding gold chaton wall
internal += vec3(0.45, 0.18, 0.04) * pow(rimGlow, 2.5) * 0.45;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 col = internal * (1.0 - F) * (0.55 + 0.45 * ao);
col += env * F * 1.15;
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.35;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
return col;
}
// ------------------------------------------------------------------------
// Heat-Blued Steel Screw (Thin-Film Oxide Interference Royal Blue)
// ------------------------------------------------------------------------
if (mat == MAT_BLUED_STEEL) {
float NoV = max(dot(N, V), 0.0);
// Thin-film thermally blued steel: deep cornflower-cobalt shifting to indigo-cyan at grazing
vec3 blueCore = vec3(0.08, 0.22, 0.66);
vec3 blueGraze = vec3(0.28, 0.54, 0.92);
vec3 F0 = mix(blueGraze, blueCore, pow(NoV, 0.65));
float rough = 0.075;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(NoV, F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.2;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += blueCore * 0.08 * ao;
return col;
}
// ------------------------------------------------------------------------
// Gilt Brass Escapement Wheel Glimpse (Inside Mainplate Well)
// ------------------------------------------------------------------------
vec3 F0 = vec3(0.88, 0.70, 0.36);
float rough = 0.18;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.78 * ao;
col += F0 * 0.04 * ao;
return col;
}
// ============================================================================
// Raymarching & Camera
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / min(uResolution.x, uResolution.y);
// Gentle orbital motion so anisotropic perlage rings continuously sweep
float orbit = 0.35 + uTime * 0.14;
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x Macro loupe view centered on ruby chaton + blued screw + shingled perlage + anglage
ta = vec3(0.08, 0.19, -0.02);
ro = ta + vec3(sin(orbit) * 0.78, 0.98, cos(orbit) * 0.78);
fov = 2.65;
} else {
// Hero three-quarter luxury product framing with generous bench margin
ta = vec3(0.00, 0.08, 0.00);
ro = vec3(sin(orbit) * 3.10, 2.95, cos(orbit) * 3.10);
fov = 1.85;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
bool hit = false;
const float tMax = 14.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.00045) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
vec3 N = calcNormal(p);
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
col = shadeSurface(p, N, -rd, isBench);
// Subtle atmospheric depth fade into studio backdrop at far bench horizon
float fog = 1.0 - exp(-max(t - 4.0, 0.0) * 0.12);
vec3 bgCol = sampleStudioEnv(rd, uLightMode) * 0.32;
col = mix(col, bgCol, clamp(fog, 0.0, 0.92));
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.32;
}
// Subtle photographic vignette
float vig = 1.0 - 0.18 * dot(uv, uv);
col *= clamp(vig, 0.72, 1.0);
// Pure neutral grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Exposure & ACES filmic tone mapping (headroom capped <= 245 sRGB, 0% clipped)
col *= 0.76;
col = acesToneMap(col);
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2)) * 0.94;
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Engine-Turned Guilloche — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/07_engine_turned_perlon_guilloche.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// ============================================================================
// Constants & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_MAINPLATE = 1; // Lower rhodium perlage deck
const int MAT_BRIDGE_TOP = 2; // Upper rhodium perlage bridge top
const int MAT_ANGLAGE = 3; // Mirror-polished 45-deg rhodium bevels & walls
const int MAT_GOLD_CHATON = 4; // 18k yellow gold jewel setting
const int MAT_RUBY = 5; // Synthetic corundum ruby jewel bearing
const int MAT_BLUED_STEEL = 6; // Heat-blued steel screw
const int MAT_BRASS_WHEEL = 7; // Subtle gilt escapement wheel glimpse
// Key horological feature centers in XZ
const vec2 C_JEWEL = vec2(-0.16, 0.08);
const vec2 C_SCREW1 = vec2( 0.40, -0.14);
const vec2 C_SCREW2 = vec2(-0.56, -0.34);
const vec2 C_WELL = vec2( 0.44, 0.46);
// ============================================================================
// Math & Hashing Helpers
// ============================================================================
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i);
float b = hash21(i + vec2(1.0, 0.0));
float c = hash21(i + vec2(0.0, 1.0));
float d = hash21(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float 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);
}
// ============================================================================
// 2D & 3D Horological SDF Primitives
// ============================================================================
float sdExtrudeChamfer(float d2d, float y, float yMin, float yMax, float bevel) {
float h = 0.5 * (yMax - yMin);
float yc = y - 0.5 * (yMin + yMax);
vec2 w = vec2(d2d, abs(yc) - h);
float dBox = min(max(w.x, w.y), 0.0) + length(max(w, 0.0));
// 45-degree chamfer on top edge
float dChamfer = (d2d + (yc - h)) * 0.70710678 + bevel * 0.70710678;
return max(dBox, dChamfer);
}
float sdCylinder(vec3 p, vec2 c, float r, float yMin, float yMax, float bevel) {
float d2d = length(p.xz - c) - r;
return sdExtrudeChamfer(d2d, p.y, yMin, yMax, bevel);
}
// Curved Swiss watch movement bridge silhouette in XZ
float sdBridge2D(vec2 xz) {
float dOuter = length(xz) - 1.10;
// Sweeping horological balance-cock cutaway on upper-right quadrant
float dCut1 = length(xz - vec2(0.58, 0.54)) - 0.62;
// Secondary graceful waist curve on upper-left
float dCut2 = length(xz - vec2(-0.88, 0.72)) - 0.42;
// Combine outer disc minus smooth cutaways
float d = smax(dOuter, -dCut1, 0.09);
d = smax(d, -dCut2, 0.08);
// Subtle decorative curved extension lobe around primary screw
float dLobe = length(xz - vec2(0.42, -0.14)) - 0.34;
d = smin(d, dLobe, 0.14);
return d;
}
// Mainplate outer silhouette in XZ
float sdMainplate2D(vec2 xz) {
float d = length(xz) - 1.24;
// Subtle movement casing notch on outer rim
float dNotch = length(xz - vec2(-1.26, 0.0)) - 0.12;
d = smax(d, -dNotch, 0.03);
return d;
}
// ============================================================================
// Hero Assembly SDF (Watch Movement Mainplate, Bridge, Jewel, Chaton, Screws)
// ============================================================================
float mapMainplate(vec3 p) {
float d2d = sdMainplate2D(p.xz);
float d = sdExtrudeChamfer(d2d, p.y, 0.0, 0.105, 0.014);
// Recessed escapement bearing well on exposed lower mainplate
float rWell = length(p.xz - C_WELL);
float dWell = max(rWell - 0.23, -(p.y - 0.055));
float dWellChamfer = (-(rWell - 0.23) + (p.y - 0.105)) * 0.70710678;
dWell = min(dWell, -dWellChamfer + 0.012);
d = max(d, -dWell);
return d;
}
float mapBridge(vec3 p) {
float d2d = sdBridge2D(p.xz);
// Bridge sits from y = 0.095 to y = 0.225 with prominent 45-deg mirror anglage
float d = sdExtrudeChamfer(d2d, p.y, 0.095, 0.225, 0.030);
// Conical jewel countersink (oil sink + chaton bore)
float rJ = length(p.xz - C_JEWEL);
float dJ = max(rJ - 0.188, -(p.y - 0.150));
float dJBevel = ((rJ - 0.188) - (p.y - 0.225)) * 0.70710678 - 0.018;
d = max(d, -max(dJ, dJBevel));
// Primary screw counterbore
float rS1 = length(p.xz - C_SCREW1);
float dS1 = max(rS1 - 0.158, -(p.y - 0.155));
float dS1Bevel = ((rS1 - 0.158) - (p.y - 0.225)) * 0.70710678 - 0.012;
d = max(d, -max(dS1, dS1Bevel));
// Secondary screw counterbore
float rS2 = length(p.xz - C_SCREW2);
float dS2 = max(rS2 - 0.105, -(p.y - 0.165));
float dS2Bevel = ((rS2 - 0.105) - (p.y - 0.225)) * 0.70710678 - 0.010;
d = max(d, -max(dS2, dS2Bevel));
return d;
}
float mapGoldChaton(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Gold setting ring seated in the countersink
float dOuter = r - 0.174;
float dInner = 0.114 - r;
float d2d = max(dOuter, dInner);
float dRing = sdExtrudeChamfer(d2d, p.y, 0.148, 0.214, 0.011);
// Subtle torus lip on top of gold chaton
vec2 tor = vec2(r - 0.144, p.y - 0.207);
float dLip = length(tor) - 0.014;
return smin(dRing, dLip, 0.008);
}
float mapRuby(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Synthetic corundum ruby dome with central pivot hole & oil sink
float dCyl = sdExtrudeChamfer(r - 0.116, p.y, 0.148, 0.204, 0.016);
// Gentle concave oil-sink bowl on top of the ruby
float dBowl = length(vec3(q.x, (p.y - 0.260) * 1.15, q.y)) - 0.088;
float dRuby = max(dCyl, -dBowl);
// Central pivot bore
float dBore = 0.030 - r;
dRuby = max(dRuby, dBore);
return dRuby;
}
float mapPivot(vec3 p) {
// Polished steel pivot pin inside the ruby center
vec2 q = p.xz - C_JEWEL;
float r = length(q);
return sdExtrudeChamfer(r - 0.025, p.y, 0.140, 0.194, 0.006);
}
float mapScrewSingle(vec3 p, vec2 c, float radius, float yBase, float yTop, float angle) {
vec2 q = (p.xz - c) * rot2(angle);
float r = length(q);
float dHead = sdExtrudeChamfer(r - radius, p.y, yBase, yTop, radius * 0.14);
// Subtle dome curvature on screw head
float dDome = length(vec3(q.x, (p.y - (yTop - radius * 2.2)) * 1.0, q.y)) - radius * 2.24;
dHead = max(dHead, dDome);
// Precision horological screwdriver slot with beveled mouth
float slotHalfW = radius * 0.145;
float slotDepth = 0.030;
float dSlotBox = max(abs(q.y) - slotHalfW, -(p.y - (yTop - slotDepth)));
float dSlotBevel = (abs(q.y) - slotHalfW - (p.y - yTop)) * 0.70710678 - 0.005;
float dSlot = max(dSlotBox, dSlotBevel);
return max(dHead, -dSlot);
}
float mapBluedScrews(vec3 p) {
float s1 = mapScrewSingle(p, C_SCREW1, 0.142, 0.150, 0.232, 0.58);
float s2 = mapScrewSingle(p, C_SCREW2, 0.092, 0.160, 0.228, -0.82);
return min(s1, s2);
}
float mapBrassWheel(vec3 p) {
// Escapement wheel ring + hub visible inside the mainplate bearing well
vec2 q = p.xz - C_WELL;
float r = length(q);
float ang = atan(q.y, q.x) + uTime * 0.18;
float teeth = 0.009 * smoothstep(-0.2, 0.6, cos(ang * 18.0));
float dRim = max((r - (0.195 + teeth)), 0.132 - r);
float dHub = r - 0.056;
// 4 spokes
vec2 qs = q * rot2(uTime * 0.18);
float dSpoke = min(abs(qs.x), abs(qs.y)) - 0.016;
dSpoke = max(dSpoke, r - 0.16);
float d2d = min(min(dRim, dHub), dSpoke);
return sdExtrudeChamfer(d2d, p.y, 0.052, 0.088, 0.005);
}
float mapHero(vec3 p) {
float d = mapMainplate(p);
d = min(d, mapBridge(p));
d = min(d, mapGoldChaton(p));
d = min(d, mapRuby(p));
d = min(d, mapPivot(p));
d = min(d, mapBluedScrews(p));
d = min(d, mapBrassWheel(p));
return d;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormal(vec3 p) {
const float e = 0.00045;
const vec2 k = vec2(1.0, -1.0);
return normalize(
k.xyy * mapScene(p + k.xyy * e) +
k.yyx * mapScene(p + k.yyx * e) +
k.yxy * mapScene(p + k.yxy * e) +
k.xxx * mapScene(p + k.xxx * e)
);
}
// ============================================================================
// Hero-Only Soft Shadow (Zero-Bug Rule #3: clean res = min(res, k*h/t), no ph!)
// ============================================================================
float calcSoftShadow(vec3 ro, vec3 rd, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.0005) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > 4.5) 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 = 0; i < 5; i++) {
float h = 0.012 + 0.045 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Material Identification
// ============================================================================
int identifyMaterial(vec3 p, vec3 N, bool isBench) {
if (isBench) return MAT_BENCH;
float dMain = mapMainplate(p);
float dBrdg = mapBridge(p);
float dChat = mapGoldChaton(p);
float dRuby = mapRuby(p);
float dPiv = mapPivot(p);
float dScrew = mapBluedScrews(p);
float dWheel = mapBrassWheel(p);
float dMin = dMain;
int mat = MAT_MAINPLATE;
if (dBrdg < dMin) { dMin = dBrdg; mat = MAT_BRIDGE_TOP; }
if (dChat < dMin) { dMin = dChat; mat = MAT_GOLD_CHATON; }
if (dRuby < dMin) { dMin = dRuby; mat = MAT_RUBY; }
if (dPiv < dMin) { dMin = dPiv; mat = MAT_ANGLAGE; }
if (dScrew < dMin) { dMin = dScrew; mat = MAT_BLUED_STEEL; }
if (dWheel < dMin) { dMin = dWheel; mat = MAT_BRASS_WHEEL; }
// Separate flat perlage top surfaces from mirror-polished 45-deg anglage bevels & walls
if (mat == MAT_BRIDGE_TOP) {
if (N.y < 0.965 || p.y < 0.219) {
mat = MAT_ANGLAGE;
}
} else if (mat == MAT_MAINPLATE) {
if (N.y < 0.965 || p.y < 0.099) {
mat = MAT_ANGLAGE;
}
}
return mat;
}
// ============================================================================
// Shingled Engine-Turned Perlage (Circular Abrasive-Swirl Rosettes)
// ============================================================================
struct PerlageData {
vec2 tangent2D; // Concentric groove tangent in XZ (unit vector)
vec2 radial2D; // Radial direction from winning rosette center
float rNorm; // Normalized radius [0..1] inside winning rosette
float grooveSlope; // Band-limited concentric micro-groove radial derivative
float edgeLip; // Crisp outer abrasive ring highlight/seam
float centerDimple;// Soft zero-velocity center dimple
float cellTone; // Subtle rosette-to-rosette burnishing variation
float rosetteMask; // Coverage mask
};
PerlageData evalPerlage(vec2 uv, float openAmount, float scaleMul) {
// uOpen = 0.0 -> fine Swiss watchmaking perlage (small, delicate overlapping circles)
// uOpen = 1.0 -> bold vintage automotive dashboard engine-turning (large, punchy rosettes)
float pitch = mix(0.145, 0.340, clamp(openAmount, 0.0, 1.0)) * scaleMul;
float dx = pitch;
float dy = pitch * 0.72;
float rosetteR = pitch * mix(0.74, 0.78, openAmount);
// Analytic screen-space pixel footprint for fwidth antialiasing
float fwUV = max(length(fwidth(uv)), 0.00055);
float aaEdge = max(fwUV * 1.35, 0.0007);
// Base grid cell
float rowF = floor(uv.y / dy);
int baseY = int(rowF);
// Initialize with fallback
PerlageData outP;
outP.tangent2D = vec2(1.0, 0.0);
outP.radial2D = vec2(0.0, 1.0);
outP.rNorm = 0.5;
outP.grooveSlope = 0.0;
outP.edgeLip = 0.0;
outP.centerDimple = 0.0;
outP.cellTone = 0.5;
outP.rosetteMask = 0.0;
// Groove frequency: finer grooves on watchmaking perlage, bolder on dashboard engine-turning
float grooveCycles = mix(15.0, 22.0, openAmount);
// Paint-order shingled iteration: lower rows & columns first, later stamps overwrite earlier!
for (int iy = -1; iy <= 2; iy++) {
int gy = baseY + iy;
// Brick stagger every odd row
float rowShift = (mod(float(gy), 2.0) > 0.5) ? (0.5 * dx) : 0.0;
int baseX = int(floor((uv.x - rowShift) / dx));
for (int ix = -1; ix <= 2; ix++) {
int gx = baseX + ix;
vec2 gid = vec2(float(gx), float(gy));
vec2 rnd = hash22(gid + vec2(17.3, 41.7));
// Tiny hand-indexed engine-turning micro-jitter (1.2% of pitch)
vec2 jitter = (rnd - 0.5) * pitch * 0.022;
vec2 center = vec2((float(gx) + 0.5) * dx + rowShift, (float(gy) + 0.5) * dy) + jitter;
vec2 rel = uv - center;
float dist = length(rel);
float rNorm = dist / rosetteR;
if (rNorm < 1.12) {
// Crisp fwidth-antialiased circular stamp boundary
float cov = 1.0 - smoothstep(rosetteR - aaEdge, rosetteR + aaEdge, dist);
if (cov > 0.001) {
vec2 radDir = (dist > 1e-5) ? (rel / dist) : vec2(1.0, 0.0);
vec2 tanDir = vec2(-radDir.y, radDir.x);
// Concentric abrasive swirl micro-grooves (band-limited via fwUV)
float phase = rNorm * grooveCycles * TAU + rnd.x * TAU;
float cyclesPerPx = (fwUV / rosetteR) * grooveCycles;
float grooveFade = 1.0 - smoothstep(0.28, 0.95, cyclesPerPx);
float fineFade = 1.0 - smoothstep(0.18, 0.65, cyclesPerPx * 2.1);
float g1 = cos(phase) * grooveFade;
float g2 = cos(phase * 2.13 + rnd.y * 5.0) * 0.42 * fineFade;
// Subtle angular scratch break-up (abrasive peg grit isn't mathematically uniform)
float ang = atan(rel.y, rel.x);
float gritMod = 0.72 + 0.28 * sin(ang * 3.0 + rnd.x * 12.0 + rNorm * 7.0);
float gSlope = (g1 + g2) * gritMod;
// Crisp outer rim ring left by the spinning peg perimeter
float rimBand = exp(-pow((rNorm - 0.955) / max(0.032, aaEdge / rosetteR * 1.4), 2.0));
// Subtle zero-velocity burnish dimple at exact center
float dimple = exp(-pow(rNorm / 0.11, 2.0));
// Subtle per-rosette pressure/tone variation
float tone = 0.88 + 0.24 * rnd.y;
outP.tangent2D = normalize(mix(outP.tangent2D, tanDir, cov));
outP.radial2D = normalize(mix(outP.radial2D, radDir, cov));
outP.rNorm = mix(outP.rNorm, clamp(rNorm, 0.0, 1.0), cov);
outP.grooveSlope = mix(outP.grooveSlope, gSlope, cov);
outP.edgeLip = mix(outP.edgeLip, rimBand, cov);
outP.centerDimple = mix(outP.centerDimple, dimple, cov);
outP.cellTone = mix(outP.cellTone, tone, cov);
outP.rosetteMask = max(outP.rosetteMask, cov);
}
}
}
}
return outP;
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 sampleStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base studio cyclorama (dark negative-fill charcoal for rich metallic contrast)
vec3 env = mix(vec3(0.035, 0.038, 0.044), vec3(0.085, 0.090, 0.100), smoothstep(-0.2, 0.65, y));
if (lightMode == 1) {
// Pure neutral grayscale studio
env = vec3(mix(0.040, 0.095, smoothstep(-0.2, 0.65, y)));
}
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.28, 0.82, 0.42));
if (lightMode == 2) sb1Dir = normalize(vec3(0.86, 0.28, 0.38)); // Grazing raking strip
if (lightMode == 3) sb1Dir = normalize(vec3(-0.18, 0.46, -0.86)); // Strong backlit rim box
float sb1Dot = dot(d, sb1Dir);
float sb1 = smoothstep(0.68, 0.94, sb1Dot);
float sb1Core = smoothstep(0.88, 0.985, sb1Dot);
// Secondary crisp rim/accent strip light
vec3 sb2Dir = normalize(vec3(-0.76, 0.46, -0.45));
if (lightMode == 2) sb2Dir = normalize(vec3(-0.82, 0.22, -0.52));
if (lightMode == 3) sb2Dir = normalize(vec3(0.32, 0.72, 0.60));
float sb2 = smoothstep(0.72, 0.95, dot(d, sb2Dir));
// Warm/cool studio color grading by lightMode
vec3 cKey = vec3(1.04, 0.99, 0.93);
vec3 cRim = vec3(0.86, 0.94, 1.06);
if (lightMode == 1) {
cKey = vec3(1.0);
cRim = vec3(1.0);
} else if (lightMode == 2) {
cKey = vec3(1.08, 0.96, 0.84);
cRim = vec3(0.78, 0.88, 1.05);
} else if (lightMode == 3) {
cKey = vec3(1.05, 1.02, 0.98);
cRim = vec3(0.92, 0.95, 1.02);
}
float keyGain = (lightMode == 3) ? 2.5 : 2.6;
float rimGain = (lightMode == 3) ? 1.15 : 1.65;
env += cKey * (sb1 * 1.15 + sb1Core * keyGain);
env += cRim * (sb2 * rimGain);
// Crisp horizontal studio light-ribbon around horizon for anglage edge glints
float horizonStrip = exp(-pow((y - 0.14) * 9.5, 2.0)) * (0.45 + 0.55 * smoothstep(-0.2, 0.8, sin(atan(d.z, d.x) * 2.0 + 0.8)));
vec3 stripCol = (lightMode == 1) ? vec3(0.55) : vec3(0.52, 0.56, 0.62);
env += stripCol * horizonStrip;
// Dark negative-fill flags (absorptive matte black studio cards for high-contrast metal sculpting)
float flag1 = smoothstep(0.74, 0.90, dot(d, normalize(vec3(-0.35, 0.52, 0.78))));
float flag2 = smoothstep(0.78, 0.92, dot(d, normalize(vec3(0.78, 0.42, -0.45))));
float negFill = max(flag1, flag2);
env *= mix(1.0, 0.14, negFill);
return env;
}
// ============================================================================
// Physically-Based Anisotropic GGX & Isotropic GGX BRDF
// ============================================================================
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float a2 = ax * ay;
vec3 v = vec3(ay * ToH, ax * BoH, a2 * NoH);
float v2 = dot(v, v);
float w2 = a2 / max(v2, 1e-6);
return a2 * w2 * w2 / PI;
}
float V_SmithGGX_Aniso(float NoV, float NoL, float ToV, float BoV, float ToL, float BoL, float ax, float ay) {
float lambdaV = NoL * length(vec3(ax * ToV, ay * BoV, NoV));
float lambdaL = NoV * length(vec3(ax * ToL, ay * BoL, NoL));
return 0.5 / max(lambdaV + lambdaL, 1e-5);
}
vec3 evalAnisoSpecular(vec3 N, vec3 V, vec3 L, vec3 T, vec3 B, vec3 F0, float alphaT, float alphaB) {
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float ToH = dot(T, H);
float BoH = dot(B, H);
float ToV = dot(T, V);
float BoV = dot(B, V);
float ToL = dot(T, L);
float BoL = dot(B, L);
float D = D_GGX_Aniso(NoH, ToH, BoH, alphaT, alphaB);
float G = V_SmithGGX_Aniso(NoV, NoL, ToV, BoV, ToL, BoL, alphaT, alphaB);
vec3 F = fresnelSchlick(VoH, F0);
return D * G * F * NoL;
}
vec3 evalIsoSpecular(vec3 N, vec3 V, vec3 L, vec3 F0, float rough) {
float a = max(rough * rough, 0.0018);
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float a2 = a * a;
float dDen = NoH * NoH * (a2 - 1.0) + 1.0;
float D = a2 / max(PI * dDen * dDen, 1e-6);
float k = (rough + 1.0) * (rough + 1.0) * 0.125;
float gV = NoV / (NoV * (1.0 - k) + k);
float gL = NoL / (NoL * (1.0 - k) + k);
vec3 F = fresnelSchlick(VoH, F0);
return (D * gV * gL * F) / max(4.0 * NoV, 1e-4);
}
// ============================================================================
// ACES Filmic Tone Mapping
// ============================================================================
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);
}
// ============================================================================
// Lighting Setup by uLightMode
// ============================================================================
void getStudioLights(
int lightMode,
out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol
) {
if (lightMode == 0) {
// Hero luxury watch photography rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.85, 2.74, 2.60);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(0.95, 1.02, 1.14);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.65, 1.72, 1.85);
} else if (lightMode == 1) {
// Pure neutral grayscale rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.80);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(1.02);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.70);
} else if (lightMode == 2) {
// Grazing raking light to accentuate perlage grooves & anglage
keyDir = normalize(vec3(0.88, 0.25, 0.40));
keyCol = vec3(3.45, 3.10, 2.72);
fillDir = normalize(vec3(-0.82, 0.26, -0.50));
fillCol = vec3(0.88, 0.98, 1.15);
rimDir = normalize(vec3(-0.20, 0.62, -0.76));
rimCol = vec3(1.25, 1.30, 1.40);
} else {
// Backlit dramatic rim lighting
keyDir = normalize(vec3(-0.22, 0.42, -0.88));
keyCol = vec3(2.35, 2.30, 2.22);
fillDir = normalize(vec3(0.55, 0.64, 0.54));
fillCol = vec3(0.88, 0.90, 0.96);
rimDir = normalize(vec3(-0.78, 0.36, -0.50));
rimCol = vec3(1.25, 1.30, 1.40);
}
}
// ============================================================================
// Main Shading Function
// ============================================================================
vec3 shadeSurface(vec3 p, vec3 N, vec3 V, bool isBench) {
int mat = identifyMaterial(p, N, isBench);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getStudioLights(uLightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Hero-only soft shadows with required bias p + N * 0.014
float shKey = calcSoftShadow(p + N * 0.014, keyDir, 10.0);
float shRim = calcSoftShadow(p + N * 0.014, rimDir, 8.0);
float ao = calcAO(p, N);
// ------------------------------------------------------------------------
// Dark Horologist's Studio Bench (Micro-grained anodized slate)
// ------------------------------------------------------------------------
if (mat == MAT_BENCH) {
float grain1 = valueNoise(p.xz * 26.0);
float grain2 = valueNoise(p.xz * 82.0 + 7.3);
float micro = (grain1 * 0.6 + grain2 * 0.4);
vec3 baseCol = (uLightMode == 1)
? vec3(0.042 + 0.012 * micro)
: vec3(0.038, 0.041, 0.048) + vec3(0.010, 0.011, 0.013) * micro;
// Subtle radial studio pool-of-light vignette on bench
float distC = length(p.xz);
float spot = exp(-distC * distC * 0.14);
vec3 benchN = normalize(N + vec3((grain1 - 0.5) * 0.025, 0.0, (grain2 - 0.5) * 0.025));
float rough = 0.48 + 0.12 * micro;
vec3 F0 = vec3(0.045);
vec3 col = vec3(0.0);
col += (baseCol / PI * max(dot(benchN, keyDir), 0.0) + evalIsoSpecular(benchN, V, keyDir, F0, rough)) * keyCol * shKey;
col += (baseCol / PI * max(dot(benchN, fillDir), 0.0) + evalIsoSpecular(benchN, V, fillDir, F0, rough) * 0.6) * fillCol;
col += (baseCol / PI * max(dot(benchN, rimDir), 0.0) + evalIsoSpecular(benchN, V, rimDir, F0, rough)) * rimCol * shRim;
vec3 R = reflect(-V, benchN);
vec3 envRefl = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(benchN, V), 0.0), F0);
col += envRefl * Fenv * 0.35 * ao;
col += baseCol * (0.28 + 0.25 * spot) * ao;
// Soft grounding contact shadow right under the watch movement
float contact = smoothstep(0.0, 0.18, max(length(p.xz) - 1.20, 0.0));
col *= mix(0.45, 1.0, contact);
return col;
}
// ------------------------------------------------------------------------
// Engine-Turned Perlage Rhodium (Mainplate & Bridge Top)
// ------------------------------------------------------------------------
if (mat == MAT_BRIDGE_TOP || mat == MAT_MAINPLATE) {
float scaleMul = (mat == MAT_MAINPLATE) ? 0.78 : 1.0;
PerlageData perl = evalPerlage(p.xz, uOpen, scaleMul);
// Rhodium-plated brass F0 (cool, lustrous silvery-white precious metal)
vec3 F0 = (uLightMode == 1)
? vec3(0.77, 0.77, 0.77)
: vec3(0.76, 0.775, 0.805);
F0 *= perl.cellTone;
// Slight contrast dip right at the outer circular abrasive seam & central dimple
F0 *= (1.0 - 0.07 * perl.edgeLip - 0.05 * perl.centerDimple);
// Build tangent frame aligned with concentric abrasive grooves
vec3 T = normalize(vec3(perl.tangent2D.x, 0.0, perl.tangent2D.y));
T = normalize(T - N * dot(T, N));
vec3 B = normalize(cross(N, T));
// Perturb surface normal radially by the band-limited concentric micro-grooves
float grooveAmp = mix(0.065, 0.115, uOpen);
vec3 Np = normalize(N + B * (perl.grooveSlope * grooveAmp + perl.edgeLip * 0.085));
// Re-orthogonalize T and B to perturbed normal
T = normalize(T - Np * dot(T, Np));
B = normalize(cross(Np, T));
// Anisotropic roughness: tight along grooves (T), broad across grooves (B)
// This creates the crisp rotating specular ring / bowtie inside every rosette!
float alphaT = mix(0.065, 0.052, uOpen) + perl.centerDimple * 0.08;
float alphaB = mix(0.36, 0.44, uOpen) - perl.centerDimple * 0.14;
vec3 col = vec3(0.0);
// Direct anisotropic specular from studio softboxes
col += evalAnisoSpecular(Np, V, keyDir, T, B, F0, alphaT, alphaB) * keyCol * shKey * 0.92;
col += evalAnisoSpecular(Np, V, fillDir, T, B, F0, alphaT, alphaB) * fillCol * 0.88;
col += evalAnisoSpecular(Np, V, rimDir, T, B, F0, alphaT, alphaB) * rimCol * shRim * 0.90;
// Anisotropic studio environment sampling (bend reflection normal toward radial bitangent)
vec3 anisoDir = cross(T, V);
vec3 anisoN = normalize(cross(anisoDir, T));
float bendAmt = clamp(alphaB * 0.85, 0.0, 0.55);
vec3 bentN = normalize(mix(Np, anisoN, bendAmt));
vec3 R0 = reflect(-V, bentN);
// Sample 3 spread rays along the anisotropic radial fan for rich rotating ring glints
vec3 R1 = normalize(R0 + B * 0.16);
vec3 R2 = normalize(R0 - B * 0.16);
vec3 envCol = (sampleStudioEnv(R0, uLightMode) * 0.50 +
sampleStudioEnv(R1, uLightMode) * 0.25 +
sampleStudioEnv(R2, uLightMode) * 0.25);
vec3 Fenv = fresnelSchlick(max(dot(Np, V), 0.0), F0);
// Crisp rotating highlight ring accent where view/key half-vector aligns radially
vec3 Hkey = normalize(V + keyDir);
float ringAlign = 1.0 - abs(dot(T, Hkey));
float ringGlow = pow(clamp(ringAlign, 0.0, 1.0), mix(10.0, 14.0, uOpen));
float radialProfile = smoothstep(0.06, 0.25, perl.rNorm) * (1.0 - 0.25 * smoothstep(0.82, 1.0, perl.rNorm));
col += envCol * Fenv * (0.64 + 0.28 * ringGlow * radialProfile) * ao;
// Subtle crisp glint along the circular rosette rim
col += F0 * perl.edgeLip * (0.12 * shKey + 0.06) * ao;
// Subtle satin metallic floor so dark negative-fill areas stay rich (never crushed to 0)
col += F0 * 0.055 * ao;
return col;
}
// ------------------------------------------------------------------------
// Mirror-Polished 45-deg Rhodium Anglage Bevels & Walls
// ------------------------------------------------------------------------
if (mat == MAT_ANGLAGE) {
vec3 F0 = (uLightMode == 1) ? vec3(0.84) : vec3(0.82, 0.84, 0.87);
float rough = 0.055;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.92 * ao;
col += F0 * 0.035 * ao;
return col;
}
// ------------------------------------------------------------------------
// 18k Yellow Gold Chaton
// ------------------------------------------------------------------------
if (mat == MAT_GOLD_CHATON) {
vec3 F0 = vec3(1.00, 0.78, 0.34);
// Fine circular lathe-turning micro-brushing on the gold chaton
vec2 q = p.xz - C_JEWEL;
float r = length(q);
float lathe = sin(r * 420.0) * (1.0 - smoothstep(0.002, 0.008, length(fwidth(q))));
vec3 radDir = (r > 1e-5) ? normalize(vec3(q.x, 0.0, q.y)) : vec3(1.0, 0.0, 0.0);
vec3 Ng = normalize(N + radDir * lathe * 0.025);
float rough = 0.11;
vec3 R = reflect(-V, Ng);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(Ng, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(Ng, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(Ng, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(Ng, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += F0 * 0.05 * ao;
return col;
}
// ------------------------------------------------------------------------
// Synthetic Corundum Ruby Jewel Bearing (IOR 1.77, Deep Crimson-Magenta Glow)
// ------------------------------------------------------------------------
if (mat == MAT_RUBY) {
vec3 F0 = vec3(0.077); // Corundum IOR = 1.77
float rough = 0.035;
float NoV = max(dot(N, V), 0.0);
vec3 F = fresnelSchlick(NoV, F0);
// Refracted internal ray & Beer-Lambert ruby absorption + internal caustic bounce
vec3 refr = refract(-V, N, 1.0 / 1.77);
vec2 q = p.xz - C_JEWEL;
float rNorm = clamp(length(q) / 0.115, 0.0, 1.0);
// Deep pigeon-blood / Swiss watchmaking synthetic ruby color
vec3 rubyDeep = vec3(0.32, 0.012, 0.065);
vec3 rubyBright = vec3(0.95, 0.055, 0.240);
// Internal light scatter & caustic opposite the key light
float caustic = pow(clamp(dot(normalize(-refr), keyDir) * 0.5 + 0.5, 0.0, 1.0), 3.0);
float rimGlow = smoothstep(0.25, 0.92, rNorm);
vec3 internal = mix(rubyDeep, rubyBright, 0.35 + 0.45 * caustic * (0.4 + 0.6 * shKey) + 0.28 * rimGlow);
// Subtle warm golden reflection from the surrounding gold chaton wall
internal += vec3(0.45, 0.18, 0.04) * pow(rimGlow, 2.5) * 0.45;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 col = internal * (1.0 - F) * (0.55 + 0.45 * ao);
col += env * F * 1.15;
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.35;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
return col;
}
// ------------------------------------------------------------------------
// Heat-Blued Steel Screw (Thin-Film Oxide Interference Royal Blue)
// ------------------------------------------------------------------------
if (mat == MAT_BLUED_STEEL) {
float NoV = max(dot(N, V), 0.0);
// Thin-film thermally blued steel: deep cornflower-cobalt shifting to indigo-cyan at grazing
vec3 blueCore = vec3(0.08, 0.22, 0.66);
vec3 blueGraze = vec3(0.28, 0.54, 0.92);
vec3 F0 = mix(blueGraze, blueCore, pow(NoV, 0.65));
float rough = 0.075;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(NoV, F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.2;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += blueCore * 0.08 * ao;
return col;
}
// ------------------------------------------------------------------------
// Gilt Brass Escapement Wheel Glimpse (Inside Mainplate Well)
// ------------------------------------------------------------------------
vec3 F0 = vec3(0.88, 0.70, 0.36);
float rough = 0.18;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.78 * ao;
col += F0 * 0.04 * ao;
return col;
}
// ============================================================================
// Raymarching & Camera
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / min(uResolution.x, uResolution.y);
// Gentle orbital motion so anisotropic perlage rings continuously sweep
float orbit = 0.35 + uTime * 0.14;
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x Macro loupe view centered on ruby chaton + blued screw + shingled perlage + anglage
ta = vec3(0.08, 0.19, -0.02);
ro = ta + vec3(sin(orbit) * 0.78, 0.98, cos(orbit) * 0.78);
fov = 2.65;
} else {
// Hero three-quarter luxury product framing with generous bench margin
ta = vec3(0.00, 0.08, 0.00);
ro = vec3(sin(orbit) * 3.10, 2.95, cos(orbit) * 3.10);
fov = 1.85;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
bool hit = false;
const float tMax = 14.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.00045) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
vec3 N = calcNormal(p);
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
col = shadeSurface(p, N, -rd, isBench);
// Subtle atmospheric depth fade into studio backdrop at far bench horizon
float fog = 1.0 - exp(-max(t - 4.0, 0.0) * 0.12);
vec3 bgCol = sampleStudioEnv(rd, uLightMode) * 0.32;
col = mix(col, bgCol, clamp(fog, 0.0, 0.92));
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.32;
}
// Subtle photographic vignette
float vig = 1.0 - 0.18 * dot(uv, uv);
col *= clamp(vig, 0.72, 1.0);
// Pure neutral grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Exposure & ACES filmic tone mapping (headroom capped <= 245 sRGB, 0% clipped)
col *= 0.76;
col = acesToneMap(col);
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2)) * 0.94;
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
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;
// Engine-Turned Guilloche — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/07_engine_turned_perlon_guilloche.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// ============================================================================
// Constants & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_MAINPLATE = 1; // Lower rhodium perlage deck
const int MAT_BRIDGE_TOP = 2; // Upper rhodium perlage bridge top
const int MAT_ANGLAGE = 3; // Mirror-polished 45-deg rhodium bevels & walls
const int MAT_GOLD_CHATON = 4; // 18k yellow gold jewel setting
const int MAT_RUBY = 5; // Synthetic corundum ruby jewel bearing
const int MAT_BLUED_STEEL = 6; // Heat-blued steel screw
const int MAT_BRASS_WHEEL = 7; // Subtle gilt escapement wheel glimpse
// Key horological feature centers in XZ
const vec2 C_JEWEL = vec2(-0.16, 0.08);
const vec2 C_SCREW1 = vec2( 0.40, -0.14);
const vec2 C_SCREW2 = vec2(-0.56, -0.34);
const vec2 C_WELL = vec2( 0.44, 0.46);
// ============================================================================
// Math & Hashing Helpers
// ============================================================================
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i);
float b = hash21(i + vec2(1.0, 0.0));
float c = hash21(i + vec2(0.0, 1.0));
float d = hash21(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float 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);
}
// ============================================================================
// 2D & 3D Horological SDF Primitives
// ============================================================================
float sdExtrudeChamfer(float d2d, float y, float yMin, float yMax, float bevel) {
float h = 0.5 * (yMax - yMin);
float yc = y - 0.5 * (yMin + yMax);
vec2 w = vec2(d2d, abs(yc) - h);
float dBox = min(max(w.x, w.y), 0.0) + length(max(w, 0.0));
// 45-degree chamfer on top edge
float dChamfer = (d2d + (yc - h)) * 0.70710678 + bevel * 0.70710678;
return max(dBox, dChamfer);
}
float sdCylinder(vec3 p, vec2 c, float r, float yMin, float yMax, float bevel) {
float d2d = length(p.xz - c) - r;
return sdExtrudeChamfer(d2d, p.y, yMin, yMax, bevel);
}
// Curved Swiss watch movement bridge silhouette in XZ
float sdBridge2D(vec2 xz) {
float dOuter = length(xz) - 1.10;
// Sweeping horological balance-cock cutaway on upper-right quadrant
float dCut1 = length(xz - vec2(0.58, 0.54)) - 0.62;
// Secondary graceful waist curve on upper-left
float dCut2 = length(xz - vec2(-0.88, 0.72)) - 0.42;
// Combine outer disc minus smooth cutaways
float d = smax(dOuter, -dCut1, 0.09);
d = smax(d, -dCut2, 0.08);
// Subtle decorative curved extension lobe around primary screw
float dLobe = length(xz - vec2(0.42, -0.14)) - 0.34;
d = smin(d, dLobe, 0.14);
return d;
}
// Mainplate outer silhouette in XZ
float sdMainplate2D(vec2 xz) {
float d = length(xz) - 1.24;
// Subtle movement casing notch on outer rim
float dNotch = length(xz - vec2(-1.26, 0.0)) - 0.12;
d = smax(d, -dNotch, 0.03);
return d;
}
// ============================================================================
// Hero Assembly SDF (Watch Movement Mainplate, Bridge, Jewel, Chaton, Screws)
// ============================================================================
float mapMainplate(vec3 p) {
float d2d = sdMainplate2D(p.xz);
float d = sdExtrudeChamfer(d2d, p.y, 0.0, 0.105, 0.014);
// Recessed escapement bearing well on exposed lower mainplate
float rWell = length(p.xz - C_WELL);
float dWell = max(rWell - 0.23, -(p.y - 0.055));
float dWellChamfer = (-(rWell - 0.23) + (p.y - 0.105)) * 0.70710678;
dWell = min(dWell, -dWellChamfer + 0.012);
d = max(d, -dWell);
return d;
}
float mapBridge(vec3 p) {
float d2d = sdBridge2D(p.xz);
// Bridge sits from y = 0.095 to y = 0.225 with prominent 45-deg mirror anglage
float d = sdExtrudeChamfer(d2d, p.y, 0.095, 0.225, 0.030);
// Conical jewel countersink (oil sink + chaton bore)
float rJ = length(p.xz - C_JEWEL);
float dJ = max(rJ - 0.188, -(p.y - 0.150));
float dJBevel = ((rJ - 0.188) - (p.y - 0.225)) * 0.70710678 - 0.018;
d = max(d, -max(dJ, dJBevel));
// Primary screw counterbore
float rS1 = length(p.xz - C_SCREW1);
float dS1 = max(rS1 - 0.158, -(p.y - 0.155));
float dS1Bevel = ((rS1 - 0.158) - (p.y - 0.225)) * 0.70710678 - 0.012;
d = max(d, -max(dS1, dS1Bevel));
// Secondary screw counterbore
float rS2 = length(p.xz - C_SCREW2);
float dS2 = max(rS2 - 0.105, -(p.y - 0.165));
float dS2Bevel = ((rS2 - 0.105) - (p.y - 0.225)) * 0.70710678 - 0.010;
d = max(d, -max(dS2, dS2Bevel));
return d;
}
float mapGoldChaton(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Gold setting ring seated in the countersink
float dOuter = r - 0.174;
float dInner = 0.114 - r;
float d2d = max(dOuter, dInner);
float dRing = sdExtrudeChamfer(d2d, p.y, 0.148, 0.214, 0.011);
// Subtle torus lip on top of gold chaton
vec2 tor = vec2(r - 0.144, p.y - 0.207);
float dLip = length(tor) - 0.014;
return smin(dRing, dLip, 0.008);
}
float mapRuby(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Synthetic corundum ruby dome with central pivot hole & oil sink
float dCyl = sdExtrudeChamfer(r - 0.116, p.y, 0.148, 0.204, 0.016);
// Gentle concave oil-sink bowl on top of the ruby
float dBowl = length(vec3(q.x, (p.y - 0.260) * 1.15, q.y)) - 0.088;
float dRuby = max(dCyl, -dBowl);
// Central pivot bore
float dBore = 0.030 - r;
dRuby = max(dRuby, dBore);
return dRuby;
}
float mapPivot(vec3 p) {
// Polished steel pivot pin inside the ruby center
vec2 q = p.xz - C_JEWEL;
float r = length(q);
return sdExtrudeChamfer(r - 0.025, p.y, 0.140, 0.194, 0.006);
}
float mapScrewSingle(vec3 p, vec2 c, float radius, float yBase, float yTop, float angle) {
vec2 q = (p.xz - c) * rot2(angle);
float r = length(q);
float dHead = sdExtrudeChamfer(r - radius, p.y, yBase, yTop, radius * 0.14);
// Subtle dome curvature on screw head
float dDome = length(vec3(q.x, (p.y - (yTop - radius * 2.2)) * 1.0, q.y)) - radius * 2.24;
dHead = max(dHead, dDome);
// Precision horological screwdriver slot with beveled mouth
float slotHalfW = radius * 0.145;
float slotDepth = 0.030;
float dSlotBox = max(abs(q.y) - slotHalfW, -(p.y - (yTop - slotDepth)));
float dSlotBevel = (abs(q.y) - slotHalfW - (p.y - yTop)) * 0.70710678 - 0.005;
float dSlot = max(dSlotBox, dSlotBevel);
return max(dHead, -dSlot);
}
float mapBluedScrews(vec3 p) {
float s1 = mapScrewSingle(p, C_SCREW1, 0.142, 0.150, 0.232, 0.58);
float s2 = mapScrewSingle(p, C_SCREW2, 0.092, 0.160, 0.228, -0.82);
return min(s1, s2);
}
float mapBrassWheel(vec3 p) {
// Escapement wheel ring + hub visible inside the mainplate bearing well
vec2 q = p.xz - C_WELL;
float r = length(q);
float ang = atan(q.y, q.x) + uTime * 0.18;
float teeth = 0.009 * smoothstep(-0.2, 0.6, cos(ang * 18.0));
float dRim = max((r - (0.195 + teeth)), 0.132 - r);
float dHub = r - 0.056;
// 4 spokes
vec2 qs = q * rot2(uTime * 0.18);
float dSpoke = min(abs(qs.x), abs(qs.y)) - 0.016;
dSpoke = max(dSpoke, r - 0.16);
float d2d = min(min(dRim, dHub), dSpoke);
return sdExtrudeChamfer(d2d, p.y, 0.052, 0.088, 0.005);
}
float mapHero(vec3 p) {
float d = mapMainplate(p);
d = min(d, mapBridge(p));
d = min(d, mapGoldChaton(p));
d = min(d, mapRuby(p));
d = min(d, mapPivot(p));
d = min(d, mapBluedScrews(p));
d = min(d, mapBrassWheel(p));
return d;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormal(vec3 p) {
const float e = 0.00045;
const vec2 k = vec2(1.0, -1.0);
return normalize(
k.xyy * mapScene(p + k.xyy * e) +
k.yyx * mapScene(p + k.yyx * e) +
k.yxy * mapScene(p + k.yxy * e) +
k.xxx * mapScene(p + k.xxx * e)
);
}
// ============================================================================
// Hero-Only Soft Shadow (Zero-Bug Rule #3: clean res = min(res, k*h/t), no ph!)
// ============================================================================
float calcSoftShadow(vec3 ro, vec3 rd, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.0005) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > 4.5) 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 = 0; i < 5; i++) {
float h = 0.012 + 0.045 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Material Identification
// ============================================================================
int identifyMaterial(vec3 p, vec3 N, bool isBench) {
if (isBench) return MAT_BENCH;
float dMain = mapMainplate(p);
float dBrdg = mapBridge(p);
float dChat = mapGoldChaton(p);
float dRuby = mapRuby(p);
float dPiv = mapPivot(p);
float dScrew = mapBluedScrews(p);
float dWheel = mapBrassWheel(p);
float dMin = dMain;
int mat = MAT_MAINPLATE;
if (dBrdg < dMin) { dMin = dBrdg; mat = MAT_BRIDGE_TOP; }
if (dChat < dMin) { dMin = dChat; mat = MAT_GOLD_CHATON; }
if (dRuby < dMin) { dMin = dRuby; mat = MAT_RUBY; }
if (dPiv < dMin) { dMin = dPiv; mat = MAT_ANGLAGE; }
if (dScrew < dMin) { dMin = dScrew; mat = MAT_BLUED_STEEL; }
if (dWheel < dMin) { dMin = dWheel; mat = MAT_BRASS_WHEEL; }
// Separate flat perlage top surfaces from mirror-polished 45-deg anglage bevels & walls
if (mat == MAT_BRIDGE_TOP) {
if (N.y < 0.965 || p.y < 0.219) {
mat = MAT_ANGLAGE;
}
} else if (mat == MAT_MAINPLATE) {
if (N.y < 0.965 || p.y < 0.099) {
mat = MAT_ANGLAGE;
}
}
return mat;
}
// ============================================================================
// Shingled Engine-Turned Perlage (Circular Abrasive-Swirl Rosettes)
// ============================================================================
struct PerlageData {
vec2 tangent2D; // Concentric groove tangent in XZ (unit vector)
vec2 radial2D; // Radial direction from winning rosette center
float rNorm; // Normalized radius [0..1] inside winning rosette
float grooveSlope; // Band-limited concentric micro-groove radial derivative
float edgeLip; // Crisp outer abrasive ring highlight/seam
float centerDimple;// Soft zero-velocity center dimple
float cellTone; // Subtle rosette-to-rosette burnishing variation
float rosetteMask; // Coverage mask
};
PerlageData evalPerlage(vec2 uv, float openAmount, float scaleMul) {
// uOpen = 0.0 -> fine Swiss watchmaking perlage (small, delicate overlapping circles)
// uOpen = 1.0 -> bold vintage automotive dashboard engine-turning (large, punchy rosettes)
float pitch = mix(0.145, 0.340, clamp(openAmount, 0.0, 1.0)) * scaleMul;
float dx = pitch;
float dy = pitch * 0.72;
float rosetteR = pitch * mix(0.74, 0.78, openAmount);
// Analytic screen-space pixel footprint for fwidth antialiasing
float fwUV = max(length(fwidth(uv)), 0.00055);
float aaEdge = max(fwUV * 1.35, 0.0007);
// Base grid cell
float rowF = floor(uv.y / dy);
int baseY = int(rowF);
// Initialize with fallback
PerlageData outP;
outP.tangent2D = vec2(1.0, 0.0);
outP.radial2D = vec2(0.0, 1.0);
outP.rNorm = 0.5;
outP.grooveSlope = 0.0;
outP.edgeLip = 0.0;
outP.centerDimple = 0.0;
outP.cellTone = 0.5;
outP.rosetteMask = 0.0;
// Groove frequency: finer grooves on watchmaking perlage, bolder on dashboard engine-turning
float grooveCycles = mix(15.0, 22.0, openAmount);
// Paint-order shingled iteration: lower rows & columns first, later stamps overwrite earlier!
for (int iy = -1; iy <= 2; iy++) {
int gy = baseY + iy;
// Brick stagger every odd row
float rowShift = (mod(float(gy), 2.0) > 0.5) ? (0.5 * dx) : 0.0;
int baseX = int(floor((uv.x - rowShift) / dx));
for (int ix = -1; ix <= 2; ix++) {
int gx = baseX + ix;
vec2 gid = vec2(float(gx), float(gy));
vec2 rnd = hash22(gid + vec2(17.3, 41.7));
// Tiny hand-indexed engine-turning micro-jitter (1.2% of pitch)
vec2 jitter = (rnd - 0.5) * pitch * 0.022;
vec2 center = vec2((float(gx) + 0.5) * dx + rowShift, (float(gy) + 0.5) * dy) + jitter;
vec2 rel = uv - center;
float dist = length(rel);
float rNorm = dist / rosetteR;
if (rNorm < 1.12) {
// Crisp fwidth-antialiased circular stamp boundary
float cov = 1.0 - smoothstep(rosetteR - aaEdge, rosetteR + aaEdge, dist);
if (cov > 0.001) {
vec2 radDir = (dist > 1e-5) ? (rel / dist) : vec2(1.0, 0.0);
vec2 tanDir = vec2(-radDir.y, radDir.x);
// Concentric abrasive swirl micro-grooves (band-limited via fwUV)
float phase = rNorm * grooveCycles * TAU + rnd.x * TAU;
float cyclesPerPx = (fwUV / rosetteR) * grooveCycles;
float grooveFade = 1.0 - smoothstep(0.28, 0.95, cyclesPerPx);
float fineFade = 1.0 - smoothstep(0.18, 0.65, cyclesPerPx * 2.1);
float g1 = cos(phase) * grooveFade;
float g2 = cos(phase * 2.13 + rnd.y * 5.0) * 0.42 * fineFade;
// Subtle angular scratch break-up (abrasive peg grit isn't mathematically uniform)
float ang = atan(rel.y, rel.x);
float gritMod = 0.72 + 0.28 * sin(ang * 3.0 + rnd.x * 12.0 + rNorm * 7.0);
float gSlope = (g1 + g2) * gritMod;
// Crisp outer rim ring left by the spinning peg perimeter
float rimBand = exp(-pow((rNorm - 0.955) / max(0.032, aaEdge / rosetteR * 1.4), 2.0));
// Subtle zero-velocity burnish dimple at exact center
float dimple = exp(-pow(rNorm / 0.11, 2.0));
// Subtle per-rosette pressure/tone variation
float tone = 0.88 + 0.24 * rnd.y;
outP.tangent2D = normalize(mix(outP.tangent2D, tanDir, cov));
outP.radial2D = normalize(mix(outP.radial2D, radDir, cov));
outP.rNorm = mix(outP.rNorm, clamp(rNorm, 0.0, 1.0), cov);
outP.grooveSlope = mix(outP.grooveSlope, gSlope, cov);
outP.edgeLip = mix(outP.edgeLip, rimBand, cov);
outP.centerDimple = mix(outP.centerDimple, dimple, cov);
outP.cellTone = mix(outP.cellTone, tone, cov);
outP.rosetteMask = max(outP.rosetteMask, cov);
}
}
}
}
return outP;
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 sampleStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base studio cyclorama (dark negative-fill charcoal for rich metallic contrast)
vec3 env = mix(vec3(0.035, 0.038, 0.044), vec3(0.085, 0.090, 0.100), smoothstep(-0.2, 0.65, y));
if (lightMode == 1) {
// Pure neutral grayscale studio
env = vec3(mix(0.040, 0.095, smoothstep(-0.2, 0.65, y)));
}
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.28, 0.82, 0.42));
if (lightMode == 2) sb1Dir = normalize(vec3(0.86, 0.28, 0.38)); // Grazing raking strip
if (lightMode == 3) sb1Dir = normalize(vec3(-0.18, 0.46, -0.86)); // Strong backlit rim box
float sb1Dot = dot(d, sb1Dir);
float sb1 = smoothstep(0.68, 0.94, sb1Dot);
float sb1Core = smoothstep(0.88, 0.985, sb1Dot);
// Secondary crisp rim/accent strip light
vec3 sb2Dir = normalize(vec3(-0.76, 0.46, -0.45));
if (lightMode == 2) sb2Dir = normalize(vec3(-0.82, 0.22, -0.52));
if (lightMode == 3) sb2Dir = normalize(vec3(0.32, 0.72, 0.60));
float sb2 = smoothstep(0.72, 0.95, dot(d, sb2Dir));
// Warm/cool studio color grading by lightMode
vec3 cKey = vec3(1.04, 0.99, 0.93);
vec3 cRim = vec3(0.86, 0.94, 1.06);
if (lightMode == 1) {
cKey = vec3(1.0);
cRim = vec3(1.0);
} else if (lightMode == 2) {
cKey = vec3(1.08, 0.96, 0.84);
cRim = vec3(0.78, 0.88, 1.05);
} else if (lightMode == 3) {
cKey = vec3(1.05, 1.02, 0.98);
cRim = vec3(0.92, 0.95, 1.02);
}
float keyGain = (lightMode == 3) ? 2.5 : 2.6;
float rimGain = (lightMode == 3) ? 1.15 : 1.65;
env += cKey * (sb1 * 1.15 + sb1Core * keyGain);
env += cRim * (sb2 * rimGain);
// Crisp horizontal studio light-ribbon around horizon for anglage edge glints
float horizonStrip = exp(-pow((y - 0.14) * 9.5, 2.0)) * (0.45 + 0.55 * smoothstep(-0.2, 0.8, sin(atan(d.z, d.x) * 2.0 + 0.8)));
vec3 stripCol = (lightMode == 1) ? vec3(0.55) : vec3(0.52, 0.56, 0.62);
env += stripCol * horizonStrip;
// Dark negative-fill flags (absorptive matte black studio cards for high-contrast metal sculpting)
float flag1 = smoothstep(0.74, 0.90, dot(d, normalize(vec3(-0.35, 0.52, 0.78))));
float flag2 = smoothstep(0.78, 0.92, dot(d, normalize(vec3(0.78, 0.42, -0.45))));
float negFill = max(flag1, flag2);
env *= mix(1.0, 0.14, negFill);
return env;
}
// ============================================================================
// Physically-Based Anisotropic GGX & Isotropic GGX BRDF
// ============================================================================
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float a2 = ax * ay;
vec3 v = vec3(ay * ToH, ax * BoH, a2 * NoH);
float v2 = dot(v, v);
float w2 = a2 / max(v2, 1e-6);
return a2 * w2 * w2 / PI;
}
float V_SmithGGX_Aniso(float NoV, float NoL, float ToV, float BoV, float ToL, float BoL, float ax, float ay) {
float lambdaV = NoL * length(vec3(ax * ToV, ay * BoV, NoV));
float lambdaL = NoV * length(vec3(ax * ToL, ay * BoL, NoL));
return 0.5 / max(lambdaV + lambdaL, 1e-5);
}
vec3 evalAnisoSpecular(vec3 N, vec3 V, vec3 L, vec3 T, vec3 B, vec3 F0, float alphaT, float alphaB) {
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float ToH = dot(T, H);
float BoH = dot(B, H);
float ToV = dot(T, V);
float BoV = dot(B, V);
float ToL = dot(T, L);
float BoL = dot(B, L);
float D = D_GGX_Aniso(NoH, ToH, BoH, alphaT, alphaB);
float G = V_SmithGGX_Aniso(NoV, NoL, ToV, BoV, ToL, BoL, alphaT, alphaB);
vec3 F = fresnelSchlick(VoH, F0);
return D * G * F * NoL;
}
vec3 evalIsoSpecular(vec3 N, vec3 V, vec3 L, vec3 F0, float rough) {
float a = max(rough * rough, 0.0018);
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float a2 = a * a;
float dDen = NoH * NoH * (a2 - 1.0) + 1.0;
float D = a2 / max(PI * dDen * dDen, 1e-6);
float k = (rough + 1.0) * (rough + 1.0) * 0.125;
float gV = NoV / (NoV * (1.0 - k) + k);
float gL = NoL / (NoL * (1.0 - k) + k);
vec3 F = fresnelSchlick(VoH, F0);
return (D * gV * gL * F) / max(4.0 * NoV, 1e-4);
}
// ============================================================================
// ACES Filmic Tone Mapping
// ============================================================================
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);
}
// ============================================================================
// Lighting Setup by uLightMode
// ============================================================================
void getStudioLights(
int lightMode,
out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol
) {
if (lightMode == 0) {
// Hero luxury watch photography rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.85, 2.74, 2.60);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(0.95, 1.02, 1.14);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.65, 1.72, 1.85);
} else if (lightMode == 1) {
// Pure neutral grayscale rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.80);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(1.02);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.70);
} else if (lightMode == 2) {
// Grazing raking light to accentuate perlage grooves & anglage
keyDir = normalize(vec3(0.88, 0.25, 0.40));
keyCol = vec3(3.45, 3.10, 2.72);
fillDir = normalize(vec3(-0.82, 0.26, -0.50));
fillCol = vec3(0.88, 0.98, 1.15);
rimDir = normalize(vec3(-0.20, 0.62, -0.76));
rimCol = vec3(1.25, 1.30, 1.40);
} else {
// Backlit dramatic rim lighting
keyDir = normalize(vec3(-0.22, 0.42, -0.88));
keyCol = vec3(2.35, 2.30, 2.22);
fillDir = normalize(vec3(0.55, 0.64, 0.54));
fillCol = vec3(0.88, 0.90, 0.96);
rimDir = normalize(vec3(-0.78, 0.36, -0.50));
rimCol = vec3(1.25, 1.30, 1.40);
}
}
// ============================================================================
// Main Shading Function
// ============================================================================
vec3 shadeSurface(vec3 p, vec3 N, vec3 V, bool isBench) {
int mat = identifyMaterial(p, N, isBench);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getStudioLights(uLightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Hero-only soft shadows with required bias p + N * 0.014
float shKey = calcSoftShadow(p + N * 0.014, keyDir, 10.0);
float shRim = calcSoftShadow(p + N * 0.014, rimDir, 8.0);
float ao = calcAO(p, N);
// ------------------------------------------------------------------------
// Dark Horologist's Studio Bench (Micro-grained anodized slate)
// ------------------------------------------------------------------------
if (mat == MAT_BENCH) {
float grain1 = valueNoise(p.xz * 26.0);
float grain2 = valueNoise(p.xz * 82.0 + 7.3);
float micro = (grain1 * 0.6 + grain2 * 0.4);
vec3 baseCol = (uLightMode == 1)
? vec3(0.042 + 0.012 * micro)
: vec3(0.038, 0.041, 0.048) + vec3(0.010, 0.011, 0.013) * micro;
// Subtle radial studio pool-of-light vignette on bench
float distC = length(p.xz);
float spot = exp(-distC * distC * 0.14);
vec3 benchN = normalize(N + vec3((grain1 - 0.5) * 0.025, 0.0, (grain2 - 0.5) * 0.025));
float rough = 0.48 + 0.12 * micro;
vec3 F0 = vec3(0.045);
vec3 col = vec3(0.0);
col += (baseCol / PI * max(dot(benchN, keyDir), 0.0) + evalIsoSpecular(benchN, V, keyDir, F0, rough)) * keyCol * shKey;
col += (baseCol / PI * max(dot(benchN, fillDir), 0.0) + evalIsoSpecular(benchN, V, fillDir, F0, rough) * 0.6) * fillCol;
col += (baseCol / PI * max(dot(benchN, rimDir), 0.0) + evalIsoSpecular(benchN, V, rimDir, F0, rough)) * rimCol * shRim;
vec3 R = reflect(-V, benchN);
vec3 envRefl = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(benchN, V), 0.0), F0);
col += envRefl * Fenv * 0.35 * ao;
col += baseCol * (0.28 + 0.25 * spot) * ao;
// Soft grounding contact shadow right under the watch movement
float contact = smoothstep(0.0, 0.18, max(length(p.xz) - 1.20, 0.0));
col *= mix(0.45, 1.0, contact);
return col;
}
// ------------------------------------------------------------------------
// Engine-Turned Perlage Rhodium (Mainplate & Bridge Top)
// ------------------------------------------------------------------------
if (mat == MAT_BRIDGE_TOP || mat == MAT_MAINPLATE) {
float scaleMul = (mat == MAT_MAINPLATE) ? 0.78 : 1.0;
PerlageData perl = evalPerlage(p.xz, uOpen, scaleMul);
// Rhodium-plated brass F0 (cool, lustrous silvery-white precious metal)
vec3 F0 = (uLightMode == 1)
? vec3(0.77, 0.77, 0.77)
: vec3(0.76, 0.775, 0.805);
F0 *= perl.cellTone;
// Slight contrast dip right at the outer circular abrasive seam & central dimple
F0 *= (1.0 - 0.07 * perl.edgeLip - 0.05 * perl.centerDimple);
// Build tangent frame aligned with concentric abrasive grooves
vec3 T = normalize(vec3(perl.tangent2D.x, 0.0, perl.tangent2D.y));
T = normalize(T - N * dot(T, N));
vec3 B = normalize(cross(N, T));
// Perturb surface normal radially by the band-limited concentric micro-grooves
float grooveAmp = mix(0.065, 0.115, uOpen);
vec3 Np = normalize(N + B * (perl.grooveSlope * grooveAmp + perl.edgeLip * 0.085));
// Re-orthogonalize T and B to perturbed normal
T = normalize(T - Np * dot(T, Np));
B = normalize(cross(Np, T));
// Anisotropic roughness: tight along grooves (T), broad across grooves (B)
// This creates the crisp rotating specular ring / bowtie inside every rosette!
float alphaT = mix(0.065, 0.052, uOpen) + perl.centerDimple * 0.08;
float alphaB = mix(0.36, 0.44, uOpen) - perl.centerDimple * 0.14;
vec3 col = vec3(0.0);
// Direct anisotropic specular from studio softboxes
col += evalAnisoSpecular(Np, V, keyDir, T, B, F0, alphaT, alphaB) * keyCol * shKey * 0.92;
col += evalAnisoSpecular(Np, V, fillDir, T, B, F0, alphaT, alphaB) * fillCol * 0.88;
col += evalAnisoSpecular(Np, V, rimDir, T, B, F0, alphaT, alphaB) * rimCol * shRim * 0.90;
// Anisotropic studio environment sampling (bend reflection normal toward radial bitangent)
vec3 anisoDir = cross(T, V);
vec3 anisoN = normalize(cross(anisoDir, T));
float bendAmt = clamp(alphaB * 0.85, 0.0, 0.55);
vec3 bentN = normalize(mix(Np, anisoN, bendAmt));
vec3 R0 = reflect(-V, bentN);
// Sample 3 spread rays along the anisotropic radial fan for rich rotating ring glints
vec3 R1 = normalize(R0 + B * 0.16);
vec3 R2 = normalize(R0 - B * 0.16);
vec3 envCol = (sampleStudioEnv(R0, uLightMode) * 0.50 +
sampleStudioEnv(R1, uLightMode) * 0.25 +
sampleStudioEnv(R2, uLightMode) * 0.25);
vec3 Fenv = fresnelSchlick(max(dot(Np, V), 0.0), F0);
// Crisp rotating highlight ring accent where view/key half-vector aligns radially
vec3 Hkey = normalize(V + keyDir);
float ringAlign = 1.0 - abs(dot(T, Hkey));
float ringGlow = pow(clamp(ringAlign, 0.0, 1.0), mix(10.0, 14.0, uOpen));
float radialProfile = smoothstep(0.06, 0.25, perl.rNorm) * (1.0 - 0.25 * smoothstep(0.82, 1.0, perl.rNorm));
col += envCol * Fenv * (0.64 + 0.28 * ringGlow * radialProfile) * ao;
// Subtle crisp glint along the circular rosette rim
col += F0 * perl.edgeLip * (0.12 * shKey + 0.06) * ao;
// Subtle satin metallic floor so dark negative-fill areas stay rich (never crushed to 0)
col += F0 * 0.055 * ao;
return col;
}
// ------------------------------------------------------------------------
// Mirror-Polished 45-deg Rhodium Anglage Bevels & Walls
// ------------------------------------------------------------------------
if (mat == MAT_ANGLAGE) {
vec3 F0 = (uLightMode == 1) ? vec3(0.84) : vec3(0.82, 0.84, 0.87);
float rough = 0.055;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.92 * ao;
col += F0 * 0.035 * ao;
return col;
}
// ------------------------------------------------------------------------
// 18k Yellow Gold Chaton
// ------------------------------------------------------------------------
if (mat == MAT_GOLD_CHATON) {
vec3 F0 = vec3(1.00, 0.78, 0.34);
// Fine circular lathe-turning micro-brushing on the gold chaton
vec2 q = p.xz - C_JEWEL;
float r = length(q);
float lathe = sin(r * 420.0) * (1.0 - smoothstep(0.002, 0.008, length(fwidth(q))));
vec3 radDir = (r > 1e-5) ? normalize(vec3(q.x, 0.0, q.y)) : vec3(1.0, 0.0, 0.0);
vec3 Ng = normalize(N + radDir * lathe * 0.025);
float rough = 0.11;
vec3 R = reflect(-V, Ng);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(Ng, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(Ng, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(Ng, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(Ng, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += F0 * 0.05 * ao;
return col;
}
// ------------------------------------------------------------------------
// Synthetic Corundum Ruby Jewel Bearing (IOR 1.77, Deep Crimson-Magenta Glow)
// ------------------------------------------------------------------------
if (mat == MAT_RUBY) {
vec3 F0 = vec3(0.077); // Corundum IOR = 1.77
float rough = 0.035;
float NoV = max(dot(N, V), 0.0);
vec3 F = fresnelSchlick(NoV, F0);
// Refracted internal ray & Beer-Lambert ruby absorption + internal caustic bounce
vec3 refr = refract(-V, N, 1.0 / 1.77);
vec2 q = p.xz - C_JEWEL;
float rNorm = clamp(length(q) / 0.115, 0.0, 1.0);
// Deep pigeon-blood / Swiss watchmaking synthetic ruby color
vec3 rubyDeep = vec3(0.32, 0.012, 0.065);
vec3 rubyBright = vec3(0.95, 0.055, 0.240);
// Internal light scatter & caustic opposite the key light
float caustic = pow(clamp(dot(normalize(-refr), keyDir) * 0.5 + 0.5, 0.0, 1.0), 3.0);
float rimGlow = smoothstep(0.25, 0.92, rNorm);
vec3 internal = mix(rubyDeep, rubyBright, 0.35 + 0.45 * caustic * (0.4 + 0.6 * shKey) + 0.28 * rimGlow);
// Subtle warm golden reflection from the surrounding gold chaton wall
internal += vec3(0.45, 0.18, 0.04) * pow(rimGlow, 2.5) * 0.45;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 col = internal * (1.0 - F) * (0.55 + 0.45 * ao);
col += env * F * 1.15;
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.35;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
return col;
}
// ------------------------------------------------------------------------
// Heat-Blued Steel Screw (Thin-Film Oxide Interference Royal Blue)
// ------------------------------------------------------------------------
if (mat == MAT_BLUED_STEEL) {
float NoV = max(dot(N, V), 0.0);
// Thin-film thermally blued steel: deep cornflower-cobalt shifting to indigo-cyan at grazing
vec3 blueCore = vec3(0.08, 0.22, 0.66);
vec3 blueGraze = vec3(0.28, 0.54, 0.92);
vec3 F0 = mix(blueGraze, blueCore, pow(NoV, 0.65));
float rough = 0.075;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(NoV, F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.2;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += blueCore * 0.08 * ao;
return col;
}
// ------------------------------------------------------------------------
// Gilt Brass Escapement Wheel Glimpse (Inside Mainplate Well)
// ------------------------------------------------------------------------
vec3 F0 = vec3(0.88, 0.70, 0.36);
float rough = 0.18;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.78 * ao;
col += F0 * 0.04 * ao;
return col;
}
// ============================================================================
// Raymarching & Camera
// ============================================================================
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = (pfx_frag_xy - 0.5 * uResolution.xy) / min(uResolution.x, uResolution.y);
// Gentle orbital motion so anisotropic perlage rings continuously sweep
float orbit = 0.35 + uTime * 0.14;
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x Macro loupe view centered on ruby chaton + blued screw + shingled perlage + anglage
ta = vec3(0.08, 0.19, -0.02);
ro = ta + vec3(sin(orbit) * 0.78, 0.98, cos(orbit) * 0.78);
fov = 2.65;
} else {
// Hero three-quarter luxury product framing with generous bench margin
ta = vec3(0.00, 0.08, 0.00);
ro = vec3(sin(orbit) * 3.10, 2.95, cos(orbit) * 3.10);
fov = 1.85;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
bool hit = false;
const float tMax = 14.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.00045) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
vec3 N = calcNormal(p);
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
col = shadeSurface(p, N, -rd, isBench);
// Subtle atmospheric depth fade into studio backdrop at far bench horizon
float fog = 1.0 - exp(-max(t - 4.0, 0.0) * 0.12);
vec3 bgCol = sampleStudioEnv(rd, uLightMode) * 0.32;
col = mix(col, bgCol, clamp(fog, 0.0, 0.92));
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.32;
}
// Subtle photographic vignette
float vig = 1.0 - 0.18 * dot(uv, uv);
col *= clamp(vig, 0.72, 1.0);
// Pure neutral grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Exposure & ACES filmic tone mapping (headroom capped <= 245 sRGB, 0% clipped)
col *= 0.76;
col = acesToneMap(col);
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2)) * 0.94;
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
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;
}
// Engine-Turned Guilloche — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/07_engine_turned_perlon_guilloche.glsl (MIT License)
import * as THREE from 'three';
const vertexShader = /* glsl */ `
out vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = /* glsl */ `
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;
out vec4 fragColor;
// ============================================================================
// Constants & Material IDs
// ============================================================================
const float PI = 3.14159265359;
const float TAU = 6.28318530718;
const int MAT_BENCH = 0;
const int MAT_MAINPLATE = 1; // Lower rhodium perlage deck
const int MAT_BRIDGE_TOP = 2; // Upper rhodium perlage bridge top
const int MAT_ANGLAGE = 3; // Mirror-polished 45-deg rhodium bevels & walls
const int MAT_GOLD_CHATON = 4; // 18k yellow gold jewel setting
const int MAT_RUBY = 5; // Synthetic corundum ruby jewel bearing
const int MAT_BLUED_STEEL = 6; // Heat-blued steel screw
const int MAT_BRASS_WHEEL = 7; // Subtle gilt escapement wheel glimpse
// Key horological feature centers in XZ
const vec2 C_JEWEL = vec2(-0.16, 0.08);
const vec2 C_SCREW1 = vec2( 0.40, -0.14);
const vec2 C_SCREW2 = vec2(-0.56, -0.34);
const vec2 C_WELL = vec2( 0.44, 0.46);
// ============================================================================
// Math & Hashing Helpers
// ============================================================================
mat2 rot2(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash21(i);
float b = hash21(i + vec2(1.0, 0.0));
float c = hash21(i + vec2(0.0, 1.0));
float d = hash21(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float 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);
}
// ============================================================================
// 2D & 3D Horological SDF Primitives
// ============================================================================
float sdExtrudeChamfer(float d2d, float y, float yMin, float yMax, float bevel) {
float h = 0.5 * (yMax - yMin);
float yc = y - 0.5 * (yMin + yMax);
vec2 w = vec2(d2d, abs(yc) - h);
float dBox = min(max(w.x, w.y), 0.0) + length(max(w, 0.0));
// 45-degree chamfer on top edge
float dChamfer = (d2d + (yc - h)) * 0.70710678 + bevel * 0.70710678;
return max(dBox, dChamfer);
}
float sdCylinder(vec3 p, vec2 c, float r, float yMin, float yMax, float bevel) {
float d2d = length(p.xz - c) - r;
return sdExtrudeChamfer(d2d, p.y, yMin, yMax, bevel);
}
// Curved Swiss watch movement bridge silhouette in XZ
float sdBridge2D(vec2 xz) {
float dOuter = length(xz) - 1.10;
// Sweeping horological balance-cock cutaway on upper-right quadrant
float dCut1 = length(xz - vec2(0.58, 0.54)) - 0.62;
// Secondary graceful waist curve on upper-left
float dCut2 = length(xz - vec2(-0.88, 0.72)) - 0.42;
// Combine outer disc minus smooth cutaways
float d = smax(dOuter, -dCut1, 0.09);
d = smax(d, -dCut2, 0.08);
// Subtle decorative curved extension lobe around primary screw
float dLobe = length(xz - vec2(0.42, -0.14)) - 0.34;
d = smin(d, dLobe, 0.14);
return d;
}
// Mainplate outer silhouette in XZ
float sdMainplate2D(vec2 xz) {
float d = length(xz) - 1.24;
// Subtle movement casing notch on outer rim
float dNotch = length(xz - vec2(-1.26, 0.0)) - 0.12;
d = smax(d, -dNotch, 0.03);
return d;
}
// ============================================================================
// Hero Assembly SDF (Watch Movement Mainplate, Bridge, Jewel, Chaton, Screws)
// ============================================================================
float mapMainplate(vec3 p) {
float d2d = sdMainplate2D(p.xz);
float d = sdExtrudeChamfer(d2d, p.y, 0.0, 0.105, 0.014);
// Recessed escapement bearing well on exposed lower mainplate
float rWell = length(p.xz - C_WELL);
float dWell = max(rWell - 0.23, -(p.y - 0.055));
float dWellChamfer = (-(rWell - 0.23) + (p.y - 0.105)) * 0.70710678;
dWell = min(dWell, -dWellChamfer + 0.012);
d = max(d, -dWell);
return d;
}
float mapBridge(vec3 p) {
float d2d = sdBridge2D(p.xz);
// Bridge sits from y = 0.095 to y = 0.225 with prominent 45-deg mirror anglage
float d = sdExtrudeChamfer(d2d, p.y, 0.095, 0.225, 0.030);
// Conical jewel countersink (oil sink + chaton bore)
float rJ = length(p.xz - C_JEWEL);
float dJ = max(rJ - 0.188, -(p.y - 0.150));
float dJBevel = ((rJ - 0.188) - (p.y - 0.225)) * 0.70710678 - 0.018;
d = max(d, -max(dJ, dJBevel));
// Primary screw counterbore
float rS1 = length(p.xz - C_SCREW1);
float dS1 = max(rS1 - 0.158, -(p.y - 0.155));
float dS1Bevel = ((rS1 - 0.158) - (p.y - 0.225)) * 0.70710678 - 0.012;
d = max(d, -max(dS1, dS1Bevel));
// Secondary screw counterbore
float rS2 = length(p.xz - C_SCREW2);
float dS2 = max(rS2 - 0.105, -(p.y - 0.165));
float dS2Bevel = ((rS2 - 0.105) - (p.y - 0.225)) * 0.70710678 - 0.010;
d = max(d, -max(dS2, dS2Bevel));
return d;
}
float mapGoldChaton(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Gold setting ring seated in the countersink
float dOuter = r - 0.174;
float dInner = 0.114 - r;
float d2d = max(dOuter, dInner);
float dRing = sdExtrudeChamfer(d2d, p.y, 0.148, 0.214, 0.011);
// Subtle torus lip on top of gold chaton
vec2 tor = vec2(r - 0.144, p.y - 0.207);
float dLip = length(tor) - 0.014;
return smin(dRing, dLip, 0.008);
}
float mapRuby(vec3 p) {
vec2 q = p.xz - C_JEWEL;
float r = length(q);
// Synthetic corundum ruby dome with central pivot hole & oil sink
float dCyl = sdExtrudeChamfer(r - 0.116, p.y, 0.148, 0.204, 0.016);
// Gentle concave oil-sink bowl on top of the ruby
float dBowl = length(vec3(q.x, (p.y - 0.260) * 1.15, q.y)) - 0.088;
float dRuby = max(dCyl, -dBowl);
// Central pivot bore
float dBore = 0.030 - r;
dRuby = max(dRuby, dBore);
return dRuby;
}
float mapPivot(vec3 p) {
// Polished steel pivot pin inside the ruby center
vec2 q = p.xz - C_JEWEL;
float r = length(q);
return sdExtrudeChamfer(r - 0.025, p.y, 0.140, 0.194, 0.006);
}
float mapScrewSingle(vec3 p, vec2 c, float radius, float yBase, float yTop, float angle) {
vec2 q = (p.xz - c) * rot2(angle);
float r = length(q);
float dHead = sdExtrudeChamfer(r - radius, p.y, yBase, yTop, radius * 0.14);
// Subtle dome curvature on screw head
float dDome = length(vec3(q.x, (p.y - (yTop - radius * 2.2)) * 1.0, q.y)) - radius * 2.24;
dHead = max(dHead, dDome);
// Precision horological screwdriver slot with beveled mouth
float slotHalfW = radius * 0.145;
float slotDepth = 0.030;
float dSlotBox = max(abs(q.y) - slotHalfW, -(p.y - (yTop - slotDepth)));
float dSlotBevel = (abs(q.y) - slotHalfW - (p.y - yTop)) * 0.70710678 - 0.005;
float dSlot = max(dSlotBox, dSlotBevel);
return max(dHead, -dSlot);
}
float mapBluedScrews(vec3 p) {
float s1 = mapScrewSingle(p, C_SCREW1, 0.142, 0.150, 0.232, 0.58);
float s2 = mapScrewSingle(p, C_SCREW2, 0.092, 0.160, 0.228, -0.82);
return min(s1, s2);
}
float mapBrassWheel(vec3 p) {
// Escapement wheel ring + hub visible inside the mainplate bearing well
vec2 q = p.xz - C_WELL;
float r = length(q);
float ang = atan(q.y, q.x) + uTime * 0.18;
float teeth = 0.009 * smoothstep(-0.2, 0.6, cos(ang * 18.0));
float dRim = max((r - (0.195 + teeth)), 0.132 - r);
float dHub = r - 0.056;
// 4 spokes
vec2 qs = q * rot2(uTime * 0.18);
float dSpoke = min(abs(qs.x), abs(qs.y)) - 0.016;
dSpoke = max(dSpoke, r - 0.16);
float d2d = min(min(dRim, dHub), dSpoke);
return sdExtrudeChamfer(d2d, p.y, 0.052, 0.088, 0.005);
}
float mapHero(vec3 p) {
float d = mapMainplate(p);
d = min(d, mapBridge(p));
d = min(d, mapGoldChaton(p));
d = min(d, mapRuby(p));
d = min(d, mapPivot(p));
d = min(d, mapBluedScrews(p));
d = min(d, mapBrassWheel(p));
return d;
}
float mapScene(vec3 p) {
return min(mapHero(p), p.y);
}
// ============================================================================
// Unit-Weight Tetrahedral Normals (Zero-Bug Rule #1)
// ============================================================================
vec3 calcNormal(vec3 p) {
const float e = 0.00045;
const vec2 k = vec2(1.0, -1.0);
return normalize(
k.xyy * mapScene(p + k.xyy * e) +
k.yyx * mapScene(p + k.yyx * e) +
k.yxy * mapScene(p + k.yxy * e) +
k.xxx * mapScene(p + k.xxx * e)
);
}
// ============================================================================
// Hero-Only Soft Shadow (Zero-Bug Rule #3: clean res = min(res, k*h/t), no ph!)
// ============================================================================
float calcSoftShadow(vec3 ro, vec3 rd, float k) {
float res = 1.0;
float t = 0.025;
for (int i = 0; i < 48; i++) {
float h = mapHero(ro + rd * t);
if (h < 0.0005) return 0.0;
res = min(res, k * h / t);
t += max(h, 0.015);
if (t > 4.5) 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 = 0; i < 5; i++) {
float h = 0.012 + 0.045 * float(i);
float d = mapScene(p + n * h);
occ += (h - d) * sca;
sca *= 0.68;
}
return clamp(1.0 - 2.6 * occ, 0.15, 1.0);
}
// ============================================================================
// Material Identification
// ============================================================================
int identifyMaterial(vec3 p, vec3 N, bool isBench) {
if (isBench) return MAT_BENCH;
float dMain = mapMainplate(p);
float dBrdg = mapBridge(p);
float dChat = mapGoldChaton(p);
float dRuby = mapRuby(p);
float dPiv = mapPivot(p);
float dScrew = mapBluedScrews(p);
float dWheel = mapBrassWheel(p);
float dMin = dMain;
int mat = MAT_MAINPLATE;
if (dBrdg < dMin) { dMin = dBrdg; mat = MAT_BRIDGE_TOP; }
if (dChat < dMin) { dMin = dChat; mat = MAT_GOLD_CHATON; }
if (dRuby < dMin) { dMin = dRuby; mat = MAT_RUBY; }
if (dPiv < dMin) { dMin = dPiv; mat = MAT_ANGLAGE; }
if (dScrew < dMin) { dMin = dScrew; mat = MAT_BLUED_STEEL; }
if (dWheel < dMin) { dMin = dWheel; mat = MAT_BRASS_WHEEL; }
// Separate flat perlage top surfaces from mirror-polished 45-deg anglage bevels & walls
if (mat == MAT_BRIDGE_TOP) {
if (N.y < 0.965 || p.y < 0.219) {
mat = MAT_ANGLAGE;
}
} else if (mat == MAT_MAINPLATE) {
if (N.y < 0.965 || p.y < 0.099) {
mat = MAT_ANGLAGE;
}
}
return mat;
}
// ============================================================================
// Shingled Engine-Turned Perlage (Circular Abrasive-Swirl Rosettes)
// ============================================================================
struct PerlageData {
vec2 tangent2D; // Concentric groove tangent in XZ (unit vector)
vec2 radial2D; // Radial direction from winning rosette center
float rNorm; // Normalized radius [0..1] inside winning rosette
float grooveSlope; // Band-limited concentric micro-groove radial derivative
float edgeLip; // Crisp outer abrasive ring highlight/seam
float centerDimple;// Soft zero-velocity center dimple
float cellTone; // Subtle rosette-to-rosette burnishing variation
float rosetteMask; // Coverage mask
};
PerlageData evalPerlage(vec2 uv, float openAmount, float scaleMul) {
// uOpen = 0.0 -> fine Swiss watchmaking perlage (small, delicate overlapping circles)
// uOpen = 1.0 -> bold vintage automotive dashboard engine-turning (large, punchy rosettes)
float pitch = mix(0.145, 0.340, clamp(openAmount, 0.0, 1.0)) * scaleMul;
float dx = pitch;
float dy = pitch * 0.72;
float rosetteR = pitch * mix(0.74, 0.78, openAmount);
// Analytic screen-space pixel footprint for fwidth antialiasing
float fwUV = max(length(fwidth(uv)), 0.00055);
float aaEdge = max(fwUV * 1.35, 0.0007);
// Base grid cell
float rowF = floor(uv.y / dy);
int baseY = int(rowF);
// Initialize with fallback
PerlageData outP;
outP.tangent2D = vec2(1.0, 0.0);
outP.radial2D = vec2(0.0, 1.0);
outP.rNorm = 0.5;
outP.grooveSlope = 0.0;
outP.edgeLip = 0.0;
outP.centerDimple = 0.0;
outP.cellTone = 0.5;
outP.rosetteMask = 0.0;
// Groove frequency: finer grooves on watchmaking perlage, bolder on dashboard engine-turning
float grooveCycles = mix(15.0, 22.0, openAmount);
// Paint-order shingled iteration: lower rows & columns first, later stamps overwrite earlier!
for (int iy = -1; iy <= 2; iy++) {
int gy = baseY + iy;
// Brick stagger every odd row
float rowShift = (mod(float(gy), 2.0) > 0.5) ? (0.5 * dx) : 0.0;
int baseX = int(floor((uv.x - rowShift) / dx));
for (int ix = -1; ix <= 2; ix++) {
int gx = baseX + ix;
vec2 gid = vec2(float(gx), float(gy));
vec2 rnd = hash22(gid + vec2(17.3, 41.7));
// Tiny hand-indexed engine-turning micro-jitter (1.2% of pitch)
vec2 jitter = (rnd - 0.5) * pitch * 0.022;
vec2 center = vec2((float(gx) + 0.5) * dx + rowShift, (float(gy) + 0.5) * dy) + jitter;
vec2 rel = uv - center;
float dist = length(rel);
float rNorm = dist / rosetteR;
if (rNorm < 1.12) {
// Crisp fwidth-antialiased circular stamp boundary
float cov = 1.0 - smoothstep(rosetteR - aaEdge, rosetteR + aaEdge, dist);
if (cov > 0.001) {
vec2 radDir = (dist > 1e-5) ? (rel / dist) : vec2(1.0, 0.0);
vec2 tanDir = vec2(-radDir.y, radDir.x);
// Concentric abrasive swirl micro-grooves (band-limited via fwUV)
float phase = rNorm * grooveCycles * TAU + rnd.x * TAU;
float cyclesPerPx = (fwUV / rosetteR) * grooveCycles;
float grooveFade = 1.0 - smoothstep(0.28, 0.95, cyclesPerPx);
float fineFade = 1.0 - smoothstep(0.18, 0.65, cyclesPerPx * 2.1);
float g1 = cos(phase) * grooveFade;
float g2 = cos(phase * 2.13 + rnd.y * 5.0) * 0.42 * fineFade;
// Subtle angular scratch break-up (abrasive peg grit isn't mathematically uniform)
float ang = atan(rel.y, rel.x);
float gritMod = 0.72 + 0.28 * sin(ang * 3.0 + rnd.x * 12.0 + rNorm * 7.0);
float gSlope = (g1 + g2) * gritMod;
// Crisp outer rim ring left by the spinning peg perimeter
float rimBand = exp(-pow((rNorm - 0.955) / max(0.032, aaEdge / rosetteR * 1.4), 2.0));
// Subtle zero-velocity burnish dimple at exact center
float dimple = exp(-pow(rNorm / 0.11, 2.0));
// Subtle per-rosette pressure/tone variation
float tone = 0.88 + 0.24 * rnd.y;
outP.tangent2D = normalize(mix(outP.tangent2D, tanDir, cov));
outP.radial2D = normalize(mix(outP.radial2D, radDir, cov));
outP.rNorm = mix(outP.rNorm, clamp(rNorm, 0.0, 1.0), cov);
outP.grooveSlope = mix(outP.grooveSlope, gSlope, cov);
outP.edgeLip = mix(outP.edgeLip, rimBand, cov);
outP.centerDimple = mix(outP.centerDimple, dimple, cov);
outP.cellTone = mix(outP.cellTone, tone, cov);
outP.rosetteMask = max(outP.rosetteMask, cov);
}
}
}
}
return outP;
}
// ============================================================================
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// ============================================================================
vec3 sampleStudioEnv(vec3 dir, int lightMode) {
vec3 d = normalize(dir);
float y = d.y;
// Base studio cyclorama (dark negative-fill charcoal for rich metallic contrast)
vec3 env = mix(vec3(0.035, 0.038, 0.044), vec3(0.085, 0.090, 0.100), smoothstep(-0.2, 0.65, y));
if (lightMode == 1) {
// Pure neutral grayscale studio
env = vec3(mix(0.040, 0.095, smoothstep(-0.2, 0.65, y)));
}
// Overhead main studio softbox
vec3 sb1Dir = normalize(vec3(0.28, 0.82, 0.42));
if (lightMode == 2) sb1Dir = normalize(vec3(0.86, 0.28, 0.38)); // Grazing raking strip
if (lightMode == 3) sb1Dir = normalize(vec3(-0.18, 0.46, -0.86)); // Strong backlit rim box
float sb1Dot = dot(d, sb1Dir);
float sb1 = smoothstep(0.68, 0.94, sb1Dot);
float sb1Core = smoothstep(0.88, 0.985, sb1Dot);
// Secondary crisp rim/accent strip light
vec3 sb2Dir = normalize(vec3(-0.76, 0.46, -0.45));
if (lightMode == 2) sb2Dir = normalize(vec3(-0.82, 0.22, -0.52));
if (lightMode == 3) sb2Dir = normalize(vec3(0.32, 0.72, 0.60));
float sb2 = smoothstep(0.72, 0.95, dot(d, sb2Dir));
// Warm/cool studio color grading by lightMode
vec3 cKey = vec3(1.04, 0.99, 0.93);
vec3 cRim = vec3(0.86, 0.94, 1.06);
if (lightMode == 1) {
cKey = vec3(1.0);
cRim = vec3(1.0);
} else if (lightMode == 2) {
cKey = vec3(1.08, 0.96, 0.84);
cRim = vec3(0.78, 0.88, 1.05);
} else if (lightMode == 3) {
cKey = vec3(1.05, 1.02, 0.98);
cRim = vec3(0.92, 0.95, 1.02);
}
float keyGain = (lightMode == 3) ? 2.5 : 2.6;
float rimGain = (lightMode == 3) ? 1.15 : 1.65;
env += cKey * (sb1 * 1.15 + sb1Core * keyGain);
env += cRim * (sb2 * rimGain);
// Crisp horizontal studio light-ribbon around horizon for anglage edge glints
float horizonStrip = exp(-pow((y - 0.14) * 9.5, 2.0)) * (0.45 + 0.55 * smoothstep(-0.2, 0.8, sin(atan(d.z, d.x) * 2.0 + 0.8)));
vec3 stripCol = (lightMode == 1) ? vec3(0.55) : vec3(0.52, 0.56, 0.62);
env += stripCol * horizonStrip;
// Dark negative-fill flags (absorptive matte black studio cards for high-contrast metal sculpting)
float flag1 = smoothstep(0.74, 0.90, dot(d, normalize(vec3(-0.35, 0.52, 0.78))));
float flag2 = smoothstep(0.78, 0.92, dot(d, normalize(vec3(0.78, 0.42, -0.45))));
float negFill = max(flag1, flag2);
env *= mix(1.0, 0.14, negFill);
return env;
}
// ============================================================================
// Physically-Based Anisotropic GGX & Isotropic GGX BRDF
// ============================================================================
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float D_GGX_Aniso(float NoH, float ToH, float BoH, float ax, float ay) {
float a2 = ax * ay;
vec3 v = vec3(ay * ToH, ax * BoH, a2 * NoH);
float v2 = dot(v, v);
float w2 = a2 / max(v2, 1e-6);
return a2 * w2 * w2 / PI;
}
float V_SmithGGX_Aniso(float NoV, float NoL, float ToV, float BoV, float ToL, float BoL, float ax, float ay) {
float lambdaV = NoL * length(vec3(ax * ToV, ay * BoV, NoV));
float lambdaL = NoV * length(vec3(ax * ToL, ay * BoL, NoL));
return 0.5 / max(lambdaV + lambdaL, 1e-5);
}
vec3 evalAnisoSpecular(vec3 N, vec3 V, vec3 L, vec3 T, vec3 B, vec3 F0, float alphaT, float alphaB) {
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float ToH = dot(T, H);
float BoH = dot(B, H);
float ToV = dot(T, V);
float BoV = dot(B, V);
float ToL = dot(T, L);
float BoL = dot(B, L);
float D = D_GGX_Aniso(NoH, ToH, BoH, alphaT, alphaB);
float G = V_SmithGGX_Aniso(NoV, NoL, ToV, BoV, ToL, BoL, alphaT, alphaB);
vec3 F = fresnelSchlick(VoH, F0);
return D * G * F * NoL;
}
vec3 evalIsoSpecular(vec3 N, vec3 V, vec3 L, vec3 F0, float rough) {
float a = max(rough * rough, 0.0018);
vec3 H = normalize(V + L);
float NoV = max(dot(N, V), 0.001);
float NoL = max(dot(N, L), 0.0);
if (NoL <= 0.0) return vec3(0.0);
float NoH = max(dot(N, H), 0.0);
float VoH = max(dot(V, H), 0.0);
float a2 = a * a;
float dDen = NoH * NoH * (a2 - 1.0) + 1.0;
float D = a2 / max(PI * dDen * dDen, 1e-6);
float k = (rough + 1.0) * (rough + 1.0) * 0.125;
float gV = NoV / (NoV * (1.0 - k) + k);
float gL = NoL / (NoL * (1.0 - k) + k);
vec3 F = fresnelSchlick(VoH, F0);
return (D * gV * gL * F) / max(4.0 * NoV, 1e-4);
}
// ============================================================================
// ACES Filmic Tone Mapping
// ============================================================================
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);
}
// ============================================================================
// Lighting Setup by uLightMode
// ============================================================================
void getStudioLights(
int lightMode,
out vec3 keyDir, out vec3 keyCol,
out vec3 fillDir, out vec3 fillCol,
out vec3 rimDir, out vec3 rimCol
) {
if (lightMode == 0) {
// Hero luxury watch photography rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.85, 2.74, 2.60);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(0.95, 1.02, 1.14);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.65, 1.72, 1.85);
} else if (lightMode == 1) {
// Pure neutral grayscale rig
keyDir = normalize(vec3(0.46, 0.78, 0.42));
keyCol = vec3(2.80);
fillDir = normalize(vec3(-0.72, 0.54, 0.44));
fillCol = vec3(1.02);
rimDir = normalize(vec3(-0.34, 0.48, -0.81));
rimCol = vec3(1.70);
} else if (lightMode == 2) {
// Grazing raking light to accentuate perlage grooves & anglage
keyDir = normalize(vec3(0.88, 0.25, 0.40));
keyCol = vec3(3.45, 3.10, 2.72);
fillDir = normalize(vec3(-0.82, 0.26, -0.50));
fillCol = vec3(0.88, 0.98, 1.15);
rimDir = normalize(vec3(-0.20, 0.62, -0.76));
rimCol = vec3(1.25, 1.30, 1.40);
} else {
// Backlit dramatic rim lighting
keyDir = normalize(vec3(-0.22, 0.42, -0.88));
keyCol = vec3(2.35, 2.30, 2.22);
fillDir = normalize(vec3(0.55, 0.64, 0.54));
fillCol = vec3(0.88, 0.90, 0.96);
rimDir = normalize(vec3(-0.78, 0.36, -0.50));
rimCol = vec3(1.25, 1.30, 1.40);
}
}
// ============================================================================
// Main Shading Function
// ============================================================================
vec3 shadeSurface(vec3 p, vec3 N, vec3 V, bool isBench) {
int mat = identifyMaterial(p, N, isBench);
vec3 keyDir, keyCol, fillDir, fillCol, rimDir, rimCol;
getStudioLights(uLightMode, keyDir, keyCol, fillDir, fillCol, rimDir, rimCol);
// Hero-only soft shadows with required bias p + N * 0.014
float shKey = calcSoftShadow(p + N * 0.014, keyDir, 10.0);
float shRim = calcSoftShadow(p + N * 0.014, rimDir, 8.0);
float ao = calcAO(p, N);
// ------------------------------------------------------------------------
// Dark Horologist's Studio Bench (Micro-grained anodized slate)
// ------------------------------------------------------------------------
if (mat == MAT_BENCH) {
float grain1 = valueNoise(p.xz * 26.0);
float grain2 = valueNoise(p.xz * 82.0 + 7.3);
float micro = (grain1 * 0.6 + grain2 * 0.4);
vec3 baseCol = (uLightMode == 1)
? vec3(0.042 + 0.012 * micro)
: vec3(0.038, 0.041, 0.048) + vec3(0.010, 0.011, 0.013) * micro;
// Subtle radial studio pool-of-light vignette on bench
float distC = length(p.xz);
float spot = exp(-distC * distC * 0.14);
vec3 benchN = normalize(N + vec3((grain1 - 0.5) * 0.025, 0.0, (grain2 - 0.5) * 0.025));
float rough = 0.48 + 0.12 * micro;
vec3 F0 = vec3(0.045);
vec3 col = vec3(0.0);
col += (baseCol / PI * max(dot(benchN, keyDir), 0.0) + evalIsoSpecular(benchN, V, keyDir, F0, rough)) * keyCol * shKey;
col += (baseCol / PI * max(dot(benchN, fillDir), 0.0) + evalIsoSpecular(benchN, V, fillDir, F0, rough) * 0.6) * fillCol;
col += (baseCol / PI * max(dot(benchN, rimDir), 0.0) + evalIsoSpecular(benchN, V, rimDir, F0, rough)) * rimCol * shRim;
vec3 R = reflect(-V, benchN);
vec3 envRefl = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(benchN, V), 0.0), F0);
col += envRefl * Fenv * 0.35 * ao;
col += baseCol * (0.28 + 0.25 * spot) * ao;
// Soft grounding contact shadow right under the watch movement
float contact = smoothstep(0.0, 0.18, max(length(p.xz) - 1.20, 0.0));
col *= mix(0.45, 1.0, contact);
return col;
}
// ------------------------------------------------------------------------
// Engine-Turned Perlage Rhodium (Mainplate & Bridge Top)
// ------------------------------------------------------------------------
if (mat == MAT_BRIDGE_TOP || mat == MAT_MAINPLATE) {
float scaleMul = (mat == MAT_MAINPLATE) ? 0.78 : 1.0;
PerlageData perl = evalPerlage(p.xz, uOpen, scaleMul);
// Rhodium-plated brass F0 (cool, lustrous silvery-white precious metal)
vec3 F0 = (uLightMode == 1)
? vec3(0.77, 0.77, 0.77)
: vec3(0.76, 0.775, 0.805);
F0 *= perl.cellTone;
// Slight contrast dip right at the outer circular abrasive seam & central dimple
F0 *= (1.0 - 0.07 * perl.edgeLip - 0.05 * perl.centerDimple);
// Build tangent frame aligned with concentric abrasive grooves
vec3 T = normalize(vec3(perl.tangent2D.x, 0.0, perl.tangent2D.y));
T = normalize(T - N * dot(T, N));
vec3 B = normalize(cross(N, T));
// Perturb surface normal radially by the band-limited concentric micro-grooves
float grooveAmp = mix(0.065, 0.115, uOpen);
vec3 Np = normalize(N + B * (perl.grooveSlope * grooveAmp + perl.edgeLip * 0.085));
// Re-orthogonalize T and B to perturbed normal
T = normalize(T - Np * dot(T, Np));
B = normalize(cross(Np, T));
// Anisotropic roughness: tight along grooves (T), broad across grooves (B)
// This creates the crisp rotating specular ring / bowtie inside every rosette!
float alphaT = mix(0.065, 0.052, uOpen) + perl.centerDimple * 0.08;
float alphaB = mix(0.36, 0.44, uOpen) - perl.centerDimple * 0.14;
vec3 col = vec3(0.0);
// Direct anisotropic specular from studio softboxes
col += evalAnisoSpecular(Np, V, keyDir, T, B, F0, alphaT, alphaB) * keyCol * shKey * 0.92;
col += evalAnisoSpecular(Np, V, fillDir, T, B, F0, alphaT, alphaB) * fillCol * 0.88;
col += evalAnisoSpecular(Np, V, rimDir, T, B, F0, alphaT, alphaB) * rimCol * shRim * 0.90;
// Anisotropic studio environment sampling (bend reflection normal toward radial bitangent)
vec3 anisoDir = cross(T, V);
vec3 anisoN = normalize(cross(anisoDir, T));
float bendAmt = clamp(alphaB * 0.85, 0.0, 0.55);
vec3 bentN = normalize(mix(Np, anisoN, bendAmt));
vec3 R0 = reflect(-V, bentN);
// Sample 3 spread rays along the anisotropic radial fan for rich rotating ring glints
vec3 R1 = normalize(R0 + B * 0.16);
vec3 R2 = normalize(R0 - B * 0.16);
vec3 envCol = (sampleStudioEnv(R0, uLightMode) * 0.50 +
sampleStudioEnv(R1, uLightMode) * 0.25 +
sampleStudioEnv(R2, uLightMode) * 0.25);
vec3 Fenv = fresnelSchlick(max(dot(Np, V), 0.0), F0);
// Crisp rotating highlight ring accent where view/key half-vector aligns radially
vec3 Hkey = normalize(V + keyDir);
float ringAlign = 1.0 - abs(dot(T, Hkey));
float ringGlow = pow(clamp(ringAlign, 0.0, 1.0), mix(10.0, 14.0, uOpen));
float radialProfile = smoothstep(0.06, 0.25, perl.rNorm) * (1.0 - 0.25 * smoothstep(0.82, 1.0, perl.rNorm));
col += envCol * Fenv * (0.64 + 0.28 * ringGlow * radialProfile) * ao;
// Subtle crisp glint along the circular rosette rim
col += F0 * perl.edgeLip * (0.12 * shKey + 0.06) * ao;
// Subtle satin metallic floor so dark negative-fill areas stay rich (never crushed to 0)
col += F0 * 0.055 * ao;
return col;
}
// ------------------------------------------------------------------------
// Mirror-Polished 45-deg Rhodium Anglage Bevels & Walls
// ------------------------------------------------------------------------
if (mat == MAT_ANGLAGE) {
vec3 F0 = (uLightMode == 1) ? vec3(0.84) : vec3(0.82, 0.84, 0.87);
float rough = 0.055;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.92 * ao;
col += F0 * 0.035 * ao;
return col;
}
// ------------------------------------------------------------------------
// 18k Yellow Gold Chaton
// ------------------------------------------------------------------------
if (mat == MAT_GOLD_CHATON) {
vec3 F0 = vec3(1.00, 0.78, 0.34);
// Fine circular lathe-turning micro-brushing on the gold chaton
vec2 q = p.xz - C_JEWEL;
float r = length(q);
float lathe = sin(r * 420.0) * (1.0 - smoothstep(0.002, 0.008, length(fwidth(q))));
vec3 radDir = (r > 1e-5) ? normalize(vec3(q.x, 0.0, q.y)) : vec3(1.0, 0.0, 0.0);
vec3 Ng = normalize(N + radDir * lathe * 0.025);
float rough = 0.11;
vec3 R = reflect(-V, Ng);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(Ng, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(Ng, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(Ng, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(Ng, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += F0 * 0.05 * ao;
return col;
}
// ------------------------------------------------------------------------
// Synthetic Corundum Ruby Jewel Bearing (IOR 1.77, Deep Crimson-Magenta Glow)
// ------------------------------------------------------------------------
if (mat == MAT_RUBY) {
vec3 F0 = vec3(0.077); // Corundum IOR = 1.77
float rough = 0.035;
float NoV = max(dot(N, V), 0.0);
vec3 F = fresnelSchlick(NoV, F0);
// Refracted internal ray & Beer-Lambert ruby absorption + internal caustic bounce
vec3 refr = refract(-V, N, 1.0 / 1.77);
vec2 q = p.xz - C_JEWEL;
float rNorm = clamp(length(q) / 0.115, 0.0, 1.0);
// Deep pigeon-blood / Swiss watchmaking synthetic ruby color
vec3 rubyDeep = vec3(0.32, 0.012, 0.065);
vec3 rubyBright = vec3(0.95, 0.055, 0.240);
// Internal light scatter & caustic opposite the key light
float caustic = pow(clamp(dot(normalize(-refr), keyDir) * 0.5 + 0.5, 0.0, 1.0), 3.0);
float rimGlow = smoothstep(0.25, 0.92, rNorm);
vec3 internal = mix(rubyDeep, rubyBright, 0.35 + 0.45 * caustic * (0.4 + 0.6 * shKey) + 0.28 * rimGlow);
// Subtle warm golden reflection from the surrounding gold chaton wall
internal += vec3(0.45, 0.18, 0.04) * pow(rimGlow, 2.5) * 0.45;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 col = internal * (1.0 - F) * (0.55 + 0.45 * ao);
col += env * F * 1.15;
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.35;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
return col;
}
// ------------------------------------------------------------------------
// Heat-Blued Steel Screw (Thin-Film Oxide Interference Royal Blue)
// ------------------------------------------------------------------------
if (mat == MAT_BLUED_STEEL) {
float NoV = max(dot(N, V), 0.0);
// Thin-film thermally blued steel: deep cornflower-cobalt shifting to indigo-cyan at grazing
vec3 blueCore = vec3(0.08, 0.22, 0.66);
vec3 blueGraze = vec3(0.28, 0.54, 0.92);
vec3 F0 = mix(blueGraze, blueCore, pow(NoV, 0.65));
float rough = 0.075;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(NoV, F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey * 1.2;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.95 * ao;
col += blueCore * 0.08 * ao;
return col;
}
// ------------------------------------------------------------------------
// Gilt Brass Escapement Wheel Glimpse (Inside Mainplate Well)
// ------------------------------------------------------------------------
vec3 F0 = vec3(0.88, 0.70, 0.36);
float rough = 0.18;
vec3 R = reflect(-V, N);
vec3 env = sampleStudioEnv(R, uLightMode);
vec3 Fenv = fresnelSchlick(max(dot(N, V), 0.0), F0);
vec3 col = vec3(0.0);
col += evalIsoSpecular(N, V, keyDir, F0, rough) * keyCol * shKey;
col += evalIsoSpecular(N, V, fillDir, F0, rough) * fillCol;
col += evalIsoSpecular(N, V, rimDir, F0, rough) * rimCol * shRim;
col += env * Fenv * 0.78 * ao;
col += F0 * 0.04 * ao;
return col;
}
// ============================================================================
// Raymarching & Camera
// ============================================================================
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / min(uResolution.x, uResolution.y);
// Gentle orbital motion so anisotropic perlage rings continuously sweep
float orbit = 0.35 + uTime * 0.14;
vec3 ro, ta;
float fov;
if (uCamMode == 1) {
// ~2.5x Macro loupe view centered on ruby chaton + blued screw + shingled perlage + anglage
ta = vec3(0.08, 0.19, -0.02);
ro = ta + vec3(sin(orbit) * 0.78, 0.98, cos(orbit) * 0.78);
fov = 2.65;
} else {
// Hero three-quarter luxury product framing with generous bench margin
ta = vec3(0.00, 0.08, 0.00);
ro = vec3(sin(orbit) * 3.10, 2.95, cos(orbit) * 3.10);
fov = 1.85;
}
vec3 ww = normalize(ta - ro);
vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
vec3 vv = cross(uu, ww);
vec3 rd = normalize(uv.x * uu + uv.y * vv + fov * ww);
// Raymarch scene
float t = 0.0;
bool hit = false;
const float tMax = 14.0;
for (int i = 0; i < 110; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p);
if (d < 0.00045) {
hit = true;
break;
}
t += d;
if (t > tMax) break;
}
vec3 col;
if (hit) {
vec3 p = ro + rd * t;
vec3 N = calcNormal(p);
float dHero = mapHero(p);
bool isBench = (p.y <= dHero + 0.001);
col = shadeSurface(p, N, -rd, isBench);
// Subtle atmospheric depth fade into studio backdrop at far bench horizon
float fog = 1.0 - exp(-max(t - 4.0, 0.0) * 0.12);
vec3 bgCol = sampleStudioEnv(rd, uLightMode) * 0.32;
col = mix(col, bgCol, clamp(fog, 0.0, 0.92));
} else {
col = sampleStudioEnv(rd, uLightMode) * 0.32;
}
// Subtle photographic vignette
float vig = 1.0 - 0.18 * dot(uv, uv);
col *= clamp(vig, 0.72, 1.0);
// Pure neutral grayscale enforcement for uLightMode == 1
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
// Exposure & ACES filmic tone mapping (headroom capped <= 245 sRGB, 0% clipped)
col *= 0.76;
col = acesToneMap(col);
col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2)) * 0.94;
if (uLightMode == 1) {
col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
}
fragColor = vec4(col, 1.0);
}
`;
export function createEngineTurnedGuillocheMaterial(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 | engine-turned-guilloche |
|---|---|
| Category | Metals & Armor Shaders · Industrial Metals |
| Upstream study | metals/lib/07_engine_turned_perlon_guilloche.glsl (commit 4778681) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime, uOpen, uLightMode, uCamMode |
| Source size | 936 lines · 37.0 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT pickups 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
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.
PBR StudyEuropean 4-in-1 riveted iron maille rings with accurate over-under interlocking and specular glints.
PBR StudyLocalized chloride-eaten cavities breaking through aged steel plate with dark ferric rims.
PBR StudyOxidized bronze and copper plating streaked with seafoam-green carbonate verdigris runoff.