Board-Formed Concrete shader

board-formed-concrete.glsl · 814 lines · GLSL ES 3.00 · MIT

Board-Formed ConcreteStone & Masonry
Board-Formed Concrete WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 814 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

Urban Surfaces
board-formed-concrete.glsl (814 lines)
#version 300 es
// Board-Formed Concrete (board-formed-concrete.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/urban/lib/02_board_formed_architectural_concrete.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;

// -----------------------------------------------------------------------------
// Hash & Procedural Noise
// -----------------------------------------------------------------------------

float hash11(float p) {
    p = fract(p * 0.1031);
    p *= p + 33.33;
    p *= p + p;
    return fract(p);
}

float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

vec2 hash22(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.xx + p3.yz) * p3.zy);
}

float hash13(vec3 p3) {
    p3 = fract(p3 * 0.1031);
    p3 += dot(p3, p3.zyx + 31.32);
    return fract((p3.x + p3.y) * p3.z);
}

float vnoise2(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash12(i);
    float b = hash12(i + vec2(1.0, 0.0));
    float c = hash12(i + vec2(0.0, 1.0));
    float d = hash12(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 rot = mat2(0.80, 0.60, -0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        v += a * vnoise2(p);
        p = rot * p * 2.04 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

// Returns vec2(minDist, cellId)
vec2 voronoi2(vec2 x) {
    vec2 n = floor(x);
    vec2 f = fract(x);
    float md = 8.0;
    float id = 0.0;
    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 g = vec2(float(i), float(j));
            vec2 o = hash22(n + g);
            vec2 r = g + o - f;
            float d = dot(r, r);
            if (d < md) {
                md = d;
                id = hash12(n + g + vec2(13.3, 7.1));
            }
        }
    }
    return vec2(sqrt(md), id);
}

float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}

float smax(float a, float b, float k) {
    return -smin(-a, -b, k);
}

// -----------------------------------------------------------------------------
// Architectural Concrete Specimen Parameters
// -----------------------------------------------------------------------------

const vec3 SLAB_HALF = vec3(0.86, 0.72, 0.155);
const vec3 SLAB_CENTER = vec3(0.0, 0.72, 0.0);
const float PLANK_H = 0.18; // 8 planks exactly = 1.44m tall

// Horizontal plank metrics & wood-grain imprint
struct PlankInfo {
    float id;
    float localY;
    float seamDist;
    float buttDist;
    float stepOffset;
    float groutFin;
    float grain;
    float knotMask;
};

PlankInfo evalPlank(vec2 uv) {
    PlankInfo info;
    float yClamped = clamp(uv.y, 0.001, 1.439);
    float pid = floor(yClamped / PLANK_H);
    float py = uv.y - (pid + 0.5) * PLANK_H;

    // Staggered vertical timber butt-joint on alternating planks
    float buttX = (hash11(pid * 19.3 + 4.7) - 0.5) * 1.1;
    float hasButt = step(0.35, hash11(pid * 7.1 + 2.3));
    float buttDist = mix(10.0, abs(uv.x - buttX), hasButt);
    float subId = pid + 0.5 * step(buttX, uv.x) * hasButt;

    // Distance to nearest horizontal plank seam (excluding top/bottom outer edges)
    float seamD = abs(abs(py) - 0.5 * PLANK_H);
    float interiorMask = smoothstep(0.015, 0.045, uv.y) * smoothstep(1.425, 1.395, uv.y);

    // Subtle plank-to-plank depth step from timber board thickness tolerances
    float rawStep = (hash11(subId * 23.17 + 1.9) - 0.5) * 0.0068;
    // Smooth across the joint so SDF remains Lipschitz continuous
    float stepEdge = smoothstep(0.0, 0.0065, seamD) * smoothstep(0.0, 0.0065, buttDist);
    float stepOffset = rawStep * stepEdge;

    // Raised grout-fin seam where cement paste bled between timber planks
    float finBreak = smoothstep(0.22, 0.68, vnoise2(vec2(uv.x * 9.5 + pid * 3.7, pid * 11.3)));
    float horizFin = exp(-pow(seamD / 0.0038, 2.0)) * interiorMask * (0.45 + 0.55 * finBreak);
    float vertFin = exp(-pow(buttDist / 0.0035, 2.0)) * 0.55 * finBreak;
    float groutFin = max(horizFin, vertFin);

    // Authentic timber wood-grain imprint transferred into concrete
    float seed = hash11(subId * 41.7 + 9.2);
    vec2 gUV = vec2(uv.x * 1.65 + seed * 14.0, py * 19.0 + seed * 27.0);

    // Occasional timber knot
    float knotX = (hash11(subId * 53.1 + 3.1) - 0.5) * 1.35;
    float knotY = (hash11(subId * 17.9 + 6.4) - 0.5) * 0.11;
    vec2 kp = vec2((uv.x - knotX) * 1.0, (py - knotY) * 2.6);
    float kDist = length(kp);
    float knotEnable = step(0.42, hash11(subId * 29.7 + 0.8));
    float knotWarp = knotEnable * exp(-kDist * 14.0) * 1.9;
    float knotRing = knotEnable * smoothstep(0.08, 0.015, kDist);

    // Cathedral arch + straight Douglas fir / cedar grain rings
    float warp = fbm2(vec2(gUV.x * 0.9, gUV.y * 0.16)) * 4.2 + knotWarp;
    float ringPhase = gUV.y * 1.45 + warp + sin(gUV.x * 1.4 + seed * 6.28) * 1.6;
    float rings = sin(ringPhase);
    // Asymmetric earlywood/latewood profile
    float latewood = smoothstep(-0.2, 0.85, rings) - 0.45;
    float fineFiber = vnoise2(vec2(uv.x * 14.0 + seed * 10.0, uv.y * 165.0)) - 0.5;
    float microSplinter = vnoise2(vec2(uv.x * 36.0 + seed * 5.0, uv.y * 280.0)) - 0.5;

    float grain = latewood * 0.62 + fineFiber * 0.30 + microSplinter * 0.14;
    // Slight attenuation right on top of the grout fin
    grain *= (1.0 - 0.65 * groutFin);

    info.id = subId;
    info.localY = py;
    info.seamDist = seamD;
    info.buttDist = buttDist;
    info.stepOffset = stepOffset;
    info.groutFin = groutFin;
    info.grain = grain;
    info.knotMask = knotRing;
    return info;
}

