PCB Solder Mask & Copper Traces shader

pcb-solder-mask-copper-traces.glsl · 1012 lines · GLSL ES 3.00 · MIT

PCB Solder Mask & Copper TracesSci-Fi & Noise
PCB Solder Mask & Copper Traces WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 1012 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Sci-Fi & Micro-Optics
pcb-solder-mask-copper-traces.glsl (1012 lines)
#version 300 es
// PCB Solder Mask & Copper Traces (pcb-solder-mask-copper-traces.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/electronics/lib/03_pcb_solder_mask_and_copper_traces.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 & Math Helpers
// -----------------------------------------------------------------------------
const float PI = 3.14159265359;
const vec3 PCB_HALF = vec3(1.16, 0.055, 0.80);
const float PCB_TOP = 0.11;

float hash21(vec2 p) {
    p = fract(p * vec2(123.34, 456.21));
    p += dot(p, p + 45.32);
    return fract(p.x * p.y);
}

float noise2(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 fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.8, 0.6, -0.6, 0.8);
    for (int i = 0; i < 4; i++) {
        v += a * noise2(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

float sdBox2(vec2 p, vec2 b) {
    vec2 d = abs(p) - b;
    return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
}

float sdRoundBox2(vec2 p, vec2 b, float r) {
    return sdBox2(p, b - r) - r;
}

float sdBox3(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float sdSegment(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h);
}

float sdChamferBox3(vec3 p, vec3 b, float ch, float r) {
    vec3 q = abs(p) - (b - vec3(r));
    float dBox = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
    // 45-degree top/bottom perimeter bevel
    float dEdgeXZ = max(abs(p.x) - b.x, abs(p.z) - b.z);
    float dBevel = (dEdgeXZ + abs(p.y) - b.y + ch) * 0.70710678;
    return max(dBox, dBevel);
}

// -----------------------------------------------------------------------------
// Peel-Back Mask Boundary (uOpen: 0..1 peels green mask on right half of PCB)
// -----------------------------------------------------------------------------
float getPeelSignedDist(vec2 q) {
    float openAmt = clamp(uOpen, 0.0, 1.0);
    if (openAmt <= 0.001) return -10.0;
    // Sweeps from right edge (x = 1.24) to center-left (x = -0.02)
    float peelX = mix(1.24, -0.02, smoothstep(0.0, 1.0, openAmt));
    // Slight diagonal angle + subtle organic resin tear undulation
    float wave = 0.018 * sin(q.y * 9.0 + 0.6) + 0.009 * sin(q.y * 23.0 - 1.1);
    float edge = peelX + q.y * 0.14 + wave;
    // Positive = peeled open (bare copper & raw FR-4); Negative = green solder mask
    return q.x - edge;
}

// -----------------------------------------------------------------------------
// PCB Pattern SDFs (2D in board XZ coordinates)
// -----------------------------------------------------------------------------

// Mounting holes at 4 corners
vec2 mountingHoleData(vec2 q) {
    vec2 m = abs(q) - vec2(0.98, 0.63);
    float d = length(m);
    // returns (drillDist, padDist)
    return vec2(d - 0.068, d - 0.118);
}

// QFN chip center and footprint
const vec2 QFN_C = vec2(-0.32, -0.03);
const vec2 QFN_H = vec2(0.22, 0.22);

// QFP unpopulated IC pad array center
const vec2 QFP_C = vec2(0.46, 0.04);

// QFN perimeter ENIG solder pads (8 per side)
float sdQFNPads(vec2 q) {
    vec2 p = q - QFN_C;
    vec2 ap = abs(p);
    // Horizontal sides (top/bottom)
    float pitch = 0.046;
    float idxX = clamp(round(p.x / pitch), -3.5, 3.5);
    // 8 pads centered -> half-integer offsets
    float gx = p.x - (clamp(floor(p.x / pitch) + 0.5, -3.5, 3.5)) * pitch;
    float dTB = sdRoundBox2(vec2(gx, ap.y - 0.242), vec2(0.012, 0.036), 0.003);

    float gz = p.y - (clamp(floor(p.y / pitch) + 0.5, -3.5, 3.5)) * pitch;
    float dLR = sdRoundBox2(vec2(ap.x - 0.242, gz), vec2(0.036, 0.012), 0.003);

    return min(dTB, dLR);
}

// QFP IC pad array (10 fine-pitch pads per side + central thermal pad)
float sdQFPPads(vec2 q) {
    vec2 p = q - QFP_C;
    vec2 ap = abs(p);
    float pitch = 0.040;

    float gx = p.x - (clamp(floor(p.x / pitch) + 0.5, -4.5, 4.5)) * pitch;
    float dTB = sdRoundBox2(vec2(gx, ap.y - 0.268), vec2(0.010, 0.042), 0.003);

    float gz = p.y - (clamp(floor(p.y / pitch) + 0.5, -4.5, 4.5)) * pitch;
    float dLR = sdRoundBox2(vec2(ap.x - 0.268, gz), vec2(0.042, 0.010), 0.003);

    // Central exposed ENIG thermal pad
    float dTherm = sdRoundBox2(p, vec2(0.145, 0.145), 0.012);

    return min(min(dTB, dLR), dTherm);
}

// Passive SMD 0603 pad pairs (decoupling caps near QFN)
float sdPassivePads(vec2 q) {
    float d = 1e5;
    // Left bank of 3 decoupling cap footprints
    for (int i = 0; i < 3; i++) {
        vec2 c = vec2(-0.74, -0.22 + float(i) * 0.18);
        vec2 p = q - c;
        float pair = sdRoundBox2(vec2(abs(p.x) - 0.034, p.y), vec2(0.022, 0.028), 0.004);
        d = min(d, pair);
    }
    // Top-right termination resistors
    for (int i = 0; i < 2; i++) {
        vec2 c = vec2(0.46 + float(i) * 0.14, -0.46);
        vec2 p = q - c;
        float pair = sdRoundBox2(vec2(p.x, abs(p.y) - 0.032), vec2(0.026, 0.020), 0.004);
        d = min(d, pair);
    }
    return d;
}

// Circular ENIG gold vias & drilled holes: returns vec2(drillDist, ringDist)
vec2 viaData(vec2 q) {
    float dMin = 1e5;

    // 1. QFP central thermal pad 3x3 via grid
    vec2 qp = q - QFP_C;
    vec2 qg = qp - clamp(round(qp / 0.075), -1.0, 1.0) * 0.075;
    dMin = min(dMin, length(qg));

    // 2. Differential pair fan-out & test vias (lower right & upper bus)
    vec2 v0 = vec2(0.08, -0.44);
    vec2 v1 = vec2(0.16, -0.44);
    vec2 v2 = vec2(0.24, -0.44);
    vec2 v3 = vec2(0.04, 0.44);
    vec2 v4 = vec2(0.13, 0.44);
    vec2 v5 = vec2(0.22, 0.44);
    vec2 v6 = vec2(0.31, 0.44);
    vec2 v7 = vec2(-0.62, 0.42);
    vec2 v8 = vec2(-0.52, 0.42);
    vec2 v9 = vec2(-0.82, -0.42);

    dMin = min(dMin, length(q - v0));
    dMin = min(dMin, length(q - v1));
    dMin = min(dMin, length(q - v2));
    dMin = min(dMin, length(q - v3));
    dMin = min(dMin, length(q - v4));
    dMin = min(dMin, length(q - v5));
    dMin = min(dMin, length(q - v6));
    dMin = min(dMin, length(q - v7));
    dMin = min(dMin, length(q - v8));
    dMin = min(dMin, length(q - v9));

    // 3. Perimeter ground-stitching via fence along top and bottom edges
    float fx = q.x - clamp(round(q.x / 0.14), -5.0, 5.0) * 0.14;
    float dFenceTB = length(vec2(fx, abs(q.y) - 0.69));
    dMin = min(dMin, dFenceTB);

    // 4. Right edge stitching vias
    float fz = q.y - clamp(round(q.y / 0.14), -3.0, 3.0) * 0.14;
    float dFenceR = length(vec2(q.x - 1.03, fz));
    dMin = min(dMin, dFenceR);

    // Drill radius = 0.013, ENIG annular ring outer radius = 0.029
    return vec2(dMin - 0.013, dMin - 0.029);
}

// Optical fiducial marks (gold disc + solder mask clearance ring)
vec2 fiducialData(vec2 q) {
    float d1 = length(q - vec2(-0.84, 0.46));
    float d2 = length(q - vec2(0.84, -0.46));
    float d = min(d1, d2);
    // returns (goldPadDist, maskClearanceDist)
    return vec2(d - 0.026, d - 0.056);
}

// 45-degree routed differential copper traces (exact analytical segments)
float sdCopperTraces(vec2 q) {
    float d = 1e5;
    float wDiff = 0.0085;  // half-width of differential signal trace
    float wPwr  = 0.016;   // half-width of power/ground trace

    // --- Bus 1: Two 45-deg differential pairs from QFN right side to QFP left side ---
    // Pair 1A (upper differential pair, 2 parallel traces spaced by 0.032)
    for (int i = 0; i < 2; i++) {
        float o = float(i) * 0.030;
        vec2 p0 = vec2(-0.07, -0.14 + o);
        vec2 p1 = vec2(0.01, -0.14 + o);
        vec2 p2 = vec2(0.09, -0.06 + o);
        vec2 p3 = vec2(0.19, -0.06 + o);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
    }

    // Pair 1B (lower differential pair with subtle 45-deg length-matching jog)
    for (int i = 0; i < 2; i++) {
        float o = float(i) * 0.030;
        vec2 p0 = vec2(-0.07, 0.04 + o);
        vec2 p1 = vec2(-0.01, 0.04 + o);
        vec2 p2 = vec2(0.04, 0.09 + o);
        vec2 p3 = vec2(0.09, 0.09 + o);
        vec2 p4 = vec2(0.14, 0.04 + o);
        vec2 p5 = vec2(0.19, 0.04 + o);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
        d = min(d, sdSegment(q, p3, p4) - wDiff);
        d = min(d, sdSegment(q, p4, p5) - wDiff);
    }

    // Pair 1C (bottom QFN-to-QFP differential pair)
    for (int i = 0; i < 2; i++) {
        float o = float(i) * 0.030;
        vec2 p0 = vec2(-0.07, 0.14 + o);
        vec2 p1 = vec2(0.03, 0.14 + o);
        vec2 p2 = vec2(0.11, 0.22 + o);
        vec2 p3 = vec2(0.19, 0.22 + o);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
    }

    // --- Bus 2: Top QFN fan-out -> 45-deg -> upper vias & termination resistors ---
    for (int i = 0; i < 3; i++) {
        float fi = float(i);
        float x0 = -0.22 + fi * 0.046;
        vec2 p0 = vec2(x0, -0.28);
        vec2 p1 = vec2(x0, -0.35 - fi * 0.02);
        vec2 p2 = vec2(0.08 + fi * 0.08, -0.44);
        // Split 45-deg elbow
        float dy = abs(p2.y - p1.y);
        vec2 elbow = vec2(p1.x + dy, p2.y);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, elbow) - wDiff);
        d = min(d, sdSegment(q, elbow, p2) - wDiff);
    }

    // --- Bus 3: QFP top -> resistors & right side fan-out ---
    for (int i = 0; i < 2; i++) {
        float fi = float(i);
        vec2 p0 = vec2(0.46 + fi * 0.14, -0.24);
        vec2 p1 = vec2(0.46 + fi * 0.14, -0.43);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
    }

    // Right side of QFP fanning out in 45-degree differential pairs toward right edge
    for (int i = 0; i < 4; i++) {
        float fi = float(i);
        float z0 = -0.10 + fi * 0.065;
        vec2 p0 = vec2(0.74, z0);
        vec2 p1 = vec2(0.81, z0);
        vec2 p2 = vec2(0.89, z0 + 0.08);
        vec2 p3 = vec2(0.96, z0 + 0.08);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
    }

    // --- Bus 4: Bottom QFN fan-out -> 45-deg -> lower test vias ---
    for (int i = 0; i < 4; i++) {
        float fi = float(i);
        float x0 = -0.27 + fi * 0.046;
        vec2 p0 = vec2(x0, 0.22);
        vec2 p1 = vec2(x0, 0.28 + fi * 0.015);
        vec2 p3 = vec2(0.04 + fi * 0.09, 0.44);
        float dy = abs(p3.y - p1.y);
        vec2 p2 = vec2(p1.x + dy, p3.y);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
    }

    // --- Bus 5: Wider power & decoupling traces on left side of QFN ---
    for (int i = 0; i < 3; i++) {
        float z0 = -0.22 + float(i) * 0.18;
        vec2 p0 = vec2(-0.56, z0);
        vec2 p1 = vec2(-0.70, z0);
        d = min(d, sdSegment(q, p0, p1) - wPwr);
    }
    // Power rail trunk connecting left decoupling caps to vias
    d = min(d, sdSegment(q, vec2(-0.78, -0.22), vec2(-0.82, -0.26)) - wPwr);
    d = min(d, sdSegment(q, vec2(-0.82, -0.26), vec2(-0.82, -0.42)) - wPwr);
    d = min(d, sdSegment(q, vec2(-0.78, 0.14), vec2(-0.62, 0.30)) - wPwr);
    d = min(d, sdSegment(q, vec2(-0.62, 0.30), vec2(-0.62, 0.42)) - wPwr);

    // Lower QFP fan-out bus
    for (int i = 0; i < 3; i++) {
        float fi = float(i);
        float x0 = 0.38 + fi * 0.08;
        vec2 p0 = vec2(x0, 0.31);
        vec2 p1 = vec2(x0, 0.44);
        vec2 p2 = vec2(x0 + 0.09, 0.53);
        vec2 p3 = vec2(x0 + 0.24, 0.53);
        d = min(d, sdSegment(q, p0, p1) - wDiff);
        d = min(d, sdSegment(q, p1, p2) - wDiff);
        d = min(d, sdSegment(q, p2, p3) - wDiff);
    }

    return d;
}

