Powder-Coat Matte Metal shader

powder-coat-matte-metal.glsl · 713 lines · GLSL ES 3.00 · MIT

Powder-Coat Matte MetalMetals & Armor
Powder-Coat Matte Metal WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 713 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

Industrial Metals
powder-coat-matte-metal.glsl (713 lines)
#version 300 es
// Powder-Coat Matte Metal (powder-coat-matte-metal.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/metals/lib/06_powder_coat_orange_peel_texture.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;

mat2 rot2(float a) {
    float s = sin(a), c = cos(a);
    return mat2(c, -s, s, c);
}

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);
}

// -----------------------------------------------------------------------------
// Procedural 3D Noise for Orange-Peel & Wrinkle Micro-Topography
// -----------------------------------------------------------------------------
float hash31(vec3 p) {
    p = fract(p * 0.1031);
    p += dot(p, p.yzx + 33.33);
    return fract((p.x + p.y) * p.z);
}

// Smooth cubic value noise (C1 continuous, fast, ideal for melt-flow ripples)
float vnoise(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    vec3 u = f * f * (3.0 - 2.0 * f);

    float n000 = hash31(i + vec3(0.0, 0.0, 0.0));
    float n100 = hash31(i + vec3(1.0, 0.0, 0.0));
    float n010 = hash31(i + vec3(0.0, 1.0, 0.0));
    float n110 = hash31(i + vec3(1.0, 1.0, 0.0));
    float n001 = hash31(i + vec3(0.0, 0.0, 1.0));
    float n101 = hash31(i + vec3(1.0, 0.0, 1.0));
    float n011 = hash31(i + vec3(0.0, 1.0, 1.0));
    float n111 = hash31(i + vec3(1.0, 1.0, 1.0));

    float nx00 = mix(n000, n100, u.x);
    float nx10 = mix(n010, n110, u.x);
    float nx01 = mix(n001, n101, u.x);
    float nx11 = mix(n011, n111, u.x);

    float nxy0 = mix(nx00, nx10, u.y);
    float nxy1 = mix(nx01, nx11, u.y);
    return mix(nxy0, nxy1, u.z);
}

// Characteristic baked polymer orange-peel heightfield (0..1 -> smooth semi-gloss to heavy wrinkle)
float orangePeelHeight(vec3 p, float open, float aaWrinkle) {
    // Primary liquid melt-flow undulation (classic glossy orange peel dimples)
    vec3 q = p * 16.0;
    float w1 = vnoise(q);
    // Soft domain warp for organic surface-tension flow
    float w2 = vnoise(q * 1.35 + vec3(w1 * 0.55, -w1 * 0.40, w1 * 0.50));
    float peel = w1 * 0.62 + w2 * 0.38;

    // Secondary pebble ripple (coherent rounded baked powder beads, grows with uOpen)
    float pebble = vnoise(p * 26.0 + vec3(peel * 0.85));
    // Heavy baked wrinkle finish ridges (coherent ~0.35x frequency, damped by fwidth AA)
    float wrinkle = vnoise(p * 42.0 + vec3(pebble * 0.75));
    float wrinkleRidge = 1.0 - abs(wrinkle * 2.0 - 1.0);

    float h = peel * mix(0.58, 0.95, open)
            + pebble * mix(0.10, 0.44, open) * mix(0.45, 1.0, aaWrinkle)
            + wrinkleRidge * smoothstep(0.25, 1.0, open) * 0.26 * aaWrinkle;
    return h;
}

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

