roamwright 0.61.12 → 0.61.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,484 @@
1
+ /**
2
+ * Buried-town mound (tell) ruins builder: weathered mud-brick wall stumps arranged on an eroded
3
+ * street-and-room grid with domed broken-end caps and fallen brick clods, plus curved potsherds
4
+ * and crumbled brick fragments strewn over the top and flanks. Pure geometry assembly, worker-safe.
5
+ * @module
6
+ */
7
+
8
+ import * as THREE from 'three';
9
+ import { createNoise } from '../math/noise.js';
10
+ import { mulberry32 } from '../math/random.js';
11
+ import { TAU, sstep } from '../math/scalar.js';
12
+ import { GB } from './builder.js';
13
+ import { C as defaultColor, TRS as defaultTransform } from './primitives.js';
14
+
15
+ /**
16
+ * Default layout, wall cross-section, end-cap, fallen-clod and potsherd parameters for mound ruins
17
+ * built by {@link buildMoundWalls}, {@link buildMoundSherds} and {@link buildMoundRuins}.
18
+ * Lengths (`lanes`, `rooms`, `segment`, `wallsReach`, `tallest`, `core`, `apron`, `sherdsRadius`,
19
+ * `clod`, `uvScale`) are in metres; colours (`brickColor`, `potColor`) are sRGB hex.
20
+ */
21
+ export const MOUND_RUINS = Object.freeze({
22
+ lanes: Object.freeze([11, 24]),
23
+ rooms: Object.freeze([3.5, 9]),
24
+ segment: 0.8,
25
+ standing: 0.66,
26
+ wallsReach: 160,
27
+ buried: 0.36,
28
+ tallest: 0.95,
29
+ crumble: 0.3,
30
+ core: 0.55,
31
+ apron: 1.5,
32
+ farEvery: 4,
33
+ shortest: 5,
34
+ sherds: 900,
35
+ sherdsRadius: 190,
36
+ sherdsMost: 3000,
37
+ brickColor: 0xc9ad88,
38
+ potColor: 0xa4664a,
39
+ sherdForms: Object.freeze([
40
+ Object.freeze({ arc: 0.62, tall: 0.7 }),
41
+ Object.freeze({ arc: 0.9, tall: 0.85 }),
42
+ Object.freeze({ arc: 1.25, tall: 0.6 }),
43
+ Object.freeze({ arc: 0.8, tall: 0.65, rim: true }),
44
+ ]),
45
+ sherdThick: 0.06,
46
+ rimThick: 2.2,
47
+ sherdRound: 3,
48
+ sherdUp: 2,
49
+ sherdBroken: 0.18,
50
+ rimShare: 1 / 6,
51
+ brickForms: 3,
52
+ profile: Object.freeze([
53
+ Object.freeze([-0.5, 0]),
54
+ Object.freeze([-0.25, 0.26]),
55
+ Object.freeze([-0.13, 0.82]),
56
+ Object.freeze([0, 1]),
57
+ Object.freeze([0.13, 0.82]),
58
+ Object.freeze([0.25, 0.26]),
59
+ Object.freeze([0.5, 0]),
60
+ ]),
61
+ farProfile: Object.freeze([Object.freeze([-0.5, 0]), Object.freeze([0, 1]), Object.freeze([0.5, 0])]),
62
+ cap: Object.freeze([0.35, 0.65, 0.88, 1]),
63
+ farCap: Object.freeze([1]),
64
+ capReach: 0.55,
65
+ stub: 0.6,
66
+ ends: 2,
67
+ fallen: Object.freeze([1, 5]),
68
+ clod: Object.freeze([0.14, 0.42]),
69
+ uvScale: 9,
70
+ });
71
+
72
+ let fallbackNoise = null;
73
+ const defaultNoise = () => (fallbackNoise ??= createNoise().vnoise);
74
+
75
+ // Unevenly spaced grid lines from `-reach` to `reach`.
76
+ function lines(random, reach, [least, most]) {
77
+ const out = [];
78
+ for (let at = -reach + random() * least; at < reach; at += least + random() * (most - least)) out.push(at);
79
+ return out;
80
+ }
81
+
82
+ // Standing wall height in metres at `(x, z)`: zero under deep drift or below the mound plateau rim.
83
+ function standing(mound, reach, x, z, y, plateau, noise, cfg) {
84
+ const d = Math.hypot(x - mound.x, z - mound.z);
85
+ const drift = noise(x * 0.06, z * 0.06, 933) * 0.3 + noise(x * 0.018, z * 0.018, 934) * 0.7;
86
+ if (d > reach * (0.8 + 0.35 * noise(x * 0.02, z * 0.02, 931)) || y < plateau) return 0;
87
+ const middle = sstep(reach, reach * 0.3, d);
88
+ return cfg.tallest * sstep(cfg.buried, 0.85, drift) * (0.35 + 0.65 * middle) * (0.75 + 0.5 * noise(x * 0.6, z * 0.6, 935));
89
+ }
90
+
91
+ function ridgeArrays(far) {
92
+ return { position: [], normal: [], uv: [], index: [], ends: far ? null : [] };
93
+ }
94
+
95
+ // Domed end-cap rings tapering past one broken end of a wall stretch.
96
+ function capRings(rings, [x, , z], h, w, ax, az, ground, caps, noise, cfg) {
97
+ const reach = w * cfg.capReach * (0.85 + 0.3 * noise(x * 1.3, z * 1.3, 939));
98
+ for (const t of caps) {
99
+ const cx = x + ax * t * reach;
100
+ const cz = z + az * t * reach;
101
+ const round = 1 - t * t;
102
+ const broken = 0.8 + 0.4 * noise(cx * 3.1, cz * 3.1, 940);
103
+ rings.push([cx, ground(cx, cz), cz, h * round ** 1.5 * broken, w * round ** 0.35]);
104
+ }
105
+ }
106
+
107
+ // Extrude one unbroken wall stretch along `points` with smooth vertex normals and domed broken ends.
108
+ function ridge(out, points, sx, sz, profile, ground, caps, noise, cfg) {
109
+ const first = out.position.length / 3;
110
+ const firstTriangle = out.index.length;
111
+ const n = profile.length;
112
+ const last = points.length - 1;
113
+ const ax = -sz;
114
+ const az = sx;
115
+ const rings = [];
116
+ const body = points.map(([x, y, z, high], i) => {
117
+ const end = Math.min(i, last - i);
118
+ return [x, y, z, high * Math.min(1, cfg.stub + (end * (1 - cfg.stub)) / cfg.ends), cfg.core + high * cfg.apron];
119
+ });
120
+ const [, , , h0, w0] = body[0];
121
+ const [, , , h1, w1] = body[last];
122
+ capRings(rings, body[0], h0, w0, -ax, -az, ground, caps, noise, cfg);
123
+ rings.reverse();
124
+ rings.push(...body);
125
+ capRings(rings, body[last], h1, w1, ax, az, ground, caps, noise, cfg);
126
+ out.ends?.push([body[0][0], body[0][2], -ax, -az, h0, w0], [body[last][0], body[last][2], ax, az, h1, w1]);
127
+ rings.forEach(([x, y, z, h, w], i) => {
128
+ profile.forEach(([a, up], k) => {
129
+ const slump = noise(x * 0.7 + k * 3.1, z * 0.7, 937) - 0.5 + 0.6 * (noise(x * 2.3, z * 2.3 + k * 1.7, 938) - 0.5);
130
+ const across = (a + slump * cfg.crumble * (1 - up * 0.5)) * w;
131
+ const rise = up * (1 + slump * cfg.crumble);
132
+ out.position.push(x + sx * across, y - 0.12 + rise * (h + 0.12), z + sz * across);
133
+ out.uv.push((x + sx * across) / cfg.uvScale, (z + sz * across) / cfg.uvScale);
134
+ out.normal.push(0, 0, 0);
135
+ });
136
+ if (i)
137
+ for (let k = 0; k + 1 < n; k++) {
138
+ const p = first + (i - 1) * n + k;
139
+ const q = first + i * n + k;
140
+ out.index.push(p, q, p + 1, p + 1, q, q + 1);
141
+ }
142
+ });
143
+ // Area-weighted upward-facing vertex normals so mud-brick ridges shade smoothly.
144
+ const P = out.position;
145
+ const N = out.normal;
146
+ for (let t = firstTriangle; t < out.index.length; t += 3) {
147
+ const [a, b, c] = [out.index[t] * 3, out.index[t + 1] * 3, out.index[t + 2] * 3];
148
+ const ux = P[b] - P[a];
149
+ const uy = P[b + 1] - P[a + 1];
150
+ const uz = P[b + 2] - P[a + 2];
151
+ const vx = P[c] - P[a];
152
+ const vy = P[c + 1] - P[a + 1];
153
+ const vz = P[c + 2] - P[a + 2];
154
+ const nx = uy * vz - uz * vy;
155
+ const ny = uz * vx - ux * vz;
156
+ const nz = ux * vy - uy * vx;
157
+ for (const v of [a, b, c]) {
158
+ N[v] += nx;
159
+ N[v + 1] += ny;
160
+ N[v + 2] += nz;
161
+ }
162
+ }
163
+ for (let v = first * 3; v < N.length; v += 3) {
164
+ const unit = (N[v + 1] < 0 ? -1 : 1) / (Math.hypot(N[v], N[v + 1], N[v + 2]) || 1);
165
+ N[v] *= unit;
166
+ N[v + 1] *= unit;
167
+ N[v + 2] *= unit;
168
+ }
169
+ }
170
+
171
+ // Trace one wall run in the mound's rotated frame, splitting wherever drift buries it or a doorway breaks it.
172
+ function wall(out, random, mound, frame, u0, u1, v, across, ground, plateau, far, noise, cfg) {
173
+ const { at, c, s, reach } = frame;
174
+ const count = Math.max(2, Math.round((u1 - u0) / cfg.segment));
175
+ const step = (u1 - u0) / count;
176
+ const door = random() < 0.6 ? u0 + (u1 - u0) * (0.15 + 0.7 * random()) : -1e9;
177
+ const [sx, sz] = across ? [c, s] : [s, -c];
178
+ let run = [];
179
+ const close = () => {
180
+ if (run.length >= cfg.shortest) {
181
+ const kept = far ? run.filter((_, i) => i % cfg.farEvery === 0 || i === run.length - 1) : run;
182
+ ridge(out, kept, sx, sz, far ? cfg.farProfile : cfg.profile, ground, far ? cfg.farCap : cfg.cap, noise, cfg);
183
+ }
184
+ run = [];
185
+ };
186
+ for (let k = 0; k <= count; k++) {
187
+ const u = u0 + k * step;
188
+ const wobble = (noise(u * 0.3, v * 0.3, 936) - 0.5) * 0.3;
189
+ const [x, z] = across ? at(v + wobble, u) : at(u, v + wobble);
190
+ const y = ground(x, z);
191
+ const high = Math.abs(u - door) < 0.7 ? 0 : standing(mound, reach, x, z, y, plateau, noise, cfg);
192
+ if (high < 0.06) close();
193
+ else run.push([x, y, z, high]);
194
+ }
195
+ close();
196
+ }
197
+
198
+ // Rounded six-vertex clod of melted mud brick lying half-sunk around a broken wall end.
199
+ function clod(out, x, y, z, a, up, b, turn, uvScale) {
200
+ const first = out.position.length / 3;
201
+ const c = Math.cos(turn);
202
+ const s = Math.sin(turn);
203
+ const tips = [
204
+ [a, 0, 0],
205
+ [0, 0, b],
206
+ [-a, 0, 0],
207
+ [0, 0, -b],
208
+ [0, up, 0],
209
+ [0, -up, 0],
210
+ ];
211
+ for (const [u, v, w] of tips) {
212
+ const px = x + (u * c - w * s) / 2;
213
+ const pz = z + (u * s + w * c) / 2;
214
+ const n = Math.hypot(u / a, v / up, w / b) || 1;
215
+ out.position.push(px, y + v / 2, pz);
216
+ out.normal.push(((u / a) * c) / n - ((w / b) * s) / n, v / up / n, ((u / a) * s) / n + ((w / b) * c) / n);
217
+ out.uv.push(px / uvScale, pz / uvScale);
218
+ }
219
+ for (let k = 0; k < 4; k++) {
220
+ const p = first + k;
221
+ const q = first + ((k + 1) % 4);
222
+ out.index.push(p, first + 4, q, q, first + 5, p);
223
+ }
224
+ }
225
+
226
+ function fallenClods(out, random, ground, cfg) {
227
+ for (const [x, z, ax, az, h, w] of out.ends) {
228
+ const count = Math.round(cfg.fallen[0] + (cfg.fallen[1] - cfg.fallen[0]) * random() * Math.min(1, h / cfg.tallest));
229
+ for (let n = 0; n < count; n++) {
230
+ const along = w * cfg.capReach * (0.5 + 0.9 * random());
231
+ const aside = w * 0.5 * (random() - 0.5) * 2;
232
+ const cx = x + ax * along - az * aside;
233
+ const cz = z + az * along + ax * aside;
234
+ const size = cfg.clod[0] + (cfg.clod[1] - cfg.clod[0]) * random() ** 2;
235
+ const turn = random() * TAU;
236
+ clod(
237
+ out,
238
+ cx,
239
+ ground(cx, cz) - size * 0.15,
240
+ cz,
241
+ size,
242
+ size * (0.5 + 0.3 * random()),
243
+ size * (0.6 + 0.4 * random()),
244
+ turn,
245
+ cfg.uvScale,
246
+ );
247
+ }
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Build eroded mud-brick wall stumps and broken-end clods across the summit of a buried-town mound.
253
+ * Pure typed-array arithmetic; safe on Web Workers and in Node.
254
+ *
255
+ * @param {{ x: number, z: number, r: number, h: number }} mound - Mound centre `(x, z)`, radius `r` and height `h` in metres.
256
+ * @param {number} [seed=1] - Integer seed for the layout's deterministic RNG.
257
+ * @param {boolean} [far=false] - When true, emits the lighter distant wall profile without clods.
258
+ * @param {(x: number, z: number) => number} [ground] - Terrain height sampler `(x, z) => y` in metres.
259
+ * @param {object} [options] - Optional noise and parameter overrides.
260
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise in `[0, 1)` (defaults to `createNoise().vnoise`).
261
+ * @param {Partial<typeof MOUND_RUINS>} [options.shape] - Overrides to {@link MOUND_RUINS}.
262
+ * @returns {{ position: Float32Array, normal: Float32Array, uv: Float32Array, index: Uint16Array | Uint32Array }}
263
+ */
264
+ export function buildMoundWalls(mound, seed = 1, far = false, ground = () => 0, options = {}) {
265
+ const noise = options.noise ?? defaultNoise();
266
+ const cfg = options.shape ? { ...MOUND_RUINS, ...options.shape } : MOUND_RUINS;
267
+ const random = mulberry32(seed);
268
+ const out = ridgeArrays(far);
269
+ const plateau = ground(mound.x, mound.z) - mound.h * 0.22;
270
+ const turn = random() * TAU;
271
+ const c = Math.cos(turn);
272
+ const s = Math.sin(turn);
273
+ const reach = Math.min(mound.r * cfg.standing, cfg.wallsReach);
274
+ const frame = { c, s, reach, at: (u, v) => [mound.x + u * c - v * s, mound.z + u * s + v * c] };
275
+ const us = lines(random, reach, cfg.lanes);
276
+ const vs = lines(random, reach, cfg.lanes);
277
+ for (let i = 0; i + 1 < us.length; i++)
278
+ for (let j = 0; j + 1 < vs.length; j++) {
279
+ const [u0, u1, v0, v1] = [us[i] + 1.5, us[i + 1] - 1.5, vs[j] + 1.5, vs[j + 1] - 1.5];
280
+ if (random() < 0.12) continue; // open square
281
+ wall(out, random, mound, frame, u0, u1, v0, false, ground, plateau, far, noise, cfg);
282
+ wall(out, random, mound, frame, u0, u1, v1, false, ground, plateau, far, noise, cfg);
283
+ wall(out, random, mound, frame, v0, v1, u0, true, ground, plateau, far, noise, cfg);
284
+ wall(out, random, mound, frame, v0, v1, u1, true, ground, plateau, far, noise, cfg);
285
+ for (const v of lines(random, (v1 - v0) / 2, cfg.rooms).slice(1)) {
286
+ const mid = (v0 + v1) / 2 + v;
287
+ const from = random() < 0.5 ? u0 : u0 + (u1 - u0) * random() * 0.5;
288
+ wall(out, random, mound, frame, from, from + (u1 - from) * (0.5 + random() * 0.5), mid, false, ground, plateau, far, noise, cfg);
289
+ }
290
+ }
291
+ if (out.ends) fallenClods(out, random, ground, cfg);
292
+ const points = out.position.length / 3;
293
+ return {
294
+ position: new Float32Array(out.position),
295
+ normal: new Float32Array(out.normal),
296
+ uv: new Float32Array(out.uv),
297
+ index: new (points < 65536 ? Uint16Array : Uint32Array)(out.index),
298
+ };
299
+ }
300
+
301
+ // Curved potsherd geometry (unit chord width, outer/inner faces and broken edges, lowest point at origin).
302
+ function sherdShape({ arc, tall, rim }, seed, noise, cfg) {
303
+ const R = 0.5 / Math.sin(arc / 2);
304
+ const nu = cfg.sherdRound;
305
+ const nv = cfg.sherdUp;
306
+ const broken = (i, j, k) => cfg.sherdBroken * (noise(i * 3.7 + seed, j * 2.9 + k * 5.1, 951) - 0.5) * 2;
307
+ const edge = (i, j) => i === 0 || i === nu || j === 0 || j === nv;
308
+ const point = (i, j, side) => {
309
+ const u = i + (edge(i, j) ? broken(i, j, 0) : 0);
310
+ const v = j + (edge(i, j) ? broken(i, j, 1) : 0);
311
+ const a = (u / nu - 0.5) * arc;
312
+ const thick = cfg.sherdThick * (rim && j === nv ? cfg.rimThick : 1);
313
+ const r = R - side * thick;
314
+ return [r * Math.sin(a), R - r * Math.cos(a), (v / nv - 0.5) * tall];
315
+ };
316
+ const corners = [];
317
+ const quad = (a, b, c, d) => corners.push(...a, ...b, ...c, ...a, ...c, ...d);
318
+ for (let i = 0; i < nu; i++)
319
+ for (let j = 0; j < nv; j++) {
320
+ quad(point(i, j, 0), point(i + 1, j, 0), point(i + 1, j + 1, 0), point(i, j + 1, 0));
321
+ quad(point(i, j + 1, 1), point(i + 1, j + 1, 1), point(i + 1, j, 1), point(i, j, 1));
322
+ }
323
+ const outline = [];
324
+ for (let i = 0; i < nu; i++) outline.push([i, 0]);
325
+ for (let j = 0; j < nv; j++) outline.push([nu, j]);
326
+ for (let i = nu; i > 0; i--) outline.push([i, nv]);
327
+ for (let j = nv; j > 0; j--) outline.push([0, j]);
328
+ for (let k = 0; k < outline.length; k++) {
329
+ const [i0, j0] = outline[k];
330
+ const [i1, j1] = outline[(k + 1) % outline.length];
331
+ quad(point(i1, j1, 0), point(i0, j0, 0), point(i0, j0, 1), point(i1, j1, 1));
332
+ }
333
+ const g = new THREE.BufferGeometry();
334
+ g.setAttribute('position', new THREE.Float32BufferAttribute(corners, 3));
335
+ g.computeVertexNormals();
336
+ g.computeBoundingBox();
337
+ return g;
338
+ }
339
+
340
+ // Crumbled mud-brick fragment (unit cube whose shared corners drift coherently).
341
+ function brickShape(seed, noise) {
342
+ const g = new THREE.BoxGeometry(1, 1, 1).toNonIndexed();
343
+ const a = g.attributes.position.array;
344
+ for (let i = 0; i < a.length; i += 3) {
345
+ const [x, y, z] = [a[i], a[i + 1], a[i + 2]];
346
+ const k = x * 4 + y * 2 + z + seed;
347
+ a[i] = x * (0.7 + 0.45 * noise(k, 1.3, 952));
348
+ a[i + 1] = y * (0.6 + 0.5 * noise(k, 2.9, 952));
349
+ a[i + 2] = z * (0.7 + 0.45 * noise(k, 4.1, 952));
350
+ }
351
+ g.computeVertexNormals();
352
+ return g;
353
+ }
354
+
355
+ function makeSherdCache(noise, cfg) {
356
+ return {
357
+ pot: cfg.sherdForms.filter(f => !f.rim).map((f, k) => sherdShape(f, k * 7.3, noise, cfg)),
358
+ rim: cfg.sherdForms.filter(f => f.rim).map((f, k) => sherdShape(f, 40 + k * 7.3, noise, cfg)),
359
+ brick: Array.from({ length: cfg.brickForms }, (_, k) => brickShape(k * 5.9, noise)),
360
+ };
361
+ }
362
+
363
+ const defaultSherdCache = new WeakMap();
364
+ function getSherdShapes(noise, cfg, customShape) {
365
+ if (customShape) return makeSherdCache(noise, cfg);
366
+ let cached = defaultSherdCache.get(noise);
367
+ if (!cached) defaultSherdCache.set(noise, (cached = makeSherdCache(noise, cfg)));
368
+ return cached;
369
+ }
370
+
371
+ const _slope = [0, 0];
372
+ function slopeAt(ground, x, z) {
373
+ const STEP = 0.4;
374
+ _slope[0] = (ground(x + STEP, z) - ground(x - STEP, z)) / (2 * STEP);
375
+ _slope[1] = (ground(x, z + STEP) - ground(x, z - STEP)) / (2 * STEP);
376
+ return _slope;
377
+ }
378
+
379
+ /**
380
+ * Scatter curved potsherds and crumbled mud-brick fragments across a mound and down its flanks,
381
+ * tilted to match the local terrain slope. Returns geometry builder parts (`GB.parts`). Worker-safe.
382
+ *
383
+ * @param {{ x: number, z: number, r: number, h: number }} mound - Mound centre `(x, z)` and radius `r` in metres.
384
+ * @param {number} [seed=2] - Integer seed for the scatter RNG.
385
+ * @param {(x: number, z: number) => number} [ground] - Terrain height sampler `(x, z) => y` in metres.
386
+ * @param {object} [options] - Optional bindings and overrides.
387
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise in `[0, 1)`.
388
+ * @param {typeof GB} [options.Builder] - Geometry builder constructor (defaults to `GB`).
389
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
390
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
391
+ * @param {Partial<typeof MOUND_RUINS>} [options.shape] - Overrides to {@link MOUND_RUINS}.
392
+ * @param {object} [options.sherdCache] - Pre-built `{ pot, rim, brick }` prototype geometry cache.
393
+ * @returns {object} Typed-array mesh parts from `GB.parts()`.
394
+ */
395
+ export function buildMoundSherds(mound, seed = 2, ground = () => 0, options = {}) {
396
+ const noise = options.noise ?? defaultNoise();
397
+ const Builder = options.Builder ?? GB;
398
+ const color = options.color ?? defaultColor;
399
+ const transform = options.transform ?? defaultTransform;
400
+ const cfg = options.shape ? { ...MOUND_RUINS, ...options.shape } : MOUND_RUINS;
401
+ const { pot: pots, rim: rims, brick: bricks } = options.sherdCache ?? getSherdShapes(noise, cfg, Boolean(options.shape));
402
+ const random = mulberry32(seed);
403
+ const b = new Builder();
404
+ const count = Math.min(cfg.sherdsMost, Math.round(cfg.sherds * Math.max(1, (mound.r / cfg.sherdsRadius) ** 2)));
405
+ for (let n = 0; n < count; n++) {
406
+ const a = random() * TAU;
407
+ const d = mound.r * 1.1 * Math.sqrt(random()) ** 0.8;
408
+ const x = mound.x + Math.cos(a) * d;
409
+ const z = mound.z + Math.sin(a) * d;
410
+ const size = 0.08 + random() * 0.18;
411
+ const pot = random() < 0.6;
412
+ const col = color(pot ? cfg.potColor : cfg.brickColor, 0.1, random);
413
+ const yaw = random() * TAU;
414
+ const pick = random();
415
+ const over = pot && random() < 0.35;
416
+ const [gx, gz] = slopeAt(ground, x, z);
417
+ const c = Math.cos(yaw);
418
+ const sn = Math.sin(yaw);
419
+ const tiltX = -Math.atan(gx * sn + gz * c);
420
+ const tiltZ = Math.atan(gx * c - gz * sn);
421
+ const y = ground(x, z);
422
+ if (pot) {
423
+ const list = pick < cfg.rimShare ? rims : pots;
424
+ const shape =
425
+ list[Math.floor((pick < cfg.rimShare ? pick / cfg.rimShare : (pick - cfg.rimShare) / (1 - cfg.rimShare)) * list.length)];
426
+ const lift = over ? shape.boundingBox.max.y : 0;
427
+ b.add(shape, col, transform(x, y + (lift - 0.004) * size, z, size, size, size, yaw, tiltX, tiltZ + (over ? Math.PI : 0)));
428
+ } else {
429
+ const shape = bricks[Math.floor(pick * bricks.length)];
430
+ b.add(shape, col, transform(x, y + size * 0.12, z, size * 1.3, size * 0.55, size * 0.9, yaw, tiltX, tiltZ));
431
+ }
432
+ }
433
+ return b.parts(!b.wide);
434
+ }
435
+
436
+ /**
437
+ * Build both the wall-stump arrays and (for near LOD) the strewn potsherds and brick fragments of a mound.
438
+ *
439
+ * @param {{ x: number, z: number, r: number, h: number }} mound - Mound descriptor in metres.
440
+ * @param {number} [seed=1] - Integer seed; sherds use `seed + 1`.
441
+ * @param {boolean} [far=false] - When true, returns the far wall mesh and `sherds: null`.
442
+ * @param {(x: number, z: number) => number} [ground] - Terrain height sampler `(x, z) => y` in metres.
443
+ * @param {object} [options] - Forwarded to {@link buildMoundWalls} and {@link buildMoundSherds}.
444
+ * @returns {{ walls: ReturnType<typeof buildMoundWalls>, sherds: ReturnType<typeof buildMoundSherds> | null }}
445
+ */
446
+ export function buildMoundRuins(mound, seed = 1, far = false, ground = () => 0, options = {}) {
447
+ return {
448
+ walls: buildMoundWalls(mound, seed, far, ground, options),
449
+ sherds: far ? null : buildMoundSherds(mound, seed + 1, ground, options),
450
+ };
451
+ }
452
+
453
+ /**
454
+ * Create a mound-ruins builder bound to a world's noise function, default ground sampler, builder class
455
+ * and optional shape overrides, with its own cached sherd prototype shapes (no cross-world state).
456
+ *
457
+ * @param {object} [options] - World bindings forwarded to {@link buildMoundWalls} and {@link buildMoundSherds}.
458
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Seeded value noise.
459
+ * @param {(x: number, z: number) => number} [options.ground] - Default terrain height sampler `(x, z) => y` in metres.
460
+ * @param {typeof GB} [options.Builder] - Geometry builder class.
461
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
462
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
463
+ * @param {Partial<typeof MOUND_RUINS>} [options.shape] - Overrides to {@link MOUND_RUINS}.
464
+ * @returns {{
465
+ * moundWalls: (mound: object, seed?: number, far?: boolean, ground?: (x: number, z: number) => number) => ReturnType<typeof buildMoundWalls>,
466
+ * moundSherds: (mound: object, seed?: number, ground?: (x: number, z: number) => number) => ReturnType<typeof buildMoundSherds>,
467
+ * moundRuins: (mound: object, seed?: number, far?: boolean, ground?: (x: number, z: number) => number) => ReturnType<typeof buildMoundRuins>,
468
+ * }}
469
+ */
470
+ export function createMoundRuins(options = {}) {
471
+ const noise = options.noise ?? defaultNoise();
472
+ const defaultGround = options.ground ?? (() => 0);
473
+ const cfg = options.shape ? { ...MOUND_RUINS, ...options.shape } : MOUND_RUINS;
474
+ let cache = null;
475
+ const sherdCache = () => (cache ??= makeSherdCache(noise, cfg));
476
+ const moundWalls = (mound, seed = 1, far = false, ground = defaultGround) => buildMoundWalls(mound, seed, far, ground, options);
477
+ const moundSherds = (mound, seed = 2, ground = defaultGround) =>
478
+ buildMoundSherds(mound, seed, ground, { ...options, sherdCache: sherdCache() });
479
+ const moundRuins = (mound, seed = 1, far = false, ground = defaultGround) => ({
480
+ walls: moundWalls(mound, seed, far, ground),
481
+ sherds: far ? null : moundSherds(mound, seed + 1, ground),
482
+ });
483
+ return { moundWalls, moundSherds, moundRuins };
484
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Generic fallen-masonry rubble heaps and scree scatters for ruined walls, collapsed buildings and rocky slopes.
3
+ * Pure geometry assembly into a `GB` builder; worker-safe.
4
+ * @module
5
+ */
6
+
7
+ import { TAU } from '../math/scalar.js';
8
+ import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
9
+
10
+ let fallbackRock = null;
11
+ const defaultRock = () => (fallbackRock ??= createUnitShapes().rock);
12
+
13
+ /**
14
+ * Default tone and weathering settings for heaped fallen masonry built by {@link buildRubbleHeap}.
15
+ */
16
+ export const RUBBLE_HEAP = Object.freeze({
17
+ /** Default stone colour in sRGB hex. */
18
+ hue: 0xb39c76,
19
+ /** Colour jitter amplitude applied per piece. */
20
+ jitter: 0.1,
21
+ /** Number of damp-darkened courses (blocks at course 1 are darkened). */
22
+ dampCourses: 2,
23
+ /** Share of pieces darkened by lichen or run-off staining. */
24
+ stained: 0.12,
25
+ });
26
+
27
+ /**
28
+ * Default parameters for mixed cut-block and broken-rock rubble scatter built by {@link scatterRubble}.
29
+ */
30
+ export const RUBBLE_SCATTER = Object.freeze({
31
+ /** Default stone colour in sRGB hex. */
32
+ hue: 0xb8a888,
33
+ /** Colour jitter amplitude applied per piece. */
34
+ jitter: 0.1,
35
+ /** Size multiplier for blocks and broken stone. */
36
+ scale: 1,
37
+ /** Share of pieces emitted as dressed rectangular blocks rather than rough broken rock. */
38
+ dressed: 0.45,
39
+ });
40
+
41
+ function weatheredTone(hue, random, course, cfg, color) {
42
+ const col = color(hue, cfg.jitter, random);
43
+ const k = (course < cfg.dampCourses ? 0.8 + 0.08 * course : 1) * (random() < cfg.stained ? 0.78 : 1);
44
+ return [col[0] * k, col[1] * k, col[2] * k];
45
+ }
46
+
47
+ /**
48
+ * Append a heaped pile of fallen masonry around `(x, z)` into a geometry builder: squared blocks,
49
+ * rough stone lumps, and clusters of smaller stone chips, piled higher toward the middle and half-sunk
50
+ * into the ground.
51
+ *
52
+ * @param {Pick<import('./builder.js').GB, 'box' | 'add'>} builder - Destination geometry builder.
53
+ * @param {() => number} [random=Math.random] - Repeatable random stream in `[0, 1)`.
54
+ * @param {object} options
55
+ * @param {number} [options.x=0] - Heap centre X in metres.
56
+ * @param {number} [options.z=0] - Heap centre Z in metres.
57
+ * @param {number} [options.radius=3] - Heap footprint radius in metres.
58
+ * @param {number} [options.count=12] - Number of debris pieces (blocks, lumps or chip clusters).
59
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y` in metres.
60
+ * @param {number} [options.hue] - Stone sRGB hex colour (defaults to `RUBBLE_HEAP.hue`).
61
+ * @param {import('three').BufferGeometry} [options.rock] - Unit broken stone geometry (defaults to `createUnitShapes().rock`).
62
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
63
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
64
+ * @param {Partial<typeof RUBBLE_HEAP>} [options.shape] - Overrides to {@link RUBBLE_HEAP}.
65
+ */
66
+ export function buildRubbleHeap(builder, random = Math.random, options = {}) {
67
+ const {
68
+ x = 0,
69
+ z = 0,
70
+ radius = 3,
71
+ count = 12,
72
+ ground = () => 0,
73
+ rock = defaultRock(),
74
+ color = defaultColor,
75
+ transform = defaultTransform,
76
+ } = options;
77
+ const cfg = options.shape ? { ...RUBBLE_HEAP, ...options.shape } : RUBBLE_HEAP;
78
+ const hue = options.hue ?? cfg.hue;
79
+ for (let i = 0; i < count; i++) {
80
+ const a = random() * TAU;
81
+ const d = Math.sqrt(random()) * radius;
82
+ const px = x + Math.cos(a) * d;
83
+ const pz = z + Math.sin(a) * d;
84
+ const heap = (1 - d / radius) * radius * 0.18 * random();
85
+ const s = 0.15 + 0.6 * random() * random();
86
+ const y = ground(px, pz) + heap;
87
+ const pick = random();
88
+ const col = weatheredTone(hue, random, 1 + (random() < 0.4), cfg, color);
89
+ if (pick < 0.45)
90
+ builder.box(px, y - s * 0.25, pz, s * 1.5, s * 0.72, s, col, random() * TAU, (random() - 0.5) * 0.6, (random() - 0.5) * 0.4);
91
+ else if (pick < 0.8)
92
+ builder.add(rock, col, transform(px, y - s * 0.2, pz, s * 1.3, s * 0.8, s * 1.1, random() * TAU, (random() - 0.5) * 0.4));
93
+ else
94
+ for (let k = 0; k < 3; k++) {
95
+ const cx = px + (random() - 0.5) * 0.7;
96
+ const cz = pz + (random() - 0.5) * 0.7;
97
+ const c = s * 0.35;
98
+ builder.add(rock, col, transform(cx, ground(cx, cz) - c * 0.2, cz, c * 1.3, c * 0.7, c, random() * TAU));
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Scatter fallen stone and optional cut masonry blocks over a circular patch around `(x, z)`, larger
105
+ * toward the centre. Setting `dressed: 0` emits natural scree without rectangular blocks.
106
+ *
107
+ * @param {Pick<import('./builder.js').GB, 'box' | 'add'>} builder - Destination geometry builder.
108
+ * @param {() => number} [random=Math.random] - Repeatable random stream in `[0, 1)`.
109
+ * @param {object} options
110
+ * @param {number} [options.x=0] - Patch centre X in metres.
111
+ * @param {number} [options.z=0] - Patch centre Z in metres.
112
+ * @param {number} [options.radius=4] - Patch radius in metres.
113
+ * @param {number} [options.count=20] - Number of pieces to scatter.
114
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y` in metres.
115
+ * @param {number} [options.hue] - Stone sRGB hex colour (defaults to `RUBBLE_SCATTER.hue`).
116
+ * @param {number} [options.scale] - Size multiplier (defaults to `RUBBLE_SCATTER.scale`).
117
+ * @param {number} [options.dressed] - Share of dressed cut blocks in `[0, 1]` (defaults to `RUBBLE_SCATTER.dressed`).
118
+ * @param {import('three').BufferGeometry} [options.rock] - Unit broken stone geometry (defaults to `createUnitShapes().rock`).
119
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
120
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
121
+ * @param {Partial<typeof RUBBLE_SCATTER>} [options.shape] - Overrides to {@link RUBBLE_SCATTER}.
122
+ */
123
+ export function scatterRubble(builder, random = Math.random, options = {}) {
124
+ const {
125
+ x = 0,
126
+ z = 0,
127
+ radius = 4,
128
+ count = 20,
129
+ ground = () => 0,
130
+ rock = defaultRock(),
131
+ color = defaultColor,
132
+ transform = defaultTransform,
133
+ } = options;
134
+ const cfg = options.shape ? { ...RUBBLE_SCATTER, ...options.shape } : RUBBLE_SCATTER;
135
+ const hue = options.hue ?? cfg.hue;
136
+ const scale = options.scale ?? cfg.scale;
137
+ const dressed = options.dressed ?? cfg.dressed;
138
+ for (let i = 0; i < count; i++) {
139
+ const a = random() * TAU;
140
+ const d = Math.sqrt(random()) * radius;
141
+ const px = x + Math.cos(a) * d;
142
+ const pz = z + Math.sin(a) * d;
143
+ const s = (0.25 + random() * 0.75) * (1.2 - d / radius) * scale;
144
+ const c = color(hue, cfg.jitter, random);
145
+ if (random() < dressed)
146
+ builder.box(
147
+ px,
148
+ ground(px, pz) - 0.1 * s,
149
+ pz,
150
+ 1.1 * s,
151
+ 0.5 * s,
152
+ 0.7 * s,
153
+ c,
154
+ random() * TAU,
155
+ (random() - 0.5) * 0.7,
156
+ (random() - 0.5) * 0.4,
157
+ );
158
+ else
159
+ builder.add(rock, c, transform(px, ground(px, pz) + 0.1 * s, pz, 1.2 * s, 0.7 * s, 1.0 * s, random() * TAU, (random() - 0.5) * 0.4));
160
+ }
161
+ }