// Crisp white silkscreen markings (courtyards, fiducial crosshairs, U1/U2 glyphs)
float sdSilkscreen(vec2 q) {
    float d = 1e5;
    float w = 0.0055;

    // 1. QFN courtyard corner brackets
    vec2 pQFN = abs(q - QFN_C);
    float dQFNBox = abs(sdBox2(pQFN, vec2(0.30, 0.30))) - w;
    // Keep only corners (L-brackets)
    float qfnCornerCut = min(pQFN.x - 0.18, pQFN.y - 0.18);
    d = min(d, max(dQFNBox, -qfnCornerCut));
    // QFN Pin-1 white dot
    d = min(d, length((q - QFN_C) - vec2(-0.33, -0.33)) - 0.015);

    // 2. QFP courtyard corner brackets
    vec2 pQFP = abs(q - QFP_C);
    float dQFPBox = abs(sdBox2(pQFP, vec2(0.33, 0.33))) - w;
    float qfpCornerCut = min(pQFP.x - 0.20, pQFP.y - 0.20);
    d = min(d, max(dQFPBox, -qfpCornerCut));
    // QFP Pin-1 white dot
    d = min(d, length((q - QFP_C) - vec2(-0.35, -0.35)) - 0.015);

    // 3. Fiducial silkscreen outer rings & crosshair ticks
    vec2 f1 = q - vec2(-0.84, 0.46);
    vec2 f2 = q - vec2(0.84, -0.46);
    for (int i = 0; i < 2; i++) {
        vec2 fp = (i == 0) ? f1 : f2;
        float ring = abs(length(fp) - 0.068) - 0.0045;
        float tickH = sdSegment(abs(fp), vec2(0.062, 0.0), vec2(0.090, 0.0)) - 0.004;
        float tickV = sdSegment(abs(fp), vec2(0.0, 0.062), vec2(0.0, 0.090)) - 0.004;
        d = min(d, min(ring, min(tickH, tickV)));
    }

    // 4. Clean procedural vector silkscreen legend "U1" above QFN
    vec2 u1 = q - vec2(-0.44, -0.39);
    // 'U'
    d = min(d, sdSegment(u1, vec2(0.0, -0.025), vec2(0.0, 0.020)) - 0.0045);
    d = min(d, sdSegment(u1, vec2(0.0, 0.020), vec2(0.028, 0.020)) - 0.0045);
    d = min(d, sdSegment(u1, vec2(0.028, 0.020), vec2(0.028, -0.025)) - 0.0045);
    // '1'
    d = min(d, sdSegment(u1, vec2(0.056, -0.025), vec2(0.056, 0.020)) - 0.0045);
    d = min(d, sdSegment(u1, vec2(0.046, -0.015), vec2(0.056, -0.025)) - 0.0045);
    d = min(d, sdSegment(u1, vec2(0.044, 0.020), vec2(0.068, 0.020)) - 0.0045);

    // 5. Clean procedural vector silkscreen legend "U2" above QFP
    vec2 u2 = q - vec2(0.24, -0.39);
    // 'U'
    d = min(d, sdSegment(u2, vec2(0.0, -0.025), vec2(0.0, 0.020)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.0, 0.020), vec2(0.028, 0.020)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.028, 0.020), vec2(0.028, -0.025)) - 0.0045);
    // '2'
    d = min(d, sdSegment(u2, vec2(0.044, -0.025), vec2(0.072, -0.025)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.072, -0.025), vec2(0.072, -0.002)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.072, -0.002), vec2(0.044, -0.002)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.044, -0.002), vec2(0.044, 0.020)) - 0.0045);
    d = min(d, sdSegment(u2, vec2(0.044, 0.020), vec2(0.072, 0.020)) - 0.0045);

    // 6. Subtle board outline border ticks near left & bottom-left
    float borderLine = sdSegment(q, vec2(-1.05, -0.46), vec2(-1.05, 0.46)) - 0.0038;
    d = min(d, borderLine);

    return d;
}