// Entrapped air-void bugholes (sparse concave pinholes)
float evalBugholes(vec2 uv, float seamDist) {
    // Slightly denser just below horizontal plank seams & lower lift
    float seamBoost = exp(-seamDist * 45.0) * 0.12 + smoothstep(0.45, 0.05, uv.y) * 0.08;

    // Medium bugholes (3-6mm)
    vec2 v1 = voronoi2(uv * 28.0 + vec2(3.7, 9.1));
    float active1 = step(0.84 - seamBoost, v1.y);
    float hole1 = smoothstep(0.24, 0.05, v1.x) * active1;

    // Fine pinholes (1.5-3mm)
    vec2 v2 = voronoi2(uv * 62.0 + vec2(19.2, 5.4));
    float active2 = step(0.87 - seamBoost * 0.6, v2.y);
    float hole2 = smoothstep(0.22, 0.04, v2.x) * active2 * 0.65;

    return max(hole1, hole2);
}

// Lower-lift aggregate honeycombing controlled by uOpen (0..1)
// Returns vec2(cavityDepth, pebbleMask)
vec2 evalHoneycomb(vec2 uv, float openVal) {
    if (openVal <= 0.001) return vec2(0.0);

    // Concentrated along lower lift line (y ~ 0.18) and base (y < 0.26)
    float liftBand = exp(-pow((uv.y - 0.18) / 0.075, 2.0));
    float baseBand = smoothstep(0.26, 0.02, uv.y) * 0.85;
    float edgeBias = smoothstep(0.25, 0.82, abs(uv.x)) * 0.45 + 0.55;

    float patchNoise = fbm2(vec2(uv.x * 4.2 + 2.3, uv.y * 9.5 + 1.1));
    float region = max(liftBand, baseBand) * edgeBias;
    float mask = smoothstep(0.44 - openVal * 0.24, 0.72 - openVal * 0.18, region * (0.55 + 0.75 * patchNoise));
    mask *= smoothstep(0.0, 0.25, openVal);

    if (mask <= 0.001) return vec2(0.0);

    // Exposed gravel/pebble aggregate inside pitted mortar voids
    vec2 ag1 = voronoi2(uv * 42.0 + vec2(11.3, 4.7));
    vec2 ag2 = voronoi2(uv * 78.0 + vec2(27.1, 16.9));

    // Rounded pebbles protrude slightly inside recessed dark voids
    float voidPit = smoothstep(0.18, 0.58, ag1.x) * 0.75 + smoothstep(0.15, 0.52, ag2.x) * 0.35;
    float pebble = smoothstep(0.42, 0.16, ag1.x) * mask;

    float depth = mask * (0.45 + 0.55 * voidPit) * openVal;
    return vec2(depth, pebble);
}

