Kintsugi Gold Fracture Veins shader

kintsugi-gold-fracture-veins.glsl · 829 lines · GLSL ES 3.00 · MIT

Kintsugi Gold Fracture VeinsSci-Fi & Noise
Kintsugi Gold Fracture Veins WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 829 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

Cellular & Ripple Fields
kintsugi-gold-fracture-veins.glsl (829 lines)
#version 300 es
// Kintsugi Gold Fracture Veins (kintsugi-gold-fracture-veins.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/ceramics/lib/03_kintsugi_gold_lacquer_seam.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)
// Kintsugi dark glazed stoneware dish fractured into 4 interlocking shards
// rejoined by proud urushi lacquer seams dusted with anisotropic metallic gold
// powder and subtle red-lacquer (bengara urushi) undercoat peeks.
// uOpen (0..1) separates the shards horizontally along the studio bench to
// reveal the beveled ceramic fracture faces and lacquer joint profile.
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

const float PI  = 3.14159265;
const float FAR = 18.0;

// Material IDs
const float MAT_GLAZE    = 1.0;
const float MAT_FOOT     = 2.0;
const float MAT_FRACTURE = 3.0;
const float MAT_GOLD     = 4.0;

// ---------- Safe Math & Procedural Noise ----------
float sq(float x) { return x * x; }
float sat(float x) { return clamp(x, 0.0, 1.0); }
float safePow(float x, float e) { return pow(max(x, 0.0), e); }
vec3 safeNorm(vec3 v) { return v * inversesqrt(max(dot(v, v), 1e-12)); }
vec2 safeNorm2(vec2 v) { return v * inversesqrt(max(dot(v, v), 1e-12)); }

float smin(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / max(k, 1e-6);
    return min(a, b) - h * h * k * 0.25;
}
float smax(float a, float b, float k) {
    return -smin(-a, -b, k);
}

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

float hash31(vec3 p) {
    uvec3 k = uvec3(ivec3(floor(p)));
    uint v = (k.x * 2246822519u) ^ (k.y * 3266489917u) ^ (k.z * 668265263u);
    v = (v ^ (v >> 13u)) * 2246822519u;
    return float(v >> 8u) / 16777216.0;
}

float vnoise2(vec2 p) {
    vec2 i = floor(p);
    vec2 f = p - i;
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(
        mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),
        mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), u.x),
        u.y
    );
}

float fbm2(vec2 p) {
    float s = 0.0, a = 0.52;
    mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        s += a * vnoise2(p);
        p = m * p * 2.03 + vec2(13.7, -9.2);
        a *= 0.48;
    }
    return s;
}

// ---------- 4-Shard Fracture Layout ----------
// Seed centers in dish XZ rest space, positioned so a dramatic asymmetric
// Y-and-branching kintsugi river runs across the center and rim of the dish.
vec2 shardSeed(int i) {
    if (i == 0) return vec2(-0.42,  0.34);
    if (i == 1) return vec2( 0.45,  0.25);
    if (i == 2) return vec2(-0.34, -0.42);
    return vec2( 0.37, -0.34);
}

// Horizontal separation displacement along the studio bench controlled by uOpen
vec3 shardShift(int i) {
    float op = smoothstep(0.0, 1.0, sat(uOpen));
    if (op <= 0.0001) return vec3(0.0);
    vec2 dir;
    if (i == 0)      dir = vec2(-0.115,  0.088);
    else if (i == 1) dir = vec2( 0.125,  0.068);
    else if (i == 2) dir = vec2(-0.088, -0.108);
    else             dir = vec2( 0.105, -0.092);
    return vec3(dir.x, 0.0, dir.y) * op;
}

// Bounded jagged fracture domain warp in dish rest space
// Produces authentic stoneware fracture kinks without breaking Lipschitz stability.
vec2 warpFractureXZ(vec3 q) {
    vec2 p = q.xz;
    // Macro organic curve of the fracture lines
    vec2 w1 = vec2(
        sin(p.y * 3.8 + 0.65) + 0.45 * cos(p.x * 5.2 - 0.9),
        cos(p.x * 4.1 - 0.45) + 0.45 * sin(p.y * 4.9 + 1.1)
    ) * 0.058;
    // Medium jagged stoneware kinks
    vec2 w2 = vec2(
        vnoise2(p * 6.2 + vec2(3.1, 7.4)) - 0.5,
        vnoise2(p * 6.2 + vec2(17.8, 2.3)) - 0.5
    ) * 0.056;
    // Fine chipped conchoidal micro-jags along the break wall (slight y-tilt too)
    vec2 w3 = vec2(
        vnoise2(p * 17.5 + vec2(q.y * 5.5, 4.2)) - 0.5,
        vnoise2(p * 17.5 + vec2(9.6, q.y * 5.5)) - 0.5
    ) * 0.014;
    return p + w1 + w2 + w3;
}

// Signed distance to Voronoi cell i (negative inside shard i, positive outside).
// Exact bisector max over the other 3 shards ensures watertight fit at uOpen = 0.
float shardCellSDF(vec3 q, int idx) {
    vec2 w = warpFractureXZ(q);
    vec2 si = shardSeed(idx);
    float d = -1e5;
    for (int j = 0; j < 4; j++) {
        if (j == idx) continue;
        vec2 sj = shardSeed(j);
        vec2 mid = 0.5 * (si + sj);
        vec2 nrm = safeNorm2(sj - si);
        float plane = dot(w - mid, nrm);
        d = max(d, plane);
    }
    // Slight subtle conchoidal bevel tilt through wall thickness
    return d;
}

