roamwright 0.61.17 → 0.61.19

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,329 @@
1
+ /**
2
+ * Siege and collapse rubble geometry: smouldering rubble mounds and wall-breach talus fans with
3
+ * optional cleared passageways. Pure geometry assembly into `GB` builders; worker-safe.
4
+ * @module
5
+ */
6
+
7
+ import { dim } from '../math/color.js';
8
+ import { createNoise } from '../math/noise.js';
9
+ import { TAU, lerp, sstep } from '../math/scalar.js';
10
+ import { C as defaultColor, TRS as defaultTransform, box as defaultBox, createUnitShapes, stick } from './primitives.js';
11
+ import { sampleSurface } from './sampled-surface.js';
12
+
13
+ const DEG = Math.PI / 180;
14
+ const EMBER_LIFT = 0.05;
15
+
16
+ let fallbackShapes = null;
17
+ const defaultShapes = () => (fallbackShapes ??= createUnitShapes());
18
+ let fallbackNoiseObj = null;
19
+ const defaultNoiseObj = () => (fallbackNoiseObj ??= createNoise());
20
+ const defaultVnoise = () => defaultNoiseObj().vnoise;
21
+ const defaultFbm = () => defaultNoiseObj().fbm;
22
+
23
+ /** Default sRGB hex smoke-blackened stone palette for collapse heaps and breach fans. */
24
+ export const DEFAULT_SCORCH_PALETTE = Object.freeze([0x4a443e, 0x5c544b, 0x3a3531, 0x6a5f52, 0x514a44]);
25
+
26
+ const angleDiff = (a, b) => ((((a - b) % TAU) + TAU + Math.PI) % TAU) - Math.PI;
27
+
28
+ /**
29
+ * Append a smouldering ember skin lifted slightly over a sampled mound grid (`nu + 1` points per row).
30
+ *
31
+ * @param {{ pos: number[], dat: number[] }} embers - Mutable ember position/data buffers.
32
+ * @param {number[][]} grid - Sampled mound points `[x, y, z]`.
33
+ * @param {number} nu - Column segments per row.
34
+ * @param {number} heat - Smoulder heat in `[0, 1]`.
35
+ * @param {number} phase - Flicker phase in `[0, 1)`.
36
+ */
37
+ export function addEmberSkin(embers, grid, nu, heat, phase) {
38
+ const w1 = nu + 1;
39
+ const rows = grid.length / w1 - 1;
40
+ const emit = k => {
41
+ const p = grid[k];
42
+ embers.pos.push(p[0], p[1] + EMBER_LIFT, p[2]);
43
+ embers.dat.push(phase, heat, 0);
44
+ };
45
+ for (let j = 0; j < rows; j++) {
46
+ for (let i = 0; i < nu; i++) {
47
+ const a = j * w1 + i;
48
+ emit(a);
49
+ emit(a + 1);
50
+ emit(a + w1);
51
+ emit(a + 1);
52
+ emit(a + w1 + 1);
53
+ emit(a + w1);
54
+ }
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Build a low debris or earth mound at `(x, z)` with optional broken masonry blocks, angular stones,
60
+ * charred timber beams and a smouldering ember skin.
61
+ *
62
+ * @param {object} options
63
+ * @param {{ rock: import('./builder.js').GB, stone?: import('./builder.js').GB, wood?: import('./builder.js').GB, wall?: import('./builder.js').GB }} options.builders - Destination builders (`wall` used when `earth` is true).
64
+ * @param {() => number} [options.random=Math.random] - Repeatable PRNG in `[0, 1)`.
65
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
66
+ * @param {number} [options.x=0] - Mound centre X in metres.
67
+ * @param {number} [options.z=0] - Mound centre Z in metres.
68
+ * @param {number} [options.radius=3] - Mound radius in metres.
69
+ * @param {number} [options.height=1] - Peak height above ground in metres.
70
+ * @param {boolean} [options.earth=false] - When true, emits only the smooth earth mound into `builders.wall`.
71
+ * @param {number[]} [options.col] - Optional linear RGB base colour override.
72
+ * @param {number} [options.count] - Debris piece count (defaults to `Math.round(2 + radius * 1.9)`).
73
+ * @param {number} [options.emberHeat=0] - Non-zero heat emits a smouldering skin into `embers`.
74
+ * @param {{ pos: number[], dat: number[] } | null} [options.embers=null] - Ember skin target buffers.
75
+ * @param {readonly number[]} [options.scorchPalette=DEFAULT_SCORCH_PALETTE] - sRGB hex rubble tones.
76
+ * @param {import('three').BufferGeometry} [options.stoneShape] - Angular stone geometry.
77
+ * @param {import('three').BufferGeometry} [options.boxShape] - Unit box geometry for timber sticks.
78
+ * @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Value noise in `[0, 1)`.
79
+ * @param {Function} [options.color] - Linear RGB colour helper.
80
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
81
+ * @param {() => number[]} [options.charcoalColor] - Returns linear RGB charred timber colour.
82
+ */
83
+ export function buildRubbleMound(options) {
84
+ const {
85
+ builders,
86
+ random = Math.random,
87
+ ground = () => 0,
88
+ x = 0,
89
+ z = 0,
90
+ radius: r = 3,
91
+ height: h = 1,
92
+ earth = false,
93
+ col = null,
94
+ count = undefined,
95
+ emberHeat = 0,
96
+ embers = null,
97
+ scorchPalette = DEFAULT_SCORCH_PALETTE,
98
+ stoneShape = defaultShapes().rock,
99
+ boxShape = defaultBox,
100
+ vnoise = defaultVnoise(),
101
+ color = defaultColor,
102
+ transform = defaultTransform,
103
+ charcoalColor = () => color(0x1a1512, 0.3, random),
104
+ } = options;
105
+ const pick = a => a[(random() * a.length) | 0];
106
+ const between = (a, b) => a + random() * (b - a);
107
+ const cl = col || dim(color(pick(scorchPalette), 0.1, random), 0.82);
108
+ const sd = random() * 50;
109
+ const n = count ?? Math.round(2 + r * 1.9);
110
+ const skin = emberHeat ? [] : null;
111
+ const rubbleHeight = (dx, dz) => {
112
+ const q = Math.hypot(dx, dz) / r;
113
+ return q >= 1 ? 0 : h * (1 - q * q) * (0.55 + 0.9 * vnoise((x + dx) * 1.1 + sd, (z + dz) * 1.1, 31));
114
+ };
115
+ const nu = earth ? 7 : 8;
116
+ sampleSurface(
117
+ earth ? builders.wall : builders.rock,
118
+ nu,
119
+ 3,
120
+ (u, v) => {
121
+ const a = u * TAU;
122
+ const q = 0.07 + 0.93 * v;
123
+ const wob = 0.78 + 0.44 * vnoise(Math.cos(a) * 1.7 + sd, Math.sin(a) * 1.7, 32);
124
+ const dx = Math.cos(a) * r * q * wob;
125
+ const dz = Math.sin(a) * r * q * wob;
126
+ const p = [x + dx, ground(x + dx, z + dz) + (v > 0.99 ? -0.2 : rubbleHeight(dx, dz)), z + dz];
127
+ if (skin) skin.push(p);
128
+ return p;
129
+ },
130
+ () => dim(cl, 0.92 + 0.16 * random()),
131
+ { flip: true },
132
+ );
133
+ if (skin && embers) addEmberSkin(embers, skin, nu, emberHeat, sd / 50);
134
+ if (earth) return;
135
+ for (let i = 0; i < n; i++) {
136
+ const a = random() * TAU;
137
+ const q = Math.sqrt(random()) * 1.15;
138
+ const dx = Math.cos(a) * r * q;
139
+ const dz = Math.sin(a) * r * q;
140
+ const py = ground(x + dx, z + dz) + rubbleHeight(dx, dz);
141
+ const t = random();
142
+ const s = between(0.35, 0.95);
143
+ if (t < 0.5) {
144
+ builders.stone.box(
145
+ x + dx,
146
+ py - s * 0.25,
147
+ z + dz,
148
+ s * between(0.9, 1.7),
149
+ s * between(0.5, 0.8),
150
+ s * between(0.7, 1.1),
151
+ dim(cl, between(1.0, 1.5)),
152
+ random() * 6,
153
+ between(-0.5, 0.5),
154
+ between(-0.5, 0.5),
155
+ );
156
+ } else if (t < 0.9) {
157
+ builders.rock.add(
158
+ stoneShape,
159
+ dim(cl, between(0.8, 1.3)),
160
+ transform(x + dx, py - s * 0.3, z + dz, s * 1.4, s, s * 1.2, random() * 6, random(), random()),
161
+ );
162
+ } else {
163
+ stick(
164
+ builders.wood,
165
+ [x + dx, py - 0.1, z + dz],
166
+ [x + dx + between(-1.6, 1.6), py + between(0.2, 1.3), z + dz + between(-1.6, 1.6)],
167
+ between(0.14, 0.22),
168
+ charcoalColor(),
169
+ boxShape,
170
+ );
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Build a fan of collapsed masonry spilling inward and outward through a breach in a circular
177
+ * curtain wall, with optional cleared passageway through the middle, colliders and ground scar.
178
+ *
179
+ * @param {object} options
180
+ * @param {{ rock: import('./builder.js').GB, stone: import('./builder.js').GB, wood: import('./builder.js').GB }} options.builders - Destination builders.
181
+ * @param {() => number} [options.random=Math.random] - Repeatable PRNG in `[0, 1)`.
182
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
183
+ * @param {{ x: number, z: number }} options.centre - Ring curtain centre `(x, z)` in metres.
184
+ * @param {number} options.wallRadius - Ring curtain radius in metres.
185
+ * @param {number} options.deg - Breach bearing in degrees.
186
+ * @param {number} options.half - Half-width of the breach in degrees.
187
+ * @param {number} options.heightMax - Peak rubble fan height in metres.
188
+ * @param {number} [options.cutHalf=0] - Optional half-width in degrees of a cleared path through the breach.
189
+ * @param {readonly number[]} [options.scorchPalette=DEFAULT_SCORCH_PALETTE] - sRGB hex rubble tones.
190
+ * @param {number} [options.dustHex=0x4f4841] - Base dust mound sRGB hex tone.
191
+ * @param {import('three').BufferGeometry} [options.stoneShape] - Angular rubble stone shape.
192
+ * @param {import('three').BufferGeometry} [options.boxShape] - Unit box geometry for charred timbers.
193
+ * @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Value noise in `[0, 1)`.
194
+ * @param {(x: number, z: number, octaves: number, salt: number) => number} [options.fbm] - Fractal noise in `[0, 1)`.
195
+ * @param {Function} [options.color] - Linear RGB colour helper.
196
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
197
+ * @param {() => number[]} [options.charcoalColor] - Returns linear RGB charred timber colour.
198
+ * @param {((x: number, z: number, radius: number) => void) | null} [options.onCollider=null] - Circle collider callback.
199
+ * @param {((x: number, z: number, radius: number, weight: number) => void) | null} [options.onScar=null] - Ground scar callback.
200
+ */
201
+ export function buildBreachFan(options) {
202
+ const {
203
+ builders,
204
+ random = Math.random,
205
+ ground = () => 0,
206
+ centre,
207
+ wallRadius,
208
+ deg,
209
+ half,
210
+ heightMax: hMax,
211
+ cutHalf = 0,
212
+ scorchPalette = DEFAULT_SCORCH_PALETTE,
213
+ dustHex = 0x4f4841,
214
+ stoneShape = defaultShapes().rock,
215
+ boxShape = defaultBox,
216
+ vnoise = defaultVnoise(),
217
+ fbm = defaultFbm(),
218
+ color = defaultColor,
219
+ transform = defaultTransform,
220
+ charcoalColor = () => color(0x1a1512, 0.3, random),
221
+ onCollider = null,
222
+ onScar = null,
223
+ } = options;
224
+ const pick = a => a[(random() * a.length) | 0];
225
+ const between = (a, b) => a + random() * (b - a);
226
+ const fromA = (deg - half * 1.7) * DEG;
227
+ const toA = (deg + half * 1.7) * DEG;
228
+ const r0 = wallRadius - 17;
229
+ const r1 = wallRadius + 21;
230
+ const dust = dim(color(dustHex), 0.8);
231
+ const rubbleHeight = (x, z) => {
232
+ const dx = x - centre.x;
233
+ const dz = z - centre.z;
234
+ const rad = Math.hypot(dx, dz);
235
+ const da = Math.abs(angleDiff(Math.atan2(dz, dx), deg * DEG)) / DEG;
236
+ const n = 0.6 + 0.8 * fbm(x * 0.19, z * 0.19, 2, 83);
237
+ const prof = Math.max(0, 1 - (rad > wallRadius ? (rad - wallRadius) / 19 : (wallRadius - rad) / 15) / n);
238
+ return (
239
+ hMax *
240
+ sstep(half * 1.7, half * 1.05, da) *
241
+ (cutHalf ? sstep(cutHalf, cutHalf * 2.6, da) : 1) *
242
+ prof *
243
+ (0.45 + 0.55 * prof) *
244
+ (0.75 + 0.5 * vnoise(x * 0.8, z * 0.8, 84))
245
+ );
246
+ };
247
+ sampleSurface(
248
+ builders.rock,
249
+ Math.round(half * 4.8),
250
+ 24,
251
+ (u, v) => {
252
+ const a = lerp(fromA, toA, u);
253
+ const rad = lerp(r0, r1, v);
254
+ const x = centre.x + Math.cos(a) * rad;
255
+ const z = centre.z + Math.sin(a) * rad;
256
+ const hh = rubbleHeight(x, z);
257
+ return [x, ground(x, z) + (hh > 0.03 ? hh + (vnoise(x * 1.7, z * 1.7, 85) - 0.5) * 0.5 : -0.3), z];
258
+ },
259
+ () => dim(dust, between(0.85, 1.15)),
260
+ { flip: true },
261
+ );
262
+ for (let i = 0, n = Math.round(half * 44); i < n; i++) {
263
+ const a = lerp(fromA, toA, random());
264
+ const rad = lerp(r0 - 4, r1 + 6, random());
265
+ const x = centre.x + Math.cos(a) * rad;
266
+ const z = centre.z + Math.sin(a) * rad;
267
+ const hh = rubbleHeight(x, z);
268
+ if (hh < 0.12 && (random() < 0.8 || (cutHalf && Math.abs(angleDiff(a, deg * DEG)) / DEG < cutHalf * 1.3))) continue;
269
+ const y = ground(x, z) + hh;
270
+ const t = random();
271
+ const s = between(0.35, 1.05);
272
+ const cl = dim(color(pick(scorchPalette), 0.12, random), between(0.85, 1.35));
273
+ if (t < 0.5) {
274
+ builders.stone.box(
275
+ x,
276
+ y - s * 0.3,
277
+ z,
278
+ s * between(1.0, 1.8),
279
+ s * between(0.5, 0.8),
280
+ s * between(0.7, 1.1),
281
+ cl,
282
+ random() * 6,
283
+ between(-0.6, 0.6),
284
+ between(-0.6, 0.6),
285
+ );
286
+ } else if (t < 0.83) {
287
+ builders.rock.add(stoneShape, cl, transform(x, y - s * 0.35, z, s * 1.5, s * 1.1, s * 1.3, random() * 6, random(), random()));
288
+ } else if (t < 0.86 && hh > 0.5) {
289
+ builders.stone.box(
290
+ x,
291
+ y - 0.9,
292
+ z,
293
+ between(2.0, 3.6),
294
+ between(1.2, 2.0),
295
+ between(2.0, 2.8),
296
+ cl,
297
+ random() * 6,
298
+ between(-0.5, 0.5),
299
+ between(-0.5, 0.5),
300
+ );
301
+ } else if (t > 0.94) {
302
+ stick(
303
+ builders.wood,
304
+ [x, y - 0.2, z],
305
+ [x + between(-2, 2), y + between(0.3, 1.8), z + between(-2, 2)],
306
+ between(0.14, 0.24),
307
+ charcoalColor(),
308
+ boxShape,
309
+ );
310
+ } else {
311
+ builders.rock.add(stoneShape, cl, transform(x, y - s * 0.2, z, s * 0.8, s * 0.6, s * 0.7, random() * 6, random(), random()));
312
+ }
313
+ }
314
+ if (onCollider) {
315
+ for (let a = fromA; a <= toA; a += 3.2 / wallRadius) {
316
+ for (let rad = r0; rad <= r1; rad += 3.2) {
317
+ const x = centre.x + Math.cos(a) * rad;
318
+ const z = centre.z + Math.sin(a) * rad;
319
+ if (rubbleHeight(x, z) > 1.25) onCollider(x, z, 2.0);
320
+ }
321
+ }
322
+ }
323
+ onScar?.(
324
+ centre.x + Math.cos(deg * DEG) * (wallRadius + 1),
325
+ centre.z + Math.sin(deg * DEG) * (wallRadius + 1),
326
+ half * DEG * wallRadius * 1.75,
327
+ 1,
328
+ );
329
+ }
@@ -0,0 +1,269 @@
1
+ /**
2
+ * Impact crater bowls and shattered fallen boulders: high-resolution crater surfaces with thrown
3
+ * subsoil lips and rim stones blended cleanly into sloped ground, plus shattered boulder clusters
4
+ * with surrounding rock aprons. Pure geometry assembly into `GB` builders; worker-safe.
5
+ * @module
6
+ */
7
+
8
+ import { dim } from '../math/color.js';
9
+ import { createNoise } from '../math/noise.js';
10
+ import { TAU, lerp, sstep } from '../math/scalar.js';
11
+ import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
12
+ import { sampleSurface } from './sampled-surface.js';
13
+
14
+ const BOWL_ROUND = 64;
15
+ const BOWL_OUT = 20;
16
+ const TINT_ROUND = 36;
17
+ const TINT_OUT = 12;
18
+ const BOWL_LIFT = 0.1;
19
+ const APRON_SINK = 0.1;
20
+
21
+ let fallbackShapes = null;
22
+ const defaultShapes = () => (fallbackShapes ??= createUnitShapes());
23
+ let fallbackNoise = null;
24
+ const defaultVnoise = () => (fallbackNoise ??= createNoise().vnoise);
25
+
26
+ /** Default linear RGB soil/ash colours for impact craters built by {@link buildCraterBowl}. */
27
+ export const CRATER_PALETTE = Object.freeze({
28
+ char: Object.freeze([0.034, 0.029, 0.024]),
29
+ ash: Object.freeze([0.072, 0.068, 0.062]),
30
+ subsoil: Object.freeze([0.088, 0.064, 0.04]),
31
+ turf: Object.freeze([0.052, 0.046, 0.03]),
32
+ });
33
+
34
+ const mix3 = (a, b, k) => [lerp(a[0], b[0], k), lerp(a[1], b[1], k), lerp(a[2], b[2], k)];
35
+
36
+ function defaultCraterDip(crater, dx, dz, vnoise) {
37
+ const d = Math.hypot(dx, dz) || 1e-3;
38
+ const t = d / (crater.r * (0.86 + 0.28 * vnoise((dx / d) * 1.6 + crater.x, (dz / d) * 1.6 + crater.z, 381)));
39
+ return (t < 1 ? -crater.deep * (1 - t * t) : 0) + crater.deep * 0.42 * Math.exp(-(((t - 1.06) / 0.3) ** 2));
40
+ }
41
+
42
+ /**
43
+ * Build a high-resolution impact crater bowl and thrown-subsoil lip blended cleanly into surrounding
44
+ * sloped ground, ringed by ejected stones.
45
+ *
46
+ * @param {object} options
47
+ * @param {{ earth: import('./builder.js').GB, rock: import('./builder.js').GB }} options.builders - Destination builders.
48
+ * @param {() => number} [options.random=Math.random] - Repeatable PRNG in `[0, 1)`.
49
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
50
+ * @param {number} [options.x=0] - Crater centre X in metres.
51
+ * @param {number} [options.z=0] - Crater centre Z in metres.
52
+ * @param {{ r: number, deep?: number, x?: number, z?: number }} options.crater - Crater dimensions (`r` radius, `deep` depth in metres).
53
+ * @param {(crater: object, dx: number, dz: number) => number} [options.craterDip] - Crater height profile offset `(q, dx, dz) => dy`.
54
+ * @param {typeof CRATER_PALETTE} [options.palette=CRATER_PALETTE] - Linear RGB soil/ash colours.
55
+ * @param {import('three').BufferGeometry} [options.stoneShape] - Angular stone geometry for rim boulders.
56
+ * @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Value noise in `[0, 1)`.
57
+ * @param {Function} [options.color] - Linear RGB colour helper.
58
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
59
+ */
60
+ export function buildCraterBowl(options) {
61
+ const {
62
+ builders,
63
+ random = Math.random,
64
+ ground = () => 0,
65
+ x = 0,
66
+ z = 0,
67
+ crater: q,
68
+ vnoise = defaultVnoise(),
69
+ craterDip = (cr, dx, dz) => defaultCraterDip(cr, dx, dz, vnoise),
70
+ palette = CRATER_PALETTE,
71
+ stoneShape = defaultShapes().rock,
72
+ color = defaultColor,
73
+ transform = defaultTransform,
74
+ } = options;
75
+ const between = (a, b) => a + random() * (b - a);
76
+ const sd = random() * 40;
77
+ const tints = Float32Array.from({ length: TINT_ROUND * TINT_OUT }, () => random());
78
+ const n = 8;
79
+ const R = q.r * 1.75;
80
+ let base = 0;
81
+ let gx = 0;
82
+ let gz = 0;
83
+ for (let i = 0; i < n; i++) {
84
+ const a = (i * TAU) / n;
85
+ const h = ground(x + Math.cos(a) * R, z + Math.sin(a) * R);
86
+ base += h / n;
87
+ gx += (2 * h * Math.cos(a)) / (n * R);
88
+ gz += (2 * h * Math.sin(a)) / (n * R);
89
+ }
90
+ const before = (px, pz) => base + gx * (px - x) + gz * (pz - z);
91
+ const pastApron = (px, pz) => sstep(1.12, 1.55, (Math.hypot(px - x, pz - z) / q.r) * (0.9 + 0.2 * vnoise(px * 0.4, pz * 0.4, 38)));
92
+ const surfaceAt = (px, pz) => {
93
+ const g = ground(px, pz);
94
+ const fine = before(px, pz) + craterDip(q, px - x, pz - z) * (0.85 + 0.3 * vnoise(px * 0.5 + sd, pz * 0.5, 36));
95
+ return lerp(Math.max(fine, g + BOWL_LIFT), g - APRON_SINK, pastApron(px, pz));
96
+ };
97
+ const bowlAt = (u, v) => {
98
+ const a = u * TAU;
99
+ const t = 0.05 + 1.5 * v;
100
+ const px = x + Math.cos(a) * t * q.r;
101
+ const pz = z + Math.sin(a) * t * q.r;
102
+ return [px, pz, t];
103
+ };
104
+ const tintAt = (u, v) => {
105
+ const fu = u * TINT_ROUND;
106
+ const fv = Math.min(v * (TINT_OUT - 1), TINT_OUT - 1.001);
107
+ const i0 = Math.floor(fu) % TINT_ROUND;
108
+ const i1 = (i0 + 1) % TINT_ROUND;
109
+ const j0 = Math.floor(fv);
110
+ const ku = sstep(0, 1, fu - Math.floor(fu));
111
+ const kv = sstep(0, 1, fv - j0);
112
+ const at = (i, j) => tints[j * TINT_ROUND + i];
113
+ return lerp(lerp(at(i0, j0), at(i1, j0), ku), lerp(at(i0, j0 + 1), at(i1, j0 + 1), ku), kv);
114
+ };
115
+ sampleSurface(
116
+ builders.earth,
117
+ BOWL_ROUND,
118
+ BOWL_OUT,
119
+ (u, v) => {
120
+ const [px, pz] = bowlAt(u, v);
121
+ return [px, surfaceAt(px, pz), pz];
122
+ },
123
+ (u, v) => {
124
+ const [px, pz, t] = bowlAt(u, v);
125
+ const lip = Math.exp(-(((t - 1.06) / 0.2) ** 2));
126
+ const ash = sstep(0.45, 0.75, vnoise(px * 0.35 + sd, pz * 0.35, 39)) * (1 - lip);
127
+ const clods = lip * (0.45 + 0.55 * vnoise(px * 0.9, pz * 0.9, 37));
128
+ const rays = sstep(0.58, 0.8, vnoise(u * 23 + sd, t * 1.3, 40)) * sstep(0.95, 1.1, t) * (1 - sstep(1.25, 1.5, t));
129
+ let c = mix3(palette.char, palette.ash, ash * 0.55);
130
+ c = mix3(c, palette.subsoil, Math.min(1, clods * 1.3 + rays * 0.8));
131
+ c = mix3(c, palette.turf, pastApron(px, pz) * (1 - rays * 0.6));
132
+ return dim(c, 0.88 + 0.24 * tintAt(u, v));
133
+ },
134
+ { flip: true, blend: true },
135
+ );
136
+ for (let i = 0; i < 12; i++) {
137
+ const a = random() * TAU;
138
+ const d = q.r * between(0.95, 1.5);
139
+ const s2 = between(0.2, 0.55);
140
+ const px = x + Math.cos(a) * d;
141
+ const pz = z + Math.sin(a) * d;
142
+ builders.rock.add(
143
+ stoneShape,
144
+ color(0x3f3832, 0.2, random),
145
+ transform(px, Math.max(ground(px, pz), surfaceAt(px, pz)) - s2 * 0.3, pz, s2 * 1.5, s2, s2 * 1.2, random() * 6, random(), random()),
146
+ );
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Build a shattered fallen boulder cluster with surrounding rock fragments, optional circle
152
+ * collider, ground scorch scar, and impact crater bowl ({@link buildCraterBowl}).
153
+ *
154
+ * @param {object} options
155
+ * @param {{ basalt: import('./builder.js').GB, earth?: import('./builder.js').GB, rock?: import('./builder.js').GB }} options.builders - Destination builders.
156
+ * @param {() => number} [options.random=Math.random] - Repeatable PRNG in `[0, 1)`.
157
+ * @param {(x: number, z: number) => number} [options.ground] - Surface height sampler `(x, z) => y`.
158
+ * @param {number} [options.x=0] - Impact X in metres.
159
+ * @param {number} [options.z=0] - Impact Z in metres.
160
+ * @param {number} [options.size=3] - Main boulder nominal scale in metres.
161
+ * @param {number} [options.lift=0.08] - Vertical lift share of `size`.
162
+ * @param {number} [options.fragments=3] - Secondary large boulder fragments around the core.
163
+ * @param {number} [options.scarRadius] - Ground scar radius in metres (defaults to `size * 3.4`).
164
+ * @param {object | null} [options.crater=null] - Optional crater descriptor forwarded to {@link buildCraterBowl}.
165
+ * @param {(crater: object, dx: number, dz: number) => number} [options.craterDip] - Crater height profile callback.
166
+ * @param {import('three').BufferGeometry} [options.rockShape] - Main boulder unit shape.
167
+ * @param {import('three').BufferGeometry} [options.chipShape] - Small scattered fragment shape.
168
+ * @param {() => number[]} [options.rockColor] - Returns linear RGB boulder colour.
169
+ * @param {((x: number, z: number, radius: number) => void) | null} [options.onCollider=null] - Optional circle collider callback.
170
+ * @param {((x: number, z: number, radius: number, weight: number) => void) | null} [options.onScar=null] - Optional ground scar callback.
171
+ * @param {(x: number, z: number, salt: number) => number} [options.vnoise] - Value noise forwarded to {@link buildCraterBowl}.
172
+ * @param {Function} [options.color] - Linear RGB colour helper.
173
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
174
+ */
175
+ export function buildFallenBoulder(options) {
176
+ const {
177
+ builders,
178
+ random = Math.random,
179
+ ground = () => 0,
180
+ x = 0,
181
+ z = 0,
182
+ size: s = 3,
183
+ lift = 0.08,
184
+ fragments = 3,
185
+ scarRadius = undefined,
186
+ crater = null,
187
+ craterDip = undefined,
188
+ rockShape = defaultShapes().rock,
189
+ chipShape = defaultShapes().rock,
190
+ rockColor = null,
191
+ onCollider = null,
192
+ onScar = null,
193
+ vnoise = defaultVnoise(),
194
+ color = defaultColor,
195
+ transform = defaultTransform,
196
+ } = options;
197
+ const between = (a, b) => a + random() * (b - a);
198
+ const y = ground(x, z);
199
+ const bc =
200
+ rockColor ??
201
+ (() => {
202
+ const t = between(0.012, 0.03);
203
+ return [t, t, t * 1.15];
204
+ });
205
+ builders.basalt.add(
206
+ rockShape,
207
+ bc(),
208
+ transform(
209
+ x,
210
+ y + s * lift,
211
+ z,
212
+ s * between(1.3, 1.7),
213
+ s * between(0.95, 1.25),
214
+ s * between(0.95, 1.2),
215
+ random() * 6,
216
+ between(-0.35, 0.35),
217
+ between(-0.35, 0.35),
218
+ ),
219
+ );
220
+ for (let i = 0; i < fragments; i++) {
221
+ const a = random() * TAU;
222
+ const d = s * between(0.35, 0.8);
223
+ const q = s * between(0.35, 0.7);
224
+ builders.basalt.add(
225
+ rockShape,
226
+ bc(),
227
+ transform(
228
+ x + Math.cos(a) * d,
229
+ y - q * 0.3 + (i ? 0 : s * 0.35),
230
+ z + Math.sin(a) * d,
231
+ q * between(0.9, 1.6),
232
+ q * between(0.8, 1.5),
233
+ q,
234
+ random() * 6,
235
+ between(-0.7, 0.7),
236
+ between(-0.7, 0.7),
237
+ ),
238
+ );
239
+ }
240
+ for (let i = 0, n = Math.round(8 + s * 3); i < n; i++) {
241
+ const a = random() * TAU;
242
+ const d = s * between(0.8, 2.6);
243
+ const q = between(0.25, 0.9) * Math.min(1.6, s * 0.3);
244
+ const px = x + Math.cos(a) * d;
245
+ const pz = z + Math.sin(a) * d;
246
+ builders.basalt.add(
247
+ chipShape,
248
+ bc(),
249
+ transform(px, ground(px, pz) - q * 0.3, pz, q * between(0.8, 1.8), q, q, random() * 6, random(), random()),
250
+ );
251
+ }
252
+ onCollider?.(x, z, s * 0.72);
253
+ onScar?.(x, z, scarRadius ?? s * 3.4, 1);
254
+ if (crater) {
255
+ buildCraterBowl({
256
+ builders,
257
+ random,
258
+ ground,
259
+ x,
260
+ z,
261
+ crater,
262
+ craterDip,
263
+ stoneShape: chipShape,
264
+ vnoise,
265
+ color,
266
+ transform,
267
+ });
268
+ }
269
+ }