// Tie-rod P-cone coordinates (2 rows x 3 columns, aligned with planks 2 and 5)
vec2 nearestCone(vec2 uv) {
    float colX = clamp(round(uv.x / 0.54), -1.0, 1.0) * 0.54;
    float rowY = (uv.y < 0.72) ? 0.45 : 0.99;
    return vec2(colX, rowY);
}

// -----------------------------------------------------------------------------
// Signed Distance Field
// -----------------------------------------------------------------------------

// Returns SDF to the hero architectural concrete wall specimen
float sdHero(vec3 p) {
    vec3 q = p - SLAB_CENTER;

    // Base slab with crisp 45-degree formwork chamfers on vertical and top edges
    float microR = 0.0025;
    vec3 b = SLAB_HALF - vec3(microR);
    vec3 a = abs(q) - b;
    float dBox = length(max(a, 0.0)) + min(max(a.x, max(a.y, a.z)), 0.0) - microR;

    // 45-degree triangular chamfer strips (vertical corners + top edge; bottom sits flush)
    float ch = 0.017;
    float chXZ = (a.x + a.z + ch) * 0.70710678 - microR;
    float topMask = step(0.0, q.y);
    float chXY = mix(-10.0, (a.x + q.y - b.y + ch) * 0.70710678 - microR, topMask);
    float chYZ = mix(-10.0, (a.z + q.y - b.y + ch) * 0.70710678 - microR, topMask);

    float d = max(dBox, max(chXZ, max(chXY, chYZ)));

    // Fast bounding exit for far rays
    if (d > 0.08) return d;

    // Evaluate surface relief only near the wall slab
    // Front/back face mask (strongest on Z faces, subtle wrap on X sides)
    float faceZ = smoothstep(0.06, 0.135, abs(q.z));
    float sideX = smoothstep(0.76, 0.84, abs(q.x)) * (1.0 - faceZ);
    float notTopBot = smoothstep(0.008, 0.025, p.y) * smoothstep(1.432, 1.415, p.y);
    float notChamferX = smoothstep(0.855, 0.828, abs(q.x));

    // UV for formwork boards (use X on front/back, Z on sides)
    vec2 formUV = (faceZ > 0.5) ? vec2(q.x * sign(q.z + 1e-4), p.y) : vec2(q.z + 2.5, p.y);
    PlankInfo pl = evalPlank(formUV);

    // Displacement (positive = outward proud, negative = inward recessed)
    float disp = 0.0;
    float faceWeight = faceZ * notTopBot * notChamferX;
    float sideWeight = sideX * notTopBot;

    // Plank depth steps + raised grout-fin seams + wood-grain relief
    disp += (pl.stepOffset + pl.groutFin * 0.0046 + pl.grain * 0.0024) * (faceWeight + sideWeight * 0.55);

    // Subtle entrapped air-void bugholes
    float bughole = evalBugholes(formUV, pl.seamDist);
    disp -= bughole * 0.0045 * (faceWeight + sideWeight * 0.5);

    // Lower-lift aggregate honeycombing when uOpen > 0
    vec2 hc = evalHoneycomb(formUV, clamp(uOpen, 0.0, 1.0));
    disp -= hc.x * 0.014 * (faceWeight + sideWeight * 0.65);

    // Apply bounded displacement
    d -= disp * 0.75;

    // Recessed circular tapered tie-rod P-cone holes on front & back faces
    if (faceZ > 0.2) {
        vec2 cPos = nearestCone(vec2(q.x, p.y));
        float r = length(vec2(q.x, p.y) - cPos);
        float zFromFace = SLAB_HALF.z - abs(q.z);

        // Tapered conical recess: R=0.044 at surface -> R=0.021 at depth 0.036
        float coneDepth = 0.036;
        float tDepth = clamp(zFromFace / coneDepth, 0.0, 1.0);
        float coneRad = mix(0.044, 0.021, tDepth);

        // Cone cavity SDF (negative inside cavity)
        float dConeSide = (r - coneRad) * 0.85;
        float dConeBack = zFromFace - coneDepth;
        float dCavity = max(dConeSide, dConeBack);

        // Subtract cavity with crisp 2.5mm rim bevel
        d = smax(d, -dCavity, 0.0028);

        // Recessed circular mortar/steel tie-rod plug at the back of the cone
        float plugZ = SLAB_HALF.z - 0.0315;
        float dPlug = max(r - 0.0205, abs(abs(q.z) - plugZ) - 0.0045);
        // Subtle central dimple on plug
        float dimple = smoothstep(0.009, 0.002, r) * 0.0025;
        dPlug += dimple;
        d = min(d, dPlug);
    }

    return d;
}

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

// Zero-bug Rule 1: Unit-weight tetrahedral normals
vec3 calcNormal(vec3 p) {
    const float e = 0.0011;
    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)
    );
}