// ---------- Curved Dark Glazed Stoneware Dish SDF (Rest Space) ----------
// Spherical-section shallow Japanese stoneware dish + unglazed foot ring (kodai)
float dishBodySDF(vec3 q) {
    float r = length(q.xz);
    float ang = atan(q.z, q.x + 1e-7);

    // Spherical dish shell: center at y = 1.46, radius 1.40 -> base center at y = 0.06
    vec3 sphC = vec3(0.0, 1.46, 0.0);
    float sphR = 1.40;
    float dSph = length(q - sphC) - sphR;

    // Wall half-thickness: thicker near base (0.036), tapering gracefully to 0.025,
    // with a subtle hand-thrown rolled lip right at the rim.
    float rimT = sat(r / 0.88);
    float halfT = mix(0.036, 0.024, smoothstep(0.0, 0.78, rimT))
                + 0.0045 * smoothstep(0.84, 1.0, rimT);

    // Subtle concentric potter's wheel throwing rings on dish surface
    float throwRibs = 0.0014 * sin(r * 54.0 + 0.4 * sin(ang * 2.0)) * smoothstep(0.14, 0.35, r);
    float dShell = abs(dSph) - halfT - throwRibs;

    // Hand-thrown slightly organic rim height
    float yRim = 0.355 + 0.011 * sin(3.0 * ang + 0.55) + 0.005 * cos(5.0 * ang - 0.35);
    float dTop = q.y - yRim;
    float dDish = smax(dShell, dTop, 0.019);

    // Grounded stoneware foot ring (kodai) resting cleanly on y = 0.0
    float footMidR = 0.375;
    float footHalfW = 0.036;
    float dFootRad = abs(r - footMidR) - footHalfW;
    float dFootY = max(-q.y, q.y - 0.088);
    float dFoot = max(dFootRad, dFootY);
    // Gentle chamfer at bottom of foot ring
    dFoot = smax(dFoot, (abs(r - footMidR) - footHalfW + 0.008) - q.y * 1.15, 0.005);

    return smin(dDish, dFoot, 0.016);
}

// Local width of proud kintsugi lacquer bead (slightly wider at triple junctions & organic)
float seamHalfWidth(vec3 q) {
    float n = vnoise2(q.xz * 12.0 + vec2(4.7, -3.1));
    return 0.0155 + 0.0055 * n;
}

// Distance to shard i and its attached proud kintsugi urushi lacquer bead
vec2 shardAndBeadDist(vec3 p, int idx) {
    vec3 q = p - shardShift(idx);
    float dDish = dishBodySDF(q);
    float dCell = shardCellSDF(q, idx);

    // Subtle 3D chipped roughness on the ceramic fracture bevel face
    float chip = (vnoise2(vec2(q.x + q.z, q.y) * 48.0) - 0.5) * 0.0018;
    // Slight recess inside the ceramic joint where urushi lacquer sits
    float crackGap = 0.0042 + chip;
    // Beveled shard edge (smooth max gives realistic micro-bevel along fracture edge)
    float dShard = smax(dDish, dCell + crackGap, 0.0052);

    // Proud kintsugi urushi lacquer seam crowning the fracture line on the dish surface
    // Adheres to each shard's beveled joint edge so uOpen reveals its sculpted cross-section.
    float wSeam = seamHalfWidth(q);
    // Distance along the seam wall: centered right on the fracture seam (-0.001..wSeam)
    float dAcross = max(dCell + 0.0005, -(dCell + wSeam));
    // Keep bead hugging the surface of the dish (both interior and exterior faces)
    float dBeadSurf = abs(dDish) - 0.0068;
    // Rounded proud profile along the crack edge
    float edgeT = sat((-dCell) / max(wSeam, 0.004));
    float archLift = 0.0066 * sin(edgeT * PI * 0.88) * smoothstep(0.004, 0.025, q.y);
    float dBead = max(dDish - archLift, dAcross);
    // Also fill the thin urushi bonding bridge inside the bevel groove near the surface
    dBead = min(dBead, max(dDish - 0.0015, max(dCell + 0.0005, -(dCell + 0.0075))));
    // Keep bead cleanly bounded near the dish surface (don't float away from rim)
    dBead = smax(dBead, dDish - 0.0085, 0.003);
    dBead = smax(dBead, dBeadSurf - 0.0035, 0.004);
    // Suppress tiny stray bead below bench y = 0
    dBead = max(dBead, -q.y + 0.001);

    return vec2(dShard, dBead);
}

// ---------- Distance-Only Scene SDF (Conservative Lipschitz) ----------
float mapDist(vec3 p) {
    // Cheap bounding box early-out
    vec3 bp = abs(p - vec3(0.0, 0.22, 0.0)) - vec3(1.18, 0.30, 1.18);
    float boxLB = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);
    if (boxLB > 0.22) return boxLB;

    float dMin = 1e5;
    for (int i = 0; i < 4; i++) {
        vec2 sb = shardAndBeadDist(p, i);
        // Smoothly fillet the proud lacquer bead into its shard
        float dCombined = smin(sb.x, sb.y, 0.0022);
        dMin = min(dMin, dCombined);
    }
    return dMin * 0.76;
}

// ---------- Shading Hit Classification (Called Once Per Pixel) ----------
struct HitInfo {
    float matId;
    int shardIdx;
    vec3 q;
    float dCell;
    float dDish;
    float wSeam;
};