// -----------------------------------------------------------------------------
// 3D Scene SDF (Hero = PCB + Raised Traces/Pads + QFN Chip; Bench = y=0 plane)
// -----------------------------------------------------------------------------
float mapHero(vec3 p) {
    // 1. Main beveled FR-4 PCB body resting on y = 0.0 (top at y = 0.11)
    vec3 pBoard = p - vec3(0.0, PCB_HALF.y, 0.0);
    float dBoard = sdChamferBox3(pBoard, PCB_HALF, 0.011, 0.032);

    // Subtract 4 corner mounting holes (through-holes)
    vec2 mHole = mountingHoleData(p.xz);
    dBoard = max(dBoard, -mHole.x);

    // Subtle 3D relief on top surface (only evaluated near top of PCB for speed)
    if (p.y > 0.06 && p.y < 0.22 && abs(p.x) < 1.20 && abs(p.z) < 0.84) {
        vec2 q = p.xz;

        // Raised 3D copper traces + pads relief
        float dTraces = sdCopperTraces(q);
        float dPads = min(min(sdQFNPads(q), sdQFPPads(q)), sdPassivePads(q));
        vec2 vData = viaData(q);
        vec2 fData = fiducialData(q);
        float dGold = min(min(dPads, min(vData.y, fData.x)), mHole.y);

        float traceMask = smoothstep(0.005, -0.003, dTraces);
        float goldMask = smoothstep(0.004, -0.003, dGold);

        // Solder mask thickness step along uOpen peel boundary
        float peelSD = getPeelSignedDist(q);
        float maskPresent = smoothstep(0.006, -0.004, peelSD);
        // Subtle curled lip ridge right at peel edge when uOpen > 0.02
        float peelActive = smoothstep(0.01, 0.05, uOpen);
        float peelLip = peelActive * exp(-pow((peelSD + 0.005) * 90.0, 2.0)) * 0.0045;

        float topRelief = traceMask * 0.0065 + goldMask * 0.0045 + maskPresent * 0.0028 + peelLip;
        // Apply relief smoothly only on top surface
        float topGate = smoothstep(0.08, 0.105, p.y) * smoothstep(0.02, -0.02, max(abs(q.x) - 1.12, abs(q.y) - 0.76));
        dBoard -= topRelief * topGate * 0.72;

        // Subtract drilled via barrels from top surface
        float dViaHole = max(-vData.x, 0.075 - p.y);
        dBoard = max(dBoard, dViaHole);
    }

    // 2. Matte Black QFN Chip Package resting on top of the PCB
    vec3 pQFN = p - vec3(QFN_C.x, PCB_TOP + 0.032, QFN_C.y);
    float dQFN = sdChamferBox3(pQFN, vec3(QFN_H.x, 0.032, QFN_H.y), 0.008, 0.006);

    // Pin-1 laser-etched dimple on top of QFN
    vec3 pDimple = p - vec3(QFN_C.x - 0.155, PCB_TOP + 0.068, QFN_C.y - 0.155);
    float dDimple = length(pDimple) - 0.022;
    dQFN = max(dQFN, -dDimple);

    return min(dBoard, dQFN);
}

