Weathered Terracotta Stucco shader

weathered-terracotta-stucco.glsl · 828 lines · GLSL ES 3.00 · MIT

Weathered Terracotta StuccoStone & Masonry
Weathered Terracotta Stucco WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 828 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
weathered-terracotta-stucco.glsl (828 lines)
#version 300 es
// Weathered Terracotta Stucco (weathered-terracotta-stucco.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/urban/lib/09_weathered_terracotta_stucco_wash.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;

// ============================================================================
// 09_weathered_terracotta_stucco_wash.glsl
// Mediterranean architectural wall block in hand-troweled terracotta-ochre
// mineral lime stucco with sweeping semicircular steel-trowel ridges, sandy
// lime-plaster micro-grit, sun-bleached chalky efflorescence, and vertical
// rainwater drip streaks descending from a weathered travertine sill coping.
// ============================================================================

const float PI = 3.14159265359;

// --- Deterministic Hashes & 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 vnoise3(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    vec3 u = f * f * (3.0 - 2.0 * f);
    float n000 = hash13(i + vec3(0.0, 0.0, 0.0));
    float n100 = hash13(i + vec3(1.0, 0.0, 0.0));
    float n010 = hash13(i + vec3(0.0, 1.0, 0.0));
    float n110 = hash13(i + vec3(1.0, 1.0, 0.0));
    float n001 = hash13(i + vec3(0.0, 0.0, 1.0));
    float n101 = hash13(i + vec3(1.0, 0.0, 1.0));
    float n011 = hash13(i + vec3(0.0, 1.0, 1.0));
    float n111 = hash13(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);
}

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

float fbm3(vec3 p) {
    float s = 0.0;
    float a = 0.5;
    for (int i = 0; i < 4; i++) {
        s += a * vnoise3(p);
        p = p * 2.03 + vec3(13.7, 29.3, 41.1);
        a *= 0.5;
    }
    return s;
}

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

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

// --- Sweeping Semicircular Steel-Trowel Arc Pattern ---
// Returns vec4(height, ridgeMask, burnishMask, passTint)
vec4 trowelPattern(vec2 uv) {
    vec2 g = uv * 2.75;
    vec2 id = floor(g);
    vec2 f = fract(g);

    float totalH = 0.0;
    float maxRidge = 0.0;
    float burnish = 0.0;
    float passTint = 0.5;
    float bestOrder = -1.0;

    for (int j = -1; j <= 1; j++) {
        for (int i = -1; i <= 1; i++) {
            vec2 cell = id + vec2(float(i), float(j));
            vec2 rnd = hash22(cell);
            float rnd3 = hash12(cell + 19.37);
            float rnd4 = hash12(cell + 53.11);

            // Trowel pivot & sweep radius
            vec2 center = vec2(float(i), float(j)) + 0.18 + 0.64 * rnd;
            vec2 d = f - center;
            float dist = length(d);

            // Sweep orientation: mostly sweeping upward-diagonal or horizontal arcs
            float baseAng = (rnd3 - 0.5) * 2.4 + 0.35;
            float ang = atan(d.y, d.x);
            float da = atan(sin(ang - baseAng), cos(ang - baseAng));

            // Semicircular arc span (~140 degrees)
            float arcSpan = 1.05 + 0.45 * rnd4;
            float angMask = smoothstep(arcSpan, arcSpan - 0.42, abs(da));

            float R = 0.52 + 0.38 * rnd4;
            // Inner concave fan scoop + sharp raised ridge at outer blade edge
            float inside = smoothstep(R + 0.04, R - 0.08, dist);
            float rimDelta = dist - R;
            // Asymmetric steel-blade ridge: sharp outer lip, soft inner taper
            float ridge = exp(-pow((rimDelta + 0.015) * 16.0, 2.0)) * angMask;
            float lipStep = smoothstep(0.055, -0.025, rimDelta) * smoothstep(-0.22, -0.01, rimDelta) * angMask;

            // Fine concentric sand-drag striations along the sweep arc
            float drag = sin(dist * 72.0 + rnd3 * 12.0 + vnoise2(uv * 18.0) * 1.4) * 0.5 + 0.5;
            float striation = drag * inside * angMask * smoothstep(0.15, R, dist);

            // Layering order (later trowel pass overlaps earlier one)
            float order = rnd3 + 0.15 * float(j);
            float coverage = inside * angMask;

            if (coverage > 0.05 && order > bestOrder) {
                bestOrder = order;
                passTint = rnd4;
            }

            // Accumulate sculptural height
            float hContrib = (lipStep * 0.45 + ridge * 0.85 - inside * angMask * 0.18 + striation * 0.14);
            totalH = mix(totalH, hContrib, clamp(coverage * 0.72 + ridge * 0.55, 0.0, 1.0));
            maxRidge = max(maxRidge, ridge);
            burnish = max(burnish, (inside * angMask * 0.65 + ridge * 0.5) * (0.6 + 0.4 * drag));
        }
    }

    return vec4(totalH, clamp(maxRidge, 0.0, 1.0), clamp(burnish, 0.0, 1.0), passTint);
}

