roamwright 0.61.25 → 0.61.26

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,27 @@
1
+ /**
2
+ * Pure impact-crater height-profile function: ragged bowl depression with a thrown-subsoil lip.
3
+ * Zero Three.js dependencies so terrain-height workers can import it without pulling Three.js.
4
+ * @module
5
+ */
6
+
7
+ import { createNoise } from '../math/noise.js';
8
+
9
+ let fallbackVnoise = null;
10
+ const defaultVnoise = () => (fallbackVnoise ??= createNoise().vnoise);
11
+
12
+ /**
13
+ * Compute the height offset (in metres) of an impact crater's ragged bowl and thrown-earth lip at
14
+ * local offset `(dx, dz)` from the crater centre. Negative inside the bowl (down to `-crater.deep`),
15
+ * rising cleanly along the rim near radius `crater.r * 1.06`.
16
+ *
17
+ * @param {{ x: number, z: number, r: number, deep: number }} crater - Crater centre (`x, z`), radius `r` and depth `deep` in metres.
18
+ * @param {number} dx - Offset east from crater centre in metres.
19
+ * @param {number} dz - Offset south from crater centre in metres.
20
+ * @param {(x: number, z: number, salt?: number) => number} [vnoise] - Seeded value noise in `[0, 1)`.
21
+ * @returns {number} Vertical height displacement in metres.
22
+ */
23
+ export function craterDip(crater, dx, dz, vnoise = defaultVnoise()) {
24
+ const d = Math.hypot(dx, dz) || 1e-3;
25
+ const t = d / (crater.r * (0.86 + 0.28 * vnoise((dx / d) * 1.6 + crater.x, (dz / d) * 1.6 + crater.z, 381)));
26
+ return (t < 1 ? -crater.deep * (1 - t * t) : 0) + crater.deep * 0.42 * Math.exp(-(((t - 1.06) / 0.3) ** 2));
27
+ }
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Ground-drape patch geometry and loose surface debris scatter: draped elliptical discs conforming
3
+ * to any height field with feathered ragged edges, trodden rim banking, per-vertex surface look
4
+ * attributes (`aLook`), scattered cobbles/pebbles and droppings, and faceted pebble geometry.
5
+ * Worker-safe.
6
+ * @module
7
+ */
8
+
9
+ import * as THREE from 'three';
10
+ import { createNoise } from '../math/noise.js';
11
+ import { mulberry32 } from '../math/random.js';
12
+ import { TAU, clamp, sstep } from '../math/scalar.js';
13
+
14
+ let fallbackFbm = null;
15
+ const defaultFbm = () => (fallbackFbm ??= createNoise().fbm);
16
+
17
+ /**
18
+ * Surface kind codes written into the `aLook.x` vertex attribute for `rendering/ground-drape.js`.
19
+ */
20
+ export const GROUND_PATCH_KINDS = Object.freeze({
21
+ trodden: 0,
22
+ cracked: 1,
23
+ stain: 2,
24
+ litter: 3,
25
+ });
26
+
27
+ /**
28
+ * Default feathering, mottling, rim-banking, rutting and loose-stone scatter settings for
29
+ * {@link drapeGroundPatch} and {@link scatterPatchBits}.
30
+ */
31
+ export const GROUND_PATCH_DEFAULTS = Object.freeze({
32
+ /** Fraction of patch radius out to which the patch remains full thickness. */
33
+ fadeFrom: 0.6,
34
+ /** Rim-fade noise modulation amplitude so patch edges break up organically. */
35
+ raggedFade: 0.25,
36
+ /** Vertex-colour mottling amplitude. */
37
+ mottle: 0.2,
38
+ /** Spatial frequency (cycles/m) of vertex-colour mottling noise. */
39
+ mottleGrain: 0.16,
40
+ /** Fractional radius where a trodden patch's kicked-up bank peaks. */
41
+ bankPeak: 0.66,
42
+ /** Fractional radius half-width of the trodden rim bank. */
43
+ bankWidth: 0.42,
44
+ /** Bank height in metres per metre of the patch's lesser radius. */
45
+ bankRise: 0.009,
46
+ /** Maximum rim-bank height in metres. */
47
+ bankMost: 0.22,
48
+ /** Minimum length-to-width ratio above which a trodden patch carries wheel ruts. */
49
+ rutsFrom: 1.8,
50
+ /** Square metres of trodden ground per scattered loose stone. */
51
+ stoneArea: 7,
52
+ /** Square metres of trodden ground per scattered beast dropping cluster. */
53
+ dungArea: 45,
54
+ /** Maximum scattered stones per patch (droppings cap at one quarter of this). */
55
+ bitsMost: 1400,
56
+ /** Stones generated between async `breathe()` yields. */
57
+ stonesABreath: 600,
58
+ /** Default sRGB stone colours for scattered pebbles and cobbles. */
59
+ stoneHues: Object.freeze([0x9a8c76, 0x857a68, 0xb0a28a, 0x6f6557, 0xa89478]),
60
+ /** Default sRGB colours for scattered animal droppings. */
61
+ dungHues: Object.freeze([0x4a3a26, 0x3e3424, 0x55462c, 0x4b4430]),
62
+ });
63
+
64
+ /**
65
+ * Create an empty ground-patch vertex/index/bit accumulator consumed by {@link drapeGroundPatch} and
66
+ * meshed by `rendering/ground-drape.js`.
67
+ *
68
+ * @returns {{ pos: number[], colours: number[], looks: number[], index: number[], bits: number[] }}
69
+ */
70
+ export function createGroundPatchData() {
71
+ return {
72
+ pos: [],
73
+ colours: [],
74
+ looks: [],
75
+ index: [],
76
+ bits: [],
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Build a faceted, noise-displaced pebble `BufferGeometry` with unit white vertex colours so
82
+ * instanced stones inherit per-instance colours cleanly on vertex-coloured rock materials.
83
+ *
84
+ * @param {(x: number, z: number, octaves?: number, salt?: number) => number} [fbm] - Fractal noise in `[0, 1)`.
85
+ * @returns {THREE.BufferGeometry}
86
+ */
87
+ export function buildDrapePebbleGeometry(fbm = defaultFbm()) {
88
+ const g = new THREE.IcosahedronGeometry(0.5, 0);
89
+ const a = g.attributes.position.array;
90
+ for (let i = 0; i < a.length; i += 3) {
91
+ const l = Math.hypot(a[i], a[i + 1], a[i + 2]);
92
+ const r = (0.5 * (0.7 + 0.6 * fbm((a[i] / l) * 1.9 + 7, (a[i + 2] / l) * 1.9 + (a[i + 1] / l) * 1.4, 2, 311))) / l;
93
+ a[i] *= r;
94
+ a[i + 1] *= r * 0.7;
95
+ a[i + 2] *= r;
96
+ }
97
+ g.computeVertexNormals();
98
+ g.setAttribute('color', new THREE.Float32BufferAttribute(new Float32Array(a.length).fill(1), 3));
99
+ return g;
100
+ }
101
+
102
+ /**
103
+ * Scatter kicked-loose stones and beast droppings onto a trodden ground patch around `(x, z)` in
104
+ * local frame `at`, appending packed 8-number tuples `[wx, wy, wz, size, height, turn, tilt, hex]`
105
+ * to `patches.bits`. Optionally yields via `breathe` every `cfg.stonesABreath` stones.
106
+ *
107
+ * @param {{ bits: number[] }} patches - Target patch data accumulator.
108
+ * @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} at - Local origin and ground height sampler.
109
+ * @param {() => number} rng - Dedicated repeatable PRNG stream in `[0, 1)`.
110
+ * @param {number} x - Patch centre local X in metres.
111
+ * @param {number} z - Patch centre local Z in metres.
112
+ * @param {number} rx - Patch semi-axis along X in metres.
113
+ * @param {number} rz - Patch semi-axis along Z in metres.
114
+ * @param {(() => Promise<void>) | null} [breathe=null] - Optional async frame-yield callback.
115
+ * @param {typeof GROUND_PATCH_DEFAULTS} [cfg=GROUND_PATCH_DEFAULTS] - Scatter tuning parameters.
116
+ */
117
+ export async function scatterPatchBits(patches, at, rng, x, z, rx, rz, breathe = null, cfg = GROUND_PATCH_DEFAULTS) {
118
+ const area = Math.PI * rx * rz;
119
+ const stones = Math.min(cfg.bitsMost, Math.round(area / cfg.stoneArea));
120
+ const dung = Math.min(cfg.bitsMost / 4, Math.round(area / cfg.dungArea));
121
+ const put = (px, pz, s, h, sink, col) =>
122
+ patches.bits.push(at.x0 + px, at.Y(px, pz) - sink, at.z0 + pz, s, h, rng() * TAU, (rng() - 0.5) * 0.5, col);
123
+ const inside = () => {
124
+ const a = rng() * TAU;
125
+ const d = Math.sqrt(rng()) * 0.85;
126
+ return [x + Math.cos(a) * rx * d, z + Math.sin(a) * rz * d];
127
+ };
128
+ for (let i = 0; i < stones; i++) {
129
+ const [px, pz] = inside();
130
+ const s = rng() < 0.12 ? 0.22 + rng() * 0.25 : 0.06 + rng() * 0.12;
131
+ put(px, pz, s, s, -s * 0.08, cfg.stoneHues[(rng() * cfg.stoneHues.length) | 0]);
132
+ if (breathe && i % cfg.stonesABreath === cfg.stonesABreath - 1) await breathe();
133
+ }
134
+ for (let i = 0; i < dung; i++) {
135
+ const [px, pz] = inside();
136
+ const col = cfg.dungHues[(rng() * cfg.dungHues.length) | 0];
137
+ if (rng() < 0.6) {
138
+ const balls = 3 + ((rng() * 4) | 0);
139
+ for (let k = 0; k < balls; k++) {
140
+ put(px + (rng() - 0.5) * 0.22, pz + (rng() - 0.5) * 0.22, 0.07 + rng() * 0.03, 0.06, -0.012, col);
141
+ }
142
+ } else {
143
+ put(px, pz, 0.22 + rng() * 0.12, 0.05, -0.006, col);
144
+ }
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Append one draped elliptical ground patch conforming to `at.Y(x, z)` into `patches`, with a
150
+ * ragged feathered rim, mottled linear vertex colours, per-vertex `aLook` surface parameters, and
151
+ * optional scattered loose stones and droppings for large `'trodden'` patches.
152
+ *
153
+ * @param {{ pos: number[], colours: number[], looks: number[], index: number[], bits: number[] }} patches - Accumulator from {@link createGroundPatchData}.
154
+ * @param {{ x0: number, z0: number, Y: (x: number, z: number) => number }} at - Local origin and ground height sampler.
155
+ * @param {() => number} rng - Repeatable PRNG in `[0, 1)`.
156
+ * @param {number} x - Patch centre local X in metres.
157
+ * @param {number} z - Patch centre local Z in metres.
158
+ * @param {number} rx - Patch semi-axis along X in metres.
159
+ * @param {number} rz - Patch semi-axis along Z in metres.
160
+ * @param {readonly [number, number, number]} col - Base linear RGB colour `[r, g, b]`.
161
+ * @param {object} [options]
162
+ * @param {number} [options.lift=0.04] - Vertical lift above the terrain surface in metres.
163
+ * @param {number} [options.rag=0.12] - Rim radial irregularity share.
164
+ * @param {'trodden' | 'cracked' | 'stain' | 'litter'} [options.kind='trodden'] - Patch surface kind.
165
+ * @param {(() => Promise<void>) | null} [options.breathe=null] - Optional async frame-yield callback.
166
+ * @param {(x: number, z: number, octaves?: number, salt?: number) => number} [options.fbm] - Fractal noise in `[0, 1)`.
167
+ * @param {Partial<typeof GROUND_PATCH_DEFAULTS>} [options.shape] - Overrides to {@link GROUND_PATCH_DEFAULTS}.
168
+ */
169
+ export async function drapeGroundPatch(patches, at, rng, x, z, rx, rz, col, options = {}) {
170
+ const { lift = 0.04, rag = 0.12, kind = 'trodden', breathe = null, fbm = defaultFbm() } = options;
171
+ const cfg = options.shape ? { ...GROUND_PATCH_DEFAULTS, ...options.shape } : GROUND_PATCH_DEFAULTS;
172
+ const trodden = kind === 'trodden';
173
+ const rings = Math.max(trodden ? 8 : 5, Math.ceil(Math.max(rx, rz) / 3.5));
174
+ const segs = Math.max(30, Math.ceil((Math.max(rx, rz) * TAU) / 5));
175
+ const { pos, colours, looks, index } = patches;
176
+ const first = pos.length / 3;
177
+ const edge = Array.from({ length: segs }, () => 1 + (rng() - 0.5) * 2 * rag);
178
+ const seed = Math.floor(rng() * 1e4);
179
+ // Dedicated stream so internal debris scattering never shifts the caller's layout RNG.
180
+ const own = mulberry32(seed + 7);
181
+ const long = Math.max(rx, rz) / Math.min(rx, rz);
182
+ const ruts = trodden ? clamp((long - cfg.rutsFrom) / 1.5, 0, 1) : 0;
183
+ const bearing = long > cfg.rutsFrom ? (rx >= rz ? 0 : Math.PI / 2) : own() * Math.PI;
184
+ const bank = trodden ? Math.min(cfg.bankMost, cfg.bankRise * Math.min(rx, rz)) : 0;
185
+ const code = GROUND_PATCH_KINDS[kind];
186
+ const vertex = (px, pz, y, out) => {
187
+ const wx = at.x0 + px;
188
+ const wz = at.z0 + pz;
189
+ const grain = 2 * fbm(wx * cfg.mottleGrain, wz * cfg.mottleGrain, 3, seed) - 1;
190
+ const mottle = 1 + cfg.mottle * grain;
191
+ const thick = out >= 1 ? 0 : sstep(1, cfg.fadeFrom, out + cfg.raggedFade * grain);
192
+ const rise = bank * Math.max(0, 1 - ((out - cfg.bankPeak) / cfg.bankWidth) ** 2) ** 2;
193
+ pos.push(wx, y + rise, wz);
194
+ colours.push(col[0] * mottle, col[1] * mottle, col[2] * mottle, thick);
195
+ looks.push(code, Math.min(out, 1), ruts, bearing);
196
+ };
197
+ vertex(x, z, at.Y(x, z) + lift, 0);
198
+ for (let r = 1; r <= rings; r++) {
199
+ for (let k = 0; k < segs; k++) {
200
+ const a = (k / segs) * TAU;
201
+ const d = (r / rings) * (r === rings ? edge[k] : 1);
202
+ const px = x + Math.cos(a) * rx * d;
203
+ const pz = z + Math.sin(a) * rz * d;
204
+ vertex(px, pz, at.Y(px, pz) + lift * (r === rings ? 0.3 : 1), r / rings);
205
+ }
206
+ }
207
+ for (let k = 0; k < segs; k++) {
208
+ const n = (k + 1) % segs;
209
+ index.push(first, first + 1 + n, first + 1 + k);
210
+ for (let r = 1; r < rings; r++) {
211
+ const a = first + 1 + (r - 1) * segs;
212
+ const b = a + segs;
213
+ index.push(a + k, a + n, b + k, a + n, b + n, b + k);
214
+ }
215
+ }
216
+ if (trodden && Math.min(rx, rz) > 3) {
217
+ if (breathe) await breathe();
218
+ await scatterPatchBits(patches, at, own, x, z, rx, rz, breathe, cfg);
219
+ }
220
+ }
@@ -6,8 +6,12 @@
6
6
  */
7
7
 
8
8
  import * as THREE from 'three';
9
+ import { createNoise } from '../math/noise.js';
9
10
  import { mulberry32 } from '../math/random.js';
10
- import { TAU } from '../math/scalar.js';
11
+ import { TAU, sstep } from '../math/scalar.js';
12
+
13
+ let fallbackVnoise = null;
14
+ const defaultVnoise = () => (fallbackVnoise ??= createNoise().vnoise);
11
15
 
12
16
  /**
13
17
  * Default tuft blade, stem and petal slot counts for {@link tuftGeometry}.
@@ -136,3 +140,66 @@ export function tuftGeometry(options = {}) {
136
140
  g.setIndex(index);
137
141
  return g;
138
142
  }
143
+
144
+ /** Default spatial frequency (cycles per metre) for wildflower drift noise in {@link createFlowerDrift}. */
145
+ export const DEFAULT_DRIFT_SCALE = 1 / 30;
146
+
147
+ /**
148
+ * Default drift parameters (`seed`, noise threshold `from`, peak tuft `share`) per built-in flower
149
+ * or seed-head species in {@link FLOWER_KINDS}.
150
+ */
151
+ export const DEFAULT_FLOWER_DRIFTS = Object.freeze({
152
+ poppy: Object.freeze({ seed: 41, from: 0.56, share: 0.7 }),
153
+ cornflower: Object.freeze({ seed: 42, from: 0.64, share: 0.3 }),
154
+ daisy: Object.freeze({ seed: 43, from: 0.6, share: 0.6 }),
155
+ buttercup: Object.freeze({ seed: 44, from: 0.6, share: 0.6 }),
156
+ yarrow: Object.freeze({ seed: 46, from: 0.6, share: 0.7 }),
157
+ tansy: Object.freeze({ seed: 47, from: 0.64, share: 0.6 }),
158
+ seeds: Object.freeze({ seed: 45, from: 0.5, share: 0.5 }),
159
+ });
160
+
161
+ /**
162
+ * Create a noise-driven wildflower drift sampler that evaluates per-species drift density at
163
+ * `(x, z)` and packs a chosen species and head count (`kind * 4 + heads`, or `0` for plain grass)
164
+ * into the `aFlower` attribute read by `rendering/plant-tuft.js`. Zero per-call allocation.
165
+ *
166
+ * @param {object} [options]
167
+ * @param {(x: number, z: number, salt?: number) => number} [options.vnoise] - Seeded value noise in `[0, 1)`.
168
+ * @param {Record<string, { seed: number, from: number, share: number }>} [options.drifts=DEFAULT_FLOWER_DRIFTS] - Drift table by species name.
169
+ * @param {number} [options.scale=DEFAULT_DRIFT_SCALE] - Noise coordinate scale in cycles per metre.
170
+ * @param {Record<string, number>} [options.kinds=FLOWER_KINDS] - Mapping from species name to 1-based shader flower index.
171
+ * @param {number} [options.ramp=0.2] - Smoothstep noise width from `from` to full drift `share`.
172
+ * @param {number} [options.maxHeads=3] - Maximum flower heads encoded per tuft (`1..maxHeads`).
173
+ * @returns {{
174
+ * driftOf: (kind: string, x: number, z: number) => number,
175
+ * pickFlower: (candidates: readonly string[] | null | undefined, x: number, z: number, pick: number) => number,
176
+ * }}
177
+ */
178
+ export function createFlowerDrift(options = {}) {
179
+ const {
180
+ vnoise = defaultVnoise(),
181
+ drifts = DEFAULT_FLOWER_DRIFTS,
182
+ scale = DEFAULT_DRIFT_SCALE,
183
+ kinds = FLOWER_KINDS,
184
+ ramp = 0.2,
185
+ maxHeads = 3,
186
+ } = options;
187
+
188
+ function driftOf(kind, x, z) {
189
+ const d = drifts[kind];
190
+ return d ? d.share * sstep(d.from, d.from + ramp, vnoise(x * scale, z * scale, d.seed)) : 0;
191
+ }
192
+
193
+ function pickFlower(candidates, x, z, pick) {
194
+ if (!candidates) return 0;
195
+ let below = 0;
196
+ for (let i = 0; i < candidates.length; i++) {
197
+ const kind = candidates[i];
198
+ below += driftOf(kind, x, z);
199
+ if (pick < below) return kinds[kind] * 4 + 1 + (Math.floor(pick * 1e4) % maxHeads);
200
+ }
201
+ return 0;
202
+ }
203
+
204
+ return { driftOf, pickFlower };
205
+ }
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Underground and vaulted stone-hall masonry builders: coursed stone walls with lintelled doorways
3
+ * and walk colliders, flagstone floors with optional cutouts, beam-and-slab flat ceilings, ribbed
4
+ * barrel vaults, eight-sided chambers, and corbelled stone roofs. Worker-safe.
5
+ * @module
6
+ */
7
+
8
+ import { dim } from '../math/color.js';
9
+ import { createHash } from '../math/random.js';
10
+ import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
11
+
12
+ let fallbackShapes = null;
13
+ const defaultShapes = () => (fallbackShapes ??= createUnitShapes());
14
+ let fallbackHash = null;
15
+ const defaultHash = () => (fallbackHash ??= createHash(42));
16
+
17
+ /** Default thickness of a coursed stone hall wall in metres. */
18
+ export const HALL_WALL_THICKNESS = 0.8;
19
+
20
+ /** Default pale dressed limestone sRGB hex colour for stone halls. */
21
+ export const HALL_STONE_TONE = 0xb9ad94;
22
+
23
+ /** Default darker stone sRGB hex colour for older underground chambers and corbelled roofs. */
24
+ export const HALL_OLD_STONE_TONE = 0x8a7c68;
25
+
26
+ /**
27
+ * Create a set of stone-hall masonry builders bound to a world's hash, unit shapes, transform
28
+ * helper and default wall thickness and tones.
29
+ *
30
+ * @param {object} [options]
31
+ * @param {(i: number, j: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
32
+ * @param {{ box: import('three').BufferGeometry }} [options.shapes] - Unit shapes providing `box` for vault voussoirs.
33
+ * @param {(hex: number, jitter?: number, rng?: () => number) => number[]} [options.color] - Linear RGB colour helper.
34
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
35
+ * @param {number} [options.wallThickness=HALL_WALL_THICKNESS] - Wall thickness in metres.
36
+ * @param {number} [options.tone=HALL_STONE_TONE] - Default stone sRGB hex colour for walls, floors, slab ceilings and barrel vaults.
37
+ * @param {number} [options.oldTone=HALL_OLD_STONE_TONE] - Default darker stone sRGB hex colour for octagonal chambers and corbel roofs.
38
+ * @returns {{
39
+ * wall: (K: object, a: [number, number], b: [number, number], y: number, h: number, gaps?: Array<{ at: number, w: number, h: number }>, stoneTone?: number, course?: number) => void,
40
+ * flags: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, size?: number, stoneTone?: number, skip?: ((x: number, z: number) => boolean) | null) => void,
41
+ * slabCeiling: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, stoneTone?: number) => void,
42
+ * barrelVault: (K: object, x0: number, x1: number, z0: number, z1: number, y: number, rise: number, stoneTone?: number) => void,
43
+ * octagonWalls: (K: object, c: { x: number, z: number }, r: number, y: number, h: number, doors?: Record<number, { w: number, h: number }>, stoneTone?: number) => void,
44
+ * corbelRoof: (K: object, c: { x: number, z: number }, r: number, y: number, stoneTone?: number) => void,
45
+ * }}
46
+ */
47
+ export function createStoneHalls(options = {}) {
48
+ const {
49
+ hash = defaultHash(),
50
+ shapes = defaultShapes(),
51
+ color = defaultColor,
52
+ transform = defaultTransform,
53
+ wallThickness = HALL_WALL_THICKNESS,
54
+ tone: defaultTone = HALL_STONE_TONE,
55
+ oldTone: defaultOldTone = HALL_OLD_STONE_TONE,
56
+ } = options;
57
+
58
+ /**
59
+ * Build a wall of coursed stone blocks along the centre line from `a` to `b` (`[x, z]` in metres),
60
+ * standing on elevation `y` and `h` metres high. Doorways in `gaps` (`{ at, w, h }`) receive stone
61
+ * jambs and lintels and are carved out of the box colliders pushed to `K.solid`.
62
+ */
63
+ function wall(K, a, b, y, h, gaps = [], stoneTone = defaultTone, course = 0.52) {
64
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]),
65
+ ux = (b[0] - a[0]) / len,
66
+ uz = (b[1] - a[1]) / len,
67
+ ry = -Math.atan2(uz, ux),
68
+ rows = Math.max(1, Math.round(h / course)),
69
+ rise = h / rows,
70
+ seed = Math.round(a[0] * 31 + a[1] * 17 + b[0] * 7 + b[1] * 3),
71
+ stone = color(stoneTone),
72
+ lay = (s0, s1, y0, sy, t, k) =>
73
+ K.B.stone.box(a[0] + ux * ((s0 + s1) / 2), y0, a[1] + uz * ((s0 + s1) / 2), s1 - s0, sy, t, dim(stone, k), ry);
74
+ for (let i = 0; i < rows; i++) {
75
+ const y0 = y + i * rise;
76
+ let s = i % 2 ? -0.5 : 0;
77
+ while (s < len - 0.01) {
78
+ const s0 = Math.max(0, s),
79
+ gap = gaps.find(g => y0 - y < g.h - 0.02 && s + 0.85 > g.at - g.w / 2 + 0.01 && s0 < g.at + g.w / 2 - 0.01);
80
+ if (gap) {
81
+ if (s0 < gap.at - gap.w / 2 - 0.05) lay(s0, gap.at - gap.w / 2, y0, rise - 0.015, wallThickness, 0.86);
82
+ s = gap.at + gap.w / 2;
83
+ continue;
84
+ }
85
+ let e = Math.min(len, s + 0.85 + 0.5 * hash(i, Math.round(s * 10), seed));
86
+ const next = gaps.find(g => y0 - y < g.h - 0.02 && e > g.at - g.w / 2 && s0 < g.at - g.w / 2);
87
+ if (next) e = next.at - next.w / 2;
88
+ else if (len - e < 0.3) e = len;
89
+ const worn = hash(Math.round(s * 10), i, seed + 1);
90
+ lay(s0, e - 0.015, y0, rise - 0.015, wallThickness + (worn - 0.5) * 0.05, 0.8 + 0.22 * worn - (i === 0 ? 0.08 : 0));
91
+ s = e;
92
+ }
93
+ }
94
+ for (const g of gaps) {
95
+ for (const sg of [-1, 1]) {
96
+ lay(g.at + sg * (g.w / 2 + 0.11) - 0.11, g.at + sg * (g.w / 2 + 0.11) + 0.11, y, g.h, wallThickness + 0.1, 1.04);
97
+ }
98
+ lay(g.at - g.w / 2 - 0.4, g.at + g.w / 2 + 0.4, y + g.h, 0.4, wallThickness + 0.12, 1.08);
99
+ }
100
+ // Walk colliders cover the wall segments less its doorways.
101
+ if (K.solid) {
102
+ const ox = K.O?.x ?? 0,
103
+ oz = K.O?.z ?? 0,
104
+ edges = [0, ...gaps.flatMap(g => [g.at - g.w / 2, g.at + g.w / 2]).sort((p, q) => p - q), len];
105
+ for (let i = 0; i < edges.length; i += 2) {
106
+ const m = (edges[i] + edges[i + 1]) / 2;
107
+ if (edges[i + 1] - edges[i] > 0.05) {
108
+ K.solid.push({ x: ox + a[0] + ux * m, z: oz + a[1] + uz * m, hx: (edges[i + 1] - edges[i]) / 2, hz: wallThickness / 2, rot: ry });
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Lay a floor of square stone flags over `(x0, z0)..(x1, z1)` with their top surfaces at `y`.
116
+ * An optional `skip(x, z)` callback omits individual flags for drains, pits or stair wells.
117
+ */
118
+ function flags(K, x0, x1, z0, z1, y, size = 1, stoneTone = defaultTone, skip = null) {
119
+ const nx = Math.round((x1 - x0) / size),
120
+ nz = Math.round((z1 - z0) / size),
121
+ sx = (x1 - x0) / nx,
122
+ sz = (z1 - z0) / nz;
123
+ for (let i = 0; i < nx; i++) {
124
+ for (let j = 0; j < nz; j++) {
125
+ const x = x0 + (i + 0.5) * sx,
126
+ z = z0 + (j + 0.5) * sz,
127
+ k = hash(i + Math.round(x0), j + Math.round(z0), 771);
128
+ if (skip && skip(x, z)) continue;
129
+ K.B.stone.box(x, y - 0.3, z, sx - 0.02, 0.3 - (k < 0.12 ? 0.02 : 0), sz - 0.02, dim(color(stoneTone), 0.66 + 0.2 * k));
130
+ }
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Build a flat stone ceiling at elevation `y` over `(x0, z0)..(x1, z1)`: cross-beams spanning the
136
+ * narrower dimension carrying roof slabs laid above them.
137
+ */
138
+ function slabCeiling(K, x0, x1, z0, z1, y, stoneTone = defaultTone) {
139
+ const alongX = x1 - x0 > z1 - z0,
140
+ run = alongX ? x1 - x0 : z1 - z0,
141
+ n = Math.max(1, Math.round(run / 2.2));
142
+ for (let i = 0; i <= n; i++) {
143
+ const s = (i / n) * run;
144
+ if (alongX) K.B.stone.box(x0 + s, y - 0.34, (z0 + z1) / 2, 0.42, 0.34, z1 - z0 + 0.5, dim(color(stoneTone), 0.6));
145
+ else K.B.stone.box((x0 + x1) / 2, y - 0.34, z0 + s, x1 - x0 + 0.5, 0.34, 0.42, dim(color(stoneTone), 0.6));
146
+ }
147
+ for (let i = 0; i < n; i++) {
148
+ const s = ((i + 0.5) / n) * run,
149
+ k = 0.5 + 0.12 * hash(i, Math.round(x0 + z0), 772);
150
+ if (alongX) K.B.stone.box(x0 + s, y, (z0 + z1) / 2, run / n - 0.02, 0.4, z1 - z0 + 0.6, dim(color(stoneTone), k));
151
+ else K.B.stone.box((x0 + x1) / 2, y, z0 + s, x1 - x0 + 0.6, 0.4, run / n - 0.02, dim(color(stoneTone), k));
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Build a stone barrel vault running along `x` from `x0` to `x1` over the span `z0..z1`, springing
157
+ * at elevation `y` and rising by `rise` metres, with transverse ribs proud of the voussoirs.
158
+ */
159
+ function barrelVault(K, x0, x1, z0, z1, y, rise, stoneTone = defaultTone) {
160
+ const half = (z1 - z0) / 2,
161
+ zc = (z0 + z1) / 2,
162
+ N = 14,
163
+ point = f => [zc + half * Math.cos(f), y + rise * Math.sin(f)],
164
+ ribs = Math.round((x1 - x0) / 4.5),
165
+ span = (xa, xb, proud, k0) => {
166
+ for (let j = 0; j < N; j++) {
167
+ const [za, ya] = point((j / N) * Math.PI),
168
+ [zb, yb] = point(((j + 1) / N) * Math.PI),
169
+ lenZ = Math.hypot(zb - za, yb - ya),
170
+ fm = ((j + 0.5) / N) * Math.PI,
171
+ nz = Math.cos(fm) / half,
172
+ ny = Math.sin(fm) / rise,
173
+ n = Math.hypot(nz, ny),
174
+ tilt = Math.atan2(nz / n, ny / n);
175
+ K.B.stone.add(
176
+ shapes.box,
177
+ dim(color(stoneTone), k0 + 0.1 * hash(j, Math.round(xa * 3), 773)),
178
+ transform(
179
+ (xa + xb) / 2,
180
+ (ya + yb) / 2 - (proud * ny) / n,
181
+ (za + zb) / 2 - (proud * nz) / n,
182
+ xb - xa - 0.02,
183
+ 0.5,
184
+ lenZ + 0.06,
185
+ 0,
186
+ tilt,
187
+ ),
188
+ );
189
+ }
190
+ };
191
+ for (let i = 0; i < ribs; i++) span(x0 + (i / ribs) * (x1 - x0), x0 + ((i + 1) / ribs) * (x1 - x0), 0, 0.5);
192
+ for (let i = 0; i <= ribs; i++) {
193
+ const x = Math.min(x1 - 0.25, Math.max(x0 + 0.25, x0 + (i / ribs) * (x1 - x0)));
194
+ span(x - 0.25, x + 0.25, 0.22, 0.7);
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Build the eight coursed walls of an octagonal chamber centred at `c` (`{ x, z }`) with apothem
200
+ * `r` metres to the inner face. `doors` maps side index (`0` east, `2` south, `4` west, `6` north)
201
+ * to `{ w, h }`.
202
+ */
203
+ function octagonWalls(K, c, r, y, h, doors = {}, stoneTone = defaultOldTone) {
204
+ const mid = r + wallThickness / 2,
205
+ half = (r + wallThickness) * Math.tan(Math.PI / 8);
206
+ for (let k = 0; k < 8; k++) {
207
+ const t = (k * Math.PI) / 4,
208
+ cx = c.x + Math.cos(t) * mid,
209
+ cz = c.z + Math.sin(t) * mid,
210
+ dx = -Math.sin(t),
211
+ dz = Math.cos(t),
212
+ gap = doors[k];
213
+ wall(K, [cx - dx * half, cz - dz * half], [cx + dx * half, cz + dz * half], y, h, gap ? [{ at: half, ...gap }] : [], stoneTone, 0.6);
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Build a seven-ring stepped corbelled stone roof over an octagonal chamber centred at `c` with
219
+ * apothem `r` metres, springing at elevation `y` and closed by a top capstone.
220
+ */
221
+ function corbelRoof(K, c, r, y, stoneTone = defaultOldTone) {
222
+ const rings = 7;
223
+ for (let i = 0; i < rings; i++) {
224
+ const ri = r + 0.5 - i * (r / rings),
225
+ half = (ri + 0.6) * Math.tan(Math.PI / 8);
226
+ for (let k = 0; k < 8; k++) {
227
+ const t = (k * Math.PI) / 4;
228
+ K.B.stone.box(
229
+ c.x + Math.cos(t) * ri,
230
+ y + i * 0.46,
231
+ c.z + Math.sin(t) * ri,
232
+ 1.9,
233
+ 0.5,
234
+ 2 * half,
235
+ dim(color(stoneTone), 0.62 - i * 0.03 + 0.08 * hash(i, k, 774)),
236
+ -t,
237
+ );
238
+ }
239
+ }
240
+ K.B.stone.box(c.x, y + rings * 0.46, c.z, 2.6, 0.4, 2.6, dim(color(stoneTone), 0.4), Math.PI / 8);
241
+ }
242
+
243
+ return { wall, flags, slabCeiling, barrelVault, octagonWalls, corbelRoof };
244
+ }
@@ -8,6 +8,7 @@
8
8
  import { dim } from '../math/color.js';
9
9
  import { createNoise } from '../math/noise.js';
10
10
  import { TAU, lerp, sstep } from '../math/scalar.js';
11
+ import { craterDip as defaultCraterDip } from './crater-profile.js';
11
12
  import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
12
13
  import { sampleSurface } from './sampled-surface.js';
13
14
 
@@ -33,11 +34,7 @@ export const CRATER_PALETTE = Object.freeze({
33
34
 
34
35
  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
 
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
- }
37
+ export { defaultCraterDip as craterDip };
41
38
 
42
39
  /**
43
40
  * Build a high-resolution impact crater bowl and thrown-subsoil lip blended cleanly into surrounding
@@ -4,11 +4,15 @@
4
4
  * @module
5
5
  */
6
6
 
7
+ import * as THREE from 'three';
8
+ import { createHash } from '../math/random.js';
7
9
  import { TAU } from '../math/scalar.js';
8
10
  import { C as defaultColor, TRS as defaultTransform, createUnitShapes } from './primitives.js';
9
11
 
10
12
  let fallbackRock = null;
11
13
  const defaultRock = () => (fallbackRock ??= createUnitShapes().rock);
14
+ let fallbackHash = null;
15
+ const defaultHash = () => (fallbackHash ??= createHash(42));
12
16
 
13
17
  /**
14
18
  * Default tone and weathering settings for heaped fallen masonry built by {@link buildRubbleHeap}.
@@ -38,6 +42,30 @@ export const RUBBLE_SCATTER = Object.freeze({
38
42
  dressed: 0.45,
39
43
  });
40
44
 
45
+ /**
46
+ * Build a cheap, angular faceted rubble-stone `BufferGeometry` by displacing an icosahedron's
47
+ * vertices with a seeded 2D hash and slightly lifting its base so scattered fragments seat cleanly
48
+ * on heaps and broken masonry.
49
+ *
50
+ * @param {object} [options]
51
+ * @param {(x: number, z: number, salt?: number) => number} [options.hash] - Seeded 2D hash in `[0, 1)`.
52
+ * @param {number} [options.radius=0.5] - Base icosahedron radius in metres.
53
+ * @param {number} [options.salt=23] - Hash salt.
54
+ * @returns {THREE.BufferGeometry}
55
+ */
56
+ export function createRubbleStone({ hash = defaultHash(), radius = 0.5, salt = 23 } = {}) {
57
+ const g = new THREE.IcosahedronGeometry(radius, 0);
58
+ const a = g.attributes.position.array;
59
+ for (let i = 0; i < a.length; i += 3) {
60
+ const k = 0.7 + 0.6 * hash(Math.round(a[i] * 9), Math.round(a[i + 2] * 9 + a[i + 1] * 5), salt);
61
+ a[i] *= k;
62
+ a[i + 1] = a[i + 1] * k * 0.8 + 0.3;
63
+ a[i + 2] *= k;
64
+ }
65
+ g.computeVertexNormals();
66
+ return g;
67
+ }
68
+
41
69
  function weatheredTone(hue, random, course, cfg, color) {
42
70
  const col = color(hue, cfg.jitter, random);
43
71
  const k = (course < cfg.dampCourses ? 0.8 + 0.08 * course : 1) * (random() < cfg.stained ? 0.78 : 1);