float mapScene(vec3 p) {
    return min(p.y, mapHero(p));
}

// -----------------------------------------------------------------------------
// Unit-Weight Tetrahedral Normal (Zero-Bug Rule 1)
// -----------------------------------------------------------------------------
vec3 calcNormalHero(vec3 p) {
    const float h = 0.0005;
    const vec2 k = vec2(1.0, -1.0);
    return normalize(
        k.xyy * mapHero(p + k.xyy * h) +
        k.yyx * mapHero(p + k.yyx * h) +
        k.yxy * mapHero(p + k.yxy * h) +
        k.xxx * mapHero(p + k.xxx * h)
    );
}

// -----------------------------------------------------------------------------
// Hero-Only Clean Soft Shadow (Zero-Bug Rule 3: no ph!)
// -----------------------------------------------------------------------------
float softShadow(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 > 7.5) break;
    }
    return clamp(res, 0.0, 1.0);
}

// Ambient Occlusion (hero + bench)
float calcAO(vec3 pos, vec3 nor) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 5; i++) {
        float h = 0.015 + 0.038 * float(i);
        float d = mapScene(pos + h * nor);
        occ += (h - d) * sca;
        sca *= 0.72;
    }
    return clamp(1.0 - 2.6 * occ, 0.12, 1.0);
}

// -----------------------------------------------------------------------------
// Fibreglass Weave Micro-Structure (FR-4 2116 E-Glass Cloth)
// -----------------------------------------------------------------------------
// Returns vec3(weaveHeight, dHeight/dx, dHeight/dz)
vec3 fr4Weave(vec2 q) {
    float freq = 58.0;
    vec2 uv = q * freq;
    vec2 cell = floor(uv);
    vec2 f = fract(uv) - 0.5;
    float checker = mod(cell.x + cell.y, 2.0);

    // Warp (along X) vs Weft (along Z) alternating over-under fiber bundles
    float warpProfile = cos(f.y * PI) * (0.65 + 0.35 * cos(f.x * PI));
    float weftProfile = cos(f.x * PI) * (0.65 + 0.35 * cos(f.y * PI));

    float h = mix(warpProfile, weftProfile, checker);
    // Analytical-ish gradient for crisp micro-normal perturbation
    float eps = 0.03;
    float dx = (checker < 0.5)
        ? (-sin(f.x * PI) * 0.35 * cos(f.y * PI))
        : (-sin(f.x * PI) * (0.65 + 0.35 * cos(f.y * PI)));
    float dz = (checker < 0.5)
        ? (-sin(f.y * PI) * (0.65 + 0.35 * cos(f.x * PI)))
        : (-sin(f.y * PI) * 0.35 * cos(f.x * PI));

    return vec3(h, dx, dz);
}