// Triplanar projection helper for wall faces
vec2 wallFaceUV(vec3 p) {
    vec3 ap = abs(p);
    // Front/back faces dominate when z near wall surface, sides use z-y
    if (ap.x > 0.70 && ap.z < 0.25) {
        return vec2(p.z * sign(p.x) + 1.35, p.y);
    }
    return vec2(p.x * (p.z >= 0.0 ? 1.0 : -1.0), p.y);
}

// --- Rainwater Drip Streak Field ---
// Descends from the stone sill overhang at y = 1.53
// Returns vec2(darkStain, chalkHalo)
vec2 dripStreaks(vec3 p, float weather) {
    vec2 uv = wallFaceUV(p);
    float distFromSill = max(0.0, 1.535 - p.y);

    // Subtle lateral meander following wall undulations
    float meander = (fbm2(vec2(uv.x * 3.5, uv.y * 2.2)) - 0.5) * 0.045;
    float xw = uv.x + meander;

    // Primary drip columns (multi-frequency vertical streaks)
    float col1 = vnoise2(vec2(xw * 7.5, 1.7));
    float col2 = vnoise2(vec2(xw * 18.0 + 4.3, 8.2));
    float col3 = vnoise2(vec2(xw * 42.0 - 2.1, 15.9));

    // Extra strong runoff beneath the travertine stone coping joint at x = 0.18
    float jointRunoff = exp(-pow((xw - 0.18) * 14.0, 2.0)) * 0.85;
    float edgeRunoff = exp(-pow((abs(xw) - 0.58) * 10.0, 2.0)) * 0.45;

    float streakStrength = pow(col1, 1.7) * 0.62 + pow(col2, 2.2) * 0.48 + pow(col3, 2.8) * 0.26 + jointRunoff + edgeRunoff;

    // Reach distance depends on streak strength and uOpen weathering
    float reach = mix(0.28, 1.18, weather) * (0.45 + 1.15 * streakStrength);
    float verticalFade = smoothstep(reach, 0.0, distFromSill);
    // Crisp dark origin right beneath the sill shadow line
    float sillBand = exp(-distFromSill * mix(9.5, 4.2, weather)) * (0.42 + 0.58 * col1);

    // Break up streaks organically along their vertical path
    float breakup = 0.65 + 0.35 * fbm2(vec2(xw * 12.0, p.y * 5.5));
    float stain = clamp((smoothstep(0.34, 0.88, streakStrength) * verticalFade * breakup + sillBand * 0.65) * mix(0.62, 1.25, weather), 0.0, 1.0);

    // Pale mineral calcium-carbonate halo along edges of drip streaks
    float halo = smoothstep(0.18, 0.46, streakStrength) * (1.0 - smoothstep(0.46, 0.78, streakStrength)) * verticalFade * mix(0.35, 1.0, weather);

    // Fade out on very top of wall above sill contact
    float belowSill = smoothstep(1.55, 1.51, p.y);
    return vec2(stain, halo) * belowSill;
}