float sdCylinderY(vec3 p, float r, float h) {
    vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float sdCylinderX(vec3 p, float r, float h) {
    vec2 d = abs(vec2(length(p.yz), p.x)) - vec2(r, h);
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

// -----------------------------------------------------------------------------
// Chip Geometry & Mask (Chipped Edge Revealing Bare Steel & Primer)
// -----------------------------------------------------------------------------
// Located on the prominent front-upper convex corner ridge facing the camera
float chipField(vec3 p) {
    vec3 c = p - vec3(0.305, 0.975, 0.305);
    // Elongate slightly along the upper front corner ridge
    vec3 q = vec3(c.x - c.z, c.y * 1.35, (c.x + c.z) * 0.95);
    float n1 = vnoise(p * 26.0) - 0.5;
    float n2 = vnoise(p * 62.0) - 0.5;
    float jagged = n1 * 0.032 + n2 * 0.014;
    float d = length(q * vec3(0.75, 1.05, 1.15)) - 0.088 + jagged;
    return d;
}

// Secondary micro-nick along the front lower flange corner
float chipFieldSmall(vec3 p) {
    vec3 c = p - vec3(0.61, 0.085, 0.38);
    float n = (vnoise(p * 48.0) - 0.5) * 0.015;
    return length(c * vec3(1.1, 1.6, 0.85)) - 0.036 + n;
}

float getChipSDF(vec3 p) {
    return min(chipField(p), chipFieldSmall(p));
}

// -----------------------------------------------------------------------------
// Hero SDF: Formed Sheet-Metal Industrial Enclosure Corner / Mounting Bracket
// Grounded cleanly on studio bench at y = 0.0
// -----------------------------------------------------------------------------
float mapHeroRaw(vec3 p, out int matId, out float edgeBead) {
    matId = 0; // 0 = teal powder coat, 1 = safety-yellow corner bracket clip
    edgeBead = 0.0;

    // 1. Main formed vertical enclosure corner hood (convex corner toward +X, +Z)
    // Outer shell: x in [-0.62, 0.36], z in [-0.62, 0.36], y in [0.04, 1.04]
    vec3 hoodCenter = vec3(-0.13, 0.54, -0.13);
    vec3 hoodHalf = vec3(0.39, 0.40, 0.39); // with r=0.10 -> extents +/-0.49
    float dOuter = sdRoundBox(p - hoodCenter, hoodHalf, 0.10);

    // Hollow out the back-left interior so it is a true formed sheet-metal shell
    // Wall thickness ~ 0.088
    vec3 innerCenter = vec3(-0.22, 0.49, -0.22);
    vec3 innerHalf = vec3(0.39, 0.38, 0.39);
    float dInner = sdRoundBox(p - innerCenter, innerHalf, 0.065);
    float dHood = smax(dOuter, -dInner, 0.018);

    // Cut cleanly flat at bottom above base plate so interior stays neat
    dHood = max(dHood, -(p.y - 0.03));

    // 2. Formed Horizontal Mounting Base Flange resting flush at y = 0.0
    // Height = 0.090 (center y=0.045, half-y=0.025, r=0.020 -> bottom at y=0.0 exactly!)
    vec3 flangeCenter = vec3(0.05, 0.045, 0.05);
    vec3 flangeHalf = vec3(0.62, 0.025, 0.62);
    float dFlange = sdRoundBox(p - flangeCenter, flangeHalf, 0.020);

    // Cut out the inner rear-left quadrant of the base flange to form an authentic L-bracket foot
    float dFlangeCut = sdRoundBox(p - vec3(-0.36, 0.05, -0.36), vec3(0.30, 0.12, 0.30), 0.08);
    dFlange = smax(dFlange, -dFlangeCut, 0.022);

    // Smooth formed sheet-metal bend fillet joining hood to base flange
    float dMain = smin(dHood, dFlange, 0.055);

    // 3. Stamped Emboss Details on the Formed Faces
    // 3a. Front (+Z) raised stadium reinforcement emboss
    vec3 pFront = p - vec3(-0.08, 0.56, 0.365);
    float dFrontEmboss = sdRoundBox(pFront, vec3(0.18, 0.14, 0.014), 0.024);
    dMain = smin(dMain, dFrontEmboss, 0.026);
    // Subtle recessed inner panel inside front emboss
    float dFrontRecess = sdRoundBox(p - vec3(-0.08, 0.56, 0.392), vec3(0.115, 0.075, 0.014), 0.014);
    dMain = smax(dMain, -dFrontRecess, 0.014);

    // 3b. Right (+X) stamped circular conduit knockout boss
    vec3 pRight = p - vec3(0.362, 0.54, -0.06);
    float dKnockoutOuter = sdCylinderX(pRight, 0.175, 0.018);
    dMain = smin(dMain, dKnockoutOuter, 0.024);
    // Thin circular knockout groove ring
    float rRing = abs(length(pRight.yz) - 0.122) - 0.012;
    float dKnockoutRing = max(rRing, abs(pRight.x - 0.018) - 0.014);
    dMain = smax(dMain, -dKnockoutRing, 0.008);

    // 3c. Top face formed drip-edge bead / stiffening rib
    float dTopRib = sdRoundBox(p - vec3(-0.08, 1.035, -0.08), vec3(0.24, 0.010, 0.24), 0.018);
    dMain = smin(dMain, dTopRib, 0.022);

    // 4. Punched Mounting Bolt Holes in the Base Flange (+X wing and +Z wing)
    float dHole1 = sdCylinderY(p - vec3(0.51, 0.05, -0.18), 0.078, 0.12);
    float dHole2 = sdCylinderY(p - vec3(-0.18, 0.05, 0.51), 0.078, 0.12);
    float dHole3 = sdCylinderY(p - vec3(0.49, 0.05, 0.49), 0.068, 0.12);
    float dHoles = min(dHole1, min(dHole2, dHole3));

    // Soft rounded powder-coat buildup ("picture framing") around flange rim and hole edges
    float nearHole = exp(-pow(abs(dHoles) * 24.0, 2.0));
    float nearFlangeEdge = exp(-pow((abs(p.y - 0.085)) * 28.0, 2.0)) * smoothstep(0.35, 0.55, max(abs(p.x), abs(p.z)));
    edgeBead = clamp(nearHole + nearFlangeEdge * 0.7, 0.0, 1.0);
    // Subtle physical thickening along the hole rim from electrostatic powder accumulation
    dMain -= nearHole * 0.0045;

    // Punch holes with soft powder-coated chamfer
    dMain = smax(dMain, -dHoles, 0.016);

    // 5. Safety-Yellow Powder-Coated Corner Reinforcement Gusset / Bumper Tab
    // Mounted neatly on the front-right lower corner of the base flange (resting at y=0.0)
    vec3 yPos = p - vec3(0.45, 0.115, 0.45);
    float dYellowClip = sdRoundBox(yPos, vec3(0.12, 0.028, 0.12), 0.016);
    // Vertical tab wrapping slightly up the corner fillet
    float dYellowTab = sdRoundBox(p - vec3(0.37, 0.18, 0.37), vec3(0.055, 0.075, 0.055), 0.018);
    float dYellow = smin(dYellowClip, dYellowTab, 0.024);
    // Share the corner bolt hole cleanly through the yellow washer/clip
    dYellow = smax(dYellow, -dHole3, 0.012);

    float dFinal = dMain;
    if (dYellow < dFinal) {
        dFinal = dYellow;
        matId = 1;
    }

    // 6. Chipped Polymer Edge Carve-out (steps down by coating thickness ~0.011 into bare steel)
    float dChip = getChipSDF(p);
    float chipStep = smoothstep(0.012, -0.008, dChip);
    dFinal += chipStep * 0.0105;

    return dFinal;
}

float mapHero(vec3 p) {
    int dummyMat;
    float dummyBead;
    return mapHeroRaw(p, dummyMat, dummyBead);
}

// -----------------------------------------------------------------------------
// Normals, Soft Shadows & Ambient Occlusion (Zero-Bug Rules Enforced)
// -----------------------------------------------------------------------------
// Unit-weight tetrahedral normals: const vec2 k = vec2(1., -1.);
vec3 calcNormalHero(vec3 p) {
    const float e = 0.0012;
    const vec2 k = vec2(1.0, -1.0);
    return normalize(
        k.xyy * mapHero(p + k.xyy * e) +
        k.yyx * mapHero(p + k.yyx * e) +
        k.yxy * mapHero(p + k.yxy * e) +
        k.xxx * mapHero(p + k.xxx * e)
    );
}

// Clean hero-only soft shadow: res = min(res, k*h/t), no ph!, tMin=0.025, step max(h,0.015)
float calcSoftShadow(vec3 ro, vec3 rd, float tMax, float k) {
    float res = 1.0;
    float t = 0.025;
    for (int i = 0; i < 48; i++) {
        float h = mapHero(ro + rd * t);
        res = min(res, k * h / t);
        t += max(h, 0.015);
        if (res < 0.004 || t > tMax) break;
    }
    return clamp(res, 0.0, 1.0);
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 5; i++) {
        float h = 0.025 + 0.055 * float(i);
        float d = min(mapHero(p + h * n), p.y + h * n.y);
        occ += (h - d) * sca;
        sca *= 0.68;
    }
    return clamp(1.0 - 2.3 * occ, 0.14, 1.0);
}

// -----------------------------------------------------------------------------
// High-Contrast Studio Environment (Softboxes + Dark Negative-Fill Flags)
// -----------------------------------------------------------------------------
vec3 sampleStudioEnv(vec3 rd, int lightMode) {
    vec3 dir = normalize(rd);

    // Base dark-neutral studio cyclorama gradient
    float up = clamp(dir.y * 0.5 + 0.5, 0.0, 1.0);
    vec3 baseSky = mix(vec3(0.055, 0.060, 0.068), vec3(0.14, 0.15, 0.17), smoothstep(0.35, 0.95, up));
    if (lightMode == 1) {
        baseSky = vec3(mix(0.065, 0.155, smoothstep(0.35, 0.95, up)));
    } else if (lightMode == 2) {
        baseSky *= 0.62;
    } else if (lightMode == 3) {
        baseSky *= 0.58;
    }

    vec3 col = baseSky;

    // Softbox helper (rectangular area light projected on unit sphere)
    // 1. Overhead Hero Softbox (crisp rectangular reflection that bends across orange peel)
    vec3 sb1Dir = normalize(vec3(0.28, 0.88, 0.38));
    vec3 sb1Right = normalize(cross(vec3(0.0, 0.0, 1.0), sb1Dir));
    vec3 sb1Up = cross(sb1Dir, sb1Right);
    vec2 sb1UV = vec2(dot(dir, sb1Right), dot(dir, sb1Up)) / max(dot(dir, sb1Dir), 0.001);
    float sb1Mask = step(0.0, dot(dir, sb1Dir))
        * smoothstep(0.56, 0.44, abs(sb1UV.x))
        * smoothstep(0.34, 0.24, abs(sb1UV.y));
    // Subtle softbox diffuser falloff
    float sb1Fall = 1.0 - 0.28 * dot(sb1UV, sb1UV);

    // 2. Side Key Strip Softbox (grazing reflection band)
    vec3 sb2Dir = normalize(vec3(0.86, 0.34, 0.38));
    if (lightMode == 2) sb2Dir = normalize(vec3(0.95, 0.16, 0.26));
    vec3 sb2Right = normalize(cross(vec3(0.0, 1.0, 0.0), sb2Dir));
    vec3 sb2Up = cross(sb2Dir, sb2Right);
    vec2 sb2UV = vec2(dot(dir, sb2Right), dot(dir, sb2Up)) / max(dot(dir, sb2Dir), 0.001);
    float sb2Mask = step(0.0, dot(dir, sb2Dir))
        * smoothstep(0.24, 0.15, abs(sb2UV.x))
        * smoothstep(0.62, 0.48, abs(sb2UV.y));

    // 3. Left Cool Fill Softbox
    vec3 sb3Dir = normalize(vec3(-0.78, 0.42, 0.46));
    vec3 sb3Right = normalize(cross(vec3(0.0, 1.0, 0.0), sb3Dir));
    vec3 sb3Up = cross(sb3Dir, sb3Right);
    vec2 sb3UV = vec2(dot(dir, sb3Right), dot(dir, sb3Up)) / max(dot(dir, sb3Dir), 0.001);
    float sb3Mask = step(0.0, dot(dir, sb3Dir))
        * smoothstep(0.45, 0.30, abs(sb3UV.x))
        * smoothstep(0.42, 0.28, abs(sb3UV.y));

    // 4. Rear Rim Softbox (strong in backlit mode, crisp edge separator in hero)
    vec3 sb4Dir = normalize(vec3(-0.48, 0.52, -0.70));
    vec3 sb4Right = normalize(cross(vec3(0.0, 1.0, 0.0), sb4Dir));
    vec3 sb4Up = cross(sb4Dir, sb4Right);
    vec2 sb4UV = vec2(dot(dir, sb4Right), dot(dir, sb4Up)) / max(dot(dir, sb4Dir), 0.001);
    float sb4Mask = step(0.0, dot(dir, sb4Dir))
        * smoothstep(0.58, 0.42, abs(sb4UV.x))
        * smoothstep(0.45, 0.30, abs(sb4UV.y));

    // Light colors & intensities per uLightMode
    vec3 c1, c2, c3, c4;
    if (lightMode == 1) {
        // Pure neutral grayscale studio
        c1 = vec3(3.2);
        c2 = vec3(2.4);
        c3 = vec3(1.1);
        c4 = vec3(2.0);
    } else if (lightMode == 2) {
        // Grazing raking light
        c1 = vec3(0.85, 0.88, 0.94);
        c2 = vec3(4.6, 4.35, 4.05);
        c3 = vec3(0.55, 0.62, 0.72);
        c4 = vec3(1.4, 1.45, 1.55);
    } else if (lightMode == 3) {
        // Backlit dramatic rim
        c1 = vec3(1.1, 1.12, 1.16);
        c2 = vec3(0.85, 0.86, 0.90);
        c3 = vec3(0.75, 0.82, 0.92);
        c4 = vec3(5.8, 5.65, 5.45);
    } else {
        // Hero balanced warm-key / cool-fill studio
        c1 = vec3(3.45, 3.35, 3.20);
        c2 = vec3(2.75, 2.62, 2.45);
        c3 = vec3(1.05, 1.18, 1.38);
        c4 = vec3(2.35, 2.42, 2.55);
    }

    col += c1 * sb1Mask * max(sb1Fall, 0.4);
    col += c2 * sb2Mask;
    col += c3 * sb3Mask;
    col += c4 * sb4Mask;

    // Dark Negative-Fill Flags (essential for crisp specular contrast across glossy orange peel!)
    vec3 nf1Dir = normalize(vec3(0.55, 0.25, 0.79));
    float nf1 = smoothstep(0.86, 0.94, dot(dir, nf1Dir));
    vec3 nf2Dir = normalize(vec3(-0.32, 0.68, 0.66));
    float nf2 = smoothstep(0.88, 0.95, dot(dir, nf2Dir));
    col *= (1.0 - 0.78 * nf1) * (1.0 - 0.72 * nf2);

    // Subtle bench bounce from below horizon
    float below = smoothstep(0.04, -0.35, dir.y);
    vec3 bounceCol = (lightMode == 1) ? vec3(0.085) : vec3(0.08, 0.085, 0.095);
    col = mix(col, bounceCol, below);

    return col;
}

// -----------------------------------------------------------------------------
// PBR GGX Shading
// -----------------------------------------------------------------------------
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_Smith(float NoV, float NoL, float r) {
    float k = (r + 1.0);
    k = (k * k) * 0.125;
    float gv = NoV / (NoV * (1.0 - k) + k);
    float gl = NoL / (NoL * (1.0 - k) + k);
    return gv * gl;
}

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

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

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

    vec3 F0 = mix(vec3(0.042), albedo, metallic);
    float a = max(rough * rough, 0.004);
    float D = D_GGX(NoH, a);
    float G = G_Smith(NoV, NoL, rough);
    vec3 F = F_Schlick(VoH, F0);

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

    // Dual-lobe baked polymer resin skin (sharp glossy orange-peel highlight)
    vec3 coatSpec = vec3(0.0);
    if (clearcoat > 0.001) {
        float ac = max(coatRough * coatRough, 0.003);
        float Dc = D_GGX(NoH, ac);
        float Gc = G_Smith(NoV, NoL, coatRough);
        vec3 Fc = F_Schlick(VoH, vec3(0.045));
        coatSpec = clearcoat * (Dc * Gc * Fc) / (4.0 * NoV * NoL + 1e-5);
    }

    return (diff + spec + coatSpec) * lightCol * NoL * shadow;
}