HitInfo classifyHit(vec3 p) {
    HitInfo info;
    info.matId = MAT_GLAZE;
    info.shardIdx = 0;
    info.q = p;
    info.dCell = -1.0;
    info.dDish = 0.0;
    info.wSeam = 0.016;

    float bestD = 1e5;
    float bestShardD = 1e5;
    float bestBeadD = 1e5;

    for (int i = 0; i < 4; i++) {
        vec2 sb = shardAndBeadDist(p, i);
        float dComb = smin(sb.x, sb.y, 0.0022);
        if (dComb < bestD) {
            bestD = dComb;
            bestShardD = sb.x;
            bestBeadD = sb.y;
            info.shardIdx = i;
        }
    }

    vec3 q = p - shardShift(info.shardIdx);
    info.q = q;
    info.dDish = dishBodySDF(q);
    info.dCell = shardCellSDF(q, info.shardIdx);
    info.wSeam = seamHalfWidth(q);

    // Is hit on the proud gold-dusted urushi lacquer bead?
    if (bestBeadD < bestShardD + 0.0014) {
        info.matId = MAT_GOLD;
        return info;
    }

    // Is hit on the exposed ceramic fracture bevel face?
    if (info.dCell + 0.0068 > info.dDish) {
        info.matId = MAT_FRACTURE;
        return info;
    }

    // Is hit on the unglazed raw stoneware foot ring (kodai)?
    float r = length(q.xz);
    if (q.y < 0.052 && abs(r - 0.375) < 0.062) {
        info.matId = MAT_FOOT;
        return info;
    }

    info.matId = MAT_GLAZE;
    return info;
}

// ---------- Raymarch, Normal, Soft Shadow, AO ----------
vec3 calcNormal(vec3 p) {
    const float e = 0.0011;
    vec2 k = vec2(1.0, -1.0);
    return safeNorm(
        k.xyy * mapDist(p + k.xyy * e) +
        k.yyx * mapDist(p + k.yyx * e) +
        k.yxy * mapDist(p + k.yxy * e) +
        k.xxx * mapDist(p + k.xxx * e)
    );
}

float traceScene(vec3 ro, vec3 rd, float tMax) {
    float t = 0.004;
    for (int i = 0; i < 180; i++) {
        float d = mapDist(ro + rd * t);
        if (d < 0.00045) return t;
        t += max(0.00042, d * 0.86);
        if (t > tMax) break;
    }
    return -1.0;
}

float calcSoftShadow(vec3 ro, vec3 rd, float tMax) {
    float res = 1.0;
    float t = 0.012;
    for (int i = 0; i < 36; i++) {
        float h = mapDist(ro + rd * t);
        res = min(res, clamp(6.5 * h / max(t, 0.01), 0.04, 1.0));
        if (res < 0.045) break;
        t += clamp(h * 0.84, 0.009, 0.16);
        if (t > tMax) break;
    }
    return res;
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 4; i++) {
        float h = 0.015 * float(i * i);
        float d = mapDist(p + n * h);
        occ += max(0.0, h - d) * sca;
        sca *= 0.65;
    }
    return clamp(1.0 - 2.9 * occ, 0.14, 1.0);
}

// Grounded contact shadow on studio bench y = 0
float benchContactShadow(vec3 p, vec3 lDir) {
    // Analytic ambient occlusion under the curved dish overhang + raytraced soft shadow
    float dNear = mapDist(p + vec3(0.0, 0.012, 0.0));
    float dMid  = mapDist(p + vec3(0.0, 0.075, 0.0));
    float contactAO = smoothstep(0.002, 0.11, dNear) * (0.45 + 0.55 * smoothstep(0.02, 0.24, dMid));
    float raySh = calcSoftShadow(p + vec3(0.0, 0.008, 0.0), lDir, 3.0);
    return mix(0.14, 1.0, (0.35 + 0.65 * contactAO) * (0.24 + 0.76 * raySh));
}