// Zero-bug Rule 3: Clean hero-only soft shadow (no ph, bias p+N*0.014, tMin=0.025, step max(h,0.015))
float softShadowHero(vec3 ro, vec3 rd, float tMin, float tMax, float k) {
    float res = 1.0;
    float t = tMin;
    for (int i = 0; i < 48; i++) {
        float h = sdHero(ro + rd * t);
        res = min(res, k * h / t);
        if (res < 0.002 || t > tMax) break;
        t += max(h, 0.015);
    }
    return clamp(res, 0.0, 1.0);
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 5; i++) {
        float h = 0.018 * float(i) * float(i) * 0.45 + 0.012;
        float d = mapScene(p + n * h);
        occ += (h - d) * sca;
        sca *= 0.72;
    }
    return clamp(1.0 - 1.95 * occ, 0.18, 1.0);
}

// -----------------------------------------------------------------------------
// Rainwater Drip Staining (uOpen 0..1)
// -----------------------------------------------------------------------------

// Returns vec2(darkRunoff, efflorescenceHalo)
vec2 evalRainStains(vec2 uv, float openVal) {
    if (openVal <= 0.001) return vec2(0.0);

    // 1) Top parapet runoff curtains dripping down from y = 1.44
    float topDist = max(0.0, 1.44 - uv.y);
    float colNoise1 = fbm2(vec2(uv.x * 7.5 + 1.7, 2.1));
    float colNoise2 = vnoise2(vec2(uv.x * 26.0 + 4.3, uv.y * 1.4));
    float streakLen = 0.18 + 0.58 * pow(colNoise1, 1.35) * openVal;
    float topMask = smoothstep(streakLen + 0.08, 0.0, topDist) * smoothstep(0.32, 0.78, colNoise1 * 0.65 + colNoise2 * 0.45);
    // Subtle wavy drip paths
    float dripPath = smoothstep(0.38, 0.75, vnoise2(vec2(uv.x * 32.0 + sin(uv.y * 8.0) * 0.35, uv.y * 2.2)));
    float topRunoff = topMask * (0.45 + 0.55 * dripPath);

    // 2) Iconic rainwater runoff ribbons directly beneath the 6 tie-rod P-cones
    vec2 cPos = nearestCone(uv);
    float dx = uv.x - cPos.x;
    float dy = cPos.y - uv.y; // positive below cone
    float coneStream = 0.0;
    if (dy > 0.015) {
        float coneLen = (0.22 + 0.24 * hash12(cPos * 11.7)) * openVal;
        float spread = 0.022 + dy * 0.065;
        float wobble = (vnoise2(vec2(uv.y * 14.0, cPos.x * 9.0)) - 0.5) * 0.014;
        float xProf = exp(-pow((dx + wobble) / spread, 2.0));
        float yProf = smoothstep(coneLen + 0.06, 0.02, dy) * smoothstep(0.015, 0.055, dy);
        coneStream = xProf * yProf * 0.95;
    }

    // 3) Subtle damp darkening right at the bottom capillary line
    float capillary = smoothstep(0.14 * openVal + 0.01, 0.0, uv.y) * 0.38 * openVal;

    float darkRunoff = clamp((topRunoff * 0.78 + coneStream * 0.90 + capillary) * openVal, 0.0, 1.0);

    // Pale whitish calcium-carbonate efflorescence along edges of stains & joints
    float effEdge = smoothstep(0.12, 0.38, darkRunoff) * smoothstep(0.75, 0.32, darkRunoff);
    float effTex = smoothstep(0.45, 0.82, fbm2(uv * vec2(18.0, 10.0) + 7.1));
    float eff = effEdge * effTex * openVal * 0.65;

    return vec2(darkRunoff, eff);
}

// -----------------------------------------------------------------------------
// Studio Environment (Softboxes + Negative-Fill Flags)
// -----------------------------------------------------------------------------

