// Small geometry toolkit used by the procedural modellers. import * as THREE from 'three' /** Accumulates non-indexed triangles (+ optional per-vertex scalar attributes) for one material. */ export class Builder { pos: number[] = [] nrm: number[] = [] uv: number[] = [] extra: Record = {} current: Record = {} m = new THREE.Matrix4() nm = new THREE.Matrix3() private v = new THREE.Vector3() private n = new THREE.Vector3() setMatrix(m: THREE.Matrix4) { this.m.copy(m) this.nm.getNormalMatrix(m) return this } /** set the value of a custom attribute for subsequently added vertices */ set(name: string, value: number) { this.current[name] = value if (!this.extra[name]) this.extra[name] = new Array(this.pos.length / 3).fill(0) return this } private pushVertex(x: number, y: number, z: number, nx: number, ny: number, nz: number, u: number, w: number) { this.v.set(x, y, z).applyMatrix4(this.m) this.n.set(nx, ny, nz).applyMatrix3(this.nm).normalize() this.pos.push(this.v.x, this.v.y, this.v.z) this.nrm.push(this.n.x, this.n.y, this.n.z) this.uv.push(u, w) for (const k in this.extra) this.extra[k].push(this.current[k] ?? 0) } /** Quad a-b-c-d (counter-clockwise when seen from the front). uvs: 4 pairs */ quad(a: THREE.Vector3Like, b: THREE.Vector3Like, c: THREE.Vector3Like, d: THREE.Vector3Like, uvs?: number[], normal?: THREE.Vector3Like) { let nx: number, ny: number, nz: number if (normal) { nx = normal.x; ny = normal.y; nz = normal.z } else { const e1 = new THREE.Vector3(b.x - a.x, b.y - a.y, b.z - a.z) const e2 = new THREE.Vector3(d.x - a.x, d.y - a.y, d.z - a.z) const n = e1.cross(e2).normalize() nx = n.x; ny = n.y; nz = n.z } const t = uvs || [0, 0, 1, 0, 1, 1, 0, 1] this.pushVertex(a.x, a.y, a.z, nx, ny, nz, t[0], t[1]) this.pushVertex(b.x, b.y, b.z, nx, ny, nz, t[2], t[3]) this.pushVertex(c.x, c.y, c.z, nx, ny, nz, t[4], t[5]) this.pushVertex(a.x, a.y, a.z, nx, ny, nz, t[0], t[1]) this.pushVertex(c.x, c.y, c.z, nx, ny, nz, t[4], t[5]) this.pushVertex(d.x, d.y, d.z, nx, ny, nz, t[6], t[7]) } /** quad whose 4 corners carry a relief height (aRH) and a relief slope across x (aRS) */ quadRelief(a: THREE.Vector3Like, b: THREE.Vector3Like, c: THREE.Vector3Like, d: THREE.Vector3Like, hs: number[], uvs: number[]) { const dx = Math.abs(b.x - a.x) || 1 const slope = (hs[1] - hs[0]) / dx * Math.sign(b.x - a.x || 1) const corners = [a, b, c, a, c, d] const hIdx = [0, 1, 2, 0, 2, 3] const uvIdx = [0, 1, 2, 0, 2, 3] const e1 = new THREE.Vector3(b.x - a.x, b.y - a.y, b.z - a.z) const e2 = new THREE.Vector3(d.x - a.x, d.y - a.y, d.z - a.z) const n = e1.cross(e2).normalize() for (let k = 0; k < 6; k++) { this.current['aRH'] = hs[hIdx[k]] this.current['aRS'] = slope const p = corners[k] this.pushVertexPublic(p.x, p.y, p.z, n.x, n.y, n.z, uvs[uvIdx[k] * 2], uvs[uvIdx[k] * 2 + 1]) } this.current['aRH'] = 0 this.current['aRS'] = 0 } pushVertexPublic(x: number, y: number, z: number, nx: number, ny: number, nz: number, u: number, w: number) { this.pushVertex(x, y, z, nx, ny, nz, u, w) } tri(a: THREE.Vector3Like, b: THREE.Vector3Like, c: THREE.Vector3Like, uvs?: number[]) { const e1 = new THREE.Vector3(b.x - a.x, b.y - a.y, b.z - a.z) const e2 = new THREE.Vector3(c.x - a.x, c.y - a.y, c.z - a.z) const n = e1.cross(e2).normalize() const t = uvs || [0, 0, 1, 0, 0.5, 1] this.pushVertex(a.x, a.y, a.z, n.x, n.y, n.z, t[0], t[1]) this.pushVertex(b.x, b.y, b.z, n.x, n.y, n.z, t[2], t[3]) this.pushVertex(c.x, c.y, c.z, n.x, n.y, n.z, t[4], t[5]) } /** Axis-aligned box in local space. uvScale: metres per texture repeat (world-ish mapping). */ box(cx: number, cy: number, cz: number, sx: number, sy: number, sz: number, uvScale = 1, vMode?: (y: number) => number) { const x0 = cx - sx / 2, x1 = cx + sx / 2, y0 = cy - sy / 2, y1 = cy + sy / 2, z0 = cz - sz / 2, z1 = cz + sz / 2 const s = 1 / uvScale const V = vMode || ((y: number) => y * s) // +z this.quad({ x: x0, y: y0, z: z1 }, { x: x1, y: y0, z: z1 }, { x: x1, y: y1, z: z1 }, { x: x0, y: y1, z: z1 }, [x0 * s, V(y0), x1 * s, V(y0), x1 * s, V(y1), x0 * s, V(y1)]) // -z this.quad({ x: x1, y: y0, z: z0 }, { x: x0, y: y0, z: z0 }, { x: x0, y: y1, z: z0 }, { x: x1, y: y1, z: z0 }, [-x1 * s, V(y0), -x0 * s, V(y0), -x0 * s, V(y1), -x1 * s, V(y1)]) // +x this.quad({ x: x1, y: y0, z: z1 }, { x: x1, y: y0, z: z0 }, { x: x1, y: y1, z: z0 }, { x: x1, y: y1, z: z1 }, [-z1 * s, V(y0), -z0 * s, V(y0), -z0 * s, V(y1), -z1 * s, V(y1)]) // -x this.quad({ x: x0, y: y0, z: z0 }, { x: x0, y: y0, z: z1 }, { x: x0, y: y1, z: z1 }, { x: x0, y: y1, z: z0 }, [z0 * s, V(y0), z1 * s, V(y0), z1 * s, V(y1), z0 * s, V(y1)]) // +y this.quad({ x: x0, y: y1, z: z1 }, { x: x1, y: y1, z: z1 }, { x: x1, y: y1, z: z0 }, { x: x0, y: y1, z: z0 }, [x0 * s, z1 * s, x1 * s, z1 * s, x1 * s, z0 * s, x0 * s, z0 * s]) // -y this.quad({ x: x0, y: y0, z: z0 }, { x: x1, y: y0, z: z0 }, { x: x1, y: y0, z: z1 }, { x: x0, y: y0, z: z1 }, [x0 * s, z0 * s, x1 * s, z0 * s, x1 * s, z1 * s, x0 * s, z1 * s]) } /** Append an arbitrary BufferGeometry transformed by the builder matrix (and an extra local matrix). */ geometry(g: THREE.BufferGeometry, local?: THREE.Matrix4) { const geo = g.index ? g.toNonIndexed() : g const p = geo.getAttribute('position'), n = geo.getAttribute('normal'), t = geo.getAttribute('uv') const saveM = this.m.clone() if (local) this.setMatrix(saveM.clone().multiply(local)) for (let i = 0; i < p.count; i++) { this.pushVertex(p.getX(i), p.getY(i), p.getZ(i), n ? n.getX(i) : 0, n ? n.getY(i) : 1, n ? n.getZ(i) : 0, t ? t.getX(i) : 0, t ? t.getY(i) : 0) } if (local) this.setMatrix(saveM) } get count() { return this.pos.length / 3 } build() { 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('uv', new THREE.Float32BufferAttribute(this.uv, 2)) for (const k in this.extra) g.setAttribute(k, new THREE.Float32BufferAttribute(this.extra[k], 1)) g.computeBoundingSphere() return g } } export const V3 = (x: number, y: number, z: number) => new THREE.Vector3(x, y, z) export function trs(x: number, y: number, z: number, ry = 0, s = 1) { const m = new THREE.Matrix4() m.compose(new THREE.Vector3(x, y, z), new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), ry), new THREE.Vector3(s, s, s)) return m } /** * Chinese roof with concave slopes and upturned corners (飞檐翘角). * corners: eave polygon (local XZ, CCW seen from above) at height eaveY; * tops: matching top points (ridge ends or a single apex repeated). */ export function curvedRoof(b: Builder, corners: THREE.Vector2[], tops: THREE.Vector3[], eaveY: number, curl: number, flare: number, uvScale = 1.2, segU = 8, segV = 8) { const n = corners.length let area = 0 for (let k = 0; k < n; k++) { const a = corners[k], c = corners[(k + 1) % n] area += a.x * c.y - c.x * a.y } if (area > 0) { corners = [...corners].reverse() tops = [...tops].reverse() } const center = new THREE.Vector2() corners.forEach((c) => center.add(c)) center.multiplyScalar(1 / n) for (let k = 0; k < n; k++) { const c0 = corners[k], c1 = corners[(k + 1) % n] const t0 = tops[k], t1 = tops[(k + 1) % n] const P = (u: number, v: number) => { const bx = c0.x + (c1.x - c0.x) * u, bz = c0.y + (c1.y - c0.y) * u const tx = t0.x + (t1.x - t0.x) * u, tz = t0.z + (t1.z - t0.z) * u, ty = t0.y + (t1.y - t0.y) * u const x = bx + (tx - bx) * v, z = bz + (tz - bz) * v const rise = ty - eaveY let y = eaveY + rise * Math.pow(v, 1.6) const cf = Math.pow(Math.abs(2 * u - 1), 4) const lift = cf * Math.pow(1 - v, 2.5) y += curl * lift // flare outward at the corners const dx = x - center.x, dz = z - center.y const l = Math.hypot(dx, dz) || 1 return new THREE.Vector3(x + (dx / l) * flare * lift, y, z + (dz / l) * flare * lift) } const edgeLen = c0.distanceTo(c1) for (let i = 0; i < segU; i++) for (let j = 0; j < segV; j++) { const u0 = i / segU, u1 = (i + 1) / segU, v0 = j / segV, v1 = (j + 1) / segV const a = P(u0, v0), bq = P(u1, v0), c = P(u1, v1), d = P(u0, v1) const slopeLen = 6 b.quad(a, bq, c, d, [ (u0 * edgeLen) / uvScale, (v0 * slopeLen) / uvScale, (u1 * edgeLen) / uvScale, (v0 * slopeLen) / uvScale, (u1 * edgeLen) / uvScale, (v1 * slopeLen) / uvScale, (u0 * edgeLen) / uvScale, (v1 * slopeLen) / uvScale, ]) // underside (dark soffit) – same quad reversed b.quad(d, c, bq, a, [0, 0, 0, 0, 0, 0, 0, 0]) } } }