// ---------- Studio Environment (Softboxes + Negative-Fill Flags) ----------
vec3 studioEnv(vec3 d, vec3 p, float rough) {
    vec3 bg = mix(vec3(0.030, 0.034, 0.040), vec3(0.18, 0.20, 0.23), smoothstep(-0.20, 0.80, d.y));
    vec3 a = safeNorm(d + vec3(0.06 * p.x, 0.03 * p.y, 0.06 * p.z));
    vec2 uv = a.xz / max(0.14, a.y);
    float blur = 0.055 + rough * 0.85;

    // Warm overhead-left gallery softbox
    float keyBox = 1.0 - smoothstep(-blur, blur, max(abs(uv.x + 0.54) - 0.58, abs(uv.y + 0.20) - 0.76));
    // Cool right rim strip softbox
    float rimStrip = 1.0 - smoothstep(-blur, blur, max(abs(uv.x - 1.24) - 0.18, abs(uv.y - 0.16) - 1.48));
    // Negative-fill black flag carving dramatic dark contrast into glossy glaze & gold
    float negFlag = 1.0 - smoothstep(-blur, blur, max(abs(uv.y - 1.32) - 0.34, abs(uv.x - 0.10) - 1.65));
    float above = smoothstep(0.04, 0.22, a.y);

    float az = atan(a.z, a.x + 1e-7);
    float leftStrip = exp(-sq((az + 1.12) / (0.12 + rough * 0.48)))
                    * smoothstep(-0.30, 0.05, a.y) * (1.0 - smoothstep(0.48, 0.84, a.y));
    float rightStrip = exp(-sq((az - 0.62) / (0.15 + rough * 0.52)))
                     * smoothstep(-0.25, 0.08, a.y) * (1.0 - smoothstep(0.50, 0.85, a.y));
    float backPanel = exp(-sq((az - 2.60) / (0.28 + rough * 0.56)))
                    * smoothstep(-0.15, 0.12, a.y) * (1.0 - smoothstep(0.56, 0.86, a.y));

    if (uLightMode == 1) {
        vec3 col = bg * (1.0 - 0.82 * negFlag * above);
        col += above * (vec3(1.25) * keyBox + vec3(0.90) * rimStrip);
        col += vec3(0.88) * leftStrip + vec3(0.60) * rightStrip;
        return vec3(dot(col, vec3(0.299, 0.587, 0.114)));
    }
    if (uLightMode == 2) {
        vec3 col = bg * 0.50 * (1.0 - 0.86 * negFlag * above);
        col += vec3(2.85, 2.48, 2.05) * leftStrip + vec3(0.68, 0.80, 0.96) * rightStrip;
        col += above * vec3(0.38, 0.42, 0.48) * keyBox;
        return col;
    }
    if (uLightMode == 3) {
        vec3 col = bg * 0.40 * (1.0 - 0.78 * negFlag * above);
        col += vec3(3.15, 2.68, 2.15) * backPanel + vec3(0.76, 0.88, 1.08) * rightStrip;
        col += above * (vec3(0.32, 0.36, 0.44) * keyBox + vec3(1.95, 1.74, 1.48) * rimStrip);
        return col;
    }

    vec3 col = bg * (1.0 - 0.88 * negFlag * above);
    col += above * (vec3(1.62, 1.54, 1.42) * keyBox + vec3(1.02, 1.18, 1.44) * rimStrip);
    col += vec3(1.22, 1.16, 1.08) * leftStrip + vec3(0.78, 0.86, 0.98) * rightStrip;
    col += vec3(0.86, 0.76, 0.64) * backPanel;
    return col;
}

// Isotropic GGX specular
float ggxSpec(vec3 n, vec3 v, vec3 l, float rough) {
    vec3 h = safeNorm(v + l);
    float ndl = max(dot(n, l), 0.0);
    float ndv = max(dot(n, v), 0.01);
    float ndh = max(dot(n, h), 0.0);
    float a2 = max(0.0012, sq(sq(rough)));
    float denom = PI * sq(ndh * ndh * (a2 - 1.0) + 1.0);
    return min(6.5, (a2 / max(denom, 1e-5)) * ndl / (4.0 * (ndl + 0.12) * (ndv + 0.12)));
}

// Anisotropic GGX specular for brush-burnished kintsugi gold powder luster
float ggxAnisoSpec(vec3 n, vec3 t, vec3 b, vec3 v, vec3 l, float roughT, float roughB) {
    vec3 h = safeNorm(v + l);
    float ndl = max(dot(n, l), 0.0);
    float ndv = max(dot(n, v), 0.01);
    float ndh = max(dot(n, h), 0.0);
    float tdh = dot(t, h);
    float bdh = dot(b, h);
    float aT = max(0.04, roughT * roughT);
    float aB = max(0.04, roughB * roughB);
    float e = sq(tdh / aT) + sq(bdh / aB) + ndh * ndh;
    float D = 1.0 / max(PI * aT * aB * e * e, 1e-5);
    return min(7.0, D * ndl / (4.0 * (ndl + 0.12) * (ndv + 0.12)));
}

// ---------- Lighting Setup per uLightMode ----------
void getLights(vec3 p, out vec3 lKey, out vec3 keyCol, out vec3 lFill, out vec3 fillCol, out vec3 lRim, out vec3 rimCol) {
    if (uLightMode == 1) {
        lKey    = safeNorm(vec3(-2.1, 3.6, 2.2) - 0.16 * p);
        keyCol  = vec3(0.92);
        lFill   = safeNorm(vec3(2.8, 1.9, 1.3));
        fillCol = vec3(0.26);
        lRim    = safeNorm(vec3(0.4, 2.1, -3.2));
        rimCol  = vec3(0.32);
    } else if (uLightMode == 2) {
        // Grazing raking light skimming across proud kintsugi beads & wheel ribs
        lKey    = safeNorm(vec3(-3.8, 0.85, 0.85) - 0.10 * p);
        keyCol  = vec3(1.88, 1.66, 1.38);
        lFill   = safeNorm(vec3(2.6, 1.4, 1.8));
        fillCol = vec3(0.20, 0.24, 0.30);
        lRim    = safeNorm(vec3(1.4, 1.1, -2.5));
        rimCol  = vec3(0.45, 0.50, 0.60);
    } else if (uLightMode == 3) {
        // Dramatic rear rim light tracing the dish lip and gold ridge
        lKey    = safeNorm(vec3(0.25, 1.45, -3.6) - 0.12 * p);
        keyCol  = vec3(2.05, 1.74, 1.36);
        lFill   = safeNorm(vec3(-2.1, 2.1, 2.6));
        fillCol = vec3(0.28, 0.35, 0.44);
        lRim    = safeNorm(vec3(2.4, 1.5, -1.8));
        rimCol  = vec3(0.84, 0.76, 0.64);
    } else {
        // Studio hero lighting
        lKey    = safeNorm(vec3(-2.2, 3.4, 2.1) - 0.16 * p);
        keyCol  = vec3(1.12, 1.06, 0.98);
        lFill   = safeNorm(vec3(2.7, 1.8, 1.2) - 0.12 * p);
        fillCol = vec3(0.24, 0.29, 0.36);
        lRim    = safeNorm(vec3(0.65, 1.9, -3.1));
        rimCol  = vec3(0.52, 0.56, 0.64);
    }
}

