// Tools for building the city's geometry and its materials: a triangle builder with per-face // colors and flat normals, box and prism helpers, the building material that paints facades // (brick, lap siding, stone, glass, framed windows, weathering) and the surface material that // paints streets (asphalt, sidewalk slabs, pavers, planks, worn paint). The detail is drawn per // pixel from world position, so there are no texture files, and it fades with distance so it // never shimmers. import * as THREE from "three"; const tmpColor = new THREE.Color(); /** Collects flat-shaded triangles with per-vertex colors into one BufferGeometry. Give it a * `surfOf(color)` and each triangle also carries a surface kind for the surface material. */ export class Builder { constructor(surfOf = null) { this.pos = []; this.col = []; this.nrm = []; this.surf = []; this.surfOf = surfOf; } tri(a, b, c, color) { tmpColor.set(color); const ux = b[0] - a[0], uy = b[1] - a[1], uz = b[2] - a[2]; const vx = c[0] - a[0], vy = c[1] - a[1], vz = c[2] - a[2]; let nx = uy * vz - uz * vy; let ny = uz * vx - ux * vz; let nz = ux * vy - uy * vx; const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l; const s = this.surfOf ? this.surfOf(color) : 0; for (const p of [a, b, c]) { this.pos.push(p[0], p[1], p[2]); this.col.push(tmpColor.r, tmpColor.g, tmpColor.b); this.nrm.push(nx, ny, nz); this.surf.push(s); } } /** A quad a-b-c-d, counter-clockwise seen from the side it faces. */ quad(a, b, c, d, color) { this.tri(a, b, c, color); this.tri(a, c, d, color); } /** An axis-aligned box from (x0, y0, z0) to (x1, y1, z1); `bottom` false skips the underside. */ box(x0, y0, z0, x1, y1, z1, color, top = color, bottom = false) { this.quad([x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0], top); this.quad([x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1], color); this.quad([x1, y0, z0], [x0, y0, z0], [x0, y1, z0], [x1, y1, z0], color); this.quad([x1, y0, z1], [x1, y0, z0], [x1, y1, z0], [x1, y1, z1], color); this.quad([x0, y0, z0], [x0, y0, z1], [x0, y1, z1], [x0, y1, z0], color); if (bottom) this.quad([x0, y0, z0], [x1, y0, z0], [x1, y0, z1], [x0, y0, z1], color); } get count() { return this.pos.length / 3; } geometry() { const g = new THREE.BufferGeometry(); g.setAttribute("position", new THREE.Float32BufferAttribute(this.pos, 3)); g.setAttribute("normal", new THREE.Float32BufferAttribute(this.nrm, 3)); g.setAttribute("color", new THREE.Float32BufferAttribute(this.col, 3)); if (this.surfOf) g.setAttribute("aSurf", new THREE.Float32BufferAttribute(this.surf, 1)); g.computeBoundingSphere(); return g; } } /** A box geometry with its base at y = 0, for instancing with scale. */ export function unitBox() { const g = new THREE.BoxGeometry(1, 1, 1); g.translate(0, 0.5, 0); return g; } /** A bay window: wide against the wall (z = 0), narrower at the front (z = 1). Base at y = 0. */ export function bayGeometry() { const b = new Builder(); const back = 0.5; const front = 0.3; const pts = [[-back, 0], [-front, 1], [front, 1], [back, 0]]; for (let k = 0; k < 3; k++) { const [x0, z0] = pts[k]; const [x1, z1] = pts[k + 1]; b.quad([x0, 0, z0], [x1, 0, z1], [x1, 1, z1], [x0, 1, z0], 0xffffff); } b.quad([-back, 1, 0], [-front, 1, 1], [front, 1, 1], [back, 1, 0], 0xffffff); b.quad([-back, 0, 0], [back, 0, 0], [front, 0, 1], [-front, 0, 1], 0xffffff); const g = b.geometry(); g.deleteAttribute("color"); return g; } export function standard(color, o = {}) { return new THREE.MeshStandardMaterial({ color, roughness: o.roughness ?? 0.9, metalness: o.metalness ?? 0, flatShading: o.flat ?? true, ...o.extra }); } export function vertexColored(o = {}) { return new THREE.MeshStandardMaterial({ vertexColors: true, roughness: o.roughness ?? 0.92, metalness: 0, ...o.extra }); } // Shared GLSL: hashes and value noise. const NOISE = /* glsl */ ` float cHash(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); } // Box-filtered periodic band: the share of [x - w/2, x + w/2] where fract(x) is within hw of 0.5. float cBandI(float x, float hw) { return floor(x) * 2.0 * hw + clamp(fract(x) - (0.5 - hw), 0.0, 2.0 * hw); } float cBand(float x, float hw, float w) { w = max(w, 1e-4); return (cBandI(x + 0.5 * w, hw) - cBandI(x - 0.5 * w, hw)) / w; } float cNoise(vec2 p) { vec2 i = floor(p); vec2 f = fract(p); f = f * f * (3.0 - 2.0 * f); return mix(mix(cHash(i), cHash(i + vec2(1.0, 0.0)), f.x), mix(cHash(i + vec2(0.0, 1.0)), cHash(i + vec2(1.0, 1.0)), f.x), f.y); } `; // Facade styles, carried per instance in aStyle.x. export const STYLE = { plain: 0, brick: 1, siding: 2, stone: 3, glass: 4 }; const FACADE_VERTEX_HEAD = /* glsl */ ` attribute vec4 aWin; attribute vec4 aBox; attribute vec2 aStyle; varying vec3 vWPos; varying vec3 vWNrm; varying vec4 vWin; varying vec4 vBox; varying vec2 vStyle; `; const FACADE_VERTEX_BODY = /* glsl */ ` { vec4 wpos = vec4(transformed, 1.0); vec3 wnrm = objectNormal; #ifdef USE_INSTANCING wpos = instanceMatrix * wpos; wnrm = mat3(instanceMatrix) * wnrm; #endif wpos = modelMatrix * wpos; vWPos = wpos.xyz; vWNrm = normalize(mat3(modelMatrix) * wnrm); vWin = aWin; vBox = aBox; vStyle = aStyle; } `; const FACADE_FRAGMENT_HEAD = /* glsl */ ` varying vec3 vWPos; varying vec3 vWNrm; varying vec4 vWin; varying vec4 vBox; varying vec2 vStyle; float bGlass = 0.0; float bLit = 0.0; float bRough = -1.0; ${NOISE} `; const FACADE_FRAGMENT_COLOR = /* glsl */ ` { vec3 wn = normalize(vWNrm); float seed = fract(vBox.w); float shop = step(1.5, vBox.w); float style = vStyle.x; float dist = length(vWPos - cameraPosition); float detail = 1.0 - smoothstep(30.0, 110.0, dist); float mid = 1.0 - smoothstep(120.0, 420.0, dist); float v = vWPos.y - vBox.x; float top = vBox.y - vBox.x; vec3 base = diffuseColor.rgb; if (abs(wn.y) < 0.45) { vec2 tg = normalize(vec2(-wn.z, wn.x)); float u = dot(vWPos.xz, tg) + seed * 37.0; // weathering: soft blotches, streaks down from the roof, grime at the foot float n = cNoise(vec2(u * 0.3, v * 0.3) + seed * 9.0) * 0.6 + cNoise(vec2(u * 1.6, v * 0.5)) * 0.4; base *= 0.9 + 0.15 * n * mid; float streak = cNoise(vec2(u * 2.3, seed * 7.0)) * smoothstep(top - 8.0, top, v); base *= 1.0 - 0.08 * streak * mid; base *= mix(0.72, 1.0, smoothstep(-0.4, 3.4, v)); bRough = 0.88; if (style > 0.5 && style < 1.5) { // brick in running bond, with pale mortar float row = floor(v / 0.077); float bu = u / 0.225 + mod(row, 2.0) * 0.5; float mortar = max(step(0.85, fract(v / 0.077)), step(0.93, fract(bu))); float tone = cHash(vec2(floor(bu), row) + seed); vec3 brick = base * (0.84 + 0.28 * tone); base = mix(base * (0.95 + 0.1 * tone * mid), mix(brick, vec3(0.62, 0.58, 0.53), mortar * 0.8), detail); } else if (style > 1.5 && style < 2.5) { // painted lap siding: a shadow under the lip of every board, corner boards in trim float b = fract(v / 0.18); float lap = mix(0.74, 1.04, smoothstep(0.0, 0.22, b)); base *= mix(0.97, lap, detail); bRough = 0.6; } else if (style > 2.5 && style < 3.5) { // stone: rusticated courses at street level, fine ashlar above float low = step(v, vBox.z); float course = mix(0.78, 0.56, low); float g = fract(v / course); float joint = step(0.93, g) * mix(0.45, 1.0, low) + step(0.975, fract(u / 1.5 + floor(v / course) * 0.5)) * (1.0 - low) * 0.4; base *= 1.0 - joint * 0.24 * detail; base *= 0.97 + 0.06 * cHash(vec2(floor(u / 1.5 + floor(v / course) * 0.5), floor(v / course))) * detail; } diffuseColor.rgb = base; vec3 wallTone = base; if (vWin.x > 0.0) { float gf = vBox.z; vec2 c = vec2(u / vWin.y, (v - gf) / vWin.x); vec2 cell = floor(c); vec2 f = fract(c); vec2 hw = vec2(vWin.z, vWin.w) * 0.5; vec2 d = abs(f - 0.5); float band = step(gf + 0.15, v) * step(v, top - 0.85); // antialiased: the share of this pixel that is glass, so grids never shimmer vec2 fwc = fwidth(c); float glass = cBand(c.x, hw.x, fwc.x) * cBand(c.y, hw.y, fwc.y); float fw = style > 3.5 ? 0.035 : 0.075; float frame = (step(d.x, hw.x + fw * 0.5) * step(d.y, hw.y + fw * 0.6)) * (1.0 - glass); float sill = step(d.x, hw.x + fw * 0.9) * step(abs(f.y - (0.5 - hw.y - fw * 0.6 - 0.02)), 0.022); vec3 trim = style > 3.5 ? vec3(0.15, 0.17, 0.2) : (style > 2.5 ? base * 0.78 : vec3(0.92, 0.9, 0.85)); float frameMask = max(frame, sill * (style > 3.5 ? 0.0 : 1.0)) * band; diffuseColor.rgb = mix(diffuseColor.rgb, trim, frameMask * (0.35 + 0.65 * detail)); // glass, sometimes behind blinds, a few lit from inside float r = cHash(cell + seed * 113.0) * detail + 0.25 * (1.0 - detail); float yIn = (f.y - (0.5 - hw.y)) / (2.0 * hw.y); float blind = step(0.5, r) * step(1.0 - cHash(cell * 1.3 + seed) * 0.8, yIn); vec3 pane = mix(vec3(0.06, 0.08, 0.11), vec3(0.17, 0.21, 0.26), mix(0.45, cHash(cell * 1.7 + seed), mid)); vec3 shade = mix(vec3(0.86, 0.8, 0.7), vec3(0.74, 0.77, 0.8), cHash(cell * 2.1)); vec3 glassColor = mix(pane, shade, blind); float win = glass * band; diffuseColor.rgb = mix(diffuseColor.rgb, glassColor, win); bGlass = win * (1.0 - blind); bLit = win * (1.0 - blind) * step(0.965, cHash(cell + seed * 7.0)) * 0.5 * mid; // storefronts: big glass at street level, a colored sign band above if (shop > 0.5 && v < gf) { float sb = step(gf - 0.75, v) * step(v, gf - 0.12); float sw = step(0.5, v) * step(v, gf - 0.95) * step(0.05, fract(u / 3.2)) * step(0.04, abs(fract(u / 1.6) - 0.5) * 2.0); vec3 sign = vec3(0.12, 0.25, 0.2); float sr = cHash(vec2(seed * 31.0, 1.0)); sign = sr < 0.25 ? vec3(0.42, 0.1, 0.12) : sr < 0.5 ? vec3(0.08, 0.12, 0.25) : sr < 0.75 ? vec3(0.1, 0.1, 0.1) : sign; diffuseColor.rgb = mix(diffuseColor.rgb, sign, sb); diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.1, 0.12, 0.13), sw); bGlass = max(bGlass, sw); bLit = max(bLit, sw * 0.1 * mid); } } } else if (wn.y > 0.6) { // roofs: tar and gravel, a little lighter at the parapet float n = cNoise(vWPos.xz * 0.5) * 0.5 + cNoise(vWPos.xz * 3.0) * 0.5; diffuseColor.rgb = mix(base, vec3(0.42, 0.42, 0.41), 0.55) * (0.85 + 0.25 * n); bRough = 0.95; } } `; /** The facade material: any box gets floors, framed windows, blinds, storefronts and a wall * finish without a texture. Instances carry aWin = (story height, column spacing, window * width, window height), aBox = (facade base y, roof y, ground-floor height, seed + 2 for a * storefront) and aStyle = (STYLE, unused). */ export function buildingMaterial(o = {}) { const m = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: o.roughness ?? 0.88, metalness: 0, flatShading: true }); m.onBeforeCompile = (shader) => { shader.vertexShader = shader.vertexShader .replace("#include ", `#include \n${FACADE_VERTEX_HEAD}`) .replace("#include ", `#include \n${FACADE_VERTEX_BODY}`); shader.fragmentShader = shader.fragmentShader .replace("#include ", `#include \n${FACADE_FRAGMENT_HEAD}`) .replace("#include ", `#include \n${FACADE_FRAGMENT_COLOR}`) .replace("#include ", "#include \nif (bRough >= 0.0) roughnessFactor = bRough;\nroughnessFactor = mix(roughnessFactor, 0.06, bGlass);") .replace("#include ", "#include \ntotalEmissiveRadiance += vec3(1.0, 0.72, 0.42) * bLit;"); }; m.customProgramCacheKey = () => "claudia-facade"; return m; } /** Facade attributes for a mesh that is not instanced: the same values on every vertex. */ export function windowAttributes(geometry, win, box, style = STYLE.plain) { const n = geometry.attributes.position.count; const a = new Float32Array(n * 4); const b = new Float32Array(n * 4); const s = new Float32Array(n * 2); for (let i = 0; i < n; i++) { a.set(win, i * 4); b.set(box, i * 4); s[i * 2] = style; } geometry.setAttribute("aWin", new THREE.BufferAttribute(a, 4)); geometry.setAttribute("aBox", new THREE.BufferAttribute(b, 4)); geometry.setAttribute("aStyle", new THREE.BufferAttribute(s, 2)); return geometry; } // Surface kinds for the street material. export const SURF = { none: 0, asphalt: 1, slab: 2, pavers: 3, grass: 4, concrete: 5, planks: 6, track: 7, paint: 8 }; const SURFACE_COLOR = /* glsl */ ` { float s = vSurf; vec2 p = vSPos.xz; float dist = length(vSPos - cameraPosition); float detail = 1.0 - smoothstep(22.0, 85.0, dist); float mid = 1.0 - smoothstep(90.0, 380.0, dist); vec3 c = diffuseColor.rgb; float n1 = cNoise(p * 0.07); float n2 = cNoise(p * 0.55); float grain = cHash(floor(p * 22.0)) - 0.5; if (s > 0.5 && s < 1.5) { // asphalt: big soft blotches, repair patches, fine grain, hairline cracks c *= 0.86 + 0.22 * n1 * mid; vec2 cp = floor(p / 7.0); vec2 fp = fract(p / 7.0); float pr = cHash(cp); if (pr > 0.7) { float inP = step(0.15, fp.x) * step(fp.x, 0.2 + 0.55 * cHash(cp + 3.0)) * step(0.25, fp.y) * step(fp.y, 0.3 + 0.5 * cHash(cp + 7.0)); c *= mix(1.0, 0.8 + 0.3 * cHash(cp + 11.0), inP * mid); } c *= 1.0 + grain * 0.16 * detail; float crack = 1.0 - smoothstep(0.0, 0.006, abs(cNoise(p * 0.85) - 0.5)); c *= 1.0 - crack * 0.14 * (1.0 - smoothstep(10.0, 30.0, dist)); sRough = 0.96; } else if (s > 1.5 && s < 2.5) { // concrete sidewalk slabs with joints, each slab its own tone vec2 g = fract(p / 1.6); float joint = max(step(g.x, 0.018), step(g.y, 0.018)); c *= 0.93 + 0.11 * cHash(floor(p / 1.6)) * mid; c *= 0.94 + 0.08 * n2 * mid; c *= 1.0 - joint * 0.24 * detail; c *= 1.0 + grain * 0.06 * detail; sRough = 0.9; } else if (s > 2.5 && s < 3.5) { // the promenade's pavers, in running bond float row = floor(p.y / 0.32); float px = p.x / 0.64 + mod(row, 2.0) * 0.5; vec2 cell = vec2(floor(px), row); float grout = max(step(0.93, fract(p.y / 0.32)), step(0.95, fract(px))); c *= 0.88 + 0.2 * cHash(cell) * mid; c = mix(c, c * 0.72, grout * detail); sRough = 0.85; } else if (s > 3.5 && s < 4.5) { // grass: patchy and alive c *= 0.78 + 0.34 * n2 * (0.6 + 0.4 * n1); c *= 1.0 + grain * 0.12 * detail; sRough = 1.0; } else if (s > 4.5 && s < 5.5) { c *= 0.9 + 0.14 * n2 * mid; } else if (s > 5.5 && s < 6.5) { // weathered planks across the pier float plank = floor(p.x / 0.28); float seam = step(0.94, fract(p.x / 0.28)); vec3 wood = vec3(0.47, 0.38, 0.29) * (0.78 + 0.34 * cHash(vec2(plank, floor(p.y / 4.0)))); wood *= 0.9 + 0.15 * cNoise(vec2(p.x * 3.0, p.y * 0.4)); c = mix(c, wood, 0.8); c *= 1.0 - seam * 0.5 * detail; sRough = 0.9; } else if (s > 6.5 && s < 7.5) { c *= 0.88 + 0.16 * n2; } else if (s > 7.5) { // paint, worn where the wheels go float wear = smoothstep(0.45, 0.8, cNoise(p * 1.8)) * (0.5 + 0.5 * cNoise(p * 0.2)); c = mix(c, vec3(0.3, 0.3, 0.32), wear * 0.55 * mid); sRough = 0.7; } diffuseColor.rgb = c; } `; /** The street material: vertex colors set the base, aSurf picks the finish. */ export function surfaceMaterial(o = {}) { const m = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: o.roughness ?? 0.92, metalness: 0, ...o.extra }); m.onBeforeCompile = (shader) => { shader.vertexShader = shader.vertexShader .replace("#include ", "#include \nattribute float aSurf;\nvarying float vSurf;\nvarying vec3 vSPos;") .replace("#include ", "#include \nvSurf = aSurf;\nvSPos = (modelMatrix * vec4(transformed, 1.0)).xyz;"); shader.fragmentShader = shader.fragmentShader .replace("#include ", `#include \nvarying float vSurf;\nvarying vec3 vSPos;\nfloat sRough = -1.0;\n${NOISE}`) .replace("#include ", `#include \n${SURFACE_COLOR}`) .replace("#include ", "#include \nif (sRough >= 0.0) roughnessFactor = sRough;"); }; m.customProgramCacheKey = () => `claudia-surface-${o.key ?? ""}`; return m; } /** A canvas texture with text, for signs and billboards. */ export function textTexture(width, height, draw) { const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; const ctx = canvas.getContext("2d"); draw(ctx, width, height); const t = new THREE.CanvasTexture(canvas); t.colorSpace = THREE.SRGBColorSpace; t.anisotropy = 4; return t; }