vec3 sampleStudioEnv(vec3 rd, int lightMode) {
    float y = clamp(rd.y, -1.0, 1.0);

    // Base architectural cyclorama gradient (soft mid-light gallery gray)
    vec3 skyBot = vec3(0.14, 0.145, 0.155);
    vec3 skyMid = vec3(0.24, 0.25, 0.265);
    vec3 skyTop = vec3(0.32, 0.335, 0.355);

    if (lightMode == 1) {
        // Pure neutral grayscale studio
        skyBot = vec3(0.15);
        skyMid = vec3(0.25);
        skyTop = vec3(0.34);
    } else if (lightMode == 2) {
        // Grazing mode: slightly moodier ambient so raking light sculpts relief
        skyBot = vec3(0.09, 0.095, 0.105);
        skyMid = vec3(0.15, 0.155, 0.168);
        skyTop = vec3(0.20, 0.21, 0.225);
    } else if (lightMode == 3) {
        // Backlit mode: luminous cyclorama backdrop behind specimen
        skyBot = vec3(0.12, 0.125, 0.135);
        skyMid = vec3(0.28, 0.295, 0.315);
        skyTop = vec3(0.38, 0.40, 0.43);
    }

    vec3 col = mix(skyBot, skyMid, smoothstep(-0.25, 0.18, y));
    col = mix(col, skyTop, smoothstep(0.18, 0.85, y));

    // Overhead softbox
    vec3 topDir = normalize(vec3(-0.15, 0.94, 0.30));
    float topBox = smoothstep(0.76, 0.93, dot(rd, topDir));

    // Key softbox
    vec3 keyBoxDir = normalize(vec3(-0.62, 0.56, 0.55));
    if (lightMode == 2) keyBoxDir = normalize(vec3(-0.94, 0.24, 0.24));
    if (lightMode == 3) keyBoxDir = normalize(vec3(-0.15, 0.42, -0.89));
    float keyBox = smoothstep(0.78, 0.94, dot(rd, keyBoxDir));

    // Rim strip light
    vec3 rimDir = normalize(vec3(0.72, 0.48, -0.50));
    float rimStrip = smoothstep(0.84, 0.96, dot(rd, rimDir));

    // Negative-fill flags (dark studio absorption panels to carve architectural contrast)
    vec3 negFlag1 = normalize(vec3(0.78, 0.12, 0.61));
    float flag1 = smoothstep(0.74, 0.91, dot(rd, negFlag1));
    vec3 negFlag2 = normalize(vec3(-0.82, 0.05, -0.57));
    float flag2 = smoothstep(0.78, 0.93, dot(rd, negFlag2));

    vec3 keyTint = (lightMode == 1) ? vec3(1.0) : vec3(1.03, 1.01, 0.97);
    vec3 rimTint = (lightMode == 1) ? vec3(1.0) : vec3(0.94, 0.97, 1.04);

    col += topBox * 0.55 * vec3(1.0);
    col += keyBox * (lightMode == 3 ? 1.35 : 0.95) * keyTint;
    col += rimStrip * 0.52 * rimTint;

    // Apply negative fill flags
    col *= (1.0 - 0.52 * flag1);
    col *= (1.0 - 0.38 * flag2);

    return col;
}

// -----------------------------------------------------------------------------
// PBR Shading Helpers (GGX + ACES)
// -----------------------------------------------------------------------------

float D_GGX(float NoH, float r) {
    float a = r * r;
    float a2 = a * a;
    float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
    return a2 / (3.14159265 * d * d + 1e-6);
}