// ---------- Tier-3 fwidth-Filtered Micro-Detail ----------
// Glaze vitrified ripples & fine iron-oxide oil-spot dimples
float glazeMicroHeight(vec3 q, float fw) {
    float r = length(q.xz);
    float f1 = 1.0 - smoothstep(0.15, 0.85, fw * 110.0);
    float wheel = sin(r * 135.0 + 1.8 * vnoise2(q.xz * 12.0)) * 0.42 * f1;
    float f2 = 1.0 - smoothstep(0.12, 0.72, fw * 210.0);
    float ripple = (vnoise2(q.xz * 48.0 + vec2(q.y * 22.0, -3.4)) - 0.5) * 0.55 * f2;
    return wheel + ripple;
}

vec3 perturbGlazeNormal(vec3 q, vec3 n, float rayT, float focal) {
    float fwRaw = min(length(fwidth(q.xz)), 0.06);
    float fw = max(fwRaw, rayT / max(focal * min(uResolution.x, uResolution.y), 1.0));
    float eps = max(0.0014, fw * 0.65);

    float h0 = glazeMicroHeight(q, fw);
    float hx = glazeMicroHeight(q + vec3(eps, 0.0, 0.0), fw);
    float hz = glazeMicroHeight(q + vec3(0.0, 0.0, eps), fw);

    vec3 grad = vec3((hx - h0) / eps, 0.0, (hz - h0) / eps) * 0.00055;
    grad -= n * dot(grad, n);
    return safeNorm(n - grad);
}

// Local tangent along the fracture seam for anisotropic gold-brush luster
void seamFrame(vec3 q, int idx, vec3 n, out vec3 tSeam, out vec3 bSeam) {
    const float e = 0.003;
    float gx = shardCellSDF(q + vec3(e, 0.0, 0.0), idx) - shardCellSDF(q - vec3(e, 0.0, 0.0), idx);
    float gz = shardCellSDF(q + vec3(0.0, 0.0, e), idx) - shardCellSDF(q - vec3(0.0, 0.0, e), idx);
    vec3 across = safeNorm(vec3(gx, 0.0, gz));
    // Project across-seam vector onto surface tangent plane
    bSeam = across - n * dot(across, n);
    if (dot(bSeam, bSeam) < 1e-6) {
        bSeam = safeNorm(cross(n, abs(n.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0)));
    } else {
        bSeam = safeNorm(bSeam);
    }
    tSeam = safeNorm(cross(n, bSeam));
}

