roamwright 0.61.27 → 0.61.28

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,309 @@
1
+ /**
2
+ * Subterranean chamber and cavern rock geometry: jointed living-rock walls with door openings,
3
+ * sagging cavern roofs, domed caverns with ceiling shafts and entry gaps, and hexagonal crystal
4
+ * clusters and cavern mineral columns. Pure geometry assembly into a `GB` builder; worker-safe.
5
+ * @module
6
+ */
7
+
8
+ import * as THREE from 'three';
9
+ import { dim } from '../math/color.js';
10
+ import { mulberry32 } from '../math/random.js';
11
+ import { lerp, TAU } from '../math/scalar.js';
12
+ import { GB } from './builder.js';
13
+ import { defaultRockHash as defaultHash, defaultRockNoise as defaultNoise, sampleRockSheet } from './creased-sheet.js';
14
+ import { C as defaultColor, TRS as defaultTransform, defaultUnitShapes as defaultShapes } from './primitives.js';
15
+ import { jointed } from './strata-rocks.js';
16
+
17
+ /** Default lime-crusted sandstone sRGB tones for underground chamber rock. */
18
+ export const CAVERN_ROCK_PALETTE = Object.freeze([0x8f7a62, 0x9c876c, 0x84705a, 0xa8937a]);
19
+
20
+ /** Default translucent mineral crystal sRGB tones for hexagonal prisms and columns. */
21
+ export const CRYSTAL_PALETTE = Object.freeze([0xd8d4e6, 0xc8b8e0, 0xe6dcc0, 0xbcd0d8]);
22
+
23
+ function sampleStoneTone(x, y, z, k, vnoise, palette, color) {
24
+ return dim(
25
+ color(palette[((x * 0.11 + z * 0.13 + y * 0.31) | 0) & (palette.length - 1)]),
26
+ k * (0.72 + 0.5 * vnoise(x * 0.3 + z * 0.23, y * 0.5, 991)),
27
+ );
28
+ }
29
+
30
+ /**
31
+ * Build a jointed living-rock chamber wall from `a` to `b` (`[x, z]` in metres) between elevations
32
+ * `y0` and `y1`, facing along `normal` (`[nx, nz]`), leaving rectangular doorway cutouts in `doors`.
33
+ *
34
+ * @param {import('./builder.js').GB} builder - Destination geometry builder.
35
+ * @param {[number, number]} a - Start point `[x, z]` in metres.
36
+ * @param {[number, number]} b - End point `[x, z]` in metres.
37
+ * @param {number} y0 - Floor elevation in metres.
38
+ * @param {number} y1 - Ceiling elevation in metres.
39
+ * @param {[number, number]} normal - Inward horizontal normal `[nx, nz]`.
40
+ * @param {Array<[number, number, number]>} [doors=[]] - Cutouts as `[fromFraction, toFraction, doorHeight]`.
41
+ * @param {object} [options]
42
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise sampler in `[0, 1)`.
43
+ * @param {readonly number[]} [options.palette=CAVERN_ROCK_PALETTE] - Rock sRGB hex palette.
44
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
45
+ */
46
+ export function buildRockWall(builder, a, b, y0, y1, normal, doors = [], options = {}) {
47
+ const vnoise = options.noise ?? defaultNoise();
48
+ const palette = options.palette ?? CAVERN_ROCK_PALETTE;
49
+ const color = options.color ?? defaultColor;
50
+ const length = Math.hypot(b[0] - a[0], b[1] - a[1]);
51
+ const cuts = [0, ...doors.flatMap(([from, to]) => [from, to]), 1];
52
+ const part = (u0, u1, low, high) => {
53
+ if (u1 - u0 < 0.002 || high - low < 0.05) return;
54
+ const at = (u, v) => {
55
+ const t = lerp(u0, u1, u);
56
+ const x = lerp(a[0], b[0], t);
57
+ const z = lerp(a[1], b[1], t);
58
+ const y = lerp(low, high, v);
59
+ const edge = Math.min(1, t * length, (1 - t) * length, (y - y0) * 2 + 0.2);
60
+ const out =
61
+ (0.55 * (jointed(vnoise(t * length * 0.22, y * 0.3, 992), 3) - 0.5) + 0.25 * (vnoise(t * length * 0.9, y * 0.9, 993) - 0.5)) * edge;
62
+ return [x + normal[0] * out, y, z + normal[1] * out];
63
+ };
64
+ sampleRockSheet(
65
+ builder,
66
+ Math.max(1, Math.round(((u1 - u0) * length) / 1.1)),
67
+ Math.max(1, Math.round((high - low) / 1.1)),
68
+ at,
69
+ (u, v) => {
70
+ const p = at(u, v);
71
+ return sampleStoneTone(p[0], p[1], p[2], 0.75 + 0.35 * v, vnoise, palette, color);
72
+ },
73
+ normal[0] * (b[1] - a[1]) - normal[1] * (b[0] - a[0]) > 0,
74
+ );
75
+ };
76
+ for (let k = 0; k < cuts.length - 1; k += 2) part(cuts[k], cuts[k + 1], y0, y1);
77
+ for (const [from, to, height] of doors) part(from, to, y0 + height, y1);
78
+ }
79
+
80
+ /**
81
+ * Build a sagging living-rock chamber ceiling over the axis-aligned rectangle `(x0..x1, z0..z1)`
82
+ * at nominal elevation `y`.
83
+ *
84
+ * @param {import('./builder.js').GB} builder - Destination geometry builder.
85
+ * @param {number} x0 - Minimum X in metres.
86
+ * @param {number} z0 - Minimum Z in metres.
87
+ * @param {number} x1 - Maximum X in metres.
88
+ * @param {number} z1 - Maximum Z in metres.
89
+ * @param {number} y - Nominal roof height in metres.
90
+ * @param {number} [sag=0.9] - Maximum downward hang in metres.
91
+ * @param {object} [options]
92
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise sampler in `[0, 1)`.
93
+ * @param {readonly number[]} [options.palette=CAVERN_ROCK_PALETTE] - Rock sRGB hex palette.
94
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
95
+ */
96
+ export function buildRockRoof(builder, x0, z0, x1, z1, y, sag = 0.9, options = {}) {
97
+ const vnoise = options.noise ?? defaultNoise();
98
+ const palette = options.palette ?? CAVERN_ROCK_PALETTE;
99
+ const color = options.color ?? defaultColor;
100
+ const at = (u, v) => {
101
+ const x = lerp(x0, x1, u);
102
+ const z = lerp(z0, z1, v);
103
+ return [x, y - sag * jointed(vnoise(x * 0.16, z * 0.16, 994), 3) + 0.3 * vnoise(x * 0.6, z * 0.6, 995), z];
104
+ };
105
+ sampleRockSheet(builder, Math.max(2, Math.round((x1 - x0) / 1.6)), Math.max(2, Math.round((z1 - z0) / 1.6)), at, (u, v) => {
106
+ const p = at(u, v);
107
+ return sampleStoneTone(p[0], p[1], p[2], 0.55, vnoise, palette, color);
108
+ });
109
+ }
110
+
111
+ /**
112
+ * Compute how far a cavern dome's wall stands at its foot along bearing `angle` (radians, from +X
113
+ * toward +Z), widening outward by 9 m across the entry passage bearing span `gap`.
114
+ *
115
+ * @param {number} radius - Nominal cavern radius in metres.
116
+ * @param {[number, number]} gap - Entry passage bearing range `[minAngle, maxAngle]` in radians.
117
+ * @param {number} angle - Bearing around the cavern centre in radians.
118
+ * @param {object} [options]
119
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise sampler in `[0, 1)`.
120
+ * @returns {number} Radial distance from the cavern centre in metres.
121
+ */
122
+ export function domeReach(radius, gap, angle, options = {}) {
123
+ const vnoise = options.noise ?? defaultNoise();
124
+ return (
125
+ radius * (1 + 0.1 * (vnoise(Math.cos(angle) * 2 + 3, Math.sin(angle) * 2 + 3, 996) - 0.5)) + (angle > gap[0] && angle < gap[1] ? 9 : 0)
126
+ );
127
+ }
128
+
129
+ /**
130
+ * Build a domed underground cavern centred at `(cx, cz)` over `floor`: curved rock vault descending
131
+ * from a circular roof opening (`hole` radius) with a vertical shaft rising `through` metres, plus
132
+ * the enclosing wall recessed across the entry passage bearing span `gap`.
133
+ *
134
+ * @param {import('./builder.js').GB} builder - Destination geometry builder.
135
+ * @param {number} cx - Cavern centre X in metres.
136
+ * @param {number} cz - Cavern centre Z in metres.
137
+ * @param {number} floor - Floor elevation in metres.
138
+ * @param {number} radius - Nominal cavern radius in metres.
139
+ * @param {number} height - Vault apex height above `floor` in metres.
140
+ * @param {number} hole - Circular ceiling opening radius in metres.
141
+ * @param {number} through - Vertical shaft thickness above the apex in metres.
142
+ * @param {[number, number]} gap - Passage opening bearing range `[minAngle, maxAngle]` in radians.
143
+ * @param {object} [options]
144
+ * @param {(x: number, z: number, salt: number) => number} [options.noise] - Value noise sampler in `[0, 1)`.
145
+ * @param {readonly number[]} [options.palette=CAVERN_ROCK_PALETTE] - Rock sRGB hex palette.
146
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
147
+ */
148
+ export function buildRockDome(builder, cx, cz, floor, radius, height, hole, through, gap, options = {}) {
149
+ const vnoise = options.noise ?? defaultNoise();
150
+ const palette = options.palette ?? CAVERN_ROCK_PALETTE;
151
+ const color = options.color ?? defaultColor;
152
+ const columns = 44;
153
+ const bearing = u => u * TAU;
154
+ sampleRockSheet(
155
+ builder,
156
+ columns,
157
+ 7,
158
+ (u, v) => {
159
+ const a = bearing(u);
160
+ const r = lerp(hole, radius, v) * (1 + 0.1 * (v > 0.05 ? vnoise(Math.cos(a) * 2 + 3, Math.sin(a) * 2 + v * 3, 996) - 0.5 : 0));
161
+ const y =
162
+ floor + height - (height * 0.55 * (r / radius) ** 2 + 0.5 * jointed(vnoise(Math.cos(a) * 3, Math.sin(a) * 3 + v * 4, 997), 2) * v);
163
+ return [cx + Math.cos(a) * r, y, cz + Math.sin(a) * r];
164
+ },
165
+ (u, v) =>
166
+ sampleStoneTone(
167
+ cx + Math.cos(bearing(u)) * radius * v,
168
+ floor + height,
169
+ cz + Math.sin(bearing(u)) * radius * v,
170
+ 0.5 + 0.2 * v,
171
+ vnoise,
172
+ palette,
173
+ color,
174
+ ),
175
+ true,
176
+ );
177
+ const wallTop = floor + height * 0.45;
178
+ sampleRockSheet(
179
+ builder,
180
+ columns,
181
+ 4,
182
+ (u, v) => {
183
+ const a = bearing(u);
184
+ const r = domeReach(radius, gap, a, { noise: vnoise }) + 0.8 * (1 - v) * vnoise(a * 7, v * 3, 998);
185
+ return [cx + Math.cos(a) * r, lerp(floor - 0.5, wallTop + 0.3, v), cz + Math.sin(a) * r];
186
+ },
187
+ (u, v) =>
188
+ sampleStoneTone(
189
+ cx + Math.cos(bearing(u)) * radius,
190
+ floor + v * 4,
191
+ cz + Math.sin(bearing(u)) * radius,
192
+ 0.6 + 0.3 * v,
193
+ vnoise,
194
+ palette,
195
+ color,
196
+ ),
197
+ );
198
+ sampleRockSheet(
199
+ builder,
200
+ 16,
201
+ 2,
202
+ (u, v) => {
203
+ const a = bearing(u);
204
+ const r = hole * (1 + 0.14 * (vnoise(Math.cos(a) * 2, Math.sin(a) * 2, 999) - 0.5));
205
+ return [cx + Math.cos(a) * r, floor + height + through * v, cz + Math.sin(a) * r];
206
+ },
207
+ (u, v) =>
208
+ sampleStoneTone(
209
+ cx + Math.cos(bearing(u)) * hole,
210
+ floor + height + v,
211
+ cz + Math.sin(bearing(u)) * hole,
212
+ 0.5 + 0.3 * v,
213
+ vnoise,
214
+ palette,
215
+ color,
216
+ ),
217
+ );
218
+ }
219
+
220
+ /**
221
+ * Append a cluster of leaning hexagonal mineral crystal prisms sprouting from `(x, y, z)` into
222
+ * `builder`, with the primary crystal reaching `size` metres.
223
+ *
224
+ * @param {import('./builder.js').GB} builder - Destination geometry builder.
225
+ * @param {number} x - Cluster root X in metres.
226
+ * @param {number} y - Cluster root Y in metres.
227
+ * @param {number} z - Cluster root Z in metres.
228
+ * @param {number} size - Tallest prism length in metres.
229
+ * @param {() => number} [random] - Deterministic PRNG in `[0, 1)`.
230
+ * @param {number} [lean=0.5] - Maximum outward tilt in radians.
231
+ * @param {object} [options]
232
+ * @param {object} [options.shapes] - Unit shapes containing `cyl6` and `cone6`.
233
+ * @param {readonly number[]} [options.palette=CRYSTAL_PALETTE] - Crystal sRGB hex palette.
234
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
235
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
236
+ */
237
+ export function buildCrystalCluster(builder, x, y, z, size, random = mulberry32(1), lean = 0.5, options = {}) {
238
+ const shapes = options.shapes ?? defaultShapes();
239
+ const palette = options.palette ?? CRYSTAL_PALETTE;
240
+ const color = options.color ?? defaultColor;
241
+ const transform = options.transform ?? defaultTransform;
242
+ const count = 4 + ((random() * 5) | 0);
243
+ for (let k = 0; k < count; k++) {
244
+ const a = random() * TAU;
245
+ const tilt = k ? lean * (0.3 + random()) : lean * 0.2 * random();
246
+ const length = size * (k ? 0.35 + random() * 0.6 : 1);
247
+ const thick = length * (0.1 + random() * 0.07);
248
+ const tone = dim(color(palette[(random() * palette.length) | 0]), 0.8 + random() * 0.5);
249
+ const turn = new THREE.Matrix4()
250
+ .makeRotationY(a)
251
+ .multiply(new THREE.Matrix4().makeRotationZ(tilt))
252
+ .setPosition(x, y - 0.1, z);
253
+ builder.add(shapes.cyl6, tone, turn.clone().multiply(transform(0, 0, 0, thick, length, thick)));
254
+ builder.add(shapes.cone6, dim(tone, 1.2), turn.clone().multiply(transform(0, length, 0, thick, thick * 1.5, thick)));
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Scatter hexagonal crystal clusters across a circular cavern floor around `(cx, cz)` and erect one
260
+ * stacked mineral column at `column` (`{ x, z, h }`).
261
+ *
262
+ * @param {number} cx - Cavern centre X in metres.
263
+ * @param {number} cz - Cavern centre Z in metres.
264
+ * @param {(x: number, z: number) => number} floorAt - Floor height sampler `(x, z) => y` in metres.
265
+ * @param {number} radius - Cavern radius in metres.
266
+ * @param {{ x: number, z: number, h: number }} column - Primary crystal column position and height in metres.
267
+ * @param {() => number} [random] - Deterministic PRNG in `[0, 1)`.
268
+ * @param {object} [options]
269
+ * @param {number} [options.clusters=46] - Number of crystal clusters scattered on the cavern floor.
270
+ * @param {(x: number, y: number, salt: number) => number} [options.hash] - Deterministic 2D hash in `[0, 1)`.
271
+ * @param {object} [options.shapes] - Unit shapes containing `cyl6` and `cone6`.
272
+ * @param {readonly number[]} [options.palette=CRYSTAL_PALETTE] - Crystal sRGB hex palette.
273
+ * @param {Function} [options.color] - Linear RGB colour helper (`C`).
274
+ * @param {Function} [options.transform] - Transform matrix helper (`TRS`).
275
+ * @param {typeof GB} [options.Builder=GB] - Geometry builder class.
276
+ * @returns {GB} Populated crystal geometry builder.
277
+ */
278
+ export function buildCavernCrystal(cx, cz, floorAt, radius, column, random = mulberry32(1), options = {}) {
279
+ const clusters = options.clusters ?? 46;
280
+ const hash2 = options.hash ?? defaultHash();
281
+ const shapes = options.shapes ?? defaultShapes();
282
+ const palette = options.palette ?? CRYSTAL_PALETTE;
283
+ const color = options.color ?? defaultColor;
284
+ const transform = options.transform ?? defaultTransform;
285
+ const Builder = options.Builder ?? GB;
286
+ const builder = new Builder();
287
+ for (let k = 0; k < clusters; k++) {
288
+ const a = random() * TAU;
289
+ const r = radius * (0.35 + 0.62 * Math.sqrt(random()));
290
+ const x = cx + Math.cos(a) * r;
291
+ const z = cz + Math.sin(a) * r;
292
+ buildCrystalCluster(builder, x, floorAt(x, z), z, 0.5 + random() * random() * 3.2, random, 0.5, {
293
+ shapes,
294
+ palette,
295
+ color,
296
+ transform,
297
+ });
298
+ }
299
+ const foot = floorAt(column.x, column.z);
300
+ for (let d = 0; d < column.h; d += 0.9) {
301
+ const girth = 1 + 0.16 * Math.sin(d * 1.3) + 0.2 * hash2(d * 10, 1, 990);
302
+ builder.add(
303
+ shapes.cyl6,
304
+ dim(color(palette[0]), 0.7 + 0.3 * hash2(d * 10, 2, 990)),
305
+ transform(column.x, foot + d - 0.2, column.z, girth, 1, girth * 0.9, d * 0.4),
306
+ );
307
+ }
308
+ return builder;
309
+ }