// --- Hero SDF with Material ID ---
// matId: 0 = terracotta stucco, 1 = travertine stone sill coping, 2 = lime mortar bed
vec2 mapHeroMat(vec3 p) {
    float weather = clamp(uOpen, 0.0, 1.0);

    // 1) Main Mediterranean Stucco Wall Block (grounded at y = 0.0)
    vec3 wallCenter = vec3(0.0, 0.775, 0.0);
    vec3 wallHalf = vec3(0.75, 0.775, 0.28);

    // Hand-plastered organic corner wobble & gentle wall float undulation
    float wobble = (fbm3(p * 2.2) - 0.5) * 0.011;
    float dWall = sdRoundBox(p - wallCenter, wallHalf, 0.028) + wobble;

    // Slight base plinth flare/settling wear at bottom 5cm
    float baseMask = smoothstep(0.08, 0.0, p.y);
    dWall -= baseMask * 0.0025;

    // Sweeping semicircular trowel relief on vertical stucco faces
    float verticalFace = smoothstep(1.53, 1.48, p.y) * smoothstep(0.015, 0.05, p.y);
    if (dWall < 0.06 && verticalFace > 0.01) {
        vec2 uv = wallFaceUV(p);
        vec4 tr = trowelPattern(uv);
        // Sculptural 3D trowel ridge relief
        float relief = (tr.x * 0.0042 + tr.y * 0.0022) * verticalFace;
        // Subtle weathered plaster spalling/flaking near bottom & corners when uOpen rises
        float cornerMask = smoothstep(0.55, 0.74, abs(p.x)) * smoothstep(0.45, 0.05, p.y);
        float spallNoise = fbm2(uv * 5.2 + vec2(3.1, 7.4));
        float spall = smoothstep(0.64 - 0.16 * weather, 0.72 - 0.16 * weather, spallNoise) * (baseMask * 0.7 + cornerMask * 0.9) * weather;
        dWall -= relief;
        dWall += spall * 0.0055;
    }

    // 2) Weathered Travertine / Limestone Sill Coping at Top Edge (y in [1.535, 1.705])
    vec3 sillP = p - vec3(0.0, 1.622, 0.0);
    // Gentle forward drainage slope on top surface of coping
    float topSlope = clamp(sillP.z * 0.032, -0.015, 0.015);
    vec3 sillHalf = vec3(0.815, 0.084 - topSlope * 0.35, 0.342);
    float sillChisel = (fbm3(p * 6.5 + vec3(4.2, 1.1, 8.7)) - 0.5) * 0.0042;
    float dSill = sdRoundBox(sillP, sillHalf, 0.014) + sillChisel;

    // Recessed architectural drip groove (throating) under front overhang
    vec3 grooveP = vec3(p.x, p.y - 1.538, p.z - 0.312);
    float dGroove = sdRoundBox(grooveP, vec3(0.83, 0.008, 0.009), 0.003);
    dSill = max(dSill, -dGroove);

    // Subtle stone block joint seam across the coping at x = 0.18
    float jointDist = abs(p.x - 0.18);
    float jointGroove = smoothstep(0.009, 0.001, jointDist) * 0.0045;
    dSill += jointGroove;

    // 3) Thin Recessed Lime Mortar Bed Joint under Coping (y ~ 1.538)
    float dMortar = sdRoundBox(p - vec3(0.0, 1.536, 0.0), vec3(0.756, 0.011, 0.286), 0.004) + (vnoise3(p * 28.0) - 0.5) * 0.0015;

    // Combine with material IDs
    vec2 res = vec2(dWall, 0.0);
    if (dMortar < res.x) {
        res = vec2(dMortar, 2.0);
    }
    if (dSill < res.x) {
        res = vec2(dSill, 1.0);
    }

    // Ensure hero bottom never dips below bench plane y = 0.0
    res.x = max(res.x, -p.y);
    return res;
}

float mapHero(vec3 p) {
    return mapHeroMat(p).x;
}

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

// --- Zero-Bug Rule (1): Unit-Weight Tetrahedral Normals ---
vec3 calcHeroNormal(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)
    );
}

// --- Micro-Bump Height for Fine Sandy Grit, Craquelure & Travertine Pores ---
float microBumpHeight(vec3 p, float matId) {
    float weather = clamp(uOpen, 0.0, 1.0);
    if (matId < 0.5) {
        // Stucco: fine sandy lime-plaster micro-grit + trowel drag + hairline cracks
        vec2 uv = wallFaceUV(p);
        float grit1 = vnoise2(uv * 95.0);
        float grit2 = vnoise2(uv * 210.0 + 7.3);
        float grit = grit1 * 0.55 + grit2 * 0.45;

        // Occasional pinhole air bubbles in hand-troweled lime
        float pin = smoothstep(0.84, 0.94, vnoise2(uv * 48.0 + 13.1));

        // Hairline shrinkage craquelure
        vec2 crackUV = uv * 4.5 + fbm2(uv * 6.5) * 0.65;
        float c1 = abs(vnoise2(crackUV) - 0.5);
        float c2 = abs(vnoise2(crackUV * 2.1 + 5.7) - 0.5);
        float crack = smoothstep(0.022 + 0.012 * weather, 0.003, min(c1, c2 * 1.15)) * (0.35 + 0.65 * weather);

        return (grit * 0.0016 - pin * 0.0018 - crack * 0.0022);
    } else if (matId < 1.5) {
        // Travertine stone coping: sedimentary horizontal veins & porous cavities
        float vein = fbm3(vec3(p.x * 4.5, p.y * 28.0, p.z * 4.5));
        float pore = smoothstep(0.76, 0.89, vnoise3(vec3(p.x * 26.0, p.y * 72.0, p.z * 26.0)));
        float fineGrain = vnoise3(p * 110.0);
        return (vein * 0.0014 - pore * 0.0028 + fineGrain * 0.0007);
    } else {
        // Mortar joint: coarse sandy lime grit
        return vnoise3(p * 130.0) * 0.002;
    }
}