// ---------- Object Shading ----------
vec3 shadeObject(vec3 p, vec3 n, vec3 rd, HitInfo hit, float rayT, float focal) {
    vec3 v = -rd;
    vec3 lKey, keyCol, lFill, fillCol, lRim, rimCol;
    getLights(p, lKey, keyCol, lFill, fillCol, lRim, rimCol);

    float shKey = calcSoftShadow(p + n * 0.004, lKey, 2.8);
    float ao = calcAO(p, n);

    vec3 q = hit.q;
    float fwRaw = min(length(fwidth(q.xz)), 0.06);
    float fw = max(fwRaw, rayT / max(focal * min(uResolution.x, uResolution.y), 1.0));

    vec3 albedo = vec3(0.04);
    float roughBase = 0.25;
    float roughCoat = 0.06;
    float coatWeight = 0.0;
    float metal = 0.0;
    float f0 = 0.045;
    bool isAnisoGold = false;
    vec3 tSeam = vec3(1.0, 0.0, 0.0);
    vec3 bSeam = vec3(0.0, 0.0, 1.0);
    float roughT = 0.16;
    float roughB = 0.34;
    vec3 Ns = n;

    if (hit.matId == MAT_GLAZE) {
        Ns = perturbGlazeNormal(q, n, rayT, focal);

        float r = length(q.xz);
        // Tenmoku / Kuro-Raku dark iron-glazed stoneware:
        // Deep charcoal-obsidian body breaking to warm iron-rust brown near rim & ridges
        float rimBreak = smoothstep(0.68, 0.90, r) * smoothstep(0.24, 0.36, q.y);
        float glazeVar = fbm2(q.xz * 7.5 + vec2(2.1, -4.3));
        float oilSpots = smoothstep(0.70, 0.88, vnoise2(q.xz * 34.0 + vec2(q.y * 12.0, 5.1)));
        float oilFilt = 1.0 - smoothstep(0.18, 0.90, fw * 140.0);

        vec3 darkGlaze = mix(vec3(0.022, 0.021, 0.023), vec3(0.048, 0.036, 0.028), glazeVar);
        vec3 rustBreak = vec3(0.145, 0.068, 0.032);
        vec3 oilSpotCol = vec3(0.11, 0.085, 0.065);

        albedo = mix(darkGlaze, rustBreak, 0.65 * rimBreak * (0.55 + 0.45 * glazeVar));
        albedo = mix(albedo, oilSpotCol, 0.35 * oilSpots * oilFilt);

        // Subtle warm charcoal soot-stain right beside the kintsugi seam
        float nearSeam = exp(-sq(max(-hit.dCell - hit.wSeam, 0.0) / 0.025));
        albedo *= mix(1.0, 0.82, nearSeam * 0.5);

        roughBase = mix(0.20, 0.28, rimBreak);
        roughCoat = 0.058;
        coatWeight = 0.88;
        f0 = 0.048; // silicate ceramic glaze IOR ~ 1.56
    } else if (hit.matId == MAT_FOOT) {
        // Unglazed iron-rich raw stoneware clay foot ring (kodai)
        float grain = fbm2(q.xz * 32.0 + vec2(q.y * 40.0, 1.3));
        float grog = smoothstep(0.76, 0.92, vnoise2(q.xz * 95.0));
        float grogFilt = 1.0 - smoothstep(0.15, 0.85, fw * 240.0);
        albedo = mix(vec3(0.19, 0.145, 0.115), vec3(0.28, 0.22, 0.175), grain);
        albedo = mix(albedo, vec3(0.08, 0.06, 0.05), 0.45 * grog * grogFilt);
        roughBase = 0.76;
        f0 = 0.032;
    } else if (hit.matId == MAT_FRACTURE) {
        // Exposed stoneware fracture wall revealed as uOpen increases:
        // Granular unglazed stoneware core, thin dark glaze edge, and red-urushi primer trace
        float fNoise = fbm2(vec2(q.x + q.z, q.y) * 44.0);
        float grog = vnoise2(vec2(q.x - q.z, q.y) * 140.0);
        float grogFilt = 1.0 - smoothstep(0.12, 0.78, fw * 260.0);
        vec3 clayCore = mix(vec3(0.21, 0.165, 0.135), vec3(0.32, 0.26, 0.21), fNoise);
        clayCore *= mix(0.88, 1.12, grog * grogFilt + 0.5 * (1.0 - grogFilt));

        // Thin dark vitrified glaze crust along outer & inner edges of the fracture wall
        float edgeDist = abs(hit.dDish);
        float glazeCrust = smoothstep(0.007, 0.001, edgeDist);
        albedo = mix(clayCore, vec3(0.045, 0.035, 0.030), 0.75 * glazeCrust);

        // Traces of deep vermilion-red urushi lacquer bonding primer near the bevel shoulder
        float primer = smoothstep(0.011, 0.002, edgeDist) * smoothstep(0.38, 0.72, fbm2(q.xz * 26.0 + q.y * 18.0));
        albedo = mix(albedo, vec3(0.42, 0.055, 0.030), 0.55 * primer);

        // Micro-rough normal perturbation for fractured stoneware grit
        float eps = max(0.0015, fw * 0.7);
        float gx = vnoise2(vec2(q.x + eps + q.z, q.y) * 90.0) - vnoise2(vec2(q.x - eps + q.z, q.y) * 90.0);
        float gy = vnoise2(vec2(q.x + q.z, q.y + eps) * 90.0) - vnoise2(vec2(q.x + q.z, q.y - eps) * 90.0);
        vec3 gritG = vec3(gx, gy, gx * 0.5) * 0.16 * grogFilt;
        gritG -= n * dot(gritG, n);
        Ns = safeNorm(n - gritG);

        roughBase = mix(0.78, 0.24, primer * 0.6 + glazeCrust * 0.4);
        coatWeight = 0.25 * primer;
        roughCoat = 0.12;
        f0 = 0.036;
    } else {
        // MAT_GOLD: Proud urushi lacquer seam dusted with anisotropic 24k gold powder
        // plus tiny vermilion red-lacquer (bengara urushi) undercoat peeks along the bead edge!
        seamFrame(q, hit.shardIdx, n, tSeam, bSeam);

        // Coordinate along seam & across seam
        float edgeFrac = sat((-hit.dCell) / max(hit.wSeam, 0.004));
        float brushNoise = fbm2(vec2(dot(q, tSeam) * 24.0, dot(q, bSeam) * 65.0));
        float streak = vnoise2(vec2(dot(q, tSeam) * 14.0, dot(q, bSeam) * 110.0));

        // Tiny red-lacquer (bengara urushi) undercoat peeks right along the bead shoulder
        // and occasional hand-dusted micro-thinnings
        float shoulder = smoothstep(0.68, 0.96, edgeFrac) + smoothstep(0.14, 0.0, edgeFrac) * 0.55;
        float redPeek = smoothstep(0.46, 0.76, shoulder * (0.65 + 0.55 * brushNoise));
        // Also reveal red urushi undercoat slightly on the recessed underside of the joint when uOpen > 0
        float jointCut = smoothstep(-0.002, -0.010, hit.dDish) * sat(uOpen * 1.4);
        redPeek = sat(max(redPeek * 0.85, jointCut * 0.75));

        // fwidth-filtered gold powder micro-glints (crystalline makie gold grains)
        float glintFilt = 1.0 - smoothstep(0.10, 0.70, fw * 190.0);
        vec2 gCell = floor(vec2(dot(q, tSeam), dot(q, bSeam)) * 220.0);
        float gHash1 = hash21(gCell);
        float gHash2 = hash21(gCell + vec2(17.3, 41.9));
        float sparkle = smoothstep(0.78, 0.98, gHash1) * glintFilt;

        // Subtle brush-stroke + gold-powder micro-normal perturbation
        vec3 brushPerturb = bSeam * ((streak - 0.5) * 0.09 + (gHash2 - 0.5) * 0.14 * glintFilt)
                          + tSeam * ((gHash1 - 0.5) * 0.08 * glintFilt);
        Ns = safeNorm(n + brushPerturb * (1.0 - 0.7 * redPeek));

        // Rich 24k burnished gold powder color vs deep vermilion-crimson urushi lacquer
        vec3 goldCol = mix(vec3(0.88, 0.67, 0.22), vec3(0.99, 0.83, 0.36), 0.45 + 0.35 * brushNoise + 0.20 * sparkle);
        vec3 redUrushi = vec3(0.46, 0.052, 0.026);

        albedo = mix(goldCol, redUrushi, redPeek);
        metal = mix(0.95, 0.0, redPeek);
        roughBase = mix(0.22, 0.14, redPeek);
        roughT = mix(0.13, 0.14, redPeek); // smooth along brush stroke
        roughB = mix(0.34, 0.16, redPeek); // broader specular spread across brush stroke
        coatWeight = mix(0.18, 0.72, redPeek); // glossy urushi clearcoat on red peeks
        roughCoat = 0.07;
        f0 = mix(0.88, 0.046, redPeek);
        isAnisoGold = (redPeek < 0.65);
    }

    // Neutral-light grayscale mode override
    if (uLightMode == 1) {
        float lum = dot(albedo, vec3(0.299, 0.587, 0.114));
        if (hit.matId == MAT_GOLD) {
            albedo = vec3(mix(0.48, 0.68, sat(lum)));
            metal *= 0.45;
        } else {
            albedo = vec3(mix(0.16, 0.38, sat(lum * 1.4)));
            metal *= 0.30;
        }
    }

    // Direct diffuse lighting
    float ndlKey  = max(dot(Ns, lKey), 0.0);
    float ndlFill = max(dot(Ns, lFill), 0.0);
    float ndlRim  = max(dot(Ns, lRim), 0.0);

    vec3 diff = albedo * (1.0 - 0.88 * metal) * (
        keyCol * (ndlKey * shKey) +
        fillCol * (0.42 + 0.58 * ndlFill) +
        rimCol * (0.45 * ndlRim) +
        vec3(0.065, 0.072, 0.082) * (0.50 + 0.50 * Ns.y)
    );

    // Specular & studio softbox environment reflections
    float cosI = sat(dot(Ns, v));
    vec3 F0vec = mix(vec3(f0), albedo, metal);
    vec3 F = F0vec + (vec3(1.0) - F0vec) * safePow(1.0 - cosI, 5.0);
    float Fcoat = 0.048 + 0.952 * safePow(1.0 - cosI, 5.0);

    vec3 refl = reflect(rd, Ns);
    // Slight anisotropic stretch of environment lookup along brush direction for gold
    vec3 envReflDir = refl;
    if (isAnisoGold) {
        vec3 anisoN = safeNorm(Ns + bSeam * (dot(v, bSeam) * 0.22));
        envReflDir = reflect(rd, anisoN);
    }
    vec3 envBase = studioEnv(envReflDir, p, roughBase);
    vec3 envCoat = studioEnv(refl, p, roughCoat);

    // Self-occlusion of metallic reflections inside dish bowl
    if (metal > 0.35) {
        float selfOcc = 1.0;
        for (int j = 1; j <= 3; j++) {
            float s = 0.06 * float(j * j);
            selfOcc *= mix(0.68, 1.0, smoothstep(0.002, 0.38, mapDist(p + Ns * 0.006 + refl * s) / s));
        }
        envBase *= selfOcc;
    }

    float specKey, specFill, specRim;
    if (isAnisoGold) {
        specKey  = ggxAnisoSpec(Ns, tSeam, bSeam, v, lKey, roughT, roughB);
        specFill = ggxAnisoSpec(Ns, tSeam, bSeam, v, lFill, roughT, roughB);
        specRim  = ggxAnisoSpec(Ns, tSeam, bSeam, v, lRim, roughT, roughB);
    } else {
        specKey  = ggxSpec(Ns, v, lKey, roughBase);
        specFill = ggxSpec(Ns, v, lFill, roughBase);
        specRim  = ggxSpec(Ns, v, lRim, roughBase);
    }

    float specCoatKey = ggxSpec(Ns, v, lKey, roughCoat);
    float specCoatRim = ggxSpec(Ns, v, lRim, roughCoat);

    vec3 specDirect = F * (keyCol * specKey * shKey + fillCol * specFill * 0.55 + rimCol * specRim * 0.70)
                    + coatWeight * (0.28 + 0.72 * Fcoat) * (keyCol * specCoatKey * shKey + rimCol * specCoatRim * 0.80);
    vec3 specEnv = (F * envBase * (0.65 + 0.35 * metal) + coatWeight * Fcoat * envCoat * 0.88) * (0.55 + 0.45 * shKey);

    vec3 col = ao * diff + specDirect + specEnv * ao;

    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.299, 0.587, 0.114)));
    }
    return max(col, vec3(0.0));
}