// -----------------------------------------------------------------------------
// 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);
}

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

    float open = clamp(uOpen, 0.0, 1.0);

    // Camera setup: uCamMode 0 = hero 3/4 studio framing, 1 = ~2.5x macro on orange-peel corner & chip
    vec3 ro, ta;
    float focal;
    float sway = sin(uTime * 0.25) * 0.04;

    if (uCamMode == 1) {
        // ~2.5x Macro view focused on the convex corner bend, orange-peel softbox reflection & chipped edge
        ta = vec3(0.24, 0.87, 0.24);
        ro = vec3(1.06, 1.32, 1.12);
        ro.xz = rot2(sway * 0.45) * (ro.xz - ta.xz) + ta.xz;
        focal = 2.95;
    } else {
        // Hero 3/4 studio view showing full formed enclosure corner, base flange, yellow tab & contact shadow with clean margin
        ta = vec3(0.06, 0.44, 0.06);
        ro = vec3(2.85, 1.88, 3.05);
        ro.xz = rot2(sway) * ro.xz;
        focal = 2.45;
    }

    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(uv.x * rt + uv.y * up + focal * fw);

    // Background studio backdrop (subtle dark-neutral studio gradient)
    vec3 bgCol = sampleStudioEnv(rd, uLightMode) * 0.28 + vec3(0.055, 0.060, 0.068);
    if (uLightMode == 1) {
        bgCol = vec3(dot(bgCol, vec3(0.3333)));
    }
    vec3 col = bgCol;

    // Raymarch scene: dScene = min(dHero, p.y)
    float t = 0.0;
    float tMax = 14.0;
    bool hit = false;
    for (int i = 0; i < 128; i++) {
        vec3 p = ro + rd * t;
        float dHero = mapHero(p);
        float dScene = min(dHero, p.y);
        if (dScene < 0.0007) {
            hit = true;
            break;
        }
        t += dScene * 0.92;
        if (t > tMax) break;
    }

    if (hit) {
        vec3 p = ro + rd * t;
        int matId;
        float edgeBead;
        float dHero = mapHeroRaw(p, matId, edgeBead);

        // Zero-bug rule #2: exact bench vs hero separation
        bool isBench = (p.y <= dHero + 0.001);

        // Key / Fill / Rim light definitions per uLightMode
        vec3 L1, L2, L3;
        vec3 C1, C2, C3;
        if (uLightMode == 1) {
            // Pure neutral grayscale studio lighting
            L1 = normalize(vec3(0.55, 0.78, 0.42));
            L2 = normalize(vec3(-0.72, 0.48, 0.48));
            L3 = normalize(vec3(-0.42, 0.56, -0.70));
            C1 = vec3(2.45);
            C2 = vec3(0.82);
            C3 = vec3(1.35);
        } else if (uLightMode == 2) {
            // Grazing raking light to accentuate orange peel & edge buildup
            L1 = normalize(vec3(0.92, 0.22, 0.30));
            L2 = normalize(vec3(-0.55, 0.65, 0.52));
            L3 = normalize(vec3(-0.35, 0.30, -0.88));
            C1 = vec3(3.20, 3.02, 2.82);
            C2 = vec3(0.58, 0.64, 0.74);
            C3 = vec3(1.15, 1.20, 1.28);
        } else if (uLightMode == 3) {
            // Backlit silhouette & luminous rim
            L1 = normalize(vec3(-0.46, 0.54, -0.70));
            L2 = normalize(vec3(0.60, 0.52, 0.60));
            L3 = normalize(vec3(0.75, 0.35, -0.55));
            C1 = vec3(3.85, 3.72, 3.55);
            C2 = vec3(0.88, 0.92, 0.98);
            C3 = vec3(1.65, 1.72, 1.85);
        } else {
            // Hero studio lighting
            L1 = normalize(vec3(0.56, 0.76, 0.44));
            L2 = normalize(vec3(-0.74, 0.44, 0.50));
            L3 = normalize(vec3(-0.45, 0.58, -0.68));
            C1 = vec3(2.65, 2.55, 2.40);
            C2 = vec3(0.76, 0.86, 1.02);
            C3 = vec3(1.55, 1.62, 1.74);
        }

        vec3 V = -rd;

        if (isBench) {
            // Studio bench surface at y = 0.0
            vec3 N = vec3(0.0, 1.0, 0.0);
            float ao = calcAO(p, N);

            // Hero-only soft shadows (bias p + N*0.014)
            float sh1 = calcSoftShadow(p + N * 0.014, L1, 5.5, 9.0);
            float sh2 = calcSoftShadow(p + N * 0.014, L2, 5.5, 6.0);

            // Subtle satin dark-slate studio bench micro-grain
            float grain = vnoise(p * 65.0) * 0.018;
            float radial = smoothstep(4.2, 0.6, length(p.xz));
            vec3 benchAlbedo = (uLightMode == 1)
                ? vec3(0.11 + grain) * mix(0.65, 1.0, radial)
                : vec3(0.105, 0.112, 0.122) * mix(0.62, 1.0, radial) + grain;

            vec3 direct = evalDirectLight(N, V, L1, C1, benchAlbedo, 0.0, 0.44, 0.0, 0.3, sh1)
                        + evalDirectLight(N, V, L2, C2, benchAlbedo, 0.0, 0.44, 0.0, 0.3, sh2)
                        + evalDirectLight(N, V, L3, C3 * 0.45, benchAlbedo, 0.0, 0.44, 0.0, 0.3, 1.0);

            // Subtle glossy bench reflection of studio & dark contact grounding
            vec3 R = reflect(-V, N);
            vec3 envR = sampleStudioEnv(R, uLightMode);
            float NoV = max(dot(N, V), 0.001);
            vec3 F = F_Schlick(NoV, vec3(0.035));
            vec3 amb = benchAlbedo * (0.18 + 0.14 * radial) * ao + envR * F * 0.32 * ao;

            col = direct + amb;

            // Smooth distance fog blending into studio cyclorama
            float fog = 1.0 - exp(-0.022 * t * t);
            col = mix(col, bgCol, clamp(fog, 0.0, 0.92));
        } else {
            // Hero: Formed Sheet-Metal Industrial Enclosure Corner with Baked Powder Coat
            vec3 Ng = calcNormalHero(p);
            float ao = calcAO(p, Ng);

            // Evaluate chip layers: intact coat vs fractured primer halo vs bare cold-rolled steel
            float dChip = getChipSDF(p);
            float bareSteelMask = smoothstep(0.002, -0.014, dChip);
            float primerMask = smoothstep(0.016, 0.003, dChip) * (1.0 - bareSteelMask);
            float intactMask = 1.0 - clamp(bareSteelMask + primerMask, 0.0, 1.0);

            // Orange-peel / wrinkle micro-normal perturbation (masked out inside chip, damped via fwidth(p))
            float fp = length(fwidth(p));
            float aaWrinkle = smoothstep(0.024, 0.003, fp);
            float aaGrad = 1.0 / (1.0 + fp * 42.0);
            float eps = max(0.0022, fp * 0.45);
            float h0 = orangePeelHeight(p, open, aaWrinkle);
            vec3 gradPeel = vec3(
                orangePeelHeight(p + vec3(eps, 0.0, 0.0), open, aaWrinkle) - h0,
                orangePeelHeight(p + vec3(0.0, eps, 0.0), open, aaWrinkle) - h0,
                orangePeelHeight(p + vec3(0.0, 0.0, eps), open, aaWrinkle) - h0
            ) / eps;
            // Project gradient onto tangent plane
            gradPeel -= Ng * dot(gradPeel, Ng);

            // Coherent amplitude scaling from smooth semi-gloss (uOpen=0) to heavy baked wrinkle coat (uOpen=1)
            // Slightly smoother right on thick rounded edge-buildup beads
            float peelStrength = mix(0.0068, 0.0195, open) * aaGrad * (1.0 - edgeBead * 0.22);
            vec3 N = normalize(Ng - gradPeel * peelStrength * intactMask);

            // Crisp micro-bevel normal at the fractured paint chip lip + brushed steel grain inside chip
            if (intactMask < 0.99) {
                float ce = 0.002;
                vec3 chipGrad = vec3(
                    getChipSDF(p + vec3(ce, 0.0, 0.0)) - dChip,
                    getChipSDF(p + vec3(0.0, ce, 0.0)) - dChip,
                    getChipSDF(p + vec3(0.0, 0.0, ce)) - dChip
                ) / ce;
                chipGrad -= Ng * dot(chipGrad, Ng);
                float lipBand = exp(-pow((dChip - 0.002) * 85.0, 2.0));
                N = normalize(N - chipGrad * lipBand * 0.35);

                // Fine directional cold-rolled steel grain inside bare metal core
                float steelGrain = (vnoise(p * vec3(190.0, 40.0, 190.0)) - 0.5);
                N = normalize(N + vec3(steelGrain, 0.0, -steelGrain) * 0.08 * bareSteelMask);
            }

            // Material parameters
            // Deep baked industrial-teal polymer coat (main enclosure) & safety-yellow corner bracket clip
            vec3 tealBase = vec3(0.024, 0.235, 0.268);
            vec3 yellowBase = vec3(0.76, 0.46, 0.018);
            vec3 coatAlbedo = (matId == 1) ? yellowBase : tealBase;
            if (uLightMode == 1) {
                // Pure neutral mid-gray albedo override for grayscale evaluation mode
                coatAlbedo = (matId == 1) ? vec3(0.55) : vec3(0.45);
            }

            // Subtle pigment depth variation in orange-peel valleys & edge buildup richness
            coatAlbedo *= mix(0.92, 1.06, clamp(h0 * 0.85, 0.0, 1.0));
            coatAlbedo *= (1.0 - edgeBead * 0.06);

            // Matte zinc-phosphate primer & bare cold-rolled steel
            vec3 primerAlbedo = vec3(0.42, 0.44, 0.43);
            vec3 steelAlbedo = vec3(0.58, 0.60, 0.64);

            vec3 albedo = coatAlbedo * intactMask + primerAlbedo * primerMask + steelAlbedo * bareSteelMask;
            float metallic = bareSteelMask;

            // Roughness & dual-lobe glossy polymer resin skin
            // uOpen=0 -> glossy semi-smooth enamel; uOpen=1 -> heavier wrinkle scatter with crisp micro-glints
            float coatRough = mix(0.085, 0.22, open) + (1.0 - clamp(h0, 0.0, 1.0)) * 0.035;
            float baseRough = mix(0.24, 0.44, open);
            float rough = baseRough * intactMask + 0.74 * primerMask + 0.30 * bareSteelMask;
            float clearcoat = intactMask * mix(0.95, 0.72, open);

            // Hero-only soft shadows (bias p + Ng*0.014)
            float sh1 = calcSoftShadow(p + Ng * 0.014, L1, 4.5, 10.0);
            float sh2 = calcSoftShadow(p + Ng * 0.014, L2, 4.5, 7.0);
            float sh3 = calcSoftShadow(p + Ng * 0.014, L3, 4.5, 8.0);

            vec3 direct = evalDirectLight(N, V, L1, C1, albedo, metallic, rough, clearcoat, coatRough, sh1)
                        + evalDirectLight(N, V, L2, C2, albedo, metallic, rough, clearcoat, coatRough, sh2)
                        + evalDirectLight(N, V, L3, C3, albedo, metallic, rough, clearcoat, coatRough, sh3);

            // Studio IBL Reflections (distorted cleanly by the orange-peel normal N!)
            float NoV = max(dot(N, V), 0.001);
            vec3 R = reflect(-V, N);
            vec3 envSharp = sampleStudioEnv(R, uLightMode);
            // Softly blurred studio reflection for broader roughness lobe
            vec3 envBroad = 0.50 * sampleStudioEnv(normalize(R + N * 0.28), uLightMode)
                          + 0.25 * sampleStudioEnv(normalize(R + vec3(0.18, 0.14, -0.15)), uLightMode)
                          + 0.25 * sampleStudioEnv(normalize(R + vec3(-0.16, 0.12, 0.18)), uLightMode);

            vec3 envSpec = mix(envSharp, envBroad, smoothstep(0.06, 0.42, rough));
            vec3 envCoat = mix(envSharp, envBroad, smoothstep(0.05, 0.26, coatRough));

            vec3 F0 = mix(vec3(0.042), albedo, metallic);
            vec3 F = F_Schlick(NoV, F0);
            vec3 Fc = F_Schlick(NoV, vec3(0.048));

            // Diffuse studio hemisphere fill (keeps shadows rich & readable, zero crushed blacks)
            vec3 skyDiff = sampleStudioEnv(N, uLightMode) * 0.18 + vec3(0.11, 0.12, 0.13);
            if (uLightMode == 1) skyDiff = vec3(dot(skyDiff, vec3(0.3333)));
            vec3 diffIBL = (1.0 - metallic) * (1.0 - F) * albedo * skyDiff * ao;

            // Specular IBL: crisp studio softbox reflections rippling across the orange peel
            vec3 specIBL = envSpec * F * (1.0 - rough * 0.55) * mix(0.65, 1.0, ao);
            vec3 coatIBL = envCoat * Fc * clearcoat * (1.0 - coatRough * 0.45) * mix(0.70, 1.0, ao);

            // Subtle grazing Fresnel sheen along thick rounded powder-coat edges
            float rimFresnel = pow(1.0 - NoV, 4.0) * intactMask;
            vec3 edgeGlow = sampleStudioEnv(R, uLightMode) * rimFresnel * 0.14 * (0.6 + 0.4 * edgeBead);

            col = direct + diffIBL + specIBL + coatIBL + edgeGlow;
        }
    }

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

    // ACES Filmic Tone Mapping & sRGB Gamma Encoding (trimmed headroom so lumMax <= 245, 0% clipped)
    col = acesToneMap(col * 0.84) * 0.92;
    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
    }
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierpowder-coat-matte-metal
CategoryMetals & Armor Shaders · Industrial Metals
Upstream studymetals/lib/06_powder_coat_orange_peel_texture.glsl (commit 4778681)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size713 lines · 29.3 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); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal

All metals & armor shaders
Engine-Turned Guilloche WebGL2 procedural shaderAnimated

Overlapping circular perlage abrasive swirls catching directional metallic sheen across steel plate.

Metals & Armor