vec3 perturbNormal(vec3 p, vec3 N, float matId) {
    const float eps = 0.0016;
    float h0 = microBumpHeight(p, matId);
    vec3 grad = vec3(
        microBumpHeight(p + vec3(eps, 0.0, 0.0), matId) - h0,
        microBumpHeight(p + vec3(0.0, eps, 0.0), matId) - h0,
        microBumpHeight(p + vec3(0.0, 0.0, eps), matId) - h0
    ) / eps;
    // Project gradient onto tangent plane
    grad -= N * dot(grad, N);
    return normalize(N - grad * 0.55);
}

// --- Zero-Bug Rule (3): Clean Hero-Only Soft Shadow (no ph) ---
float softShadow(vec3 ro, vec3 rd, float tMax, float k) {
    float res = 1.0;
    float t = 0.025;
    for (int i = 0; i < 64; i++) {
        float h = mapHero(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);
}

// --- Ambient Occlusion ---
float calcAO(vec3 p, vec3 N) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 0; i < 5; i++) {
        float h = 0.018 + 0.055 * float(i);
        float d = mapScene(p + N * h);
        occ += (h - d) * sca;
        sca *= 0.68;
    }
    return clamp(1.0 - 2.35 * occ, 0.14, 1.0);
}

// --- Studio Environment Sky with Softboxes + Negative-Fill Flags ---
vec3 sampleStudioSky(vec3 rd, int lightMode) {
    float y = clamp(rd.y, -1.0, 1.0);

    // Base studio cyclorama gradient
    vec3 zenith, horizon, floorBounce;
    if (lightMode == 1) {
        // Pure neutral grayscale studio
        zenith = vec3(0.24);
        horizon = vec3(0.42);
        floorBounce = vec3(0.18);
    } else if (lightMode == 2) {
        // Grazing raking studio (slightly moodier backdrop to accentuate relief)
        zenith = vec3(0.17, 0.19, 0.22);
        horizon = vec3(0.32, 0.31, 0.30);
        floorBounce = vec3(0.16, 0.14, 0.13);
    } else if (lightMode == 3) {
        // Backlit studio
        zenith = vec3(0.20, 0.22, 0.26);
        horizon = vec3(0.38, 0.36, 0.34);
        floorBounce = vec3(0.18, 0.16, 0.15);
    } else {
        // Hero warm-neutral architectural studio
        zenith = vec3(0.23, 0.25, 0.29);
        horizon = vec3(0.44, 0.42, 0.39);
        floorBounce = vec3(0.21, 0.19, 0.17);
    }

    vec3 col = y >= 0.0
        ? mix(horizon, zenith, pow(y, 0.65))
        : mix(horizon, floorBounce, pow(-y, 0.55));

    // Overhead diffused studio softbox
    vec3 ovDir = normalize(vec3(0.0, 1.0, 0.12));
    float ovBox = smoothstep(0.62, 0.90, dot(rd, ovDir));
    vec3 ovTint = (lightMode == 1) ? vec3(1.0) : vec3(1.00, 0.98, 0.94);
    col += ovTint * ovBox * 0.95;

    // Key softbox (tracks lightMode direction)
    vec3 keyDir;
    vec3 keyTint;
    float keyPow = 1.15;
    if (lightMode == 1) {
        keyDir = normalize(vec3(-0.60, 0.65, 0.46));
        keyTint = vec3(1.0);
    } else if (lightMode == 2) {
        keyDir = normalize(vec3(-0.92, 0.24, 0.26));
        keyTint = vec3(1.00, 0.93, 0.84);
        keyPow = 1.45;
    } else if (lightMode == 3) {
        keyDir = normalize(vec3(-0.26, 0.48, -0.84));
        keyTint = vec3(1.00, 0.95, 0.88);
        keyPow = 1.65;
    } else {
        keyDir = normalize(vec3(-0.60, 0.65, 0.46));
        keyTint = vec3(1.00, 0.95, 0.88);
    }
    float keyBox = smoothstep(0.76, 0.96, dot(rd, keyDir));
    col += keyTint * keyBox * keyPow;

    // Fill softbox (cool-neutral opposite flank)
    vec3 fillDir = normalize(vec3(0.76, 0.42, 0.44));
    float fillBox = smoothstep(0.68, 0.93, dot(rd, fillDir));
    vec3 fillTint = (lightMode == 1) ? vec3(0.95) : vec3(0.82, 0.89, 0.98);
    col += fillTint * fillBox * 0.42;

    // Negative-fill studio flags (matte black absorption panels on flanks for sculptural contrast)
    vec3 negFlagL = normalize(vec3(-0.88, -0.05, -0.45));
    vec3 negFlagR = normalize(vec3(0.92, 0.02, -0.38));
    float flagMask = smoothstep(0.74, 0.92, dot(rd, negFlagL)) + smoothstep(0.76, 0.93, dot(rd, negFlagR));
    col *= (1.0 - 0.58 * clamp(flagMask, 0.0, 1.0));

    return col;
}