// ---------- Grounded Studio Bench at y = 0 ----------
vec3 shadeBench(vec3 p, vec3 rd) {
    vec3 lKey, keyCol, lFill, fillCol, lRim, rimCol;
    getLights(p, lKey, keyCol, lFill, fillCol, lRim, rimCol);

    float grain = hash31(p * 230.0) - 0.5;
    float slateVar = fbm2(p.xz * 2.2) - 0.5;
    vec3 base = (uLightMode == 1) ? vec3(0.23) : vec3(0.18, 0.185, 0.195);
    base *= (1.0 + 0.025 * grain + 0.045 * slateVar);

    float sh = benchContactShadow(p, lKey);
    float pool = 0.38 + 0.70 * exp(-0.15 * dot(p.xz - vec2(-0.15, 0.12), p.xz - vec2(-0.15, 0.12)));
    float ndl = max(lKey.y, 0.08);
    vec3 diff = base * (keyCol * ndl * (0.24 + 0.76 * sh) + fillCol * 0.32 + vec3(0.10)) * pool;

    vec3 refl = reflect(rd, vec3(0.0, 1.0, 0.0));
    float F = 0.024 + 0.976 * safePow(1.0 - sat(-rd.y), 5.0);
    vec3 env = studioEnv(refl, p, 0.62) * F * 0.36 * sh;

    vec3 col = diff + env;
    float fog = smoothstep(3.4, 11.5, length(p.xz));
    vec3 bg = (uLightMode == 1) ? vec3(0.10) : vec3(0.065, 0.072, 0.084);
    col = mix(col, bg, fog);

    if (uLightMode == 1) {
        col = vec3(dot(col, vec3(0.299, 0.587, 0.114)));
    }
    return max(col, vec3(0.0));
}