float V_SmithGGX(float NoV, float NoL, float r) {
    float a = r * r;
    float a2 = a * a;
    float gv = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2);
    float gl = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2);
    return 0.5 / (gv + gl + 1e-5);
}

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 roughness, float metallic) {
    float NoL = max(dot(N, L), 0.0);
    if (NoL <= 0.0) return vec3(0.0);
    float NoV = max(dot(N, V), 0.001);
    vec3 H = normalize(V + L);
    float NoH = max(dot(N, H), 0.0);
    float VoH = max(dot(V, H), 0.0);

    vec3 F0 = mix(vec3(0.04), albedo, metallic);
    float D = D_GGX(NoH, roughness);
    float Vis = V_SmithGGX(NoV, NoL, roughness);
    vec3 F = F_Schlick(VoH, F0);

    vec3 kD = (1.0 - F) * (1.0 - metallic);
    vec3 diff = kD * albedo / 3.14159265;
    vec3 spec = D * Vis * F;

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

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

// -----------------------------------------------------------------------------
// Micro-Normal Perturbation for Concrete Grain & Cement Texture
// -----------------------------------------------------------------------------

vec3 perturbConcreteNormal(vec3 p, vec3 N) {
    vec3 q = p - SLAB_CENTER;
    float faceZ = smoothstep(0.05, 0.12, abs(q.z));
    vec2 uv = (faceZ > 0.5) ? vec2(q.x * sign(q.z + 1e-4), p.y) : vec2(q.z + 2.5, p.y);

    float eps = 0.0018;
    // Fine cement sand + crisp timber fiber ridges
    float h0 = vnoise2(uv * vec2(18.0, 190.0)) * 0.55 + vnoise2(uv * 135.0) * 0.45;
    float hx = vnoise2((uv + vec2(eps, 0.0)) * vec2(18.0, 190.0)) * 0.55 + vnoise2((uv + vec2(eps, 0.0)) * 135.0) * 0.45;
    float hy = vnoise2((uv + vec2(0.0, eps)) * vec2(18.0, 190.0)) * 0.55 + vnoise2((uv + vec2(0.0, eps)) * 135.0) * 0.45;

    vec2 grad = vec2(hx - h0, hy - h0) / eps;
    float strength = 0.00055;

    // Tangent frame on wall face or side
    vec3 up = abs(N.y) < 0.95 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
    vec3 T = normalize(cross(up, N));
    vec3 B = cross(N, T);

    return normalize(N - (T * grad.x + B * grad.y) * strength);
}

// -----------------------------------------------------------------------------
// Main Render
// -----------------------------------------------------------------------------

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

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

    // Camera setup: 0 = Hero 3/4 architectural view, 1 = ~2.5x macro detail view
    vec3 ro, ta;
    float fov;
    if (uCamMode == 1) {
        // Macro ~2.5x framing a tie-rod P-cone (0.0, 0.45), horizontal grout-fin seam (0.36),
        // timber grain imprint, bugholes, and lower-lift honeycombing/rainwater drip
        ro = vec3(0.24, 0.46, 0.92);
        ta = vec3(-0.04, 0.38, 0.14);
        fov = 1.65;
    } else {
        // Hero view: grounded full architectural specimen on studio bench
        float subtleOrbit = sin(uTime * 0.18) * 0.04;
        ro = vec3(0.82 + subtleOrbit, 0.94, 2.38);
        ta = vec3(0.02, 0.68, 0.0);
        fov = 1.62;
    }

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

    // Raymarching
    float t = 0.0;
    float tMax = 18.0;
    bool hit = false;
    for (int i = 0; i < 115; i++) {
        vec3 p = ro + rd * t;
        float h = mapScene(p);
        if (h < 0.0007 * (1.0 + t * 0.35)) {
            hit = true;
            break;
        }
        // Conservative step near the detailed specimen surface to prevent overshoot
        float stepScale = (t < 4.0 && h < 0.05) ? 0.72 : 0.95;
        t += h * stepScale;
        if (t > tMax) break;
    }

    vec3 col = sampleStudioEnv(rd, uLightMode);

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

        vec3 N = calcNormal(p);
        vec3 V = -rd;

        // Material properties
        vec3 albedo;
        float roughness;
        float metallic = 0.0;

        if (isBench) {
            // Matte architectural plaster/slate studio bench
            float benchMacro = fbm2(p.xz * 1.15 + 3.2);
            float benchFine = vnoise2(p.xz * 28.0);
            vec3 benchCol = mix(vec3(0.145, 0.150, 0.158), vec3(0.185, 0.190, 0.198), benchMacro * 0.75 + benchFine * 0.25);
            // Soft radial vignette on bench toward horizon
            float distFade = smoothstep(1.2, 7.5, length(p.xz));
            benchCol = mix(benchCol, vec3(0.13, 0.135, 0.142), distFade);
            albedo = benchCol;
            roughness = mix(0.76, 0.88, benchFine);
        } else {
            // Board-formed architectural Portland cement concrete
            N = perturbConcreteNormal(p, N);

            vec3 q = p - SLAB_CENTER;
            float faceZ = smoothstep(0.05, 0.12, abs(q.z));
            vec2 formUV = (faceZ > 0.5) ? vec2(q.x * sign(q.z + 1e-4), p.y) : vec2(q.z + 2.5, p.y);
            PlankInfo pl = evalPlank(formUV);

            // Authentic cool-gray Portland cement palette with subtle pour-batch mottling
            float mottle = fbm2(p.xy * 1.85 + vec2(p.z * 3.1, 4.2));
            float fineMottle = fbm2(p.xy * 9.5 + vec2(11.3, 2.7));
            float plankTint = (hash11(pl.id * 31.3 + 5.1) - 0.5) * 0.065;

            vec3 cementLight = vec3(0.56, 0.57, 0.565);
            vec3 cementMid   = vec3(0.45, 0.46, 0.458);
            vec3 cementDark  = vec3(0.34, 0.35, 0.355);

            albedo = mix(cementDark, cementLight, clamp(0.25 + mottle * 0.65 + plankTint, 0.0, 1.0));
            albedo = mix(albedo, cementMid, fineMottle * 0.35);

            // Wood-grain imprint subtle tonal transfer (slightly darker in recessed earlywood/knots)
            albedo *= (1.0 + pl.grain * 0.085 - pl.knotMask * 0.11);

            // Raised grout-fin seams: slightly lighter cement-rich laitance paste
            albedo = mix(albedo, vec3(0.60, 0.605, 0.595), pl.groutFin * 0.42);

            // Base matte concrete roughness (0.78 - 0.93)
            roughness = clamp(0.84 + fineMottle * 0.10 - pl.groutFin * 0.08, 0.72, 0.95);

            // Entrapped air-void bugholes: darker interior shadowing
            float bughole = evalBugholes(formUV, pl.seamDist);
            albedo *= (1.0 - bughole * 0.36);
            roughness = mix(roughness, 0.95, bughole);

            // Tie-rod P-cone interior & recessed mortar/steel plug
            if (faceZ > 0.25) {
                vec2 cPos = nearestCone(vec2(q.x, p.y));
                float r = length(vec2(q.x, p.y) - cPos);
                float zFromFace = SLAB_HALF.z - abs(q.z);

                // Smooth chamfered cone wall (slightly denser, smoother steel-formed surface)
                float inCone = smoothstep(0.048, 0.040, r) * smoothstep(0.002, 0.010, zFromFace);
                albedo = mix(albedo, vec3(0.36, 0.37, 0.375), inCone * 0.45);
                roughness = mix(roughness, 0.68, inCone * 0.5);

                // Recessed mortar/oxidized steel plug at the base of the cone
                float onPlug = smoothstep(0.022, 0.018, r) * smoothstep(0.024, 0.030, zFromFace);
                if (onPlug > 0.01) {
                    float plugTex = vnoise2(vec2(q.x, p.y) * 140.0);
                    vec3 plugCol = mix(vec3(0.24, 0.25, 0.26), vec3(0.31, 0.32, 0.33), plugTex);
                    albedo = mix(albedo, plugCol, onPlug);
                    roughness = mix(roughness, 0.52, onPlug);
                    metallic = mix(metallic, 0.35, onPlug);
                }
            }

            // uOpen: Rainwater drip staining & efflorescence
            if (openVal > 0.001) {
                vec2 rain = evalRainStains(formUV, openVal);
                // Dark mineral runoff tint (charcoal-olive weathered concrete)
                vec3 stainCol = vec3(0.16, 0.165, 0.160);
                albedo = mix(albedo, stainCol, rain.x * 0.68);
                // Whitish mineral efflorescence rim
                albedo = mix(albedo, vec3(0.68, 0.685, 0.675), rain.y * 0.55);
                roughness = mix(roughness, 0.70, rain.x * 0.35);

                // Lower-lift aggregate honeycombing: dark mortar voids + exposed gravel pebbles
                vec2 hc = evalHoneycomb(formUV, openVal);
                if (hc.x > 0.001) {
                    vec2 ag = voronoi2(formUV * 42.0 + vec2(11.3, 4.7));
                    float pebbleHue = ag.y;
                    vec3 pebA = vec3(0.38, 0.35, 0.31); // warm river gravel
                    vec3 pebB = vec3(0.24, 0.25, 0.27); // dark basalt aggregate
                    vec3 pebC = vec3(0.50, 0.47, 0.42); // pale quartz/limestone
                    vec3 pebbleCol = mix(mix(pebA, pebB, smoothstep(0.2, 0.6, pebbleHue)), pebC, smoothstep(0.65, 0.95, pebbleHue));

                    vec3 voidMortar = vec3(0.19, 0.195, 0.19);
                    vec3 hcCol = mix(voidMortar, pebbleCol, hc.y);
                    albedo = mix(albedo, hcCol, clamp(hc.x * 1.15, 0.0, 1.0));
                    roughness = mix(roughness, mix(0.94, 0.58, hc.y), clamp(hc.x, 0.0, 1.0));
                }
            }

            // Subtle chamfer highlight brightening (slight cement paste density along beveled edges)
            float edgeChamf = smoothstep(0.81, 0.855, abs(q.x)) + smoothstep(1.39, 1.435, p.y);
            albedo = mix(albedo, albedo * 1.06, clamp(edgeChamf, 0.0, 1.0) * 0.45);
        }

        // Pure neutral grayscale enforcement for uLightMode == 1
        if (uLightMode == 1) {
            float luma = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
            albedo = vec3(luma);
        }

        // Studio Lighting Rig
        vec3 keyDir, fillDir, rimDir;
        vec3 keyCol, fillCol, rimCol;
        float shadowK = 14.0;

        if (uLightMode == 1) {
            // Mode 1: Pure neutral grayscale
            keyDir  = normalize(vec3(-0.60, 0.62, 0.50));
            fillDir = normalize(vec3(0.65, 0.35, 0.67));
            rimDir  = normalize(vec3(0.45, 0.55, -0.70));
            keyCol  = vec3(2.45);
            fillCol = vec3(0.55);
            rimCol  = vec3(0.75);
            shadowK = 14.0;
        } else if (uLightMode == 2) {
            // Mode 2: Dramatic grazing light raking sharply across timber grain & grout fins
            keyDir  = normalize(vec3(-0.94, 0.25, 0.22));
            fillDir = normalize(vec3(0.55, 0.42, 0.72));
            rimDir  = normalize(vec3(0.80, 0.35, -0.48));
            keyCol  = vec3(3.10, 2.98, 2.82);
            fillCol = vec3(0.38, 0.41, 0.46);
            rimCol  = vec3(0.62, 0.66, 0.72);
            shadowK = 20.0;
        } else if (uLightMode == 3) {
            // Mode 3: Backlit studio cyclorama with soft front bounce fill
            keyDir  = normalize(vec3(-0.22, 0.52, -0.82));
            fillDir = normalize(vec3(-0.45, 0.40, 0.80));
            rimDir  = normalize(vec3(0.68, 0.45, -0.58));
            keyCol  = vec3(2.85, 2.90, 3.00);
            fillCol = vec3(0.88, 0.89, 0.91);
            rimCol  = vec3(1.45, 1.48, 1.55);
            shadowK = 12.0;
        } else {
            // Mode 0: Hero museum gallery lighting
            keyDir  = normalize(vec3(-0.62, 0.58, 0.53));
            fillDir = normalize(vec3(0.68, 0.38, 0.62));
            rimDir  = normalize(vec3(0.52, 0.54, -0.66));
            keyCol  = vec3(2.55, 2.48, 2.38);
            fillCol = vec3(0.52, 0.56, 0.63);
            rimCol  = vec3(0.85, 0.89, 0.96);
            shadowK = 14.0;
        }

        // Zero-bug Rule 3: Hero-only soft shadow with exact bias & tMin
        float shKey = softShadowHero(p + N * 0.014, keyDir, 0.025, 6.5, shadowK);
        float ao = calcAO(p, N);

        vec3 Lo = vec3(0.0);
        Lo += evalDirectLight(N, V, keyDir, keyCol, albedo, roughness, metallic) * shKey;
        Lo += evalDirectLight(N, V, fillDir, fillCol, albedo, roughness, metallic) * (0.45 + 0.55 * ao);
        Lo += evalDirectLight(N, V, rimDir, rimCol, albedo, roughness, metallic) * (0.35 + 0.65 * ao);

        // Indirect studio IBL (diffuse irradiance + rough specular reflection)
        vec3 envDiff = sampleStudioEnv(N, uLightMode);
        vec3 R = reflect(-V, N);
        vec3 envSpec = sampleStudioEnv(R, uLightMode);

        float NoV = max(dot(N, V), 0.001);
        vec3 F0 = mix(vec3(0.04), albedo, metallic);
        vec3 F = F_Schlick(NoV, F0);
        vec3 kD = (1.0 - F) * (1.0 - metallic);

        vec3 ambient = kD * albedo * envDiff * 0.95;
        ambient += F * envSpec * (1.0 - roughness * 0.72) * 0.55;
        ambient *= ao;

        // Subtle warm bounce from the studio bench onto the lower concrete faces
        if (!isBench) {
            float benchBounce = smoothstep(0.65, 0.0, p.y) * max(0.0, -N.y * 0.5 + 0.5);
            vec3 bounceCol = (uLightMode == 1) ? vec3(0.07) : vec3(0.075, 0.077, 0.080);
            ambient += albedo * bounceCol * benchBounce * ao;
        }

        col = Lo + ambient;

        // Gentle studio atmospheric integration toward distant bench horizon
        if (isBench) {
            float fog = 1.0 - exp(-0.0045 * t * t);
            col = mix(col, sampleStudioEnv(rd, uLightMode), clamp(fog, 0.0, 0.92));
        }
    }

    // Subtle studio lens vignette (keeps corners soft without crushing blacks)
    vec2 qv = fragCoord / uResolution;
    float vig = 0.86 + 0.14 * pow(16.0 * qv.x * qv.y * (1.0 - qv.x) * (1.0 - qv.y), 0.22);
    col *= vig;

    // ACES Filmic Tone Mapping & sRGB Gamma
    col = acesToneMap(col * 0.96);
    col = pow(max(col, vec3(0.0)), vec3(1.0 / 2.2));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierboard-formed-concrete
CategoryStone & Masonry Shaders · Urban Surfaces
Upstream studyurban/lib/02_board_formed_architectural_concrete.glsl (commit bb42233)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size814 lines · 31.3 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT impacts sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Voronoi/Worley cellular fields (Voronoi 1908; Worley 1996); GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

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

All stone & masonry shaders