// --- Oren-Nayar Rough Diffuse + GGX Microfacet Specular ---
vec3 shadePBR(vec3 N, vec3 V, vec3 L, vec3 lightCol, vec3 albedo, float roughness, float F0) {
    float NdotL = max(dot(N, L), 0.0);
    if (NdotL <= 0.0) return vec3(0.0);
    float NdotV = max(dot(N, V), 0.001);

    // Oren-Nayar diffuse for authentic powdery chalk / mineral plaster / travertine
    float sigma2 = roughness * roughness;
    float A = 1.0 - 0.5 * (sigma2 / (sigma2 + 0.33));
    float B = 0.45 * (sigma2 / (sigma2 + 0.09));
    float sinThetaL = sqrt(max(0.0, 1.0 - NdotL * NdotL));
    float sinThetaV = sqrt(max(0.0, 1.0 - NdotV * NdotV));
    vec3 Lp = L - N * NdotL;
    vec3 Vp = V - N * NdotV;
    float cosPhiDiff = max(0.0, dot(normalize(Lp + 1e-5), normalize(Vp + 1e-5)));
    float sinAlpha = max(sinThetaL, sinThetaV);
    float tanBeta = min(sinThetaL, sinThetaV) / max(min(NdotL, NdotV), 0.05);
    float orenNayar = (A + B * cosPhiDiff * sinAlpha * tanBeta);

    // GGX specular
    vec3 H = normalize(L + V);
    float NdotH = max(dot(N, H), 0.0);
    float VdotH = max(dot(V, H), 0.0);
    float a = max(roughness * roughness, 0.04);
    float a2 = a * a;
    float denom = (NdotH * NdotH * (a2 - 1.0) + 1.0);
    float D = a2 / (PI * denom * denom);

    float k = (roughness + 1.0);
    k = (k * k) * 0.125;
    float Gv = NdotV / (NdotV * (1.0 - k) + k);
    float Gl = NdotL / (NdotL * (1.0 - k) + k);
    float G = Gv * Gl;

    float F = F0 + (1.0 - F0) * pow(1.0 - VdotH, 5.0);
    float spec = (D * G * F) / max(4.0 * NdotV * NdotL, 0.001);

    vec3 kD = vec3(1.0 - F);
    return (kD * albedo * (orenNayar / PI) + vec3(spec)) * lightCol * NdotL;
}

// --- ACES Filmic Tone Mapping ---
vec3 acesFilm(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);
}