// ---------- Camera & Render ----------
void getCamera(out vec3 eye, out vec3 target, out float focal) {
    float drift = (uTime > 0.0) ? (0.045 * sin(uTime * 0.42)) : 0.0;
    if (uCamMode == 1) {
        // ~2.5x macro close-up on central kintsugi triple-junction, gold-powder luster,
        // red-lacquer peeks, and beveled fracture wall
        eye    = vec3(0.32 + drift * 0.4, 0.88, 0.76);
        target = vec3(-0.02, 0.10, -0.02);
        focal  = 4.10;
    } else {
        // Hero 3/4 elevated studio view showing full fractured dish & gold seam network
        eye    = vec3(0.88 + drift, 1.66, 1.98);
        target = vec3(0.0, 0.13, -0.02);
        focal  = 2.68;
    }
}

vec3 renderSample(vec2 offset) {
    vec3 eye, target;
    float focal;
    getCamera(eye, target, focal);

    vec3 fwd = safeNorm(target - eye);
    vec3 right = safeNorm(cross(fwd, vec3(0.0, 1.0, 0.0)));
    vec3 up = cross(right, fwd);

    vec2 res = max(uResolution, vec2(1.0));
    vec2 uv = (2.0 * (gl_FragCoord.xy + offset) - res) / max(1.0, min(res.y, res.x * 0.75));
    vec3 rd = safeNorm(focal * fwd + uv.x * right + uv.y * up);

    float benchT = (rd.y < -0.001) ? min(FAR, -eye.y / rd.y) : FAR;
    float hitT = traceScene(eye, rd, benchT);

    if (hitT > 0.0) {
        vec3 p = eye + rd * hitT;
        vec3 n = calcNormal(p);
        HitInfo hit = classifyHit(p);
        return shadeObject(p, n, rd, hit, hitT, focal);
    }

    if (benchT < FAR) {
        vec3 p = eye + rd * benchT;
        return shadeBench(p, rd);
    }

    vec3 bgLow  = (uLightMode == 1) ? vec3(0.10) : vec3(0.065, 0.072, 0.084);
    vec3 bgHigh = (uLightMode == 1) ? vec3(0.05) : vec3(0.032, 0.038, 0.048);
    return mix(bgLow, bgHigh, smoothstep(0.0, 0.45, rd.y));
}

// ACES Filmic Tone Mapping
vec3 acesToneMap(vec3 x) {
    x = max(x, vec3(0.0));
    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(vec3(
        (x.r * (a * x.r + b)) / max(x.r * (c * x.r + d) + e, 1e-5),
        (x.g * (a * x.g + b)) / max(x.g * (c * x.g + d) + e, 1e-5),
        (x.b * (a * x.b + b)) / max(x.b * (c * x.b + d) + e, 1e-5)
    ), 0.0, 1.0);
}

void main() {
    // 2x rotated-grid supersampling
    vec3 c0 = renderSample(vec2(-0.24,  0.18));
    vec3 c1 = renderSample(vec2( 0.24, -0.18));
    vec3 lin = 0.5 * (c0 + c1);

    // Subtle studio vignette
    vec2 q = gl_FragCoord.xy / max(uResolution, vec2(1.0)) - 0.5;
    lin *= (1.0 - 0.16 * dot(q, q));

    vec3 mapped = acesToneMap(lin * 0.90);
    vec3 srgb = pow(max(mapped, vec3(0.0)), vec3(1.0 / 2.2));
    fragColor = vec4(clamp(srgb, 0.0, 1.0), 1.0);
}

Mathematical models & shader specifications

Identifierkintsugi-gold-fracture-veins
CategorySci-Fi, Plasma & Noise Shaders · Cellular & Ripple Fields
Upstream studyceramics/lib/03_kintsugi_gold_lacquer_seam.glsl (commit 4750467)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size829 lines · 32.0 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT holy 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); Schlick Fresnel reflectance (1994); 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 sci-fi & noise shaders
OLED Subpixel Matrix WebGL2 procedural shaderAnimated

Diamond PenTile RGB emitter array for close-up cybernetic visors, HUD screens, and holo-panels.

Sci-Fi & Noise