// -----------------------------------------------------------------------------
// Studio Environment Sky (Softboxes + Negative-Fill Flags + Light Modes)
// -----------------------------------------------------------------------------
vec3 studioEnv(vec3 rd) {
    float y = clamp(rd.y, -1.0, 1.0);

    // Base studio cyclorama gradient
    vec3 skyLow  = vec3(0.075, 0.078, 0.085);
    vec3 skyMid  = vec3(0.16, 0.165, 0.178);
    vec3 skyHigh = vec3(0.24, 0.25, 0.27);
    vec3 col = mix(skyLow, skyMid, smoothstep(-0.15, 0.25, y));
    col = mix(col, skyHigh, smoothstep(0.25, 0.85, y));

    if (uLightMode == 1) {
        // Pure neutral grayscale studio
        float gLow = 0.08, gMid = 0.17, gHigh = 0.25;
        float g = mix(gLow, gMid, smoothstep(-0.15, 0.25, y));
        g = mix(g, gHigh, smoothstep(0.25, 0.85, y));
        col = vec3(g);
    }

    // Overhead key softbox (large rectangular diffuser)
    vec3 keyDir = normalize(vec3(-0.35, 0.85, 0.38));
    if (uLightMode == 2) keyDir = normalize(vec3(-0.92, 0.26, 0.28)); // grazing
    if (uLightMode == 3) keyDir = normalize(vec3(0.05, 0.36, -0.93)); // backlit

    // Softbox projection on ceiling/sphere
    float keyDot = max(dot(rd, keyDir), 0.0);
    float keyBox = smoothstep(0.76, 0.92, keyDot);
    vec3 keyTint = (uLightMode == 1) ? vec3(1.0) : vec3(1.04, 1.01, 0.96);
    float keyPow = (uLightMode == 3) ? 1.80 : 1.50;
    col += keyTint * keyBox * keyPow;

    // Secondary fill strip softbox (right-front)
    vec3 fillDir = normalize(vec3(0.72, 0.56, 0.42));
    if (uLightMode == 3) fillDir = normalize(vec3(-0.55, 0.55, 0.62));
    float fillBox = smoothstep(0.72, 0.90, max(dot(rd, fillDir), 0.0));
    vec3 fillTint = (uLightMode == 1) ? vec3(1.0) : vec3(0.92, 0.97, 1.05);
    col += fillTint * fillBox * 0.70;

    // Crisp rear rim strip light (for beveled edges & gold gleam)
    vec3 rimDir = normalize(vec3(0.18, 0.42, -0.88));
    float rimBox = smoothstep(0.84, 0.96, max(dot(rd, rimDir), 0.0));
    vec3 rimTint = (uLightMode == 1) ? vec3(1.0) : vec3(0.96, 1.0, 1.04);
    col += rimTint * rimBox * ((uLightMode == 3) ? 1.25 : 0.82);

    // Negative-fill studio flags (black velvet absorbers on left/right flanks for crisp contrast)
    float negFlagL = smoothstep(0.75, 0.93, max(dot(rd, normalize(vec3(-0.88, 0.22, -0.42))), 0.0));
    float negFlagR = smoothstep(0.78, 0.95, max(dot(rd, normalize(vec3(0.88, 0.16, -0.45))), 0.0));
    if (uLightMode != 2) {
        col *= (1.0 - 0.72 * negFlagL);
    }
    col *= (1.0 - 0.65 * negFlagR);

    // Ground bounce below horizon
    if (rd.y < 0.0) {
        vec3 bounce = (uLightMode == 1) ? vec3(0.07) : vec3(0.068, 0.072, 0.078);
        col = mix(col, bounce, smoothstep(0.0, -0.35, rd.y));
    }

    return col;
}

// -----------------------------------------------------------------------------
// Cook-Torrance GGX PBR + Optional Clearcoat Layer
// -----------------------------------------------------------------------------
float D_GGX(float NoH, float a) {
    float a2 = a * a;
    float d = NoH * NoH * (a2 - 1.0) + 1.0;
    return a2 / (PI * d * d + 1e-6);
}

float G_SchlickGGX(float NoV, float NoL, float roughness) {
    float r = roughness + 1.0;
    float k = (r * r) / 8.0;
    float gv = NoV / (NoV * (1.0 - k) + k);
    float gl = NoL / (NoL * (1.0 - k) + k);
    return gv * gl;
}

vec3 F_Schlick(float cosTheta, vec3 F0) {
    return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}

vec3 shadeLight(
    vec3 N, vec3 V, vec3 L, vec3 lightCol,
    vec3 albedo, float metallic, float roughness,
    float clearcoat, float clearcoatRough,
    float shadow
) {
    float NoL = max(dot(N, L), 0.0);
    if (NoL <= 0.0 || shadow <= 0.001) return vec3(0.0);

    vec3 H = normalize(V + L);
    float NoV = max(dot(N, V), 0.001);
    float NoH = max(dot(N, H), 0.0);
    float HoV = max(dot(H, V), 0.0);

    vec3 F0 = mix(vec3(0.04), albedo, metallic);
    float a = max(roughness * roughness, 0.004);
    float D = D_GGX(NoH, a);
    float G = G_SchlickGGX(NoV, NoL, roughness);
    vec3 F = F_Schlick(HoV, F0);

    vec3 spec = (D * G * F) / (4.0 * NoV * NoL + 0.001);
    vec3 kD = (1.0 - F) * (1.0 - metallic);
    vec3 diff = kD * albedo / PI;

    vec3 res = (diff + spec) * lightCol * NoL * shadow;

    // Glossy dielectric clearcoat (for translucent LPI emerald solder mask)
    if (clearcoat > 0.01) {
        float ac = max(clearcoatRough * clearcoatRough, 0.003);
        float Dc = D_GGX(NoH, ac);
        float Gc = G_SchlickGGX(NoV, NoL, clearcoatRough);
        vec3 Fc = F_Schlick(HoV, vec3(0.045));
        vec3 ccSpec = (Dc * Gc * Fc) / (4.0 * NoV * NoL + 0.001);
        res += ccSpec * clearcoat * lightCol * NoL * shadow;
    }

    return res;
}

// ACES Filmic Tone Mapping (clean museum highlight roll-off, no clipping)
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);
}