// --- Material Evaluation for Hero Architectural Wall Block ---
void evalHeroMaterial(vec3 p, vec3 N, float matId, out vec3 albedo, out float roughness, out float F0) {
    float weather = clamp(uOpen, 0.0, 1.0);

    if (matId < 0.5) {
        // ============================================================
        // HAND-TROWELED TERRACOTTA-OCHRE MINERAL LIME STUCCO
        // ============================================================
        vec2 uv = wallFaceUV(p);
        vec4 tr = trowelPattern(uv); // height, ridge, burnish, passTint

        // Multi-scale mineral pigment & plaster wash variation
        float washMacro = fbm2(uv * 1.45 + vec2(2.3, 1.7));
        float washMed = fbm2(uv * 4.2 + vec2(9.1, 3.4));
        float fineGrit = vnoise2(uv * 110.0);
        float microSpeck = vnoise2(uv * 240.0 + 4.9);

        // Warm Mediterranean terracotta-ochre mineral palette
        vec3 colDeepTerra = vec3(0.42, 0.18, 0.11);
        vec3 colWarmTerra = vec3(0.59, 0.28, 0.16);
        vec3 colOchreSun  = vec3(0.68, 0.40, 0.22);
        vec3 colPaleLime  = vec3(0.76, 0.67, 0.56);

        vec3 baseCol = mix(colDeepTerra, colWarmTerra, smoothstep(0.22, 0.78, washMacro));
        baseCol = mix(baseCol, colOchreSun, smoothstep(0.35, 0.85, washMed) * 0.52);

        // Individual overlapping steel-trowel pass warmth/value shift
        baseCol *= mix(0.90, 1.09, tr.w);
        // Trowel ridges catch slightly lighter warm-ochre burnish; troughs hold deeper pigment
        baseCol = mix(baseCol * 0.91, baseCol * 1.08 + vec3(0.03, 0.02, 0.01), clamp(tr.x * 0.65 + 0.35, 0.0, 1.0));
        baseCol += vec3(0.045, 0.035, 0.025) * tr.y;

        // Fine sandy silica aggregate speckle
        float sandLight = smoothstep(0.72, 0.96, fineGrit);
        float sandDark  = smoothstep(0.22, 0.04, microSpeck);
        baseCol = mix(baseCol, colPaleLime * 0.95, sandLight * 0.18);
        baseCol *= (1.0 - sandDark * 0.14);

        // Exposed pale lime-sand undercoat in weathered lower/corner spall patches
        float baseMask = smoothstep(0.11, 0.0, p.y);
        float cornerMask = smoothstep(0.55, 0.74, abs(p.x)) * smoothstep(0.45, 0.05, p.y);
        float spallNoise = fbm2(uv * 5.2 + vec2(3.1, 7.4));
        float spallMask = smoothstep(0.62 - 0.16 * weather, 0.70 - 0.16 * weather, spallNoise) * (baseMask * 0.75 + cornerMask * 0.9) * weather;
        baseCol = mix(baseCol, vec3(0.69, 0.62, 0.53), clamp(spallMask, 0.0, 0.9));

        // Sun-bleached chalky efflorescence & lime-wash patina patches
        vec2 effUV = uv * 1.85 + vec2(fbm2(uv * 3.1) * 0.45, fbm2(uv * 2.8 + 5.2) * 0.45);
        float effBase = fbm2(effUV);
        float effDetail = fbm2(uv * 8.5 + 11.3);
        float sunBias = smoothstep(0.15, 1.25, p.y) * 0.22 + tr.y * 0.18;
        float effThresh = mix(0.64, 0.36, weather);
        float effMask = smoothstep(effThresh, effThresh + 0.26, effBase + effDetail * 0.22 + sunBias);
        // Powdery mineral salt bloom tends to cluster softly along trowel ridge shoulders
        effMask = clamp(effMask * mix(0.45, 0.92, weather), 0.0, 0.92);

        // Darker vertical rainwater drip streaks descending from stone sill coping
        vec2 drips = dripStreaks(p, weather);
        float dripStain = drips.x;
        float dripHalo = drips.y;

        // Apply chalky efflorescence + drip mineral halos
        vec3 colChalk = vec3(0.84, 0.79, 0.72);
        float totalChalk = clamp(effMask + dripHalo * 0.55, 0.0, 0.92);
        baseCol = mix(baseCol, colChalk, totalChalk * 0.68);

        // Apply darker mineral/rainwater runoff streaks (deep warm umber-charcoal)
        vec3 colDrip = vec3(0.19, 0.12, 0.09);
        baseCol = mix(baseCol, colDrip, dripStain * 0.74);

        // Subtle rising damp darkening right at bench contact (bottom 4cm)
        baseCol *= mix(0.82, 1.0, smoothstep(0.0, 0.055, p.y));

        // Hairline shrinkage craquelure darkening
        vec2 crackUV = uv * 4.5 + fbm2(uv * 6.5) * 0.65;
        float c1 = abs(vnoise2(crackUV) - 0.5);
        float c2 = abs(vnoise2(crackUV * 2.1 + 5.7) - 0.5);
        float crack = smoothstep(0.022 + 0.012 * weather, 0.003, min(c1, c2 * 1.15)) * (0.35 + 0.65 * weather);
        baseCol *= (1.0 - crack * 0.35);

        albedo = baseCol;
        // Burnished trowel steel compression is slightly smoother; chalk & sand are ultra-matte
        roughness = mix(0.88, 0.64, tr.z * 0.75);
        roughness = mix(roughness, 0.96, totalChalk);
        roughness = mix(roughness, 0.72, dripStain * 0.55);
        roughness = clamp(roughness + (fineGrit - 0.5) * 0.08, 0.52, 0.98);
        F0 = mix(0.032, 0.022, totalChalk);

    } else if (matId < 1.5) {
        // ============================================================
        // WEATHERED TRAVERTINE / LIMESTONE SILL COPING
        // ============================================================
        float strata = fbm3(vec3(p.x * 2.8, p.y * 22.0, p.z * 2.8));
        float mottle = fbm3(p * 5.2 + vec3(7.1, 2.3, 4.8));
        float pores = smoothstep(0.76, 0.89, vnoise3(vec3(p.x * 26.0, p.y * 72.0, p.z * 26.0)));

        // Subtle tonal difference across the stone coping joint at x = 0.18
        float blockTone = p.x > 0.18 ? 0.96 : 1.03;

        vec3 stoneLight = vec3(0.77, 0.72, 0.63);
        vec3 stoneMid   = vec3(0.64, 0.58, 0.50);
        vec3 stonePore  = vec3(0.39, 0.34, 0.29);

        vec3 col = mix(stoneMid, stoneLight, smoothstep(0.25, 0.78, strata * 0.7 + mottle * 0.3)) * blockTone;
        col = mix(col, stonePore, pores * 0.68);

        // Weathering dust & subtle lichen/soot on top surface and near stone joint
        float topMask = smoothstep(0.45, 0.92, N.y);
        float lichenNoise = fbm2(p.xz * 7.5 + 3.2);
        float grime = smoothstep(0.45, 0.82, lichenNoise) * topMask * mix(0.25, 0.72, weather);
        col = mix(col, vec3(0.42, 0.40, 0.33), grime * 0.55);

        // Darken inside coping joint seam
        float jointDark = smoothstep(0.011, 0.002, abs(p.x - 0.18));
        col *= (1.0 - jointDark * 0.45);

        albedo = col;
        roughness = clamp(mix(0.76, 0.92, pores * 0.7 + grime * 0.4), 0.65, 0.96);
        F0 = 0.036;

    } else {
        // ============================================================
        // RECESSED LIME MORTAR BED JOINT
        // ============================================================
        float grain = vnoise3(p * 90.0);
        albedo = mix(vec3(0.56, 0.51, 0.44), vec3(0.68, 0.63, 0.55), grain);
        roughness = 0.94;
        F0 = 0.025;
    }
}