// -----------------------------------------------------------------------------
// Main Render
// -----------------------------------------------------------------------------
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;

    // Camera setup (0 = Hero 3/4 view, 1 = ~2.5x Macro on QFN/traces/vias/peel-back)
    float gentleSway = sin(uTime * 0.25) * 0.035;
    vec3 ro, ta;
    float focal;

    if (uCamMode == 1) {
        // ~2.5x Macro close-up centered on QFN edge, differential pairs, QFP pads & uOpen peel line
        ro = vec3(0.24 + gentleSway * 0.35, 0.72, 0.88);
        ta = vec3(0.04, 0.09, 0.01);
        focal = 2.15;
    } else {
        // Hero 3/4 museum framing of entire beveled PCB specimen
        ro = vec3(0.22 + gentleSway, 1.62, 2.28);
        ta = vec3(0.0, 0.05, 0.02);
        focal = 1.95;
    }

    vec3 fw = normalize(ta - ro);
    vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));
    vec3 up = cross(rt, fw);
    vec3 rd = normalize(fw * focal + rt * uv.x + up * uv.y);

    // Raymarch scene
    float t = 0.0;
    float tMax = 18.0;
    bool hit = false;
    for (int i = 0; i < 128; i++) {
        vec3 p = ro + rd * t;
        float d = mapScene(p);
        if (d < 0.00045) {
            hit = true;
            break;
        }
        t += d * 0.92;
        if (t > tMax) break;
    }

    vec3 col = studioEnv(rd);

    if (hit) {
        vec3 p = ro + rd * t;
        float dHero = mapHero(p);
        // Zero-Bug Rule 2: exact bench classification
        bool isBench = (p.y <= dHero + 0.001);

        vec3 N = isBench ? vec3(0.0, 1.0, 0.0) : calcNormalHero(p);
        vec3 V = -rd;

        // Material defaults
        vec3 albedo = vec3(0.11, 0.115, 0.122);
        float metallic = 0.0;
        float roughness = 0.44;
        float clearcoat = 0.0;
        float clearcoatRough = 0.12;
        vec3 sssTint = vec3(0.0);

        if (isBench) {
            // Studio anodized matte-slate bench
            float grain = fbm2(p.xz * 18.0);
            float fine = noise2(p.xz * 120.0);
            albedo = vec3(0.105, 0.110, 0.118) * (0.94 + 0.10 * grain);
            if (uLightMode == 1) albedo = vec3(0.112) * (0.94 + 0.10 * grain);
            roughness = 0.42 + 0.06 * fine;
            N = normalize(N + vec3(fine - 0.5, 0.0, grain - 0.5) * 0.012);
        } else {
            vec2 q = p.xz;
            // Classify QFN chip vs PCB board
            vec3 pQFN = p - vec3(QFN_C.x, PCB_TOP + 0.032, QFN_C.y);
            float dQFN = sdChamferBox3(pQFN, vec3(QFN_H.x, 0.032, QFN_H.y), 0.008, 0.006);
            bool isQFN = (dQFN < 0.003 && p.y > PCB_TOP + 0.002);

            if (isQFN) {
                // Matte black epoxy mold compound QFN chip package
                vec2 ql = q - QFN_C;
                float pebble = noise2(ql * 260.0);
                float micro = fbm2(ql * 75.0);
                albedo = vec3(0.048, 0.050, 0.055) * (0.90 + 0.20 * micro);
                roughness = 0.54 + 0.10 * pebble;
                metallic = 0.0;
                // Subtle micro-pebble normal perturbation on top face
                if (N.y > 0.7) {
                    N = normalize(N + vec3(pebble - 0.5, 0.0, noise2(ql * 260.0 + 9.3) - 0.5) * 0.045);

                    // Laser-etched matte silver-gray IC markings on top face
                    float dMark = 1e5;
                    // Stylized laser-etched brand & part-code micro-bars
                    dMark = min(dMark, sdBox2(ql - vec2(0.01, -0.055), vec2(0.115, 0.012)));
                    dMark = min(dMark, sdBox2(ql - vec2(-0.02, 0.005), vec2(0.085, 0.008)));
                    dMark = min(dMark, sdBox2(ql - vec2(0.02, 0.055), vec2(0.105, 0.008)));
                    // Segmented character dashes
                    float segBreak = smoothstep(0.15, 0.45, sin(ql.x * 135.0));
                    float markMask = smoothstep(0.003, -0.002, dMark) * (0.35 + 0.65 * segBreak);
                    albedo = mix(albedo, vec3(0.30, 0.31, 0.33), markMask * 0.85);
                    roughness = mix(roughness, 0.42, markMask);

                    // Subtle pin-1 dimple interior darkening
                    float dimpleM = smoothstep(0.024, 0.016, length(ql - vec2(-0.155, -0.155)));
                    albedo *= (1.0 - 0.25 * dimpleM);
                } else {
                    // QFN side walls: expose metallic tin-silver side-wettable flank leads along lower edge
                    float pitch = 0.046;
                    float leadCoord = (abs(N.x) > abs(N.z)) ? ql.y : ql.x;
                    float gLead = leadCoord - clamp(floor(leadCoord / pitch) + 0.5, -3.5, 3.5) * pitch;
                    float inLead = smoothstep(0.013, 0.009, abs(gLead)) * smoothstep(PCB_TOP + 0.024, PCB_TOP + 0.014, p.y);
                    albedo = mix(albedo, vec3(0.82, 0.83, 0.85), inLead);
                    metallic = mix(metallic, 0.95, inLead);
                    roughness = mix(roughness, 0.22, inLead);
                }
            } else {
                // PCB Substrate, Beveled Fibreglass Edge, Solder Mask, Copper Traces, ENIG Gold & Silkscreen
                vec3 weave = fr4Weave(q);
                float weaveVal = clamp(weave.x * 0.5 + 0.5, 0.0, 1.0);

                // Side & beveled FR-4 edges (N.y < 0.72)
                if (N.y < 0.72) {
                    // Check if inside a plated through-hole or via barrel
                    vec2 mHole = mountingHoleData(q);
                    vec2 vData = viaData(q);
                    bool inPlatedBarrel = (mHole.x < 0.012 || vData.x < 0.008);

                    if (inPlatedBarrel) {
                        // ENIG gold / copper plated through-hole barrel wall
                        albedo = vec3(0.92, 0.70, 0.32);
                        metallic = 0.96;
                        roughness = 0.24;
                    } else {
                        // Layered FR-4 fibreglass epoxy edge showing internal copper planes!
                        float yNorm = clamp(p.y / PCB_TOP, 0.0, 1.0);
                        float strata = sin(yNorm * PI * 14.0) * 0.5 + 0.5;
                        float edgeGrain = noise2(vec2(p.x + p.z, p.y * 8.0) * 45.0);
                        vec3 fr4Edge = mix(vec3(0.32, 0.30, 0.18), vec3(0.49, 0.45, 0.27), strata * 0.65 + edgeGrain * 0.35);

                        // Inner multi-layer copper power/ground plane strata (2 thin copper lines in 4-layer stackup)
                        float innerCu1 = exp(-pow((yNorm - 0.34) * 55.0, 2.0));
                        float innerCu2 = exp(-pow((yNorm - 0.66) * 55.0, 2.0));
                        float innerCu = clamp(innerCu1 + innerCu2, 0.0, 1.0);

                        albedo = mix(fr4Edge, vec3(0.92, 0.56, 0.40), innerCu * 0.85);
                        metallic = innerCu * 0.85;
                        roughness = mix(0.46 + 0.10 * edgeGrain, 0.24, innerCu);
                        N = normalize(N + vec3(0.0, (strata - 0.5) * 0.08, 0.0));
                    }
                } else {
                    // Top surface of the PCB
                    float dTraces = sdCopperTraces(q);
                    float dPads = min(min(sdQFNPads(q), sdQFPPads(q)), sdPassivePads(q));
                    vec2 vData = viaData(q);
                    vec2 fData = fiducialData(q);
                    vec2 mHole = mountingHoleData(q);

                    float dGold = min(min(dPads, min(vData.y, fData.x)), mHole.y);
                    float dSilk = sdSilkscreen(q);

                    float traceMask = smoothstep(0.0025, -0.0020, dTraces);
                    float goldMask  = smoothstep(0.0020, -0.0018, dGold);
                    float fidClear  = smoothstep(0.0025, -0.0020, fData.y);
                    float silkMask  = smoothstep(0.0020, -0.0015, dSilk);

                    // Peel-back state from uOpen
                    float peelSD = getPeelSignedDist(q);
                    // Solder mask covers where peelSD < 0, except over gold pads & fiducial clearance rings
                    float peelMask = smoothstep(0.0035, -0.0035, peelSD);
                    float hasSolderMask = peelMask * (1.0 - max(goldMask, fidClear));

                    // --- 1. Base Raw FR-4 Fibreglass Substrate ---
                    vec3 fr4Dark = vec3(0.30, 0.28, 0.15);
                    vec3 fr4Light = vec3(0.52, 0.48, 0.28);
                    float epoxyVar = fbm2(q * 12.0);
                    vec3 rawFR4 = mix(fr4Dark, fr4Light, weaveVal * 0.78 + epoxyVar * 0.22);
                    float fr4Rough = 0.36 + 0.14 * (1.0 - weaveVal);

                    // --- 2. Bare Bright Copper Traces (exposed when peeled) ---
                    float cuGrain = noise2(q * vec2(180.0, 45.0));
                    vec3 bareCopper = vec3(0.95, 0.56, 0.41) * (0.94 + 0.10 * cuGrain);
                    float cuRough = 0.19 + 0.05 * cuGrain;

                    // Combine raw FR-4 + bare copper
                    vec3 baseAlbedo = mix(rawFR4, bareCopper, traceMask);
                    float baseMetal = traceMask * 0.98;
                    float baseRough = mix(fr4Rough, cuRough, traceMask);

                    // --- 3. Translucent Glossy Emerald-Green Solder Mask ---
                    // Deep rich PCB emerald over FR-4 (revealing cross-hatched fibreglass weave underneath)
                    vec3 maskOverFR4 = mix(vec3(0.004, 0.062, 0.022), vec3(0.014, 0.128, 0.044), weaveVal * 0.85 + epoxyVar * 0.15);
                    // Raised copper traces under translucent green mask glow lighter jade-emerald
                    vec3 maskOverCu  = vec3(0.022, 0.225, 0.078) * (0.94 + 0.10 * cuGrain);
                    vec3 maskAlbedo  = mix(maskOverFR4, maskOverCu, traceMask);

                    // Subtle translucent subsurface green-jade sheen along trace ridges
                    albedo = mix(baseAlbedo, maskAlbedo, hasSolderMask);
                    metallic = mix(baseMetal, traceMask * 0.12, hasSolderMask);
                    roughness = mix(baseRough, mix(0.24, 0.18, traceMask), hasSolderMask);
                    clearcoat = hasSolderMask * 0.88;
                    clearcoatRough = 0.11;
                    sssTint = hasSolderMask * mix(vec3(0.008, 0.085, 0.028), vec3(0.024, 0.185, 0.062), traceMask);

                    // Slight bright resin highlight right along the peel-back tear lip
                    if (uOpen > 0.01) {
                        float lip = exp(-pow(peelSD * 110.0, 2.0));
                        albedo = mix(albedo, vec3(0.09, 0.30, 0.12), lip * 0.55 * (1.0 - goldMask));
                        clearcoat = max(clearcoat, lip * 0.75);
                    }

                    // --- 4. Crisp White Silkscreen (sits on top of solder mask) ---
                    float silkFinal = silkMask * hasSolderMask;
                    albedo = mix(albedo, vec3(0.86, 0.87, 0.85), silkFinal);
                    metallic = mix(metallic, 0.0, silkFinal);
                    roughness = mix(roughness, 0.38, silkFinal);
                    clearcoat = mix(clearcoat, 0.15, silkFinal);

                    // --- 5. ENIG Gold-Plated Pads, Vias, Fiducials & Mounting Rings ---
                    float goldMicro = noise2(q * 210.0);
                    vec3 enigGold = vec3(0.98, 0.77, 0.33) * (0.95 + 0.08 * goldMicro);
                    albedo = mix(albedo, enigGold, goldMask);
                    metallic = mix(metallic, 0.99, goldMask);
                    roughness = mix(roughness, 0.14 + 0.04 * goldMicro, goldMask);
                    clearcoat = mix(clearcoat, 0.0, goldMask);
                    sssTint *= (1.0 - goldMask);

                    // Perturb normal with FR-4 fibreglass weave micro-relief (stronger on bare FR-4, softer under glossy mask)
                    float weaveNormalAmp = mix(0.065, 0.024, hasSolderMask) * (1.0 - goldMask) * (1.0 - traceMask * 0.65);
                    N = normalize(N + vec3(weave.y, 0.0, weave.z) * weaveNormalAmp);
                }
            }
        }

        // Pure neutral grayscale mode (uLightMode == 1): zero chroma in all material albedos & SSS
        if (uLightMode == 1) {
            albedo = vec3(dot(albedo, vec3(0.2126, 0.7152, 0.0722)));
            sssTint = vec3(dot(sssTint, vec3(0.2126, 0.7152, 0.0722)));
        }

        // Lighting setup per uLightMode
        vec3 L1, L2, L3;
        vec3 C1, C2, C3;
        float shK = 14.0;

        if (uLightMode == 1) {
            // Pure neutral grayscale studio lighting
            L1 = normalize(vec3(-0.38, 0.82, 0.42));
            L2 = normalize(vec3(0.70, 0.55, 0.45));
            L3 = normalize(vec3(0.15, 0.45, -0.88));
            C1 = vec3(1.85);
            C2 = vec3(0.68);
            C3 = vec3(0.76);
        } else if (uLightMode == 2) {
            // Grazing raking key light to emphasize 3D copper traces & weave micro-relief
            L1 = normalize(vec3(-0.92, 0.25, 0.29));
            L2 = normalize(vec3(0.65, 0.62, 0.44));
            L3 = normalize(vec3(0.25, 0.32, -0.91));
            C1 = vec3(2.45, 2.34, 2.18);
            C2 = vec3(0.45, 0.49, 0.56);
            C3 = vec3(0.75, 0.80, 0.86);
            shK = 18.0;
        } else if (uLightMode == 3) {
            // Backlit rim light catching translucent emerald mask & beveled FR-4 edges
            L1 = normalize(vec3(0.06, 0.36, -0.93));
            L2 = normalize(vec3(-0.55, 0.62, 0.56));
            L3 = normalize(vec3(0.68, 0.48, -0.55));
            C1 = vec3(2.35, 2.31, 2.25);
            C2 = vec3(0.58, 0.63, 0.70);
            C3 = vec3(0.90, 0.93, 0.96);
            shK = 12.0;
        } else {
            // Hero museum studio lighting
            L1 = normalize(vec3(-0.36, 0.83, 0.42));
            L2 = normalize(vec3(0.72, 0.54, 0.44));
            L3 = normalize(vec3(0.18, 0.44, -0.88));
            C1 = vec3(1.90, 1.85, 1.76);
            C2 = vec3(0.62, 0.68, 0.75);
            C3 = vec3(0.82, 0.86, 0.91);
        }

        // Shadows (Zero-Bug Rule 3: bias p + N * 0.014)
        float sh1 = softShadow(p + N * 0.014, L1, shK);
        float sh2 = softShadow(p + N * 0.014, L2, 10.0);
        float ao  = calcAO(p, N);

        vec3 Lo = vec3(0.0);
        Lo += shadeLight(N, V, L1, C1, albedo, metallic, roughness, clearcoat, clearcoatRough, sh1);
        Lo += shadeLight(N, V, L2, C2, albedo, metallic, roughness, clearcoat, clearcoatRough, sh2);
        Lo += shadeLight(N, V, L3, C3, albedo, metallic, roughness, clearcoat, clearcoatRough, 1.0);

        // Translucent solder-mask subsurface forward/rim glow
        if (dot(sssTint, vec3(1.0)) > 0.001) {
            float rim = pow(clamp(1.0 - max(dot(N, V), 0.0), 0.0, 1.0), 2.5);
            float backGlow = pow(clamp(dot(V, -L1), 0.0, 1.0), 3.0) * 0.5 + 0.5;
            Lo += sssTint * (0.28 * rim + 0.16 * backGlow) * C1 * 0.35 * ao;
        }

        // Studio IBL (diffuse irradiance + glossy reflection + clearcoat reflection)
        vec3 R = reflect(-V, N);
        float NoV = max(dot(N, V), 0.001);
        vec3 F0 = mix(vec3(0.04), albedo, metallic);
        vec3 F_env = F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - NoV, 0.0, 1.0), 5.0);

        vec3 envDiff = studioEnv(N) * (1.0 - metallic) * (1.0 - F_env) * albedo * 0.85;
        vec3 envSpecDir = normalize(mix(R, N, roughness * roughness * 0.65));
        vec3 envSpec = studioEnv(envSpecDir) * F_env * mix(1.0, 0.38, roughness);

        vec3 envCC = vec3(0.0);
        if (clearcoat > 0.01) {
            vec3 F_cc = F_Schlick(NoV, vec3(0.045));
            envCC = studioEnv(R) * F_cc * clearcoat * 0.68;
        }

        // Bench subtle contact reflection of PCB emerald/FR-4 body
        if (isBench) {
            float boardProx = smoothstep(0.65, 0.0, sdRoundBox2(p.xz, PCB_HALF.xz, 0.05));
            envSpec *= (1.0 - 0.55 * boardProx);
        }

        col = Lo + (envDiff + envSpec + envCC) * ao;

        // Subtle atmospheric distance fade into studio cyclorama
        float fog = 1.0 - exp(-0.0045 * t * t);
        col = mix(col, studioEnv(rd), clamp(fog, 0.0, 0.65));
    }

    // Subtle studio vignette
    float vig = 1.0 - 0.18 * dot(uv, uv);
    col *= clamp(vig, 0.78, 1.0);

    // ACES filmic tone mapping + gentle highlight headroom (0% clipping, lum <= 246) + sRGB gamma
    col = acesToneMap(col * 0.80);
    col *= 0.915;
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierpcb-solder-mask-copper-traces
CategorySci-Fi, Plasma & Noise Shaders · Sci-Fi & Micro-Optics
Upstream studyelectronics/lib/03_pcb_solder_mask_and_copper_traces.glsl (commit 14ad58d)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size1012 lines · 40.8 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT scifi sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

Krzysztof Narkowicz, filmic curve · Inigo Quilez, tetrahedral SDF normal

All sci-fi & noise shaders