// --- Studio Bench Material ---
void evalBenchMaterial(vec3 p, int lightMode, out vec3 albedo, out float roughness, out float F0) {
    float radial = length(p.xz);
    float micro = fbm2(p.xz * 6.5) * 0.04 + vnoise2(p.xz * 48.0) * 0.02;
    if (lightMode == 1) {
        albedo = vec3(0.38 + micro) * mix(1.0, 0.78, smoothstep(1.2, 6.5, radial));
    } else {
        vec3 warmSlate = vec3(0.36, 0.34, 0.32) + vec3(micro);
        albedo = warmSlate * mix(1.0, 0.76, smoothstep(1.2, 6.5, radial));
    }
    roughness = 0.82 + micro * 0.8;
    F0 = 0.03;
}

// --- Raymarching ---
float raymarch(vec3 ro, vec3 rd, float maxT) {
    float t = 0.0;
    for (int i = 0; i < 128; i++) {
        vec3 p = ro + rd * t;
        float d = mapScene(p);
        if (d < 0.0007 || t > maxT) break;
        t += d * 0.88;
    }
    return t;
}

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

    // --- Camera Setup (uCamMode: 0 = hero, 1 = ~2.5x macro) ---
    vec3 ro, ta;
    float fl;
    float gentleOrbit = sin(uTime * 0.16) * 0.07;

    if (uCamMode == 1) {
        // ~2.5x Macro: tight study of travertine sill overhang, mortar line,
        // rainwater drip origins, semicircular steel-trowel arcs & chalky patina
        float ang = 0.24 + gentleOrbit * 0.45;
        ta = vec3(0.06, 1.18, 0.26);
        float dist = 1.24;
        ro = ta + vec3(sin(ang) * dist, 0.08, cos(ang) * dist);
        fl = 2.55;
    } else {
        // Hero 3/4 architectural studio composition (full block + top sill + grounded bench)
        float ang = 0.44 + gentleOrbit;
        ta = vec3(0.0, 0.84, 0.0);
        float dist = 4.45;
        ro = ta + vec3(sin(ang) * dist, 0.62, cos(ang) * dist);
        fl = 1.95;
    }

    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(uu * uv.x + vv * uv.y + ww * fl);

    // Background studio sky
    vec3 col = sampleStudioSky(rd, uLightMode);

    float maxT = 18.0;
    float t = raymarch(ro, rd, maxT);

    if (t < maxT) {
        vec3 p = ro + rd * t;
        vec2 heroInfo = mapHeroMat(p);
        float dHero = heroInfo.x;
        float matId = heroInfo.y;

        // Zero-Bug Rule (2): Exact bench discrimination
        bool isBench = (p.y <= dHero + 0.001);

        vec3 N;
        vec3 albedo;
        float roughness;
        float F0;

        if (isBench) {
            N = vec3(0.0, 1.0, 0.0);
            evalBenchMaterial(p, uLightMode, albedo, roughness, F0);
        } else {
            N = calcHeroNormal(p);
            N = perturbNormal(p, N, matId);
            evalHeroMaterial(p, N, matId, albedo, roughness, F0);
            if (uLightMode == 1) {
                albedo = vec3(0.45);
            }
        }

        vec3 V = -rd;
        float ao = calcAO(p, N);

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

        if (uLightMode == 1) {
            // Pure neutral grayscale studio lighting
            keyDir  = normalize(vec3(-0.60, 0.65, 0.46));
            keyCol  = vec3(2.55);
            fillDir = normalize(vec3(0.76, 0.42, 0.44));
            fillCol = vec3(0.68);
            rimDir  = normalize(vec3(0.52, 0.56, -0.64));
            rimCol  = vec3(0.82);
        } else if (uLightMode == 2) {
            // Grazing raking light skimming across trowel ridges & sandy plaster
            keyDir  = normalize(vec3(-0.92, 0.24, 0.26));
            keyCol  = vec3(1.00, 0.92, 0.82) * 3.15;
            fillDir = normalize(vec3(0.72, 0.55, 0.42));
            fillCol = vec3(0.76, 0.84, 0.96) * 0.52;
            rimDir  = normalize(vec3(0.45, 0.65, -0.62));
            rimCol  = vec3(0.98, 0.95, 0.90) * 0.72;
            shadowK = 18.0;
        } else if (uLightMode == 3) {
            // Backlit rim light with luminous soft front fill
            keyDir  = normalize(vec3(-0.26, 0.48, -0.84));
            keyCol  = vec3(1.00, 0.95, 0.87) * 3.35;
            fillDir = normalize(vec3(0.24, 0.36, 0.90));
            fillCol = vec3(0.86, 0.90, 0.97) * 1.05;
            rimDir  = normalize(vec3(0.74, 0.52, -0.42));
            rimCol  = vec3(1.00, 0.94, 0.85) * 1.15;
            shadowK = 12.0;
        } else {
            // Hero warm Mediterranean architectural studio lighting
            keyDir  = normalize(vec3(-0.60, 0.65, 0.46));
            keyCol  = vec3(1.00, 0.94, 0.86) * 2.68;
            fillDir = normalize(vec3(0.76, 0.42, 0.44));
            fillCol = vec3(0.78, 0.86, 0.98) * 0.64;
            rimDir  = normalize(vec3(0.52, 0.56, -0.64));
            rimCol  = vec3(1.00, 0.95, 0.88) * 0.88;
        }

        // Zero-Bug Rule (3): Hero-only soft shadow with bias p + N * 0.014
        float shKey = softShadow(p + N * 0.014, keyDir, 8.0, shadowK);
        float shRim = softShadow(p + N * 0.014, rimDir, 7.0, 10.0);

        // Direct lighting
        vec3 lit = vec3(0.0);
        lit += shadePBR(N, V, keyDir, keyCol, albedo, roughness, F0) * shKey;
        lit += shadePBR(N, V, fillDir, fillCol, albedo, roughness, F0) * (0.45 + 0.55 * ao);
        lit += shadePBR(N, V, rimDir, rimCol, albedo, roughness, F0) * shRim;

        // Ambient studio IBL (diffuse sky irradiance + rough specular reflection)
        vec3 skyIrr = sampleStudioSky(N, uLightMode);
        vec3 R = reflect(-V, N);
        vec3 skyRefl = sampleStudioSky(R, uLightMode);
        float NdotV = max(dot(N, V), 0.001);
        float fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0) * (1.0 - roughness * 0.72);

        // Subtle warm bounce light from studio bench onto lower block surfaces
        vec3 benchBounce = (uLightMode == 1 ? vec3(0.26) : vec3(0.30, 0.25, 0.21)) * smoothstep(1.2, 0.0, p.y) * max(0.0, 0.5 - 0.5 * N.y);

        vec3 ambientDiff = (skyIrr * 0.58 + benchBounce * 0.35) * albedo * ao;
        vec3 ambientSpec = skyRefl * fresnel * (1.0 - roughness * 0.75) * ao * 0.65;

        col = lit + ambientDiff + ambientSpec;

        // Soft studio distance fade on the bench into horizon
        if (isBench) {
            float fog = 1.0 - exp(-0.0065 * t * t);
            col = mix(col, sampleStudioSky(rd, uLightMode), clamp(fog, 0.0, 1.0));
        }
    }

    // Subtle studio exposure & ACES filmic tone mapping
    col *= 1.04;
    col = acesFilm(col);

    // Gentle studio vignette (preserves luminance well above black)
    vec2 q = gl_FragCoord.xy / uResolution.xy;
    float vig = 0.88 + 0.12 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.18);
    col *= vig;

    // Force strict neutral monochrome output in grayscale clay mode
    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.2126, 0.7152, 0.0722)));
    }

    // Gamma encode to sRGB
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));
    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifierweathered-terracotta-stucco
CategoryStone & Masonry Shaders · Urban Surfaces
Upstream studyurban/lib/09_weathered_terracotta_stucco_wash.glsl (commit bb42233)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size828 lines · 31.8 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT nature sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Oren–Nayar rough diffuse reflectance (1994); 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 stone & masonry shaders