@threenative/procedural-environment 0.1.3 → 0.1.4

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,3959 @@
1
+ import * as THREE from 'three';
2
+ import { MeshStandardMaterial, MeshBasicMaterial, Shape, ExtrudeGeometry, TorusGeometry, LatheGeometry, Vector2, BufferGeometry, Float32BufferAttribute, Vector3, PointLight, Mesh, Group, Color, BufferAttribute, CylinderGeometry, Quaternion, IcosahedronGeometry } from 'three';
3
+ import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
4
+ import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js';
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+
12
+ // vendor/stylized-biome-kit/index.js
13
+ var stylized_biome_kit_exports = {};
14
+ __export(stylized_biome_kit_exports, {
15
+ BARK: () => BARK,
16
+ GRASS: () => GRASS,
17
+ GROUND: () => GROUND,
18
+ LEAF: () => LEAF,
19
+ PINE: () => PINE,
20
+ RNG: () => RNG,
21
+ ROCK: () => ROCK,
22
+ STRATA: () => STRATA,
23
+ THREE: () => THREE,
24
+ Tris: () => Tris,
25
+ WATER: () => WATER,
26
+ arch: () => arch,
27
+ blob: () => blob,
28
+ boulder: () => boulder,
29
+ bush: () => bush,
30
+ clamp: () => clamp,
31
+ cliff: () => cliff,
32
+ cloud: () => cloud,
33
+ col: () => col,
34
+ columnCactus: () => columnCactus,
35
+ deadTree: () => deadTree,
36
+ displace: () => displace,
37
+ facet: () => facet,
38
+ fbm: () => fbm,
39
+ fern: () => fern,
40
+ flat: () => flat,
41
+ flowers: () => flowers,
42
+ frond: () => frond,
43
+ grassTuft: () => grassTuft,
44
+ groundPatch: () => groundPatch2,
45
+ lathe: () => lathe,
46
+ lerp: () => lerp,
47
+ log: () => log,
48
+ materials: () => materials,
49
+ merge: () => merge,
50
+ mesa: () => mesa,
51
+ mixc: () => mixc,
52
+ mountain: () => mountain,
53
+ noise3: () => noise3,
54
+ paint: () => paint,
55
+ paintFaces: () => paintFaces,
56
+ palm: () => palm,
57
+ pebbles: () => pebbles,
58
+ piece: () => piece,
59
+ pine: () => pine,
60
+ profile: () => profile,
61
+ roundTree: () => roundTree,
62
+ saguaro: () => saguaro,
63
+ scatter: () => scatter,
64
+ shrub: () => shrub,
65
+ sky: () => sky,
66
+ smoothstep: () => smoothstep,
67
+ spires: () => spires,
68
+ strataField: () => strataField,
69
+ stump: () => stump,
70
+ terrain: () => terrain,
71
+ terrainSample: () => terrainSample,
72
+ tube: () => tube,
73
+ twistedTree: () => twistedTree,
74
+ uniforms: () => uniforms,
75
+ water: () => water,
76
+ waterfall: () => waterfall,
77
+ weld: () => weld,
78
+ xform: () => xform
79
+ });
80
+
81
+ // vendor/stylized-biome-kit/palettes.js
82
+ var ROCK = {
83
+ alpine: { base: 10660022, light: 14277600, dark: 6713487, cap: 7915076, capDark: 4889146 },
84
+ snow: { base: 12767978, light: 16186111, dark: 9479888, cap: 16777215, capDark: 14477560 },
85
+ desert: { base: 13135962, light: 15049090, dark: 10308934, cap: 15116406, capDark: 14258783 }
86
+ };
87
+ var STRATA = {
88
+ desert: {
89
+ bands: [12147786, 13200728, 13728614, 12739164, 14192252, 11490120],
90
+ streaks: [15846329, 15315878, 16178379],
91
+ top: 15248250,
92
+ topDark: 14391397
93
+ }
94
+ };
95
+ var PINE = {
96
+ forest: { low: 1002042, mid: 2524735, tip: 8835143, trunk: 7029811 },
97
+ teal: { low: 869960, mid: 1866344, tip: 6080146, trunk: 6111288 },
98
+ olive: { low: 3955754, mid: 8822836, tip: 14079058, trunk: 7227952 },
99
+ snow: { low: 5934284, mid: 11849970, tip: 16777215, trunk: 5917266 }
100
+ };
101
+ var LEAF = {
102
+ green: { dark: 3116860, light: 8376394 },
103
+ deep: { dark: 2059077, light: 4171872 },
104
+ autumn: { dark: 13660202, light: 16102984 },
105
+ frost: { dark: 11127023, light: 16777215 },
106
+ desert: { dark: 12098094, light: 15191890 },
107
+ palm: { dark: 4168242, light: 11130942 }
108
+ };
109
+ var GRASS = {
110
+ green: { base: 5222460, tip: 10675800 },
111
+ dry: { base: 14264433, tip: 16178332 },
112
+ frost: { base: 10863850, tip: 16120575 }
113
+ };
114
+ var GROUND = {
115
+ grass: { a: 5617982, b: 8376908, path: 14272928 },
116
+ snow: { a: 14674683, b: 16777215, path: 13097202 },
117
+ sand: { a: 15249545, b: 15910812, path: 14260856, wet: 13209468 }
118
+ };
119
+ var WATER = {
120
+ river: { shallow: 10154210, deep: 3384038, foam: 16777215 },
121
+ ice: { shallow: 14283263, deep: 8370156, foam: 16777215 },
122
+ alpine: { shallow: 9429970, deep: 3112900, foam: 16777215 }
123
+ };
124
+ var BARK = { brown: 7029811, dark: 3813181, grey: 8154994, palm: 9071180 };
125
+
126
+ // vendor/stylized-biome-kit/core.js
127
+ var uniforms = { uTime: { value: 0 } };
128
+ var RNG = class {
129
+ constructor(seed = 1) {
130
+ this.s = seed * 2654435761 >>> 0 || 1;
131
+ }
132
+ next() {
133
+ let t = this.s += 1831565813;
134
+ t = Math.imul(t ^ t >>> 15, t | 1);
135
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
136
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
137
+ }
138
+ range(a, b) {
139
+ return a + (b - a) * this.next();
140
+ }
141
+ int(a, b) {
142
+ return Math.floor(this.range(a, b + 1));
143
+ }
144
+ pick(arr) {
145
+ return arr[Math.floor(this.next() * arr.length)];
146
+ }
147
+ chance(p) {
148
+ return this.next() < p;
149
+ }
150
+ seed() {
151
+ return Math.floor(this.next() * 1e9);
152
+ }
153
+ };
154
+ function hash3(x, y, z, s) {
155
+ let h = Math.imul(x, 374761393) ^ Math.imul(y, 668265263) ^ Math.imul(z, 1274126177) ^ Math.imul(s, 974711);
156
+ h = Math.imul(h ^ h >>> 13, 1274126177);
157
+ return ((h ^ h >>> 16) >>> 0) / 4294967295;
158
+ }
159
+ function noise3(x, y, z, seed = 0) {
160
+ const xi = Math.floor(x), yi = Math.floor(y), zi = Math.floor(z);
161
+ const xf = x - xi, yf = y - yi, zf = z - zi;
162
+ const u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf), w = zf * zf * (3 - 2 * zf);
163
+ const l = (a, b, t) => a + (b - a) * t;
164
+ const c = (dx, dy, dz) => hash3(xi + dx, yi + dy, zi + dz, seed);
165
+ return l(
166
+ l(l(c(0, 0, 0), c(1, 0, 0), u), l(c(0, 1, 0), c(1, 1, 0), u), v),
167
+ l(l(c(0, 0, 1), c(1, 0, 1), u), l(c(0, 1, 1), c(1, 1, 1), u), v),
168
+ w
169
+ ) * 2 - 1;
170
+ }
171
+ function fbm(x, y, z, octaves = 3, seed = 0) {
172
+ let a = 0.5, f = 1, sum = 0, norm = 0;
173
+ for (let i = 0; i < octaves; i++) {
174
+ sum += a * noise3(x * f, y * f, z * f, seed + i * 17);
175
+ norm += a;
176
+ a *= 0.5;
177
+ f *= 2.03;
178
+ }
179
+ return sum / norm;
180
+ }
181
+ var clamp = (x, a, b) => Math.min(b, Math.max(a, x));
182
+ var lerp = (a, b, t) => a + (b - a) * t;
183
+ var smoothstep = (a, b, x) => {
184
+ const t = clamp((x - a) / (b - a), 0, 1);
185
+ return t * t * (3 - 2 * t);
186
+ };
187
+ function profile(points) {
188
+ return (t) => {
189
+ if (t <= points[0][0]) return points[0][1];
190
+ for (let i = 1; i < points.length; i++) {
191
+ if (t <= points[i][0]) {
192
+ const [t0, v0] = points[i - 1], [t1, v1] = points[i];
193
+ return lerp(v0, v1, smoothstep(0, 1, (t - t0) / (t1 - t0)));
194
+ }
195
+ }
196
+ return points[points.length - 1][1];
197
+ };
198
+ }
199
+ var col = (h) => new THREE.Color(h);
200
+ var _tmp = new THREE.Color();
201
+ function mixc(out, a, b, t) {
202
+ out.set(a);
203
+ _tmp.set(b);
204
+ return out.lerp(_tmp, clamp(t, 0, 1));
205
+ }
206
+ function ensureColor(geo) {
207
+ if (!geo.attributes.color) {
208
+ const n = geo.attributes.position.count;
209
+ geo.setAttribute("color", new THREE.BufferAttribute(new Float32Array(n * 3).fill(1), 3));
210
+ }
211
+ return geo;
212
+ }
213
+ function ensureSurf(geo) {
214
+ if (!geo.attributes.surf) geo.setAttribute("surf", new THREE.BufferAttribute(new Float32Array(geo.attributes.position.count * 4), 4));
215
+ return geo.attributes.surf;
216
+ }
217
+ function ensureCap(geo) {
218
+ if (!geo.attributes.cap) geo.setAttribute("cap", new THREE.BufferAttribute(new Float32Array(geo.attributes.position.count * 4), 4));
219
+ return geo.attributes.cap;
220
+ }
221
+ function paint(geo, fn) {
222
+ ensureColor(geo);
223
+ const pos = geo.attributes.position, nor = geo.attributes.normal, c = geo.attributes.color;
224
+ const sa = fn.length >= 5 ? ensureSurf(geo) : null, ca = fn.length >= 6 ? ensureCap(geo) : null;
225
+ const p = new THREE.Vector3(), n = new THREE.Vector3(), o = new THREE.Color(), sv = new THREE.Vector4(), cv = new THREE.Vector4();
226
+ for (let i = 0; i < pos.count; i++) {
227
+ p.fromBufferAttribute(pos, i);
228
+ if (nor) n.fromBufferAttribute(nor, i);
229
+ o.setRGB(1, 1, 1);
230
+ sv.set(0, 0, 0, 0);
231
+ cv.set(1, 1, 1, 0);
232
+ fn(p, n, o, i, sv, cv);
233
+ c.setXYZ(i, o.r, o.g, o.b);
234
+ if (sa) sa.setXYZW(i, sv.x, sv.y, sv.z, sv.w);
235
+ if (ca) ca.setXYZW(i, cv.x, cv.y, cv.z, cv.w);
236
+ }
237
+ return geo;
238
+ }
239
+ function paintFaces(geo, fn) {
240
+ ensureColor(geo);
241
+ const pos = geo.attributes.position, c = geo.attributes.color, sa = fn.length >= 5 ? ensureSurf(geo) : null;
242
+ const a = new THREE.Vector3(), b = new THREE.Vector3(), d = new THREE.Vector3();
243
+ const cen = new THREE.Vector3(), n = new THREE.Vector3(), o = new THREE.Color(), sv = new THREE.Vector4();
244
+ for (let f = 0; f < pos.count / 3; f++) {
245
+ a.fromBufferAttribute(pos, f * 3);
246
+ b.fromBufferAttribute(pos, f * 3 + 1);
247
+ d.fromBufferAttribute(pos, f * 3 + 2);
248
+ cen.copy(a).add(b).add(d).multiplyScalar(1 / 3);
249
+ n.copy(b).sub(a).cross(d.sub(a)).normalize();
250
+ o.setRGB(1, 1, 1);
251
+ sv.set(0, 0, 0, 0);
252
+ fn(cen, n, o, f, sv);
253
+ for (let k = 0; k < 3; k++) {
254
+ c.setXYZ(f * 3 + k, o.r, o.g, o.b);
255
+ if (sa) sa.setXYZW(f * 3 + k, sv.x, sv.y, sv.z, sv.w);
256
+ }
257
+ }
258
+ return geo;
259
+ }
260
+ function flat(geo) {
261
+ const g = geo.index ? geo.toNonIndexed() : geo;
262
+ g.computeVertexNormals();
263
+ return g;
264
+ }
265
+ function displace(geo, fn) {
266
+ const pos = geo.attributes.position, p = new THREE.Vector3();
267
+ for (let i = 0; i < pos.count; i++) {
268
+ p.fromBufferAttribute(pos, i);
269
+ fn(p, i);
270
+ pos.setXYZ(i, p.x, p.y, p.z);
271
+ }
272
+ pos.needsUpdate = true;
273
+ return geo;
274
+ }
275
+ function xform(geo, { pos = [0, 0, 0], rot = [0, 0, 0], scale = 1 } = {}) {
276
+ const s = Array.isArray(scale) ? scale : [scale, scale, scale];
277
+ const m = new THREE.Matrix4().compose(
278
+ new THREE.Vector3(...pos),
279
+ new THREE.Quaternion().setFromEuler(new THREE.Euler(...rot, "YXZ")),
280
+ new THREE.Vector3(...s)
281
+ );
282
+ geo.applyMatrix4(m);
283
+ return geo;
284
+ }
285
+ function merge(geos, withSurf = false) {
286
+ const list = geos.map((g) => {
287
+ const n = g.index ? g.toNonIndexed() : g;
288
+ if (!n.attributes.normal) n.computeVertexNormals();
289
+ return ensureColor(n);
290
+ });
291
+ const total = list.reduce((s, g) => s + g.attributes.position.count, 0);
292
+ const P = new Float32Array(total * 3), N = new Float32Array(total * 3), C = new Float32Array(total * 3), S = withSurf ? new Float32Array(total * 4) : null, K = withSurf ? new Float32Array(total * 4) : null, G = withSurf ? new Float32Array(total * 3).fill(1e3) : null;
293
+ let o = 0, so = 0;
294
+ for (const g of list) {
295
+ P.set(g.attributes.position.array, o);
296
+ N.set(g.attributes.normal.array, o);
297
+ C.set(g.attributes.color.array, o);
298
+ if (S && g.attributes.surf) S.set(g.attributes.surf.array, so);
299
+ if (K && g.attributes.cap) K.set(g.attributes.cap.array, so);
300
+ if (G && g.attributes.edge) G.set(g.attributes.edge.array, o);
301
+ o += g.attributes.position.count * 3;
302
+ so += g.attributes.position.count * 4;
303
+ }
304
+ const out = new THREE.BufferGeometry();
305
+ out.setAttribute("position", new THREE.BufferAttribute(P, 3));
306
+ out.setAttribute("normal", new THREE.BufferAttribute(N, 3));
307
+ out.setAttribute("color", new THREE.BufferAttribute(C, 3));
308
+ if (S) {
309
+ out.setAttribute("surf", new THREE.BufferAttribute(S, 4));
310
+ out.setAttribute("cap", new THREE.BufferAttribute(K, 4));
311
+ out.setAttribute("edge", new THREE.BufferAttribute(G, 3));
312
+ }
313
+ out.computeBoundingSphere();
314
+ out.computeBoundingBox();
315
+ return out;
316
+ }
317
+ function weld(geo) {
318
+ const pos = geo.attributes.position, map = /* @__PURE__ */ new Map(), P = [], I = [];
319
+ for (let i = 0; i < pos.count; i++) {
320
+ const x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i), key = `${Math.round(x * 1e4)},${Math.round(y * 1e4)},${Math.round(z * 1e4)}`;
321
+ let id = map.get(key);
322
+ if (id === void 0) {
323
+ id = P.length / 3;
324
+ map.set(key, id);
325
+ P.push(x, y, z);
326
+ }
327
+ I.push(id);
328
+ }
329
+ const g = new THREE.BufferGeometry();
330
+ g.setAttribute("position", new THREE.Float32BufferAttribute(P, 3));
331
+ g.setIndex(I);
332
+ return g;
333
+ }
334
+ function facet(geo, { angle = 27 } = {}) {
335
+ const pos = geo.attributes.position.array, idx = geo.index.array, nt = idx.length / 3, nv = pos.length / 3;
336
+ const V3 = (i, out) => out.set(pos[i * 3], pos[i * 3 + 1], pos[i * 3 + 2]);
337
+ const a = new THREE.Vector3(), b = new THREE.Vector3(), c = new THREE.Vector3(), e = new THREE.Vector3();
338
+ const FN = [], FC = [], edges = /* @__PURE__ */ new Map(), key = (i, j) => i < j ? i * nv + j : j * nv + i;
339
+ for (let t = 0; t < nt; t++) {
340
+ V3(idx[t * 3], a);
341
+ V3(idx[t * 3 + 1], b);
342
+ V3(idx[t * 3 + 2], c);
343
+ FC.push(a.clone().add(b).add(c).multiplyScalar(1 / 3));
344
+ FN.push(b.clone().sub(a).cross(e.copy(c).sub(a)).normalize());
345
+ for (let k = 0; k < 3; k++) {
346
+ const kk = key(idx[t * 3 + k], idx[t * 3 + (k + 1) % 3]);
347
+ if (!edges.has(kk)) edges.set(kk, []);
348
+ edges.get(kk).push(t);
349
+ }
350
+ }
351
+ const P = new Float32Array(nt * 9), N = new Float32Array(nt * 9), E = new Float32Array(nt * 9).fill(1e3), limit = Math.cos(angle * Math.PI / 180);
352
+ for (let t = 0; t < nt; t++) {
353
+ const v = [idx[t * 3], idx[t * 3 + 1], idx[t * 3 + 2]];
354
+ for (let k = 0; k < 3; k++) {
355
+ P.set([pos[v[k] * 3], pos[v[k] * 3 + 1], pos[v[k] * 3 + 2]], t * 9 + k * 3);
356
+ N.set([FN[t].x, FN[t].y, FN[t].z], t * 9 + k * 3);
357
+ const i = v[(k + 1) % 3], j = v[(k + 2) % 3], other = edges.get(key(i, j)).find((o) => o !== t);
358
+ if (other === void 0 || FN[t].dot(FN[other]) > limit || e.copy(FC[other]).sub(FC[t]).dot(FN[t]) > 0) continue;
359
+ V3(v[k], a);
360
+ V3(i, b);
361
+ V3(j, c);
362
+ const height = e.copy(a).sub(b).cross(c.sub(b)).length() / c.length();
363
+ for (let m = 0; m < 3; m++) E[t * 9 + m * 3 + k] = m === k ? height : 0;
364
+ }
365
+ }
366
+ const g = new THREE.BufferGeometry();
367
+ g.setAttribute("position", new THREE.BufferAttribute(P, 3));
368
+ g.setAttribute("normal", new THREE.BufferAttribute(N, 3));
369
+ g.setAttribute("edge", new THREE.BufferAttribute(E, 3));
370
+ return g;
371
+ }
372
+ function strataField(k) {
373
+ const gh = (i) => {
374
+ const v = i * 0.61803398875 + k * 0.7548776662 + 0.31;
375
+ return v - Math.floor(v);
376
+ };
377
+ const wob = (x, z) => 0.35 * Math.sin(x * 0.31 + k) + 0.3 * Math.sin(z * 0.27 + k * 2) + 0.15 * Math.sin((x + z) * 0.9 + k * 3);
378
+ return {
379
+ bulge(x, y, z) {
380
+ const yy = y + wob(x, z), a = yy / 2.1 + k, ia = Math.floor(a), fa = a - ia;
381
+ let v = lerp((gh(ia + 3) - 0.5) * 0.11, (gh(ia + 4) - 0.5) * 0.11, smoothstep(0.86, 1, fa));
382
+ const b = yy / 0.8 + k * 1.7, ib = Math.floor(b), fb = b - ib;
383
+ if (gh(ib + 17) >= 0.7) {
384
+ const w = 0.28 + 0.25 * gh(ib + 5);
385
+ v += 0.035 * smoothstep(0.2, 0.27, fb) * (1 - smoothstep(0.23 + w, 0.3 + w, fb));
386
+ }
387
+ return v;
388
+ }
389
+ };
390
+ }
391
+ function tube(path, radius, { radial = 8, capStart = false, capEnd = true, color = null, flatten = 1 } = {}) {
392
+ const n = path.length, P = [], C = [], I = [];
393
+ const T = new THREE.Vector3(), N = new THREE.Vector3(), B = new THREE.Vector3(), p = new THREE.Vector3(), o = new THREE.Color();
394
+ for (let i = 0; i < n; i++) {
395
+ T.copy(path[Math.min(n - 1, i + 1)]).sub(path[Math.max(0, i - 1)]).normalize();
396
+ if (i === 0) {
397
+ N.set(1, 0, 0);
398
+ if (Math.abs(T.x) > 0.9) N.set(0, 0, 1);
399
+ }
400
+ N.addScaledVector(T, -N.dot(T)).normalize();
401
+ B.crossVectors(T, N);
402
+ const t = i / (n - 1);
403
+ for (let k = 0; k < radial; k++) {
404
+ const th = k / radial * Math.PI * 2, r = radius(t, th);
405
+ p.copy(path[i]).addScaledVector(N, Math.cos(th) * r).addScaledVector(B, Math.sin(th) * r * flatten);
406
+ P.push(p.x, p.y, p.z);
407
+ if (color) {
408
+ o.setRGB(1, 1, 1);
409
+ color(t, th, o);
410
+ C.push(o.r, o.g, o.b);
411
+ }
412
+ }
413
+ }
414
+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < radial; k++) {
415
+ const a = i * radial + k, b = i * radial + (k + 1) % radial, c = a + radial, d = b + radial;
416
+ I.push(a, b, c, b, d, c);
417
+ }
418
+ const cap = (ring2, pt, flip) => {
419
+ const ci = P.length / 3;
420
+ P.push(pt.x, pt.y, pt.z);
421
+ if (color) {
422
+ o.setRGB(1, 1, 1);
423
+ color(flip ? 0 : 1, 0, o);
424
+ C.push(o.r, o.g, o.b);
425
+ }
426
+ for (let k = 0; k < radial; k++) {
427
+ const a = ring2 * radial + k, b = ring2 * radial + (k + 1) % radial;
428
+ if (flip) I.push(ci, b, a);
429
+ else I.push(ci, a, b);
430
+ }
431
+ };
432
+ if (capEnd) cap(n - 1, path[n - 1], false);
433
+ if (capStart) cap(0, path[0], true);
434
+ const g = new THREE.BufferGeometry();
435
+ g.setAttribute("position", new THREE.Float32BufferAttribute(P, 3));
436
+ if (color) g.setAttribute("color", new THREE.Float32BufferAttribute(C, 3));
437
+ g.setIndex(I);
438
+ g.computeVertexNormals();
439
+ return g;
440
+ }
441
+ function lathe({ height, rings = 20, radial = 24, radius, sx = 1, sz = 1, dome = 0.04, color = null }) {
442
+ const P = [], C = [], I = [], o = new THREE.Color();
443
+ for (let j = 0; j <= rings; j++) {
444
+ const t = j / rings, y = t * height;
445
+ for (let i = 0; i < radial; i++) {
446
+ const th = i / radial * Math.PI * 2, r = radius(t, th, y);
447
+ P.push(Math.cos(th) * r * sx, y, Math.sin(th) * r * sz);
448
+ if (color) {
449
+ o.setRGB(1, 1, 1);
450
+ color(t, th, y, o, false);
451
+ C.push(o.r, o.g, o.b);
452
+ }
453
+ }
454
+ }
455
+ for (let j = 0; j < rings; j++) for (let i = 0; i < radial; i++) {
456
+ const a = j * radial + i, b = j * radial + (i + 1) % radial, c = a + radial, d = b + radial;
457
+ I.push(a, c, b, b, c, d);
458
+ }
459
+ const top = rings * radial, inner = P.length / 3;
460
+ for (let i = 0; i < radial; i++) {
461
+ P.push(P[(top + i) * 3] * 0.55, height * (1 + dome * 0.6), P[(top + i) * 3 + 2] * 0.55);
462
+ if (color) {
463
+ o.setRGB(1, 1, 1);
464
+ color(1, 0, height, o, true);
465
+ C.push(o.r, o.g, o.b);
466
+ }
467
+ }
468
+ const centre = P.length / 3;
469
+ P.push(0, height * (1 + dome), 0);
470
+ if (color) {
471
+ o.setRGB(1, 1, 1);
472
+ color(1, 0, height, o, true);
473
+ C.push(o.r, o.g, o.b);
474
+ }
475
+ for (let i = 0; i < radial; i++) {
476
+ const a = top + i, b = top + (i + 1) % radial, c = inner + i, d = inner + (i + 1) % radial;
477
+ I.push(a, c, b, b, c, d, c, centre, d);
478
+ }
479
+ const g = new THREE.BufferGeometry();
480
+ g.setAttribute("position", new THREE.Float32BufferAttribute(P, 3));
481
+ if (color) g.setAttribute("color", new THREE.Float32BufferAttribute(C, 3));
482
+ g.setIndex(I);
483
+ g.computeVertexNormals();
484
+ return g;
485
+ }
486
+ function blob(rng, { r = 1, detail = 1, squash = [1, 1, 1], lumpy = 0.25 } = {}) {
487
+ const g = new THREE.IcosahedronGeometry(r, detail), s = rng.seed() % 1e3;
488
+ displace(g, (p) => {
489
+ const k = 1 + lumpy * noise3(p.x / r * 1.3 + s, p.y / r * 1.3, p.z / r * 1.3, s);
490
+ p.multiplyScalar(k);
491
+ p.x *= squash[0];
492
+ p.y *= squash[1];
493
+ p.z *= squash[2];
494
+ });
495
+ return flat(g);
496
+ }
497
+ var NOISE_GLSL = (
498
+ /* glsl */
499
+ `
500
+ float kh(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.x + p.y) * p.z); }
501
+ float kn(vec3 p) { vec3 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);
502
+ return mix(mix(mix(kh(i), kh(i + vec3(1, 0, 0)), f.x), mix(kh(i + vec3(0, 1, 0)), kh(i + vec3(1, 1, 0)), f.x), f.y),
503
+ mix(mix(kh(i + vec3(0, 0, 1)), kh(i + vec3(1, 0, 1)), f.x), mix(kh(i + vec3(0, 1, 1)), kh(i + vec3(1, 1, 1)), f.x), f.y), f.z); }
504
+ // crisp two-tone split of a noise value, antialiased with screen-space derivatives
505
+ float cut(float v, float at) { float w = max(fwidth(v) * 1.2, 0.004); return smoothstep(at - w, at + w, v); }`
506
+ );
507
+ var ROCK_GLSL = (
508
+ /* glsl */
509
+ `
510
+ float rockDetail(vec3 p, float k) {
511
+ vec3 q = p + k * 7.31;
512
+ float strokes = cut(kn(q * vec3(1.0, 0.15, 1.0)), 0.64);
513
+ float blotch = cut(kn(q * vec3(0.3, 0.18, 0.3) + 4.0), 0.62);
514
+ vec3 f = q * vec3(0.4, 0.13, 0.4) + 0.6 * vec3(kn(q * 0.28), 0.0, kn(q * 0.28 + 7.0));
515
+ float ridge = abs(kn(f + 11.0) - 0.5), w = max(fwidth(ridge), 0.002);
516
+ float crack = (1.0 - smoothstep(w * 0.7, w * 1.3, ridge)) * cut(kn(q * 0.15 + 5.0), 0.63);
517
+ return (0.975 + 0.05 * strokes - 0.04 * blotch) * (1.0 - 0.2 * crack);
518
+ }`
519
+ );
520
+ var STRATA_GLSL = (
521
+ /* glsl */
522
+ `
523
+ uniform vec3 uBands[6]; uniform vec3 uStreaks[3];
524
+ float gh(float i, float k) { return fract(i * 0.61803398875 + k * 0.7548776662 + 0.31); }
525
+ vec3 strata(vec3 p, float k) {
526
+ float y = p.y + 0.35 * sin(p.x * 0.31 + k) + 0.3 * sin(p.z * 0.27 + k * 2.0) + 0.15 * sin((p.x + p.z) * 0.9 + k * 3.0);
527
+ y += 0.1 * (kn(p * vec3(0.9, 0.4, 0.9) + k) - 0.5); // hand-drawn wobble on the edges
528
+ float a = y / 2.1 + k, ia = floor(a), fa = fract(a), wa = max(fwidth(a) * 1.2, 0.01);
529
+ vec3 c = mix(uBands[int(gh(ia, k) * 5.999)], uBands[int(gh(ia + 1.0, k) * 5.999)], smoothstep(1.0 - wa, 1.0, fa));
530
+ c *= 0.9 + 0.15 * fa; // each layer is darker at its foot
531
+ float b = y / 0.8 + k * 1.7, ib = floor(b), fb = fract(b), wb = max(fwidth(b) * 1.2, 0.012);
532
+ float wd = 0.28 + 0.25 * gh(ib + 5.0, k);
533
+ float streak = step(0.7, gh(ib + 17.0, k)) * smoothstep(0.25 - wb, 0.25 + wb, fb) * (1.0 - smoothstep(0.25 + wd - wb, 0.25 + wd + wb, fb));
534
+ c = mix(c, uStreaks[int(gh(ib + 9.0, k) * 2.999)], streak * 0.92);
535
+ float thin = y / 0.23 + k * 3.3; // hairline layers
536
+ c *= 1.0 - 0.07 * step(0.62, gh(floor(thin) + 31.0, k)) * cut(fract(thin), 0.55);
537
+ return c * (0.97 + 0.06 * kn(vec3(p.x * 0.5, y * 6.0, p.z * 0.5)));
538
+ }`
539
+ );
540
+ var strataUniforms = {
541
+ uBands: { value: STRATA.desert.bands.map((h) => new THREE.Color(h)) },
542
+ uStreaks: { value: STRATA.desert.streaks.map((h) => new THREE.Color(h)) }
543
+ };
544
+ function surfacePatch(mat) {
545
+ mat.onBeforeCompile = (sh) => {
546
+ Object.assign(sh.uniforms, strataUniforms);
547
+ sh.vertexShader = sh.vertexShader.replace("#include <common>", "#include <common>\nattribute vec4 surf; attribute vec4 cap; attribute vec3 edge; varying vec4 vSurf; varying vec4 vCap; varying vec3 vEdge; varying vec3 vObj;").replace("#include <begin_vertex>", "#include <begin_vertex>\nvSurf = surf; vCap = cap; vEdge = edge; vObj = position;");
548
+ sh.fragmentShader = sh.fragmentShader.replace("#include <common>", `#include <common>
549
+ varying vec4 vSurf; varying vec4 vCap; varying vec3 vEdge; varying vec3 vObj;${NOISE_GLSL}${ROCK_GLSL}${STRATA_GLSL}`).replace("#include <color_fragment>", `#include <color_fragment>
550
+ {
551
+ float wRock = clamp(vSurf.x, 0.0, 1.0), wStrata = clamp(vSurf.y, 0.0, 1.0), wAny = max(wRock, wStrata);
552
+ if (wStrata > 0.002) diffuseColor.rgb = mix(diffuseColor.rgb, strata(vObj, vSurf.z) * diffuseColor.rgb, wStrata);
553
+ if (wRock > 0.002) {
554
+ // worn edge: a crisp, slightly ragged light line along the hard outward edges
555
+ float d = min(vEdge.x, min(vEdge.y, vEdge.z)) / max(vSurf.w, 0.001);
556
+ float worn = 1.0 - cut(d + (kn(vObj * 2.3 + vSurf.z) - 0.5) * 0.6, 1.0);
557
+ diffuseColor.rgb *= mix(1.0, rockDetail(vObj, vSurf.z) * (1.0 + 0.17 * worn), wRock);
558
+ }
559
+ if (vCap.a > 0.002) { // grass or snow lying on the rock, with a ragged edge and a shadow line under it
560
+ float m = vCap.a + (kn(vObj * 1.1 + 3.0) - 0.5) * 0.5;
561
+ float e = cut(m, 0.5);
562
+ diffuseColor.rgb *= 1.0 - 0.2 * (cut(m, 0.4) - e);
563
+ diffuseColor.rgb = mix(diffuseColor.rgb, vCap.rgb, e);
564
+ }
565
+ }`);
566
+ };
567
+ mat.customProgramCacheKey = () => "surface";
568
+ return mat;
569
+ }
570
+ function groundPatch(mat) {
571
+ mat.onBeforeCompile = (sh) => {
572
+ sh.vertexShader = sh.vertexShader.replace("#include <common>", "#include <common>\nattribute vec3 tint; attribute float mask; varying vec3 vTint; varying float vMask; varying vec3 vObj;").replace("#include <begin_vertex>", "#include <begin_vertex>\nvTint = tint; vMask = mask; vObj = position;");
573
+ sh.fragmentShader = sh.fragmentShader.replace("#include <common>", `#include <common>
574
+ varying vec3 vTint; varying float vMask; varying vec3 vObj;${NOISE_GLSL}`).replace("#include <color_fragment>", `#include <color_fragment>
575
+ {
576
+ vec3 g = vec3(vObj.x, 0.0, vObj.z);
577
+ float m = vMask + (kn(g * 0.8) - 0.5) * 0.3 + (kn(g * 3.3 + 9.0) - 0.5) * 0.12;
578
+ float edge = cut(m, 0.5);
579
+ diffuseColor.rgb = mix(diffuseColor.rgb, vTint, edge);
580
+ // painted ground: broad two-tone patches plus a fine grain
581
+ float patches = 0.6 * cut(kn(g * 0.22 + 4.0), 0.52) + 0.4 * cut(kn(g * 0.7 + 12.0), 0.58);
582
+ diffuseColor.rgb *= (0.955 + 0.075 * patches) * (0.98 + 0.04 * kn(g * 4.0));
583
+ diffuseColor.rgb *= 1.0 - 0.1 * edge * (1.0 - cut(m, 0.62)); // darker rim just inside the edge
584
+ }`);
585
+ };
586
+ mat.customProgramCacheKey = () => "ground";
587
+ return mat;
588
+ }
589
+ function windPatch(mat, strength, keepNormals = false) {
590
+ mat.onBeforeCompile = (sh) => {
591
+ sh.uniforms.uTime = uniforms.uTime;
592
+ if (keepNormals) sh.fragmentShader = sh.fragmentShader.replace("#include <normal_fragment_maps>", "#include <normal_fragment_maps>\n normal = normalize(vNormal);");
593
+ sh.vertexShader = sh.vertexShader.replace("#include <common>", "#include <common>\nuniform float uTime;").replace("#include <begin_vertex>", `#include <begin_vertex>
594
+ {
595
+ #ifdef USE_INSTANCING
596
+ vec4 wp = modelMatrix * instanceMatrix * vec4(position, 1.0);
597
+ #else
598
+ vec4 wp = modelMatrix * vec4(position, 1.0);
599
+ #endif
600
+ float sway = sin(uTime * 1.6 + wp.x * 0.35 + wp.z * 0.27) + 0.5 * sin(uTime * 2.7 + wp.x * 0.9);
601
+ float w = ${strength.toFixed(3)} * max(position.y, 0.0);
602
+ transformed.x += sway * w; transformed.z += sway * w * 0.6;
603
+ }`);
604
+ };
605
+ mat.customProgramCacheKey = () => "wind" + strength + keepNormals;
606
+ return mat;
607
+ }
608
+ var materials = {
609
+ solid: surfacePatch(new THREE.MeshLambertMaterial({ vertexColors: true })),
610
+ ground: groundPatch(new THREE.MeshLambertMaterial({ vertexColors: true, flatShading: true })),
611
+ foliage: windPatch(new THREE.MeshLambertMaterial({ vertexColors: true, side: THREE.DoubleSide }), 4e-3),
612
+ grass: windPatch(new THREE.MeshLambertMaterial({ vertexColors: true, side: THREE.DoubleSide }), 0.12, true),
613
+ cloud: new THREE.MeshLambertMaterial({ vertexColors: true, emissive: 16777215, emissiveIntensity: 0.35, fog: false })
614
+ };
615
+ function piece(name, parts, meta = {}) {
616
+ const g = new THREE.Group();
617
+ g.name = name;
618
+ for (const key of ["solid", "foliage", "grass", "cloud"]) {
619
+ if (!parts[key] || !parts[key].length) continue;
620
+ const m = new THREE.Mesh(merge(parts[key], key === "solid"), materials[key]);
621
+ m.name = `${name}:${key}`;
622
+ m.castShadow = key !== "grass" && key !== "cloud";
623
+ m.receiveShadow = key !== "cloud";
624
+ g.add(m);
625
+ }
626
+ g.userData = { kit: name, ...meta };
627
+ return g;
628
+ }
629
+ var Tris = class {
630
+ constructor() {
631
+ this.P = [];
632
+ this.C = [];
633
+ }
634
+ tri(a, b, c, ca, cb, cc) {
635
+ this.P.push(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
636
+ this.C.push(ca.r, ca.g, ca.b, cb.r, cb.g, cb.b, cc.r, cc.g, cc.b);
637
+ }
638
+ get count() {
639
+ return this.P.length / 9;
640
+ }
641
+ build({ normalsUp = false } = {}) {
642
+ const g = new THREE.BufferGeometry();
643
+ g.setAttribute("position", new THREE.Float32BufferAttribute(this.P, 3));
644
+ g.setAttribute("color", new THREE.Float32BufferAttribute(this.C, 3));
645
+ if (normalsUp) {
646
+ const n = new Float32Array(this.P.length);
647
+ for (let i = 1; i < n.length; i += 3) n[i] = 1;
648
+ g.setAttribute("normal", new THREE.BufferAttribute(n, 3));
649
+ } else g.computeVertexNormals();
650
+ return g;
651
+ }
652
+ };
653
+ var UP = new THREE.Vector3(0, 1, 0);
654
+ function frond(tris, {
655
+ origin,
656
+ yaw = 0,
657
+ length = 1,
658
+ width = 0.3,
659
+ rise = 0.2,
660
+ droop = 0.6,
661
+ segs = 7,
662
+ serr = 0.45,
663
+ fold = 0.25,
664
+ c0,
665
+ c1,
666
+ taper = 0.6,
667
+ edge = 0.82,
668
+ roll = 0
669
+ }) {
670
+ const f = new THREE.Vector3(Math.cos(yaw), 0, Math.sin(yaw));
671
+ const side = new THREE.Vector3(Math.sin(yaw), 0, -Math.cos(yaw));
672
+ const S = [], L = [], R = [], CS = [], CE = [];
673
+ for (let k = 0; k <= segs; k++) {
674
+ const s = k / segs;
675
+ const sp = origin.clone().addScaledVector(f, length * s).addScaledVector(UP, length * (rise * s - droop * s * s));
676
+ let w = width * Math.pow(Math.sin(Math.PI * Math.pow(s, taper)), 0.9);
677
+ if (serr && k % 2 === 0) w *= 1 - serr;
678
+ const back = serr && k % 2 === 1 ? -0.35 * length / segs : 0;
679
+ const down = fold * w;
680
+ S.push(sp);
681
+ L.push(sp.clone().addScaledVector(side, w).addScaledVector(UP, -down + roll * w).addScaledVector(f, back));
682
+ R.push(sp.clone().addScaledVector(side, -w).addScaledVector(UP, -down - roll * w).addScaledVector(f, back));
683
+ const c = c0.clone().lerp(c1, s);
684
+ CS.push(c);
685
+ CE.push(c.clone().multiplyScalar(edge));
686
+ }
687
+ for (let k = 0; k < segs; k++) {
688
+ tris.tri(S[k], L[k], L[k + 1], CS[k], CE[k], CE[k + 1]);
689
+ tris.tri(S[k], L[k + 1], S[k + 1], CS[k], CE[k + 1], CS[k + 1]);
690
+ tris.tri(S[k], R[k + 1], R[k], CS[k], CE[k + 1], CE[k]);
691
+ tris.tri(S[k], S[k + 1], R[k + 1], CS[k], CS[k + 1], CE[k + 1]);
692
+ }
693
+ }
694
+ var TAU = Math.PI * 2;
695
+ var N3 = (x, y, z) => new THREE.Vector3(x, y, z).normalize();
696
+ var scratch = new Float64Array(96);
697
+ function exitT(lump, ox, oy, oz, dx, dy, dz) {
698
+ let tin = -1e9, tmin = 1e9, m = 0;
699
+ for (const { n, d } of lump.planes) {
700
+ const nd = n.x * dx + n.y * dy + n.z * dz, slack = d - (n.x * ox + n.y * oy + n.z * oz);
701
+ if (Math.abs(nd) < 1e-6) {
702
+ if (slack < 0) return -1;
703
+ continue;
704
+ }
705
+ const t = slack / nd;
706
+ if (nd > 0) {
707
+ scratch[m++] = t;
708
+ if (t < tmin) tmin = t;
709
+ } else if (t > tin) tin = t;
710
+ }
711
+ if (!m) return -1;
712
+ let sum = 0;
713
+ for (let i = 0; i < m; i++) sum += Math.exp(-(scratch[i] - tmin) / lump.round);
714
+ const tout = tmin - lump.round * Math.log(sum);
715
+ return tout > Math.max(tin, 0) ? tout : -1;
716
+ }
717
+ function castOut(lumps, ox, oy, oz, dx, dy, dz, blend) {
718
+ let best = -1;
719
+ for (const lump of lumps) {
720
+ const t = exitT(lump, ox, oy, oz, dx, dy, dz);
721
+ if (t < 0) continue;
722
+ best = best < 0 ? t : Math.max(best, t) + blend * Math.log(1 + Math.exp(-Math.abs(best - t) / blend));
723
+ }
724
+ return best;
725
+ }
726
+ function prismLump(rng, { cx = 0, cz = 0, a, b, top, taper = 0.9, walls = 8, chips = 4, round = 0.4 }) {
727
+ const planes = [], cy = top / 2, spin = rng.range(0, TAU);
728
+ const add = (n, d) => planes.push({ n, d: d + n.x * cx + n.y * cy + n.z * cz });
729
+ for (let i = 0; i < walls; i++) {
730
+ const phi = spin + (i + rng.range(-0.32, 0.32)) / walls * TAU, x = Math.cos(phi), z = Math.sin(phi);
731
+ const R = Math.pow(Math.pow(Math.abs(a * x), 4 / 3) + Math.pow(Math.abs(b * z), 4 / 3), 3 / 4);
732
+ const n = N3(x, R * (1 - taper) / top + rng.range(-0.08, 0.08), z);
733
+ add(n, R * (1 + taper) * 0.5 * Math.hypot(n.x, n.z) * rng.range(0.84, 1));
734
+ }
735
+ for (let i = 0; i < chips; i++) {
736
+ const phi = rng.range(0, TAU), n = N3(Math.cos(phi), rng.chance(0.55) ? rng.range(0.3, 0.9) : -rng.range(0.08, 0.25), Math.sin(phi));
737
+ add(n, (a * Math.abs(n.x) + top / 2 * Math.abs(n.y) + b * Math.abs(n.z)) * rng.range(0.8, 0.93));
738
+ }
739
+ planes.push({ n: new THREE.Vector3(0, 1, 0), d: top });
740
+ return { planes, round };
741
+ }
742
+ function blobLump(rng, { c = [0, 0, 0], r, count = 14, squashY = 1, round = 0.2, min = 0.82 }) {
743
+ const planes = [], spin = rng.range(0, TAU);
744
+ for (let i = 0; i < count; i++) {
745
+ const yy = 1 - 2 * (i + 0.5) / count, rr = Math.sqrt(1 - yy * yy), phi = i * 2.399963 + spin;
746
+ const n = N3(Math.cos(phi) * rr + rng.range(-0.3, 0.3), (yy + rng.range(-0.3, 0.3)) * squashY, Math.sin(phi) * rr + rng.range(-0.3, 0.3));
747
+ planes.push({ n, d: Math.hypot(r[0] * n.x, r[1] * n.y, r[2] * n.z) * rng.range(min, 1) + n.x * c[0] + n.y * c[1] + n.z * c[2] });
748
+ }
749
+ return { planes, round };
750
+ }
751
+ function relief(S, s) {
752
+ return (x, y, z) => {
753
+ const big = S * 0.045 * fbm(x * 2 / S + s, y * 1.3 / S, z * 2 / S, 2, s);
754
+ const fine = S * 0.014 * fbm(x * 6.5 / S, y * 6.5 / S + s, z * 6.5 / S, 2, s + 3);
755
+ const where = smoothstep(0.05, 0.45, noise3(x * 0.5 / S, y * 0.3 / S, z * 0.5 / S + s, s + 9));
756
+ const gully = S * 0.06 * where * (1 - smoothstep(0, 0.2, Math.abs(noise3(x * 1.5 / S, y * 0.32 / S, z * 1.5 / S, s + 5))));
757
+ return big + fine - gully;
758
+ };
759
+ }
760
+ function axisMesh(rng, lumps, { top, fan, radial, seg, y0 = -1.5, blend = 0.3, bump, jitter = 0.3 }) {
761
+ const P = [], I = [], rows = [], side = Math.max(2, Math.round((top - fan - y0) / seg)), nf = 3;
762
+ for (let j = 0; j <= side; j++) rows.push([lerp(y0, top - fan, j / side), 0, j > 0 && j < side ? (top - fan - y0) / side : 0]);
763
+ for (let j = 1; j < nf; j++) rows.push([top - fan, j / nf * Math.PI / 2, 0]);
764
+ const put = (oy, dx, dy, dz) => {
765
+ let t = castOut(lumps, 0, oy, 0, dx, dy, dz, blend);
766
+ if (t < 0) t = 0.05;
767
+ t += bump(dx * t, oy + dy * t, dz * t) * (1 - 0.85 * dy * dy);
768
+ P.push(dx * t, oy + dy * t, dz * t);
769
+ };
770
+ for (const [oy, phi, dy] of rows) for (let i = 0; i < radial; i++) {
771
+ const th = (i + rng.range(-jitter, jitter)) / radial * TAU;
772
+ put(oy + rng.range(-jitter, jitter) * dy, Math.cos(th) * Math.cos(phi), Math.sin(phi), Math.sin(th) * Math.cos(phi));
773
+ }
774
+ put(top - fan, 0, 1, 0);
775
+ const pole = P.length / 3 - 1;
776
+ for (let j = 0; j < rows.length - 1; j++) for (let i = 0; i < radial; i++) {
777
+ const a = j * radial + i, b = j * radial + (i + 1) % radial, c = a + radial, d = b + radial;
778
+ if (rng.chance(0.5)) I.push(a, c, b, b, c, d);
779
+ else I.push(a, c, d, a, d, b);
780
+ }
781
+ const last = (rows.length - 1) * radial;
782
+ for (let i = 0; i < radial; i++) I.push(last + i, pole, last + (i + 1) % radial);
783
+ const g = new THREE.BufferGeometry();
784
+ g.setAttribute("position", new THREE.Float32BufferAttribute(P, 3));
785
+ g.setIndex(I);
786
+ return facet(g);
787
+ }
788
+ function pointMesh(rng, lumps, { centre, detail, blend = 0.15, bump, jitter = 0.22 }) {
789
+ const g = weld(new THREE.IcosahedronGeometry(1, detail)), j = jitter / (detail + 1);
790
+ displace(g, (p) => {
791
+ p.set(p.x + rng.range(-j, j), p.y + rng.range(-j, j), p.z + rng.range(-j, j)).normalize();
792
+ let t = castOut(lumps, centre[0], centre[1], centre[2], p.x, p.y, p.z, blend);
793
+ if (t < 0) t = 0.05;
794
+ t += bump(centre[0] + p.x * t, centre[1] + p.y * t, centre[2] + p.z * t);
795
+ p.set(centre[0] + p.x * t, centre[1] + p.y * t, centre[2] + p.z * t);
796
+ });
797
+ return facet(g);
798
+ }
799
+ function shadeRock(geo, pal, { height, seed = 0, detail = 1, grass = 0, snowy = false, rim = 0.1 }) {
800
+ const dark = col(pal.dark), capA = col(pal.capDark), capB = col(pal.cap), k = seed % 97 / 9.7, tmp = new THREE.Color();
801
+ return paint(geo, (p, n, o, i, surf, cap) => {
802
+ if (grass && n.y > 0.7 || snowy && n.y > 0.55) {
803
+ o.copy(capA).lerp(capB, smoothstep(0.6, 0.95, n.y));
804
+ surf.set(0, 0, k, rim);
805
+ return;
806
+ }
807
+ const t = clamp(p.y / height, 0, 1);
808
+ mixc(o, pal.base, pal.light, clamp(n.y, 0, 1) * 0.6 + 0.3 * smoothstep(0.25, 1, t));
809
+ o.lerp(dark, clamp(-n.y, 0, 1) * 0.5 + 0.4 * (1 - smoothstep(0, 0.5, t)));
810
+ const h = Math.sin(Math.round(n.x * 40) * 12.9898 + Math.round(n.y * 40) * 78.233 + Math.round(n.z * 40) * 37.719) * 43758.5453;
811
+ o.multiplyScalar(0.96 + 0.08 * (h - Math.floor(h)));
812
+ surf.set(detail, 0, k, rim);
813
+ if (grass && n.y > -0.3) {
814
+ tmp.copy(capA).lerp(capB, 0.6);
815
+ cap.set(tmp.r, tmp.g, tmp.b, 0.9 * smoothstep(height - grass * 2.6, height - grass * 0.4, p.y));
816
+ }
817
+ });
818
+ }
819
+ function cliff({ seed = 1, width = 12, depth = 9, height = 14, palette = "alpine", taper = 0.88, cap = true, lip = 0.7, shoulders = 2, steps = 0, detail = 1 } = {}) {
820
+ const rng = new RNG(seed), pal = ROCK[palette], s = rng.seed() % 997, a = width / 2, b = depth / 2, S = (a + b) / 2, H = height;
821
+ const round = clamp(S * 0.02, 0.06, 0.4);
822
+ const lumps = [prismLump(rng, { a: a * 0.82, b: b * 0.82, top: H, taper, walls: 8, chips: 4, round })];
823
+ const around = (dist, size, top, n) => {
824
+ const a0 = rng.range(0, TAU);
825
+ for (let i = 0; i < n; i++) {
826
+ const phi = a0 + i / n * TAU + rng.range(-0.6, 0.6), d = rng.range(...dist), k = rng.range(...size);
827
+ lumps.push(prismLump(rng, { cx: Math.cos(phi) * a * d, cz: Math.sin(phi) * b * d, a: a * k, b: b * k, top: H * rng.range(...top), taper: rng.range(0.8, 1.02), walls: 7, chips: 3, round }));
828
+ }
829
+ };
830
+ around([0.38, 0.55], [0.42, 0.62], [0.7, 0.96], shoulders);
831
+ around([0.5, 0.7], [0.45, 0.7], [0.22, 0.6], steps);
832
+ const radial = Math.max(14, Math.round(26 * detail));
833
+ const g = axisMesh(rng, lumps, { top: H, fan: clamp(Math.min(a, b) * 0.7, 1.2, H * 0.5), radial, seg: TAU * S / radial * 1.15, blend: round, bump: relief(S, s) });
834
+ shadeRock(g, pal, { height: H, seed: s, detail: 1, grass: cap && palette !== "snow" ? lip : 0, snowy: cap && palette === "snow", rim: clamp(S * 0.022, 0.06, 0.4) });
835
+ return piece("cliff", { solid: [g] }, { top: { y: H, rx: a * taper * 0.48, rz: b * taper * 0.48 } });
836
+ }
837
+ function boulderGeo(rng, { size, squash, pal, snowy, detail }) {
838
+ const s = rng.seed() % 997, r = [size * squash[0], size * squash[1], size * squash[2]], round = size * 0.03;
839
+ const lumps = [blobLump(rng, { r, count: 15, round, min: 0.72 })];
840
+ const phi = rng.range(0, TAU);
841
+ lumps.push(blobLump(rng, { c: [Math.cos(phi) * r[0] * 0.55, -r[1] * 0.25, Math.sin(phi) * r[2] * 0.55], r: [r[0] * 0.6, r[1] * 0.62, r[2] * 0.6], count: 11, round, min: 0.74 }));
842
+ const g = pointMesh(rng, lumps, { centre: [0, 0, 0], detail, blend: round, bump: relief(size * 1.1, s) });
843
+ g.computeBoundingBox();
844
+ const h = g.boundingBox.max.y - g.boundingBox.min.y;
845
+ g.translate(0, -g.boundingBox.min.y - h * 0.14, 0);
846
+ return shadeRock(g, pal, { height: h * 0.86, seed: s, snowy, rim: clamp(size * 0.035, 0.012, 0.09) });
847
+ }
848
+ function boulder({ seed = 1, size = 2, palette = "alpine", squash = [1, 0.72, 0.9], snowy = false, detail = 2 } = {}) {
849
+ const rng = new RNG(seed), pal = ROCK[palette];
850
+ return piece("boulder", { solid: [boulderGeo(rng, { size, squash, pal, snowy, detail })] });
851
+ }
852
+ function pebbles({ seed = 1, count = 6, size = 0.35, spread = 1.6, palette = "alpine", snowy = false } = {}) {
853
+ const rng = new RNG(seed), pal = ROCK[palette], parts = [];
854
+ for (let i = 0; i < count; i++) {
855
+ const g = boulderGeo(rng, { size: size * rng.range(0.5, 1.3), squash: [1, rng.range(0.5, 0.8), rng.range(0.7, 1)], pal, snowy, detail: 1 });
856
+ const a = rng.range(0, TAU), r = spread * Math.sqrt(rng.next());
857
+ parts.push(xform(g, { pos: [Math.cos(a) * r, 0, Math.sin(a) * r], rot: [0, rng.range(0, 6.28), 0] }));
858
+ }
859
+ return piece("pebbles", { solid: parts });
860
+ }
861
+ function spires({ seed = 1, count = 4, height = 14, radius = 2.4, palette = "snow", snowy = true } = {}) {
862
+ const rng = new RNG(seed), pal = ROCK[palette], parts = [];
863
+ for (let i = 0; i < count; i++) {
864
+ const s = rng.seed() % 997;
865
+ const h = height * (i === 0 ? 1 : rng.range(0.45, 0.9)), r = radius * (i === 0 ? 1 : rng.range(0.6, 0.95));
866
+ const lumps = [blobLump(rng, { c: [0, h * 0.42, 0], r: [r, h * 0.58, r], count: 16, squashY: 0.3, round: r * 0.04, min: 0.8 })];
867
+ const g = axisMesh(rng, lumps, { top: h, fan: h * 0.42, radial: 9, seg: h / 5, blend: 0.1, bump: relief(r * 1.8, s) });
868
+ const a = i === 0 ? 0 : rng.range(0, TAU), d = i === 0 ? 0 : radius * rng.range(1, 1.7);
869
+ shadeRock(g, pal, { height: h, seed: s, snowy, rim: 0.1 });
870
+ parts.push(xform(g, { pos: [Math.cos(a) * d, 0, Math.sin(a) * d], rot: [rng.range(-0.06, 0.06), rng.range(0, 6), rng.range(-0.06, 0.06)] }));
871
+ }
872
+ return piece("spires", { solid: parts });
873
+ }
874
+ var SHAPES = {
875
+ pillar: [[0, 1.08], [0.12, 0.92], [0.45, 0.6], [0.72, 0.84], [0.88, 1.12], [0.96, 1.15], [1, 1.02]],
876
+ block: [[0, 1.06], [0.2, 0.95], [0.6, 0.9], [0.85, 1], [0.96, 1.04], [1, 0.95]],
877
+ hoodoo: [[0, 0.72], [0.3, 0.5], [0.7, 0.42], [0.82, 0.78], [0.93, 0.92], [1, 0.74]],
878
+ shelf: [[0, 0.3], [0.35, 0.36], [0.68, 0.74], [0.88, 1.08], [0.97, 1.12], [1, 1]],
879
+ butte: [[0, 1.2], [0.3, 1], [0.7, 0.9], [0.9, 0.93], [1, 0.84]]
880
+ };
881
+ function mesa({ seed = 1, height = 18, radius = 6, shape = "pillar", sx = 1, sz = 1, palette = "desert", detail = 1 } = {}) {
882
+ const rng = new RNG(seed), pal = STRATA[palette], s = rng.seed() % 997, k = rng.range(0, 10);
883
+ const prof = profile(SHAPES[shape]), field = strataField(k); new THREE.Color();
884
+ const g = facet(lathe({
885
+ height,
886
+ sx,
887
+ sz,
888
+ radial: Math.max(12, Math.round(22 * detail)),
889
+ rings: clamp(Math.round(height / 1.05 * Math.sqrt(detail)), 10, 60),
890
+ dome: 0.015,
891
+ radius: (t, th, y) => {
892
+ const r = radius * prof(t) * (1 + 0.17 * fbm(Math.cos(th) * 1.1 + s, y * 0.07, Math.sin(th) * 1.1, 3, s)) * (1 + 0.05 * noise3(Math.cos(th) * 6, y * 0.8, Math.sin(th) * 6, s));
893
+ return r * (1 + 1.6 * field.bulge(Math.cos(th) * r * sx, y, Math.sin(th) * r * sz) * smoothstep(1, 0.9, t));
894
+ }
895
+ }));
896
+ const rim = clamp(radius * 0.022, 0.06, 0.4);
897
+ paint(g, (p, n, o, i, surf) => {
898
+ if (n.y > 0.75 && p.y > height * 0.93) {
899
+ mixc(o, pal.topDark, pal.top, 0.5 + 0.5 * noise3(p.x * 0.25, s, p.z * 0.25, s));
900
+ surf.set(0, 0, k, rim);
901
+ return;
902
+ }
903
+ const shade = 0.86 + 0.14 * smoothstep(0, 0.25, p.y / height);
904
+ o.setRGB(shade, shade, shade);
905
+ surf.set(1, 1, k, rim);
906
+ });
907
+ return piece("mesa", { solid: [g] }, { top: { y: height, rx: radius * sx * 0.55, rz: radius * sz * 0.55 } });
908
+ }
909
+ function arch({ seed = 1, span = 22, height = 13, thickness = 4.2, depth = 6.5, palette = "desert" } = {}) {
910
+ const rng = new RNG(seed), pal = STRATA[palette], s = rng.seed() % 997, k = rng.range(0, 10), S = span / 2, H = height;
911
+ const curve = new THREE.CatmullRomCurve3([
912
+ new THREE.Vector3(-S * 1, -1.5, 0),
913
+ new THREE.Vector3(-S * 0.98, H * 0.5, 0),
914
+ new THREE.Vector3(-S * 0.8, H * 0.92, 0),
915
+ new THREE.Vector3(-S * 0.35, H * rng.range(1, 1.05), 0),
916
+ new THREE.Vector3(S * 0.35, H * rng.range(1, 1.05), 0),
917
+ new THREE.Vector3(S * 0.8, H * 0.92, 0),
918
+ new THREE.Vector3(S * 0.98, H * 0.5, 0),
919
+ new THREE.Vector3(S * 1, -1.5, 0)
920
+ ], false, "catmullrom", 0.5);
921
+ const path = curve.getPoints(26), field = strataField(k), top = col(pal.top), rim = clamp(thickness * 0.03, 0.06, 0.3);
922
+ const g = tube(path, (t, th) => {
923
+ const leg = lerp(1.7, 1, smoothstep(0, 0.6, Math.sin(Math.PI * t)));
924
+ const square = Math.pow(Math.pow(Math.abs(Math.cos(th)), 5) + Math.pow(Math.abs(Math.sin(th)), 5), -0.2);
925
+ return thickness / 2 * leg * square * (1 + 0.2 * noise3(t * 9 + s, Math.cos(th) * 1.8, Math.sin(th) * 1.8, s));
926
+ }, { radial: 10, flatten: depth / thickness, capStart: true, capEnd: true });
927
+ displace(g, (p) => {
928
+ const b = 1 + 1.6 * field.bulge(p.x, p.y, p.z);
929
+ p.z *= b;
930
+ p.x += Math.sign(p.x) * (b - 1) * thickness * 0.5;
931
+ });
932
+ const f = facet(g);
933
+ paint(f, (p, n, o, i, surf) => {
934
+ if (n.y > 0.7 && p.y > H * 0.8) {
935
+ o.copy(top);
936
+ surf.set(0, 0, k, rim);
937
+ return;
938
+ }
939
+ const shade = 0.86 + 0.14 * smoothstep(0, H * 0.3, p.y);
940
+ o.setRGB(shade, shade, shade);
941
+ surf.set(1, 1, k, rim);
942
+ });
943
+ return piece("arch", { solid: [f] });
944
+ }
945
+ function mountain({ seed = 1, width = 54, depth = 40, height = 31, palette = "alpine", detail = 0.75 } = {}) {
946
+ const rng = new RNG(seed);
947
+ const flip = rng.chance(0.5) ? -1 : 1;
948
+ const peaks = [-0.48, 0.02, 0.48].map((centre, i) => ({
949
+ x: (centre + rng.range(-0.13, 0.13)) * flip,
950
+ z: rng.range(-0.3, 0.3),
951
+ height: i === 1 ? 1 : rng.range(0.51, 0.88),
952
+ twist: rng.range(0, Math.PI * 2),
953
+ spread: rng.range(0.85, 1.2)
954
+ }));
955
+ const count = Math.round(16 + detail * 30);
956
+ const geo = new THREE.PlaneGeometry(2, 2, count, count).rotateX(-Math.PI / 2);
957
+ const pos = geo.attributes.position;
958
+ let top = 0;
959
+ for (let i = 0; i < pos.count; i++) {
960
+ const u = pos.getX(i), v = pos.getZ(i);
961
+ const fade = 1 - Math.max(Math.abs(u), Math.abs(v));
962
+ const x = u * Math.sqrt(1 - v * v / 2) + rng.range(-0.025, 0.025) * fade;
963
+ const z = v * Math.sqrt(1 - u * u / 2) + rng.range(-0.025, 0.025) * fade;
964
+ let y = 0;
965
+ for (const peak of peaks) {
966
+ const dx = x - peak.x, dz = (z - peak.z) * peak.spread;
967
+ const angle = Math.atan2(dz, dx), radius = Math.hypot(dx, dz);
968
+ const spur = Math.cos(4 * angle + peak.twist + radius * 3) + 0.4 * Math.cos(7 * angle - peak.twist);
969
+ const slope = radius * (1.13 + 0.22 * spur);
970
+ y = Math.max(y, peak.height * Math.max(0, 1 - slope) ** 1.24);
971
+ }
972
+ const rim = smoothstep(1, 0.58, Math.hypot(x, z));
973
+ y = Math.max(0, y + 0.1 * fbm(x * 4, 0, z * 4, 4, seed)) * rim;
974
+ top = Math.max(top, y);
975
+ pos.setXYZ(i, x * width / 2, y, z * depth / 2);
976
+ }
977
+ for (let i = 0; i < pos.count; i++) pos.setY(i, pos.getY(i) * height / top);
978
+ const pal = ROCK[palette] ?? ROCK.alpine;
979
+ const snow = col(15857407), rock = col(palette === "snow" ? 7439776 : pal.dark);
980
+ const grass = col(pal.capDark);
981
+ const shape = facet(geo, { angle: 38 });
982
+ paint(shape, (p, n, color, _i, surface2, cap) => {
983
+ const altitude = p.y / height;
984
+ const patch = fbm(p.x * 0.22, p.y * 0.19, p.z * 0.22, 3, seed);
985
+ mixc(color, rock, pal.base, 0.46 + patch * 0.5 + n.y * 0.16);
986
+ if (palette === "snow") {
987
+ const cover = smoothstep(0.23, 0.71, altitude + n.y * 0.44 + patch * 0.25);
988
+ cap.set(snow.r, snow.g, snow.b, cover);
989
+ } else if (palette === "alpine") {
990
+ color.lerp(grass, 0.45 * (1 - smoothstep(0.08, 0.44, altitude)) * Math.max(0, n.y));
991
+ }
992
+ surface2.set(0.94, palette === "desert" ? 0.74 : 0, seed % 1024, 0.035);
993
+ });
994
+ return piece("mountain", { solid: [shape] });
995
+ }
996
+ var V = (x, y, z) => new THREE.Vector3(x, y, z);
997
+ function trunkGeo(path, r0, r1, color, radial = 6) {
998
+ const c = col(color), d = c.clone().multiplyScalar(0.72);
999
+ const g = tube(path, (t) => lerp(r0, r1, t), { radial, capEnd: true, color: (t, th, o) => o.copy(c).lerp(d, 0.5 + 0.5 * Math.sin(th * 3 + t * 9)) });
1000
+ return flat(g);
1001
+ }
1002
+ function pine({ seed = 1, height = 11, palette = "forest", density = 1, bare = 0.18, spread = 0.25, lod = 0 } = {}) {
1003
+ const rng = new RNG(seed), pal = PINE[palette], H = height;
1004
+ const low = col(pal.low), mid = col(pal.mid), tip = col(pal.tip);
1005
+ const bend = rng.range(-0.02, 0.02) * H;
1006
+ const axis = (y) => V(bend * Math.sin(y / H * 2.5), y, bend * 0.6 * Math.sin(y / H * 1.7));
1007
+ const path = [];
1008
+ for (let i = 0; i <= 8; i++) path.push(axis(H * 0.98 * i / 8));
1009
+ const solid2 = [trunkGeo(path, H * 0.03, H * 4e-3, pal.trunk)];
1010
+ const t = new Tris(), sparse = density < 0.6;
1011
+ const tiers = Math.max(5, Math.round((H * 0.9 + 3) * Math.min(1, density + 0.25) * (lod ? 0.75 : 1)));
1012
+ for (let i = 0; i < tiers; i++) {
1013
+ const u = i / (tiers - 1), y = H * (bare + (0.95 - bare) * Math.pow(u, 0.92));
1014
+ const L = H * spread * Math.pow(1 - u, 0.8) + H * 0.045;
1015
+ const count = Math.max(4, Math.round(lerp(9, 5, u) * density)), off = rng.range(0, 6.28);
1016
+ if (!sparse) {
1017
+ const n = count + 2, apex = axis(y + L * 0.42), c = axis(y), rim = [], rc = [];
1018
+ const inner = low.clone().multiplyScalar(0.72), outer = low.clone().lerp(mid, 0.25 + 0.6 * u);
1019
+ for (let k = 0; k < n * 2; k++) {
1020
+ const a = off + k / (n * 2) * 6.283, point = k % 2 === 0, r = L * (point ? rng.range(0.74, 0.9) : 0.5);
1021
+ rim.push(V(c.x + Math.cos(a) * r, c.y - L * (point ? rng.range(0.5, 0.68) : 0.22), c.z + Math.sin(a) * r));
1022
+ rc.push(point ? outer : outer.clone().multiplyScalar(0.8));
1023
+ }
1024
+ for (let k = 0; k < n * 2; k++) t.tri(apex, rim[(k + 1) % (n * 2)], rim[k], inner, rc[(k + 1) % (n * 2)], rc[k]);
1025
+ }
1026
+ const boughs = lod ? Math.ceil(count * 0.6) : count;
1027
+ for (let j = 0; j < boughs; j++) {
1028
+ if (sparse && rng.chance(0.3)) continue;
1029
+ const len = L * rng.range(0.85, 1.15), shade = rng.range(0, 1);
1030
+ frond(t, {
1031
+ origin: axis(y + L * 0.12),
1032
+ yaw: off + 0.4 + j / boughs * 6.283 + rng.range(-0.25, 0.25),
1033
+ length: len,
1034
+ width: len * rng.range(0.4, 0.52),
1035
+ rise: lerp(0.1, 0.45, u) + rng.range(-0.08, 0.08),
1036
+ droop: lerp(0.78, 0.5, u) + rng.range(-0.1, 0.1),
1037
+ segs: lod ? 4 : 6,
1038
+ serr: 0.5,
1039
+ fold: 0.32,
1040
+ c0: low.clone().lerp(mid, 0.35 + 0.5 * u),
1041
+ c1: mid.clone().lerp(tip, (0.3 + 0.7 * Math.pow(u, 0.7)) * (0.5 + 0.5 * shade))
1042
+ });
1043
+ }
1044
+ }
1045
+ const foliage = [t.build()];
1046
+ const top = new THREE.ConeGeometry(H * 0.03, H * 0.11, 5);
1047
+ top.translate(axis(H).x, H * 0.985, axis(H).z);
1048
+ foliage.push(paint(flat(top), (p, n, o) => o.copy(tip)));
1049
+ if (sparse) for (let i = 0; i < 6; i++) solid2.push(stick(rng, axis(H * rng.range(0.15, 0.7)), H * rng.range(0.08, 0.16), pal.trunk));
1050
+ return piece("pine", { solid: solid2, foliage }, { height: H });
1051
+ }
1052
+ function stick(rng, from, len, color, up = 0.25) {
1053
+ const a = rng.range(0, 6.28), dir = V(Math.cos(a), up + rng.range(-0.1, 0.25), Math.sin(a)).normalize();
1054
+ const mid = from.clone().addScaledVector(dir, len * 0.5).add(V(0, len * rng.range(-0.08, 0.1), 0));
1055
+ const end = from.clone().addScaledVector(dir, len).add(V(0, len * rng.range(0, 0.25), 0));
1056
+ return trunkGeo([from, mid, end], len * 0.045 + 0.015, 0.01, color, 4);
1057
+ }
1058
+ function deadTree({ seed = 1, height = 6, bark = "grey", snow = false } = {}) {
1059
+ const rng = new RNG(seed), H = height, lean = rng.range(-0.08, 0.08) * H;
1060
+ const path = [];
1061
+ for (let i = 0; i <= 6; i++) {
1062
+ const u = i / 6;
1063
+ path.push(V(lean * u * u, H * u, lean * 0.5 * u));
1064
+ }
1065
+ const solid2 = [trunkGeo(path, H * 0.035 + 0.04, 0.02, BARK[bark])];
1066
+ for (let i = 0; i < 7; i++) {
1067
+ const u = rng.range(0.3, 0.92);
1068
+ solid2.push(stick(rng, V(lean * u * u, H * u, lean * 0.5 * u), H * rng.range(0.1, 0.26) * (1.1 - u * 0.5), BARK[bark], 0.35));
1069
+ }
1070
+ if (snow) {
1071
+ const w = col(16777215);
1072
+ solid2.forEach((g) => paint(g, (p, n, o, i) => {
1073
+ o.fromBufferAttribute(g.attributes.color, i);
1074
+ if (n.y > 0.45) o.lerp(w, 0.9);
1075
+ }));
1076
+ }
1077
+ return piece("deadTree", { solid: solid2 }, { height: H });
1078
+ }
1079
+ function crownBlob(rng, r, pal, squash = [1, 0.8, 1]) {
1080
+ const dark = col(pal.dark), light = col(pal.light), s = rng.seed() % 997;
1081
+ return paintFaces(blob(rng, { r, detail: 2, squash, lumpy: 0.32 }), (c, n, o, f) => {
1082
+ o.copy(dark).lerp(light, Math.min(1, Math.max(0, n.y * 0.6 + 0.45)));
1083
+ o.multiplyScalar(0.93 + 0.14 * noise3(f * 0.31, s, c.y, s));
1084
+ });
1085
+ }
1086
+ function roundTree({ seed = 1, height = 6, palette = "green", bark = "brown" } = {}) {
1087
+ const rng = new RNG(seed), pal = LEAF[palette], H = height, solid2 = [];
1088
+ const lean = rng.range(-0.1, 0.1) * H, top = V(lean, H * 0.55, lean * 0.4);
1089
+ solid2.push(trunkGeo([V(0, 0, 0), V(lean * 0.3, H * 0.28, 0), top], H * 0.05 + 0.05, H * 0.03, BARK[bark]));
1090
+ const tips = [top.clone().add(V(0, H * 0.2, 0))];
1091
+ const n = rng.int(2, 4), a0 = rng.range(0, 6.28);
1092
+ for (let i = 0; i < n; i++) {
1093
+ const a = a0 + i / n * 6.283 + rng.range(-0.3, 0.3), from = V(lean * 0.6, H * rng.range(0.34, 0.5), 0);
1094
+ const end = from.clone().add(V(Math.cos(a) * H * 0.3, H * rng.range(0.12, 0.3), Math.sin(a) * H * 0.3));
1095
+ solid2.push(trunkGeo([from, from.clone().lerp(end, 0.5).add(V(0, H * 0.03, 0)), end], H * 0.03, H * 0.015, BARK[bark], 5));
1096
+ tips.push(end);
1097
+ }
1098
+ tips.forEach((p, i) => {
1099
+ solid2.push(xform(crownBlob(rng, H * (i ? rng.range(0.17, 0.24) : 0.27), pal), { pos: [p.x, p.y + H * 0.05, p.z], rot: [0, rng.range(0, 6), 0] }));
1100
+ for (let k = 0; k < 2; k++) {
1101
+ const a = rng.range(0, 6.28), d = H * 0.17;
1102
+ solid2.push(xform(crownBlob(rng, H * rng.range(0.09, 0.13), pal), { pos: [p.x + Math.cos(a) * d, p.y + H * rng.range(-0.02, 0.14), p.z + Math.sin(a) * d] }));
1103
+ }
1104
+ });
1105
+ return piece("roundTree", { solid: solid2 }, { height: H });
1106
+ }
1107
+ function twistedTree({ seed = 1, height = 4, palette = "autumn", bark = "dark" } = {}) {
1108
+ const rng = new RNG(seed), pal = LEAF[palette], H = height, solid2 = [], t = new Tris();
1109
+ const dark = col(pal.dark), light = col(pal.light);
1110
+ const grow = (from, dir, len, r, depth) => {
1111
+ const pts = [from.clone()], p = from.clone(), d = dir.clone();
1112
+ for (let i = 0; i < 4; i++) {
1113
+ d.add(V(rng.range(-0.5, 0.5), rng.range(-0.15, 0.35), rng.range(-0.5, 0.5))).normalize();
1114
+ p.addScaledVector(d, len / 4);
1115
+ pts.push(p.clone());
1116
+ }
1117
+ solid2.push(trunkGeo(pts, r, r * 0.45, BARK[bark], depth ? 5 : 6));
1118
+ if (depth < 2) for (let i = 0; i < 2; i++) grow(pts[2 + i], d.clone().add(V(rng.range(-0.9, 0.9), rng.range(0, 0.5), rng.range(-0.9, 0.9))).normalize(), len * 0.62, r * 0.5, depth + 1);
1119
+ if (depth > 0) for (let i = 0; i < 13; i++) {
1120
+ const o = pts[rng.int(2, 4)].clone().add(V(rng.range(-0.25, 0.25), rng.range(-0.05, 0.25), rng.range(-0.25, 0.25)).multiplyScalar(H * 0.3));
1121
+ const s = rng.next();
1122
+ frond(t, { origin: o, yaw: rng.range(0, 6.28), length: H * rng.range(0.1, 0.16), width: H * 0.05, rise: rng.range(0.2, 0.9), droop: 0.5, segs: 2, serr: 0, fold: 0.2, c0: dark.clone().lerp(light, s * 0.5), c1: dark.clone().lerp(light, 0.4 + s * 0.6) });
1123
+ }
1124
+ };
1125
+ grow(V(0, 0, 0), V(rng.range(-0.5, 0.5), 1, rng.range(-0.5, 0.5)).normalize(), H * 0.75, H * 0.055 + 0.03, 0);
1126
+ return piece("twistedTree", { solid: solid2, foliage: [t.build()] }, { height: H });
1127
+ }
1128
+ function palm({ seed = 1, height = 8, lean = 0.22, fronds = 18, palette = "palm", coconuts = true } = {}) {
1129
+ const rng = new RNG(seed), pal = LEAF[palette], H = height, a = rng.range(0, 6.28), k = Math.min(1.25, Math.max(0.55, H / 8));
1130
+ const at = (u) => V(Math.cos(a) * lean * H * Math.pow(u, 1.8), H * u, Math.sin(a) * lean * H * Math.pow(u, 1.8));
1131
+ const path = [];
1132
+ for (let i = 0; i <= 9; i++) path.push(at(i / 9));
1133
+ const bark = col(BARK.palm), ring2 = bark.clone().multiplyScalar(0.7);
1134
+ const trunk = flat(tube(path, (u) => (lerp(0.3, 0.18, u) * k + 0.04 * Math.exp(-u * 14)) * (1 + 0.14 * (Math.round(u * 9) % 2)), {
1135
+ radial: 6,
1136
+ color: (u, th, o) => o.copy(bark).lerp(ring2, Math.round(u * 9) % 2 ? 0 : 0.55)
1137
+ }));
1138
+ const solid2 = [trunk], crown = at(1), t = new Tris();
1139
+ const dark = col(pal.dark), light = col(pal.light), dry = col(12163642);
1140
+ for (let i = 0; i < fronds; i++) {
1141
+ const old = i >= fronds - 2, len = rng.range(2.9, 3.9) * k, rise = old ? rng.range(-0.25, 0) : rng.range(0.25, 1.25), shade = rng.next();
1142
+ frond(t, {
1143
+ origin: crown.clone().add(V(0, 0.1 * k, 0)),
1144
+ yaw: i / fronds * 6.283 * 1.9 + rng.range(-0.2, 0.2),
1145
+ length: len,
1146
+ width: len * 0.33,
1147
+ rise,
1148
+ droop: old ? 0.6 : rise * 0.85 + rng.range(0.2, 0.5),
1149
+ segs: 14,
1150
+ serr: 0.6,
1151
+ fold: 0.3,
1152
+ taper: 0.5,
1153
+ edge: 0.75,
1154
+ c0: old ? dry.clone().multiplyScalar(0.8) : dark.clone(),
1155
+ c1: old ? dry.clone() : dark.clone().lerp(light, 0.45 + 0.55 * shade)
1156
+ });
1157
+ }
1158
+ solid2.push(xform(paintFaces(blob(rng, { r: 0.3 * k, detail: 1 }), (c, n, o) => o.copy(bark).multiplyScalar(0.8)), { pos: [crown.x, crown.y - 0.05, crown.z] }));
1159
+ if (coconuts) for (let i = 0; i < 3; i++) {
1160
+ const b = rng.range(0, 6.28);
1161
+ solid2.push(xform(paintFaces(blob(rng, { r: 0.17 * k, detail: 1, lumpy: 0.05 }), (c, n, o) => o.set(5913384)), { pos: [crown.x + Math.cos(b) * 0.28 * k, crown.y - 0.28 * k, crown.z + Math.sin(b) * 0.28 * k] }));
1162
+ }
1163
+ return piece("palm", { solid: solid2, foliage: [t.build()] }, { height: H });
1164
+ }
1165
+ function log({ seed = 1, length = 3.2, radius = 0.3, bark = "brown", snow = false } = {}) {
1166
+ const rng = new RNG(seed), L = length, s = rng.seed() % 997;
1167
+ const path = [];
1168
+ for (let i = 0; i <= 5; i++) path.push(V(-L / 2 + L * i / 5, radius * 0.82, rng.range(-0.04, 0.04) * L));
1169
+ const g = flat(tube(path, (t, th) => radius * (1 + 0.1 * noise3(t * 4 + s, Math.cos(th), Math.sin(th), s)), { radial: 8, capStart: true, capEnd: true }));
1170
+ const b = col(BARK[bark]), wood = col(14466444), white = col(16777215);
1171
+ paintFaces(g, (c, n, o, f) => {
1172
+ if (Math.abs(n.x) > 0.85) {
1173
+ o.copy(wood);
1174
+ return;
1175
+ }
1176
+ o.copy(b).multiplyScalar(0.85 + 0.25 * noise3(f * 0.7, s, 0, s));
1177
+ if (snow && n.y > 0.35) o.lerp(white, 0.95);
1178
+ });
1179
+ const solid2 = [g, stick(rng, V(rng.range(-0.3, 0.3) * L, radius * 1.2, 0), radius * 2.4, BARK[bark], 0.8)];
1180
+ return piece("log", { solid: solid2 });
1181
+ }
1182
+ function stump({ seed = 1, radius = 0.42, height = 0.6, bark = "brown", snow = false } = {}) {
1183
+ const rng = new RNG(seed), s = rng.seed() % 997, b = col(BARK[bark]), wood = col(14466444), white = col(16777215);
1184
+ const g = flat(tube([V(0, -0.1, 0), V(0, height * 0.4, 0), V(0, height, 0)], (t, th) => radius * (1.25 - 0.3 * t) * (1 + 0.14 * Math.cos(th * 4 + s) * (1 - t)), { radial: 10 }));
1185
+ paintFaces(g, (c, n, o, f) => {
1186
+ if (n.y > 0.8) o.copy(snow ? white : wood);
1187
+ else o.copy(b).multiplyScalar(0.85 + 0.25 * noise3(f * 0.7, s, 0, s));
1188
+ });
1189
+ return piece("stump", { solid: [g] });
1190
+ }
1191
+ var V2 = (x, y, z) => new THREE.Vector3(x, y, z);
1192
+ function grassTuft({ seed = 1, blades = 10, height = 0.5, spread = 0.3, palette = "green", width = 0.07 } = {}) {
1193
+ const rng = new RNG(seed), pal = GRASS[palette], t = new Tris(), base = col(pal.base), tip = col(pal.tip);
1194
+ for (let i = 0; i < blades; i++) {
1195
+ const a = rng.range(0, 6.28), r = spread * Math.sqrt(rng.next()), h = height * rng.range(0.55, 1.15), w = width * rng.range(0.7, 1.2);
1196
+ const o = V2(Math.cos(a) * r, 0, Math.sin(a) * r), la = rng.range(0, 6.28), lean = h * rng.range(0.1, 0.45);
1197
+ const dir = V2(Math.cos(la), 0, Math.sin(la)), side = V2(-dir.z, 0, dir.x);
1198
+ const c1 = base.clone().lerp(tip, rng.range(0.6, 1)), cm = base.clone().lerp(c1, 0.55);
1199
+ const m = o.clone().addScaledVector(dir, lean * 0.35).add(V2(0, h * 0.55, 0)), top = o.clone().addScaledVector(dir, lean).add(V2(0, h, 0));
1200
+ const a0 = o.clone().addScaledVector(side, w), b0 = o.clone().addScaledVector(side, -w);
1201
+ const a1 = m.clone().addScaledVector(side, w * 0.7), b1 = m.clone().addScaledVector(side, -w * 0.7);
1202
+ t.tri(a0, b0, a1, base, base, cm);
1203
+ t.tri(b0, b1, a1, base, cm, cm);
1204
+ t.tri(a1, b1, top, cm, cm, c1);
1205
+ }
1206
+ return piece("grassTuft", { grass: [t.build({ normalsUp: true })] });
1207
+ }
1208
+ function flowers({ seed = 1, count = 7, spread = 0.45, height = 0.38, color = 16777215, heart = 16237873 } = {}) {
1209
+ const rng = new RNG(seed), t = new Tris(), stem = col(5219644), pc = col(color), hc = col(heart);
1210
+ for (let i = 0; i < count; i++) {
1211
+ const a = rng.range(0, 6.28), r = spread * Math.sqrt(rng.next()), h = height * rng.range(0.6, 1.1);
1212
+ const o = V2(Math.cos(a) * r, 0, Math.sin(a) * r), top = o.clone().add(V2(rng.range(-0.05, 0.05), h, rng.range(-0.05, 0.05)));
1213
+ t.tri(o.clone().add(V2(0.012, 0, 0)), o.clone().add(V2(-0.012, 0, 0)), top, stem, stem, stem);
1214
+ const R = 0.085 * rng.range(0.8, 1.2), rot = rng.range(0, 6.28), petals = 6;
1215
+ for (let k = 0; k < petals; k++) {
1216
+ const a0 = rot + k / petals * 6.283, a1 = a0 + 6.283 / petals * 0.72;
1217
+ t.tri(top, top.clone().add(V2(Math.cos(a0) * R, 0.012, Math.sin(a0) * R)), top.clone().add(V2(Math.cos(a1) * R, 0.012, Math.sin(a1) * R)), hc, pc, pc);
1218
+ }
1219
+ }
1220
+ return piece("flowers", { grass: [t.build({ normalsUp: true })] });
1221
+ }
1222
+ function leafBlob(rng, r, pal, squash) {
1223
+ const dark = col(pal.dark), light = col(pal.light), s = rng.seed() % 997;
1224
+ return paintFaces(blob(rng, { r, detail: 1, squash, lumpy: 0.3 }), (c, n, o, f) => {
1225
+ o.copy(dark).lerp(light, Math.min(1, Math.max(0, n.y * 0.65 + 0.4)));
1226
+ o.multiplyScalar(0.92 + 0.16 * noise3(f * 0.43, s, 0, s));
1227
+ });
1228
+ }
1229
+ function bush({ seed = 1, size = 1, palette = "green", blobs = 5 } = {}) {
1230
+ const rng = new RNG(seed), pal = LEAF[palette], solid2 = [];
1231
+ for (let i = 0; i < blobs; i++) {
1232
+ const a = rng.range(0, 6.28), d = i ? size * rng.range(0.35, 0.7) : 0, r = size * (i ? rng.range(0.32, 0.5) : 0.6);
1233
+ solid2.push(xform(leafBlob(rng, r, pal, [1, 0.78, 1]), { pos: [Math.cos(a) * d, r * 0.55, Math.sin(a) * d], rot: [0, rng.range(0, 6), 0] }));
1234
+ }
1235
+ return piece("bush", { solid: solid2 });
1236
+ }
1237
+ function fern({ seed = 1, size = 0.8, fronds = 9, palette = "green", serr = 0.5, wide = 0.22, upright = 0.9 } = {}) {
1238
+ const rng = new RNG(seed), pal = LEAF[palette], t = new Tris(), dark = col(pal.dark), light = col(pal.light);
1239
+ for (let i = 0; i < fronds; i++) {
1240
+ const len = size * rng.range(0.7, 1.15), s = rng.next();
1241
+ frond(t, {
1242
+ origin: V2(0, 0.02, 0),
1243
+ yaw: i / fronds * 6.283 * 1.6 + rng.range(-0.3, 0.3),
1244
+ length: len,
1245
+ width: len * wide,
1246
+ rise: upright * rng.range(0.6, 1.3),
1247
+ droop: rng.range(0.6, 1),
1248
+ segs: serr ? 8 : 5,
1249
+ serr,
1250
+ fold: 0.35,
1251
+ taper: 0.7,
1252
+ c0: dark.clone().multiplyScalar(0.85),
1253
+ c1: dark.clone().lerp(light, 0.4 + 0.6 * s)
1254
+ });
1255
+ }
1256
+ return piece("fern", { foliage: [t.build()] });
1257
+ }
1258
+ function shrub({ seed = 1, size = 0.9, palette = "desert", leaves = 22 } = {}) {
1259
+ const rng = new RNG(seed), pal = LEAF[palette], t = new Tris(), dark = col(pal.dark), light = col(pal.light);
1260
+ for (let i = 0; i < leaves; i++) {
1261
+ const len = size * rng.range(0.45, 0.8), s = rng.next(), a = rng.range(0, 6.28), d = size * 0.3 * rng.next();
1262
+ frond(t, {
1263
+ origin: V2(Math.cos(a) * d, size * rng.range(0, 0.35), Math.sin(a) * d),
1264
+ yaw: a + rng.range(-0.8, 0.8),
1265
+ length: len,
1266
+ width: len * 0.26,
1267
+ rise: rng.range(0.3, 1.5),
1268
+ droop: rng.range(0.3, 0.9),
1269
+ segs: 4,
1270
+ serr: 0,
1271
+ fold: 0.3,
1272
+ taper: 0.75,
1273
+ c0: dark.clone().multiplyScalar(0.8),
1274
+ c1: dark.clone().lerp(light, 0.3 + 0.7 * s)
1275
+ });
1276
+ }
1277
+ return piece("shrub", { foliage: [t.build()] });
1278
+ }
1279
+ var CACTUS = { groove: 3109703, ridge: 8832099};
1280
+ function ribbed(body, r0, ribs = 9) {
1281
+ const m = body.length, end = body[m - 1], dir = end.clone().sub(body[m - 2]).normalize(), path = body.slice();
1282
+ for (let k = 1; k <= 4; k++) path.push(end.clone().addScaledVector(dir, r0 * 0.9 * Math.sin(k / 4 * Math.PI / 2)));
1283
+ const n = path.length, groove = col(CACTUS.groove), ridge = col(CACTUS.ridge);
1284
+ return flat(tube(path, (t, th) => {
1285
+ const i = t * (n - 1), dome = i > m - 1 ? Math.max(0.04, Math.cos((i - (m - 1)) / 4 * Math.PI / 2)) : 1;
1286
+ return r0 * dome * (1 + 0.16 * Math.cos(ribs * th)) * (1 - 0.12 * t);
1287
+ }, { radial: ribs * 2, capEnd: true, color: (t, th, o) => o.copy(groove).lerp(ridge, (0.5 + 0.5 * Math.cos(ribs * th)) * (0.75 + 0.25 * t)) }));
1288
+ }
1289
+ function column(from, h, wob) {
1290
+ const pts = [];
1291
+ for (let i = 0; i <= 5; i++) {
1292
+ const u = i / 5;
1293
+ pts.push(from.clone().add(V2(wob * Math.sin(u * 2.4), h * u, wob * (Math.cos(u * 1.9) - 1))));
1294
+ }
1295
+ return pts;
1296
+ }
1297
+ function blossom(t, at, R = 0.24) {
1298
+ const white = col(16777215), heart = col(16767050), petals = 7, c = at.clone().add(V2(0, 0.03, 0));
1299
+ for (let k = 0; k < petals; k++) {
1300
+ const a0 = k / petals * 6.283, a1 = a0 + 6.283 / petals * 0.8;
1301
+ t.tri(c, c.clone().add(V2(Math.cos(a0) * R, 0.07, Math.sin(a0) * R)), c.clone().add(V2(Math.cos(a1) * R, 0.07, Math.sin(a1) * R)), heart, white, white);
1302
+ }
1303
+ }
1304
+ function saguaro({ seed = 1, height = 5, arms = 2, flower = true } = {}) {
1305
+ const rng = new RNG(seed), H = height, r = 0.3 + H * 0.035, solid2 = [], t = new Tris();
1306
+ const main = column(V2(0, -0.1, 0), H, rng.range(-0.12, 0.12));
1307
+ solid2.push(ribbed(main, r));
1308
+ const tops = [[main[main.length - 1], r]], a0 = rng.range(0, 6.28);
1309
+ for (let i = 0; i < arms; i++) {
1310
+ const a = a0 + i / arms * 6.283 + rng.range(-0.5, 0.5), dir = V2(Math.cos(a), 0, Math.sin(a));
1311
+ const y0 = H * rng.range(0.3, 0.55), out = r * rng.range(2.2, 3.2), up = H * rng.range(0.25, 0.45), pts = [];
1312
+ for (let k = 0; k <= 3; k++) {
1313
+ const q = k / 3 * Math.PI / 2;
1314
+ pts.push(V2(0, y0, 0).addScaledVector(dir, out * Math.sin(q)).add(V2(0, out * 0.7 * (1 - Math.cos(q)), 0)));
1315
+ }
1316
+ const elbow = pts[pts.length - 1];
1317
+ for (let k = 1; k <= 3; k++) pts.push(elbow.clone().add(V2(0, up * k / 3, 0)));
1318
+ solid2.push(ribbed(pts, r * 0.68, 7));
1319
+ tops.push([pts[pts.length - 1], r * 0.68]);
1320
+ }
1321
+ if (flower) tops.forEach(([p, rr]) => rng.chance(0.75) && blossom(t, p.clone().add(V2(0, rr * 0.8, 0))));
1322
+ return piece("saguaro", { solid: solid2, foliage: t.count ? [t.build()] : [] }, { height: H });
1323
+ }
1324
+ function columnCactus({ seed = 1, count = 4, height = 3.2, flower = true } = {}) {
1325
+ const rng = new RNG(seed), solid2 = [], t = new Tris();
1326
+ for (let i = 0; i < count; i++) {
1327
+ const a = rng.range(0, 6.28), d = i ? rng.range(0.4, 0.9) : 0, h = height * (i ? rng.range(0.35, 0.9) : 1);
1328
+ const pts = column(V2(Math.cos(a) * d, -0.1, Math.sin(a) * d), h, rng.range(-0.08, 0.08));
1329
+ solid2.push(ribbed(pts, 0.2 + h * 0.03, 8));
1330
+ if (flower && rng.chance(0.7)) blossom(t, pts[pts.length - 1].clone().add(V2(0, (0.2 + h * 0.03) * 0.8, 0)), 0.19);
1331
+ }
1332
+ return piece("columnCactus", { solid: solid2, foliage: t.count ? [t.build()] : [] }, { height });
1333
+ }
1334
+ var SURFACE_VERT = (
1335
+ /* glsl */
1336
+ `
1337
+ attribute float depth;
1338
+ varying float vDepth; varying vec3 vWorld;
1339
+ #include <fog_pars_vertex>
1340
+ void main() {
1341
+ vDepth = depth;
1342
+ vec4 wp = modelMatrix * vec4(position, 1.0);
1343
+ vWorld = wp.xyz;
1344
+ vec4 mvPosition = viewMatrix * wp;
1345
+ gl_Position = projectionMatrix * mvPosition;
1346
+ #include <fog_vertex>
1347
+ }`
1348
+ );
1349
+ var SURFACE_FRAG = (
1350
+ /* glsl */
1351
+ `
1352
+ uniform float uTime, uDepthRange; uniform vec3 uShallow, uDeep, uFoam;
1353
+ varying float vDepth; varying vec3 vWorld;
1354
+ #include <fog_pars_fragment>
1355
+ vec2 hash2(vec2 p) { p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3))); return fract(sin(p) * 43758.5453); }
1356
+ float vnoise(vec2 p) { vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);
1357
+ return mix(mix(hash2(i).x, hash2(i + vec2(1, 0)).x, f.x), mix(hash2(i + vec2(0, 1)).x, hash2(i + vec2(1, 1)).x, f.x), f.y); }
1358
+ void main() {
1359
+ if (vDepth < -0.03) discard;
1360
+ float d = clamp(vDepth / uDepthRange, 0.0, 1.0);
1361
+ vec3 c = mix(uShallow, uDeep, smoothstep(0.0, 1.0, d));
1362
+ // drifting foam curls: contour lines of slow noise, shown only in patches
1363
+ vec2 uv = vWorld.xz * 0.2;
1364
+ float n = vnoise(uv + vec2(uTime * 0.07, -uTime * 0.05)) + 0.5 * vnoise(uv * 2.3 - uTime * 0.06);
1365
+ float lines = 1.0 - smoothstep(0.02, 0.05, abs(fract(n * 2.2) - 0.5));
1366
+ lines *= smoothstep(0.5, 0.72, vnoise(uv * 0.45 + 3.7 + uTime * 0.02));
1367
+ float wob = 0.07 * sin(uTime * 1.4 + vWorld.x * 0.9 + vWorld.z * 0.7);
1368
+ float shore = 1.0 - smoothstep(0.02, 0.15, vDepth + wob);
1369
+ float ring = (1.0 - smoothstep(0.02, 0.07, abs(vDepth - 0.42 - wob * 1.6))) * 0.4;
1370
+ float foam = clamp(lines * 0.75 + shore + ring, 0.0, 1.0);
1371
+ c = mix(c, uFoam, foam);
1372
+ gl_FragColor = vec4(c, max(mix(0.5, 0.94, d), foam));
1373
+ #include <tonemapping_fragment>
1374
+ #include <colorspace_fragment>
1375
+ #include <fog_fragment>
1376
+ }`
1377
+ );
1378
+ function surfaceMaterial(palette, depthRange) {
1379
+ const pal = WATER[palette];
1380
+ return new THREE.ShaderMaterial({
1381
+ vertexShader: SURFACE_VERT,
1382
+ fragmentShader: SURFACE_FRAG,
1383
+ transparent: true,
1384
+ depthWrite: false,
1385
+ fog: true,
1386
+ uniforms: THREE.UniformsUtils.merge([THREE.UniformsLib.fog, {
1387
+ uDepthRange: { value: depthRange },
1388
+ uShallow: { value: col(pal.shallow) },
1389
+ uDeep: { value: col(pal.deep) },
1390
+ uFoam: { value: col(pal.foam) },
1391
+ uTime: { value: 0 }
1392
+ }])
1393
+ });
1394
+ }
1395
+ function water({ width = 20, length = 20, segments = 48, palette = "river", depth = null, depthRange = 2.2, x = 0, y = 0, z = 0 } = {}) {
1396
+ const g = new THREE.PlaneGeometry(width, length, segments, Math.max(1, Math.round(segments * length / width)));
1397
+ g.rotateX(-Math.PI / 2);
1398
+ const pos = g.attributes.position, d = new Float32Array(pos.count), R = Math.min(width, length) / 2;
1399
+ const fn = depth || ((px, pz) => (R * 0.92 - Math.hypot(px - x, pz - z)) * 0.3);
1400
+ for (let i = 0; i < pos.count; i++) d[i] = fn(pos.getX(i) + x, pos.getZ(i) + z);
1401
+ g.setAttribute("depth", new THREE.BufferAttribute(d, 1));
1402
+ const mat = surfaceMaterial(palette, depthRange);
1403
+ mat.uniforms.uTime = uniforms.uTime;
1404
+ const m = new THREE.Mesh(g, mat);
1405
+ m.position.set(x, y, z);
1406
+ m.renderOrder = 2;
1407
+ m.name = "water";
1408
+ m.userData = { kit: "water" };
1409
+ return m;
1410
+ }
1411
+ var FALL_VERT = (
1412
+ /* glsl */
1413
+ `
1414
+ varying vec2 vUv;
1415
+ #include <fog_pars_vertex>
1416
+ void main() { vUv = uv; vec4 mvPosition = modelViewMatrix * vec4(position, 1.0); gl_Position = projectionMatrix * mvPosition;
1417
+ #include <fog_vertex>
1418
+ }`
1419
+ );
1420
+ var FALL_NOISE = (
1421
+ /* glsl */
1422
+ `
1423
+ float h(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
1424
+ float n(vec2 p) { vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);
1425
+ return mix(mix(h(i), h(i + vec2(1, 0)), f.x), mix(h(i + vec2(0, 1)), h(i + vec2(1, 1)), f.x), f.y); }`
1426
+ );
1427
+ var FALL_FRAG = (
1428
+ /* glsl */
1429
+ `
1430
+ uniform float uTime; uniform vec3 uShallow, uDeep, uFoam; uniform vec2 uSize;
1431
+ varying vec2 vUv;
1432
+ #include <fog_pars_fragment>
1433
+ ${FALL_NOISE}
1434
+ void main() {
1435
+ float x = vUv.x * uSize.x, drop = (1.0 - vUv.y) * uSize.y; // metres across, metres fallen
1436
+ // three layers of streaks sliding down at different speeds; they stretch as the water speeds up
1437
+ float stretch = 0.42 - 0.012 * drop;
1438
+ float a = n(vec2(x * 1.6, drop * stretch - uTime * 1.9));
1439
+ float b = n(vec2(x * 4.3 + 7.0, drop * stretch * 1.6 - uTime * 3.1));
1440
+ float c2 = n(vec2(x * 9.0 + 19.0, drop * stretch * 2.4 - uTime * 4.4));
1441
+ float body = a * 0.55 + b * 0.45;
1442
+ vec3 c = mix(uDeep, uShallow, smoothstep(0.3, 0.62, body));
1443
+ float w = max(fwidth(body) * 1.5, 0.01);
1444
+ c = mix(c, uFoam, smoothstep(0.6 - w, 0.6 + w, body) * 0.9); // broad white ribbons
1445
+ c = mix(c, uFoam, smoothstep(0.66, 0.7, c2) * smoothstep(0.35, 0.6, body) * 0.8); // thin fast threads
1446
+ float lip = smoothstep(0.045, 0.0, 1.0 - vUv.y + (a - 0.5) * 0.03);
1447
+ float churn = smoothstep(0.09 + 0.1 * n(vec2(x * 2.5, uTime * 2.6)), 0.0, vUv.y); // boiling foot
1448
+ c = mix(c, uFoam, clamp(lip * 0.85 + churn, 0.0, 1.0));
1449
+ float side = min(vUv.x, 1.0 - vUv.x) + (n(vec2(vUv.x * 9.0, drop * 0.5 - uTime * 2.0)) - 0.5) * 0.1 * (0.3 + 0.7 * (1.0 - vUv.y));
1450
+ if (side < 0.03) discard;
1451
+ gl_FragColor = vec4(c, 0.95);
1452
+ #include <tonemapping_fragment>
1453
+ #include <colorspace_fragment>
1454
+ #include <fog_fragment>
1455
+ }`
1456
+ );
1457
+ var RIPPLE_FRAG = (
1458
+ /* glsl */
1459
+ `
1460
+ uniform float uTime; uniform vec3 uFoam; uniform vec2 uSize; uniform float uWidth;
1461
+ varying vec2 vUv;
1462
+ #include <fog_pars_fragment>
1463
+ ${FALL_NOISE}
1464
+ void main() {
1465
+ vec2 q = vec2((vUv.x - 0.5) * uSize.x, vUv.y * uSize.y - 0.9);
1466
+ float r = length(vec2(max(abs(q.x) - uWidth * 0.5, 0.0), q.y)) + (n(q * 1.3 + uTime * 0.3) - 0.5) * 0.35;
1467
+ float reach = uSize.y - 1.4, fade = 1.0 - smoothstep(reach * 0.25, reach, r);
1468
+ float ring = abs(fract(r * 0.75 - uTime * 0.7) - 0.5);
1469
+ float lines = (1.0 - smoothstep(0.06, 0.13, ring)) * fade;
1470
+ float boil = (1.0 - smoothstep(0.2, 0.9, r)) * (0.65 + 0.35 * n(q * 3.0 + vec2(0.0, -uTime * 2.0)));
1471
+ float alpha = clamp(lines * 0.8 + boil, 0.0, 1.0);
1472
+ if (alpha < 0.02) discard;
1473
+ gl_FragColor = vec4(uFoam, alpha);
1474
+ #include <tonemapping_fragment>
1475
+ #include <colorspace_fragment>
1476
+ #include <fog_fragment>
1477
+ }`
1478
+ );
1479
+ var SPRAY_VERT = (
1480
+ /* glsl */
1481
+ `
1482
+ uniform float uTime, uWidth, uPixels;
1483
+ varying float vLife;
1484
+ void main() {
1485
+ float life = fract(uTime * (0.55 + position.z * 0.5) + position.x);
1486
+ float side = fract(position.x * 7.31) - 0.5;
1487
+ vec3 p = vec3((position.y - 0.5) * uWidth + side * 1.4 * life, 0.1, 0.15 + (0.4 + position.z * 1.5) * life);
1488
+ p.y += 4.0 * life * (1.0 - life) * (0.35 + position.z * 1.1);
1489
+ vLife = life;
1490
+ vec4 mv = modelViewMatrix * vec4(p, 1.0);
1491
+ gl_PointSize = uPixels * (0.5 + position.z) * (1.0 - life * 0.6) / -mv.z;
1492
+ gl_Position = projectionMatrix * mv;
1493
+ }`
1494
+ );
1495
+ var SPRAY_FRAG = (
1496
+ /* glsl */
1497
+ `
1498
+ varying float vLife;
1499
+ void main() { float d = length(gl_PointCoord - 0.5); if (d > 0.5) discard; gl_FragColor = vec4(1.0, 1.0, 1.0, (1.0 - vLife) * smoothstep(0.5, 0.25, d)); }`
1500
+ );
1501
+ function waterfall({ seed = 1, width = 6, height = 8, throwDist = 1.5, palette = "river", foam = true } = {}) {
1502
+ const rng = new RNG(seed), pal = WATER[palette], g = new THREE.PlaneGeometry(width, height, 14, 20);
1503
+ const pos = g.attributes.position, uv = g.attributes.uv;
1504
+ for (let i = 0; i < pos.count; i++) {
1505
+ const tau = 1 - uv.getY(i), x = pos.getX(i);
1506
+ const bow = 0.25 * Math.cos(x / width * Math.PI) + 0.1 * Math.sin(x * 2.1 + seed);
1507
+ pos.setXYZ(i, x * (1 + 0.06 * tau), height * (1 - tau * tau), throwDist * (tau - 1) + bow * tau);
1508
+ }
1509
+ const shared = () => ({ uShallow: { value: col(pal.shallow) }, uDeep: { value: col(pal.deep) }, uFoam: { value: col(pal.foam) }, uTime: { value: 0 } });
1510
+ const mat = new THREE.ShaderMaterial({
1511
+ vertexShader: FALL_VERT,
1512
+ fragmentShader: FALL_FRAG,
1513
+ transparent: true,
1514
+ side: THREE.DoubleSide,
1515
+ fog: true,
1516
+ uniforms: THREE.UniformsUtils.merge([THREE.UniformsLib.fog, { uSize: { value: new THREE.Vector2(width, height) }, ...shared() }])
1517
+ });
1518
+ mat.uniforms.uTime = uniforms.uTime;
1519
+ const sheet = new THREE.Mesh(g, mat);
1520
+ sheet.name = "waterfall:sheet";
1521
+ sheet.renderOrder = 4;
1522
+ const reach = 2.2 + height * 0.22, rg = new THREE.PlaneGeometry(width + reach * 2, reach + 1.4);
1523
+ rg.rotateX(-Math.PI / 2);
1524
+ rg.translate(0, 0.05, -(reach + 1.4) / 2 + 0.9 + reach * 0);
1525
+ const ruv = rg.attributes.uv;
1526
+ for (let i = 0; i < ruv.count; i++) ruv.setY(i, 1 - ruv.getY(i));
1527
+ rg.translate(0, 0, reach + 1.4 - 0.9 - 0.9);
1528
+ const rmat = new THREE.ShaderMaterial({
1529
+ vertexShader: FALL_VERT,
1530
+ fragmentShader: RIPPLE_FRAG,
1531
+ transparent: true,
1532
+ depthWrite: false,
1533
+ fog: true,
1534
+ uniforms: THREE.UniformsUtils.merge([THREE.UniformsLib.fog, { uSize: { value: new THREE.Vector2(width + reach * 2, reach + 1.4) }, uWidth: { value: width }, ...shared() }])
1535
+ });
1536
+ rmat.uniforms.uTime = uniforms.uTime;
1537
+ const ripples = new THREE.Mesh(rg, rmat);
1538
+ ripples.name = "waterfall:ripples";
1539
+ ripples.renderOrder = 3;
1540
+ const count = Math.round(width * 14), sp = new Float32Array(count * 3);
1541
+ for (let i = 0; i < count * 3; i++) sp[i] = rng.next();
1542
+ const sg = new THREE.BufferGeometry();
1543
+ sg.setAttribute("position", new THREE.BufferAttribute(sp, 3));
1544
+ const smat = new THREE.ShaderMaterial({ vertexShader: SPRAY_VERT, fragmentShader: SPRAY_FRAG, transparent: true, depthWrite: false, uniforms: { uTime: uniforms.uTime, uWidth: { value: width }, uPixels: { value: 190 } } });
1545
+ const spray = new THREE.Points(sg, smat);
1546
+ spray.name = "waterfall:spray";
1547
+ spray.frustumCulled = false;
1548
+ spray.renderOrder = 5;
1549
+ const parts = [];
1550
+ if (foam) {
1551
+ const white = col(16777215), tint = col(pal.shallow);
1552
+ for (let i = 0; i < Math.round(width * 1.6); i++) {
1553
+ const r = rng.range(0.25, 0.6), b = paintFaces(blob(rng, { r, detail: 2, squash: [1, 0.55, 1], lumpy: 0.2 }), (c, n, o) => o.copy(tint).lerp(white, 0.55 + 0.45 * Math.max(0, n.y)));
1554
+ parts.push(xform(b, { pos: [rng.range(-0.5, 0.5) * width, rng.range(-0.05, 0.12), rng.range(-0.3, 0.9)] }));
1555
+ }
1556
+ }
1557
+ const group = piece("waterfall", { cloud: parts }, { height });
1558
+ group.add(sheet, ripples, spray);
1559
+ return group;
1560
+ }
1561
+ function terrain({ size = 200, segments = 160, height, color, focus = 1 }) {
1562
+ const g = new THREE.PlaneGeometry(2, 2, segments, segments);
1563
+ g.rotateX(-Math.PI / 2);
1564
+ const pos = g.attributes.position, half = size / 2;
1565
+ const warp = (u) => half * (focus * u + (1 - focus) * u * u * u);
1566
+ const rng = new RNG(7), du = 1 / segments;
1567
+ for (let i = 0; i < pos.count; i++) {
1568
+ const u = pos.getX(i), v = pos.getZ(i), edge = Math.abs(u) > 0.999 || Math.abs(v) > 0.999;
1569
+ const x = warp(u) + (edge ? 0 : rng.range(-0.3, 0.3) * (warp(u + du) - warp(u - du)));
1570
+ const z = warp(v) + (edge ? 0 : rng.range(-0.3, 0.3) * (warp(v + du) - warp(v - du)));
1571
+ pos.setXYZ(i, x, height(x, z), z);
1572
+ }
1573
+ g.computeVertexNormals();
1574
+ const tint = new Float32Array(pos.count * 3), mask = new Float32Array(pos.count), blend = { tint: new THREE.Color(), mask: 0 };
1575
+ paint(g, (p, n, o, i) => {
1576
+ blend.mask = 0;
1577
+ blend.tint.setRGB(1, 1, 1);
1578
+ color(p.x, p.z, p.y, n, o, blend);
1579
+ tint[i * 3] = blend.tint.r;
1580
+ tint[i * 3 + 1] = blend.tint.g;
1581
+ tint[i * 3 + 2] = blend.tint.b;
1582
+ mask[i] = blend.mask;
1583
+ });
1584
+ g.setAttribute("tint", new THREE.BufferAttribute(tint, 3));
1585
+ g.setAttribute("mask", new THREE.BufferAttribute(mask, 1));
1586
+ g.deleteAttribute("uv");
1587
+ const m = new THREE.Mesh(g, materials.ground);
1588
+ m.receiveShadow = true;
1589
+ m.name = "terrain";
1590
+ m.userData = { kit: "terrain" };
1591
+ return m;
1592
+ }
1593
+ function terrainSample({ seed = 1, palette = "grass" } = {}) {
1594
+ const { a, b, path = b } = GROUND[palette] ?? GROUND.grass;
1595
+ return terrain({
1596
+ size: 30,
1597
+ segments: 36,
1598
+ height: (x, z) => 1.9 + 1.8 * fbm(x * 0.14, 0, z * 0.14, 4, seed),
1599
+ color: (x, z, _y, _normal, out, blend) => {
1600
+ mixc(out, a, b, 0.5 + 0.5 * fbm(x * 0.17, 0, z * 0.17, 3, seed));
1601
+ blend.tint.set(path);
1602
+ blend.mask = 1 - smoothstep(0.85, 1.9, Math.abs(x - 4 * Math.sin(z * 0.17)));
1603
+ }
1604
+ });
1605
+ }
1606
+ function groundPatch2({ seed = 1, radius = 6, palette = "grass", bump = 0.5 } = {}) {
1607
+ const rng = new RNG(seed), pal = GROUND[palette], s = rng.seed() % 997;
1608
+ const rings = 8, radial = 36, P = [], I = [];
1609
+ P.push(0, 0, 0);
1610
+ for (let j = 1; j <= rings; j++) for (let i = 0; i < radial; i++) {
1611
+ const r = j / rings * radius * (1 + 0.1 * noise3(Math.cos(i / radial * 6.283) * 1.5 + s, Math.sin(i / radial * 6.283) * 1.5, 0, s));
1612
+ P.push(Math.cos(i / radial * 6.283) * r, 0, Math.sin(i / radial * 6.283) * r);
1613
+ }
1614
+ for (let i = 0; i < radial; i++) I.push(0, 1 + (i + 1) % radial, 1 + i);
1615
+ for (let j = 1; j < rings; j++) for (let i = 0; i < radial; i++) {
1616
+ const a = 1 + (j - 1) * radial + i, b = 1 + (j - 1) * radial + (i + 1) % radial, c = a + radial, d = b + radial;
1617
+ I.push(a, b, c, b, d, c);
1618
+ }
1619
+ const geo = new THREE.BufferGeometry();
1620
+ geo.setAttribute("position", new THREE.Float32BufferAttribute(P, 3));
1621
+ geo.setIndex(I);
1622
+ const pos = geo.attributes.position;
1623
+ for (let i = 0; i < pos.count; i++) {
1624
+ const x = pos.getX(i), z = pos.getZ(i), q = Math.hypot(x, z) / radius, fall = 1 - smoothstep(0.55, 1, q);
1625
+ pos.setY(i, (bump * (0.55 + 0.45 * fbm(x * 0.25 + s, 0, z * 0.25, 3, s)) + 0.15) * fall - 0.05);
1626
+ }
1627
+ geo.computeVertexNormals();
1628
+ paint(geo, (p, n, o) => mixc(o, pal.a, pal.b, 0.5 + 0.5 * fbm(p.x * 0.3 + s, 0, p.z * 0.3, 2, s) + (n.y - 0.9) * 2));
1629
+ return piece("groundPatch", { solid: [geo] });
1630
+ }
1631
+ function cloud({ seed = 1, size = 12 } = {}) {
1632
+ const rng = new RNG(seed), parts = [], white = col(16777215), shade = col(12047349);
1633
+ const n = rng.int(5, 8);
1634
+ for (let i = 0; i < n; i++) {
1635
+ const u = i / (n - 1) - 0.5, r = size * (0.34 - 0.3 * u * u) * rng.range(0.8, 1.15);
1636
+ const b = paintFaces(blob(rng, { r, detail: 2, squash: [1.25, 0.7, 1], lumpy: 0.18 }), (c, nn, o) => o.copy(shade).lerp(white, smoothstep(-0.5, 0.35, nn.y)));
1637
+ parts.push(xform(b, { pos: [u * size * 1.5, r * 0.45 + rng.range(-0.05, 0.08) * size, rng.range(-0.15, 0.15) * size] }));
1638
+ }
1639
+ return piece("cloud", { cloud: parts });
1640
+ }
1641
+ function sky({ top = 3114726, horizon = 13625855, radius = 900 } = {}) {
1642
+ const mat = new THREE.ShaderMaterial({
1643
+ side: THREE.BackSide,
1644
+ depthWrite: false,
1645
+ fog: false,
1646
+ uniforms: { uTop: { value: col(top) }, uHorizon: { value: col(horizon) } },
1647
+ vertexShader: "varying vec3 vDir; void main(){ vDir = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }",
1648
+ fragmentShader: `uniform vec3 uTop, uHorizon; varying vec3 vDir;
1649
+ void main(){ float t = pow(clamp(vDir.y * 1.15 + 0.08, 0.0, 1.0), 0.6); gl_FragColor = vec4(mix(uHorizon, uTop, t), 1.0);
1650
+ #include <tonemapping_fragment>
1651
+ #include <colorspace_fragment>
1652
+ }`
1653
+ });
1654
+ const m = new THREE.Mesh(new THREE.SphereGeometry(radius, 24, 12), mat);
1655
+ m.name = "sky";
1656
+ m.renderOrder = -10;
1657
+ m.frustumCulled = false;
1658
+ return m;
1659
+ }
1660
+ function scatter(source, placements) {
1661
+ const out = new THREE.Group();
1662
+ out.name = `scatter:${source.name}`;
1663
+ const m = new THREE.Matrix4(), q = new THREE.Quaternion(), e = new THREE.Euler(), p = new THREE.Vector3(), s = new THREE.Vector3();
1664
+ source.traverse((child) => {
1665
+ if (!child.isMesh) return;
1666
+ const inst = new THREE.InstancedMesh(child.geometry, child.material, placements.length);
1667
+ placements.forEach((pl, i) => {
1668
+ const sc = Array.isArray(pl.scale) ? pl.scale : [pl.scale ?? 1, pl.scale ?? 1, pl.scale ?? 1];
1669
+ e.set(pl.tiltX ?? 0, pl.rot ?? 0, pl.tiltZ ?? 0, "YXZ");
1670
+ m.compose(p.set(pl.x, pl.y ?? 0, pl.z), q.setFromEuler(e), s.set(...sc));
1671
+ inst.setMatrixAt(i, m);
1672
+ });
1673
+ inst.castShadow = child.castShadow;
1674
+ inst.receiveShadow = child.receiveShadow;
1675
+ inst.renderOrder = child.renderOrder;
1676
+ out.add(inst);
1677
+ });
1678
+ return out;
1679
+ }
1680
+
1681
+ // vendor/stylized-biome-kit/catalog.js
1682
+ var BIOMES = { forest: "Alpine forest", snow: "Snow peaks", desert: "Desert canyon", shared: "Water and sky" };
1683
+ var CATALOG = [
1684
+ // ── Alpine forest ──
1685
+ { id: "mountain-alpine", name: "Alpine mountain ridge", biome: "forest", fn: "mountain", variants: [
1686
+ { seed: 3, width: 54, depth: 40, height: 31, palette: "alpine" },
1687
+ { seed: 8, width: 66, depth: 46, height: 39, palette: "alpine" }
1688
+ ] },
1689
+ { id: "cliff-alpine", name: "Grass-top cliff", biome: "forest", fn: "cliff", variants: [
1690
+ { seed: 3, width: 18, depth: 12, height: 20, steps: 2 },
1691
+ { seed: 8, width: 13, depth: 10, height: 13, steps: 1 },
1692
+ { seed: 12, width: 10, depth: 8, height: 8, taper: 0.95 }
1693
+ ] },
1694
+ { id: "boulder-alpine", name: "Granite boulder", biome: "forest", fn: "boulder", variants: [{ seed: 2, size: 2.6 }, { seed: 5, size: 1.7 }, { seed: 9, size: 1.1, squash: [1.2, 0.6, 0.9] }] },
1695
+ { id: "pebbles-alpine", name: "Pebbles", biome: "forest", fn: "pebbles", variants: [{ seed: 4 }, { seed: 7, count: 9, spread: 2.2 }] },
1696
+ { id: "pine", name: "Pine", biome: "forest", fn: "pine", variants: [
1697
+ { seed: 1, height: 15 },
1698
+ { seed: 2, height: 11, palette: "teal" },
1699
+ { seed: 3, height: 8 },
1700
+ { seed: 4, height: 4.5, palette: "teal" }
1701
+ ] },
1702
+ { id: "pine-olive", name: "Sunlit pine", biome: "forest", fn: "pine", variants: [{ seed: 6, height: 13, palette: "olive" }, { seed: 7, height: 9, palette: "olive" }] },
1703
+ { id: "pine-sparse", name: "Sparse pine", biome: "forest", fn: "pine", variants: [{ seed: 11, height: 12, density: 0.45, bare: 0.3 }, { seed: 14, height: 9, density: 0.5, bare: 0.35, palette: "teal" }] },
1704
+ { id: "round-tree", name: "Broadleaf tree", biome: "forest", fn: "roundTree", variants: [{ seed: 2, height: 7 }, { seed: 5, height: 5.5, palette: "deep" }, { seed: 8, height: 6, palette: "autumn" }] },
1705
+ { id: "dead-tree", name: "Dead tree", biome: "forest", fn: "deadTree", variants: [{ seed: 3, height: 7, bark: "brown" }, { seed: 6, height: 5 }] },
1706
+ { id: "log", name: "Fallen log", biome: "forest", fn: "log", variants: [{ seed: 1 }, { seed: 4, length: 2.2, radius: 0.24 }] },
1707
+ { id: "stump", name: "Stump", biome: "forest", fn: "stump", variants: [{ seed: 2 }] },
1708
+ { id: "bush", name: "Bush", biome: "forest", fn: "bush", variants: [{ seed: 1, size: 1.2 }, { seed: 4, size: 0.8, palette: "deep" }] },
1709
+ { id: "fern", name: "Fern", biome: "forest", fn: "fern", variants: [{ seed: 1, size: 0.9 }, { seed: 3, size: 0.6, palette: "deep" }] },
1710
+ { id: "grass", name: "Grass tuft", biome: "forest", fn: "grassTuft", variants: [{ seed: 1 }, { seed: 2, height: 0.9, blades: 14, spread: 0.4 }] },
1711
+ { id: "flowers", name: "Daisies", biome: "forest", fn: "flowers", variants: [{ seed: 1 }, { seed: 2, color: 16769899, heart: 15764011, count: 9 }] },
1712
+ { id: "ground-grass", name: "Grass patch", biome: "forest", fn: "groundPatch", variants: [{ seed: 1, radius: 6 }] },
1713
+ // ── Snow peaks ──
1714
+ { id: "mountain-snow", name: "Snow mountain ridge", biome: "snow", fn: "mountain", variants: [
1715
+ { seed: 3, width: 64, depth: 48, height: 44, palette: "snow" },
1716
+ { seed: 8, width: 76, depth: 54, height: 52, palette: "snow" }
1717
+ ] },
1718
+ { id: "cliff-snow", name: "Snow plateau", biome: "snow", fn: "cliff", variants: [
1719
+ { seed: 21, width: 16, depth: 13, height: 12, palette: "snow", taper: 1, lip: 1.3, steps: 2 },
1720
+ { seed: 24, width: 12, depth: 10, height: 8, palette: "snow", taper: 1, lip: 1.1, steps: 1 },
1721
+ { seed: 27, width: 8, depth: 7, height: 5, palette: "snow", taper: 1.05, lip: 0.9 }
1722
+ ] },
1723
+ { id: "spires", name: "Ice spires", biome: "snow", fn: "spires", variants: [{ seed: 2, height: 18, count: 5 }, { seed: 5, height: 12, count: 4, radius: 2 }, { seed: 9, height: 7, count: 3, radius: 1.5 }] },
1724
+ { id: "boulder-snow", name: "Snow boulder", biome: "snow", fn: "boulder", variants: [{ seed: 3, size: 2.4, palette: "snow", snowy: true }, { seed: 6, size: 1.5, palette: "snow", snowy: true }, { seed: 8, size: 1, palette: "alpine", snowy: true }] },
1725
+ { id: "pebbles-snow", name: "Snowy stones", biome: "snow", fn: "pebbles", variants: [{ seed: 5, palette: "snow", snowy: true, size: 0.45 }] },
1726
+ { id: "pine-snow", name: "Snow pine", biome: "snow", fn: "pine", variants: [{ seed: 31, height: 13, palette: "snow" }, { seed: 32, height: 9, palette: "snow" }, { seed: 33, height: 5, palette: "snow" }] },
1727
+ { id: "frost-tree", name: "Frosted tree", biome: "snow", fn: "roundTree", variants: [{ seed: 41, height: 7, palette: "frost", bark: "dark" }, { seed: 44, height: 5, palette: "frost", bark: "dark" }] },
1728
+ { id: "dead-tree-snow", name: "Bare tree", biome: "snow", fn: "deadTree", variants: [{ seed: 9, height: 6, snow: true }, { seed: 12, height: 4, snow: true }] },
1729
+ { id: "log-snow", name: "Snowy log", biome: "snow", fn: "log", variants: [{ seed: 6, snow: true, bark: "grey" }] },
1730
+ { id: "stump-snow", name: "Snowy stump", biome: "snow", fn: "stump", variants: [{ seed: 3, snow: true, bark: "grey" }] },
1731
+ { id: "bush-snow", name: "Frosted bush", biome: "snow", fn: "bush", variants: [{ seed: 7, palette: "frost" }, { seed: 9, size: 0.7, palette: "frost" }] },
1732
+ { id: "grass-frost", name: "Frost grass", biome: "snow", fn: "grassTuft", variants: [{ seed: 5, palette: "frost", height: 0.45 }] },
1733
+ { id: "ground-snow", name: "Snow drift", biome: "snow", fn: "groundPatch", variants: [{ seed: 4, radius: 6, palette: "snow", bump: 1.4 }] },
1734
+ // ── Desert canyon ──
1735
+ { id: "mountain-desert", name: "Sandstone mountain ridge", biome: "desert", fn: "mountain", variants: [
1736
+ { seed: 3, width: 70, depth: 40, height: 36, palette: "desert" },
1737
+ { seed: 8, width: 82, depth: 46, height: 44, palette: "desert" }
1738
+ ] },
1739
+ { id: "mesa-pillar", name: "Mesa pillar", biome: "desert", fn: "mesa", variants: [{ seed: 1, height: 22, radius: 7 }, { seed: 4, height: 15, radius: 5.5 }, { seed: 7, height: 10, radius: 4 }] },
1740
+ { id: "mesa-block", name: "Canyon wall", biome: "desert", fn: "mesa", variants: [{ seed: 11, height: 20, radius: 6.5, shape: "block", sx: 2.2 }, { seed: 13, height: 12, radius: 6, shape: "butte", sx: 1.5 }] },
1741
+ { id: "mesa-shelf", name: "Rock shelf", biome: "desert", fn: "mesa", variants: [{ seed: 21, height: 12, radius: 7, shape: "shelf", sx: 1.3 }, { seed: 23, height: 7, radius: 5, shape: "shelf" }, { seed: 25, height: 4, radius: 3.4, shape: "shelf" }] },
1742
+ { id: "hoodoo", name: "Hoodoo", biome: "desert", fn: "mesa", variants: [{ seed: 31, height: 13, radius: 3, shape: "hoodoo" }, { seed: 33, height: 8, radius: 2.2, shape: "hoodoo" }] },
1743
+ { id: "arch", name: "Stone arch", biome: "desert", fn: "arch", variants: [{ seed: 2, span: 24, height: 14 }, { seed: 5, span: 14, height: 8, thickness: 3, depth: 4.5 }] },
1744
+ { id: "boulder-desert", name: "Sandstone rock", biome: "desert", fn: "boulder", variants: [{ seed: 2, size: 2, palette: "desert", squash: [1.2, 0.6, 1] }, { seed: 6, size: 1.2, palette: "desert" }] },
1745
+ { id: "pebbles-desert", name: "Desert stones", biome: "desert", fn: "pebbles", variants: [{ seed: 3, palette: "desert" }] },
1746
+ { id: "palm", name: "Palm", biome: "desert", fn: "palm", variants: [{ seed: 1, height: 9 }, { seed: 3, height: 6.5, lean: 0.3 }, { seed: 5, height: 3.5, lean: 0.12, coconuts: false }] },
1747
+ { id: "saguaro", name: "Saguaro", biome: "desert", fn: "saguaro", variants: [{ seed: 1, height: 5.5, arms: 2 }, { seed: 4, height: 4, arms: 3 }, { seed: 6, height: 2.6, arms: 1 }] },
1748
+ { id: "column-cactus", name: "Column cactus", biome: "desert", fn: "columnCactus", variants: [{ seed: 2, count: 5, height: 3.6 }, { seed: 5, count: 3, height: 2.2 }] },
1749
+ { id: "twisted-tree", name: "Twisted tree", biome: "desert", fn: "twistedTree", variants: [{ seed: 2, height: 4.5 }, { seed: 6, height: 3.2 }] },
1750
+ { id: "shrub", name: "Desert scrub", biome: "desert", fn: "shrub", variants: [{ seed: 1 }, { seed: 3, size: 0.6 }] },
1751
+ { id: "broadleaf", name: "Broadleaf plant", biome: "desert", fn: "fern", variants: [{ seed: 4, size: 1.1, palette: "palm", serr: 0, wide: 0.3, fronds: 8 }, { seed: 7, size: 0.8, palette: "desert", serr: 0, wide: 0.3, fronds: 7 }] },
1752
+ { id: "grass-dry", name: "Dry grass", biome: "desert", fn: "grassTuft", variants: [{ seed: 3, palette: "dry", height: 0.45 }] },
1753
+ { id: "ground-sand", name: "Sand dune", biome: "desert", fn: "groundPatch", variants: [{ seed: 6, radius: 7, palette: "sand", bump: 1.6 }] },
1754
+ { id: "terrain-sample", name: "Terrain and trail sample", biome: "forest", fn: "terrainSample", variants: [
1755
+ { seed: 1 },
1756
+ { seed: 4, palette: "snow" },
1757
+ { seed: 6, palette: "sand" }
1758
+ ] },
1759
+ // ── Water and sky ──
1760
+ { id: "sky-dome", name: "Sky dome", biome: "shared", fn: "sky", variants: [
1761
+ {},
1762
+ { top: 3836904, horizon: 14873087 },
1763
+ { top: 2394346, horizon: 15126480 }
1764
+ ] },
1765
+ { id: "water", name: "Pond", biome: "shared", fn: "water", variants: [{ width: 16, length: 16 }, { width: 10, length: 10, palette: "ice" }] },
1766
+ { id: "waterfall", name: "Waterfall", biome: "shared", fn: "waterfall", variants: [{ seed: 1, width: 7, height: 10 }, { seed: 2, width: 4, height: 6 }, { seed: 3, width: 2, height: 3.5, throwDist: 0.8 }] },
1767
+ { id: "cloud", name: "Cloud", biome: "shared", fn: "cloud", variants: [{ seed: 1, size: 9 }, { seed: 4, size: 6 }] }
1768
+ ];
1769
+ function build(entry, variant = 0, overrides = {}) {
1770
+ return stylized_biome_kit_exports[entry.fn]({ ...entry.variants[variant], ...overrides });
1771
+ }
1772
+ var TAU2 = Math.PI * 2;
1773
+ function samplePath(points, n) {
1774
+ const c = new THREE.CatmullRomCurve3(points.map(([x, z]) => new THREE.Vector3(x, 0, z)));
1775
+ return c.getPoints(n).map((p) => [p.x, p.z]);
1776
+ }
1777
+ function nearest(pts) {
1778
+ return (x, z) => {
1779
+ let m = 1e9;
1780
+ for (const [px, pz] of pts) {
1781
+ const d = (px - x) * (px - x) + (pz - z) * (pz - z);
1782
+ if (d < m) m = d;
1783
+ }
1784
+ return Math.sqrt(m);
1785
+ };
1786
+ }
1787
+ var Batch = class {
1788
+ constructor(heightAt, rng) {
1789
+ this.rng = rng;
1790
+ this.map = /* @__PURE__ */ new Map();
1791
+ this.H = heightAt;
1792
+ this.taken = [];
1793
+ }
1794
+ add(src, x, z, { scale = 1, rot, y = null, sink = 0, tiltX = 0, tiltZ = 0 } = {}) {
1795
+ if (!this.map.has(src)) this.map.set(src, []);
1796
+ this.map.get(src).push({ x, z, y: (y ?? this.H(x, z)) - sink, rot: rot ?? this.rng.range(0, TAU2), scale, tiltX, tiltZ });
1797
+ }
1798
+ /** Reserve a circle; returns false if it overlaps an earlier one. */
1799
+ claim(x, z, r) {
1800
+ for (const t of this.taken) if ((t.x - x) ** 2 + (t.z - z) ** 2 < (t.r + r) ** 2) return false;
1801
+ this.taken.push({ x, z, r });
1802
+ return true;
1803
+ }
1804
+ /** Keep a camera position and its line of sight free of large pieces. */
1805
+ keepClear(from, to, r = 9) {
1806
+ for (let i = 0; i <= 6; i++) this.taken.push({ x: lerp(from[0], to[0], i / 9), z: lerp(from[2], to[2], i / 9), r: i ? r : r * 1.8 });
1807
+ }
1808
+ build(root) {
1809
+ for (const [src, list] of this.map) root.add(scatter(src, list));
1810
+ }
1811
+ };
1812
+ function onTop(obj, rng, n) {
1813
+ const top = obj.userData.top, th = obj.rotation.y, out = [];
1814
+ for (let i = 0; i < n; i++) {
1815
+ const a = rng.range(0, TAU2), q = Math.sqrt(rng.next()), lx = Math.cos(a) * top.rx * q, lz = Math.sin(a) * top.rz * q;
1816
+ out.push({ x: obj.position.x + lx * Math.cos(th) + lz * Math.sin(th), y: obj.position.y + top.y, z: obj.position.z - lx * Math.sin(th) + lz * Math.cos(th) });
1817
+ }
1818
+ return out;
1819
+ }
1820
+ function ringOf(rng, root, H, { count, r0, r1, skip = () => false, make, sink = 2 }) {
1821
+ const out = [];
1822
+ for (let i = 0; i < count; i++) {
1823
+ const a = (i + rng.range(-0.3, 0.3)) / count * TAU2;
1824
+ if (skip(a)) continue;
1825
+ const r = rng.range(r0, r1), x = Math.cos(a) * r, z = Math.sin(a) * r, o = make(rng.seed(), a);
1826
+ o.position.set(x, H(x, z) - sink, z);
1827
+ o.rotation.y = a + Math.PI / 2 + rng.range(-0.35, 0.35);
1828
+ root.add(o);
1829
+ out.push(o);
1830
+ }
1831
+ return out;
1832
+ }
1833
+ function addClouds(root, rng, { count = 12, r0 = 260, r1 = 360, y0 = 70, y1 = 150 } = {}) {
1834
+ const kinds = [cloud({ seed: 1, size: 12 }), cloud({ seed: 2, size: 10 }), cloud({ seed: 5, size: 14 })], list = kinds.map(() => []);
1835
+ for (let i = 0; i < count; i++) {
1836
+ const a = (i + rng.range(-0.4, 0.4)) / count * TAU2, r = rng.range(r0, r1);
1837
+ list[i % 3].push({ x: Math.cos(a) * r, y: rng.range(y0, y1), z: Math.sin(a) * r, rot: -a + Math.PI / 2, scale: rng.range(2.6, 4.6) });
1838
+ }
1839
+ kinds.forEach((k, i) => root.add(scatter(k, list[i])));
1840
+ }
1841
+ var angDist = (a, b) => Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b)));
1842
+ function forestScene({ seed = 5 } = {}) {
1843
+ const rng = new RNG(seed), root = new THREE.Group(), pal = GROUND.grass, tmp = new THREE.Color();
1844
+ const path = samplePath([[10, 66], [2, 42], [9, 22], [-3, 3], [-14, -17], [-8, -38], [3, -54]], 130), dPath = nearest(path);
1845
+ const H = (x, z) => {
1846
+ const r = Math.hypot(x, z), p = smoothstep(1.5, 8, dPath(x, z));
1847
+ return 0.9 + 1.7 * fbm(x * 0.02 + 3, 0, z * 0.02, 3, 11) + p * 0.3 * fbm(x * 0.09, 0, z * 0.09, 2, 5) + 18 * Math.pow(smoothstep(50, 125, r), 1.3);
1848
+ };
1849
+ root.add(terrain({
1850
+ size: 460,
1851
+ segments: 130,
1852
+ focus: 0.35,
1853
+ height: H,
1854
+ color: (x, z, y, n, o, blend) => {
1855
+ mixc(o, pal.a, pal.b, smoothstep(-0.35, 0.4, fbm(x * 0.045, 0, z * 0.045, 3, 21)));
1856
+ o.lerp(tmp.set(4168261), 0.45 * smoothstep(0.1, 0.5, fbm(x * 0.02 + 40, 0, z * 0.02, 2, 3)));
1857
+ blend.tint.set(pal.path).lerp(tmp.set(12890248), 0.5 + 0.5 * noise3(x * 0.15, 0, z * 0.15, 9));
1858
+ blend.mask = 1 - smoothstep(0.5, 3.7, dPath(x, z));
1859
+ }
1860
+ }));
1861
+ const b = new Batch(H, new RNG(seed)), VIEW = { pos: [13, 11, 73], target: [-4, 8, 0] };
1862
+ b.keepClear(VIEW.pos, VIEW.target, 8);
1863
+ const front = Math.PI / 2;
1864
+ const t1 = ringOf(rng, root, H, { count: 13, r0: 54, r1: 64, skip: (a) => angDist(a, front) < 0.5, make: (s) => cliff({ seed: s, width: rng.range(20, 30), depth: rng.range(12, 17), height: rng.range(10, 17), taper: rng.range(0.82, 0.98), steps: rng.int(1, 2), detail: 0.85 }) });
1865
+ ringOf(rng, root, H, { count: 9, r0: 82, r1: 102, sink: 4, skip: (a) => angDist(a, front) < 0.48, make: (seed2) => mountain({ seed: seed2, width: rng.range(64, 91), depth: rng.range(42, 65), height: rng.range(27, 46), detail: 0.5 }) });
1866
+ ringOf(rng, root, H, { count: 8, r0: 128, r1: 153, sink: 5, make: (seed2) => mountain({ seed: seed2, width: rng.range(91, 132), depth: rng.range(56, 81), height: rng.range(50, 75), palette: "snow", detail: 0.4 }) });
1867
+ const pines = [
1868
+ pine({ seed: 1, height: 14 }),
1869
+ pine({ seed: 2, height: 11, palette: "teal" }),
1870
+ pine({ seed: 3, height: 9 }),
1871
+ pine({ seed: 4, height: 12, palette: "teal" }),
1872
+ pine({ seed: 6, height: 12, palette: "olive" }),
1873
+ pine({ seed: 11, height: 11, density: 0.45, bare: 0.3 })
1874
+ ];
1875
+ for (const c of t1) for (const p of onTop(c, rng, rng.int(1, 3))) b.add(rng.pick(pines), p.x, p.z, { y: p.y, sink: 0.2, scale: rng.range(0.55, 0.9) });
1876
+ for (let i = 0, placed = 0; i < 900 && placed < 80; i++) {
1877
+ const a = rng.range(0, TAU2), r = 52 * Math.sqrt(rng.next()), x = Math.cos(a) * r, z = Math.sin(a) * r;
1878
+ if (dPath(x, z) < 7 || fbm(x * 0.03, 0, z * 0.03, 2, 77) < -0.12 || !b.claim(x, z, 2.6)) continue;
1879
+ b.add(rng.pick(pines), x, z, { scale: rng.range(0.7, 1.2), sink: 0.25 });
1880
+ placed++;
1881
+ }
1882
+ const broad = [roundTree({ seed: 2, height: 7 }), roundTree({ seed: 5, height: 5.5, palette: "deep" })];
1883
+ for (let i = 0, placed = 0; i < 200 && placed < 7; i++) {
1884
+ const x = rng.range(-45, 45), z = rng.range(-45, 45);
1885
+ if (dPath(x, z) < 4 || !b.claim(x, z, 3)) continue;
1886
+ b.add(rng.pick(broad), x, z, { scale: rng.range(0.9, 1.3), sink: 0.1 });
1887
+ placed++;
1888
+ }
1889
+ const rocks = [boulder({ seed: 2, size: 2.4 }), boulder({ seed: 5, size: 1.6 }), boulder({ seed: 9, size: 1.1, squash: [1.2, 0.6, 0.9] })];
1890
+ for (let i = 0, placed = 0; i < 200 && placed < 20; i++) {
1891
+ const x = rng.range(-50, 50), z = rng.range(-50, 55);
1892
+ if (dPath(x, z) < 2.6 || !b.claim(x, z, 2)) continue;
1893
+ b.add(rng.pick(rocks), x, z, { scale: rng.range(0.6, 1.4) });
1894
+ placed++;
1895
+ }
1896
+ const small = {
1897
+ bushes: [bush({ seed: 1, size: 1.2 }), bush({ seed: 4, size: 0.9, palette: "deep" })],
1898
+ ferns: [fern({ seed: 1, size: 0.9 }), fern({ seed: 3, size: 0.7, palette: "deep" })],
1899
+ stones: [pebbles({ seed: 4 }), pebbles({ seed: 7, count: 9, spread: 2.2 })],
1900
+ logs: [log({ seed: 1 }), log({ seed: 4, length: 2.4, radius: 0.26 })],
1901
+ stumps: [stump({ seed: 2 })],
1902
+ blooms: [flowers({ seed: 1 }), flowers({ seed: 2, count: 9 }), flowers({ seed: 3, color: 16769899, heart: 15764011 })]
1903
+ };
1904
+ const sprinkle = (kinds, n, { minPath = 2.4, R = 52, scale = [0.8, 1.3], nearPath = false } = {}) => {
1905
+ for (let i = 0, placed = 0; i < n * 12 && placed < n; i++) {
1906
+ const a = rng.range(0, TAU2), r = R * Math.sqrt(rng.next()), x = Math.cos(a) * r, z = Math.sin(a) * r, d = dPath(x, z);
1907
+ if (d < minPath || nearPath && d > 9) continue;
1908
+ b.add(rng.pick(kinds), x, z, { scale: rng.range(...scale) });
1909
+ placed++;
1910
+ }
1911
+ };
1912
+ sprinkle(small.bushes, 44);
1913
+ sprinkle(small.ferns, 70);
1914
+ sprinkle(small.stones, 26, { minPath: 1.6 });
1915
+ sprinkle(small.logs, 7, { minPath: 3.5 });
1916
+ sprinkle(small.stumps, 6);
1917
+ sprinkle(small.blooms, 90, { nearPath: true, minPath: 2.2 });
1918
+ sprinkle(small.blooms, 60);
1919
+ const tufts = [grassTuft({ seed: 1 }), grassTuft({ seed: 2, height: 0.75, blades: 12, spread: 0.36 }), grassTuft({ seed: 3, height: 0.4, blades: 9 })];
1920
+ for (let i = 0; i < 11e3; i++) {
1921
+ const a = rng.range(0, TAU2), r = 60 * Math.pow(rng.next(), 0.62), x = Math.cos(a) * r, z = Math.sin(a) * r;
1922
+ if (dPath(x, z) < 2.1) continue;
1923
+ b.add(tufts[i % 3], x, z, { scale: rng.range(0.6, 1.25) });
1924
+ }
1925
+ b.build(root);
1926
+ addClouds(root, rng);
1927
+ return {
1928
+ group: root,
1929
+ view: VIEW,
1930
+ env: { skyTop: 2784742, horizon: 12575999, fog: [12575231, 80, 380], sun: { dir: [-0.6, 0.72, 0.42], color: 16773590, intensity: 3.1 }, hemi: { sky: 13624831, ground: 6127536, intensity: 1.7 }, shadow: 85 }
1931
+ };
1932
+ }
1933
+ function snowfall(count = 2600, area = 150, height = 60) {
1934
+ const g = new THREE.BufferGeometry(), P = new Float32Array(count * 3), rng = new RNG(99);
1935
+ for (let i = 0; i < count; i++) P.set([rng.range(-area, area), rng.range(0, height), rng.range(-area, area)], i * 3);
1936
+ g.setAttribute("position", new THREE.BufferAttribute(P, 3));
1937
+ const m = new THREE.ShaderMaterial({
1938
+ transparent: true,
1939
+ depthWrite: false,
1940
+ uniforms: { uTime: uniforms.uTime, uHeight: { value: height } },
1941
+ vertexShader: `uniform float uTime, uHeight; void main(){ vec3 p = position; float k = fract(position.x * 0.37);
1942
+ p.y = mod(position.y - uTime * (2.2 + k * 1.8), uHeight); p.x += sin(uTime * 0.7 + position.z) * 0.9; p.z += cos(uTime * 0.5 + position.x) * 0.9;
1943
+ vec4 mv = modelViewMatrix * vec4(p, 1.0); gl_PointSize = (1.6 + k * 1.6) * 110.0 / -mv.z; gl_Position = projectionMatrix * mv; }`,
1944
+ fragmentShader: "void main(){ float d = length(gl_PointCoord - 0.5); if (d > 0.5) discard; gl_FragColor = vec4(1.0, 1.0, 1.0, 0.9 * (1.0 - smoothstep(0.2, 0.5, d))); }"
1945
+ });
1946
+ const pts = new THREE.Points(g, m);
1947
+ pts.frustumCulled = false;
1948
+ pts.name = "snowfall";
1949
+ pts.renderOrder = 5;
1950
+ return pts;
1951
+ }
1952
+ function snowScene({ seed = 9 } = {}) {
1953
+ const rng = new RNG(seed), root = new THREE.Group(), pal = GROUND.snow, tmp = new THREE.Color();
1954
+ const lake = [-9, 10], dLake = (x, z) => Math.hypot(x - lake[0], (z - lake[1]) * 1.25) + 3 * noise3(x * 0.07, 0, z * 0.07, 4);
1955
+ const H = (x, z) => {
1956
+ const r = Math.hypot(x, z);
1957
+ const land = Math.max(0.45, 1.5 + 2 * fbm(x * 0.022 + 9, 0, z * 0.022, 3, 41)) + 0.25 * fbm(x * 0.1, 0, z * 0.1, 2, 6) + 18 * Math.pow(smoothstep(50, 125, r), 1.3);
1958
+ return land - 4.4 * smoothstep(20, 9, dLake(x, z));
1959
+ };
1960
+ root.add(terrain({
1961
+ size: 460,
1962
+ segments: 130,
1963
+ focus: 0.35,
1964
+ height: H,
1965
+ color: (x, z, y, n, o) => {
1966
+ mixc(o, pal.a, pal.b, smoothstep(-0.3, 0.35, fbm(x * 0.05, 0, z * 0.05, 3, 13)) * smoothstep(0.82, 0.98, n.y));
1967
+ o.lerp(tmp.set(11849200), smoothstep(1.2, -0.6, y) * 0.8);
1968
+ }
1969
+ }));
1970
+ root.add(water({ width: 64, length: 64, segments: 72, x: lake[0], z: lake[1], palette: "ice", depth: (x, z) => -H(x, z), depthRange: 2.4 }));
1971
+ const b = new Batch(H, new RNG(seed)), front = Math.PI / 2, dry = (x, z) => dLake(x, z) > 22, VIEW = { pos: [17, 10, 70], target: [-8, 5, 0] };
1972
+ b.keepClear(VIEW.pos, VIEW.target, 8);
1973
+ const range = (w, d, h, detail) => (seed2) => mountain({ seed: seed2, width: rng.range(...w), depth: rng.range(...d), height: rng.range(...h), palette: "snow", detail });
1974
+ ringOf(rng, root, H, { count: 10, r0: 57, r1: 74, sink: 3, skip: (a) => angDist(a, front) < 0.57, make: range([36, 52], [27, 40], [13, 26], 0.65) });
1975
+ ringOf(rng, root, H, { count: 9, r0: 90, r1: 106, sink: 5, skip: (a) => angDist(a, front) < 0.46, make: range([61, 87], [41, 60], [29, 51], 0.55) });
1976
+ ringOf(rng, root, H, { count: 8, r0: 133, r1: 155, sink: 7, make: range([94, 135], [65, 88], [55, 81], 0.45) });
1977
+ for (let i = 0; i < 9; i++) {
1978
+ const a = rng.range(0, TAU2), r = rng.range(30, 50), x = Math.cos(a) * r, z = Math.sin(a) * r;
1979
+ if (!dry(x, z) || angDist(a, front) < 0.35 || !b.claim(x, z, 6)) continue;
1980
+ const s = spires({ seed: rng.seed(), height: rng.range(10, 22), count: rng.int(3, 5), radius: rng.range(1.8, 2.8) });
1981
+ s.position.set(x, H(x, z) - 1, z);
1982
+ s.rotation.y = rng.range(0, TAU2);
1983
+ root.add(s);
1984
+ }
1985
+ const pines = [pine({ seed: 31, height: 13, palette: "snow" }), pine({ seed: 32, height: 9, palette: "snow" }), pine({ seed: 33, height: 5, palette: "snow" }), pine({ seed: 35, height: 10, palette: "snow", density: 0.5, bare: 0.3 })];
1986
+ const place = (kinds, n, { R = 52, r = 2.2, scale = [0.8, 1.3], sink = 0.15, gap = 22 } = {}) => {
1987
+ for (let i = 0, placed = 0; i < n * 14 && placed < n; i++) {
1988
+ const a = rng.range(0, TAU2), q = R * Math.sqrt(rng.next()), x = Math.cos(a) * q, z = Math.sin(a) * q;
1989
+ if (dLake(x, z) < gap || r && !b.claim(x, z, r)) continue;
1990
+ b.add(rng.pick(kinds), x, z, { scale: rng.range(...scale), sink });
1991
+ placed++;
1992
+ }
1993
+ };
1994
+ place(pines, 70, { sink: 0.25 });
1995
+ place([roundTree({ seed: 41, height: 7, palette: "frost", bark: "dark" }), roundTree({ seed: 44, height: 5, palette: "frost", bark: "dark" }), roundTree({ seed: 47, height: 6, palette: "frost", bark: "dark" })], 24, { r: 2.6 });
1996
+ place([deadTree({ seed: 9, height: 6, snow: true }), deadTree({ seed: 12, height: 4.5, snow: true })], 9);
1997
+ place([boulder({ seed: 3, size: 2.4, palette: "snow", snowy: true }), boulder({ seed: 6, size: 1.5, palette: "snow", snowy: true }), boulder({ seed: 8, size: 1.1, palette: "alpine", snowy: true })], 22, { r: 2, gap: 20, scale: [0.6, 1.5] });
1998
+ place([groundPatch2({ seed: 4, radius: 6, palette: "snow", bump: 1.3 }), groundPatch2({ seed: 8, radius: 4.5, palette: "snow", bump: 1 })], 14, { r: 0, gap: 26, sink: 0.25 });
1999
+ place([bush({ seed: 7, palette: "frost" }), bush({ seed: 9, size: 0.7, palette: "frost" })], 40, { r: 0 });
2000
+ place([pebbles({ seed: 5, palette: "snow", snowy: true, size: 0.45 })], 18, { r: 0, gap: 19 });
2001
+ place([log({ seed: 6, snow: true, bark: "grey" }), stump({ seed: 3, snow: true, bark: "grey" })], 9, { r: 0 });
2002
+ const tufts = [grassTuft({ seed: 5, palette: "frost", height: 0.45 }), grassTuft({ seed: 6, palette: "frost", height: 0.3, blades: 7 })];
2003
+ for (let i = 0; i < 2600; i++) {
2004
+ const a = rng.range(0, TAU2), q = 56 * Math.sqrt(rng.next()), x = Math.cos(a) * q, z = Math.sin(a) * q;
2005
+ if (dLake(x, z) < 20.5 || fbm(x * 0.06, 0, z * 0.06, 2, 5) < 0) continue;
2006
+ b.add(tufts[i % 2], x, z, { scale: rng.range(0.6, 1.1) });
2007
+ }
2008
+ b.build(root);
2009
+ addClouds(root, rng, { count: 10 });
2010
+ root.add(snowfall());
2011
+ return {
2012
+ group: root,
2013
+ view: VIEW,
2014
+ env: { skyTop: 3836904, horizon: 14873087, fog: [14478591, 70, 340], sun: { dir: [0.55, 0.7, 0.45], color: 16774890, intensity: 2.7 }, hemi: { sky: 14478591, ground: 8232920, intensity: 2 }, shadow: 85 }
2015
+ };
2016
+ }
2017
+ function desertScene({ seed = 4 } = {}) {
2018
+ const rng = new RNG(seed), root = new THREE.Group(), pal = GROUND.sand, tmp = new THREE.Color();
2019
+ const river = samplePath([[18, 82], [9, 48], [-4, 24], [2, 3], [8, -16], [5, -34]], 110), dRiver = nearest(river);
2020
+ const isle = [1, 4], dIsle = (x, z) => Math.hypot(x - isle[0], z - isle[1]), LIP = -34.4, UPPER = 7.3;
2021
+ const dunes = (x, z) => 1.3 + 2.4 * Math.abs(fbm(x * 0.022 + 7, 0, z * 0.022, 3, 31)) + 0.22 * fbm(x * 0.12, 0, z * 0.12, 2, 8) + 12 * Math.pow(smoothstep(55, 125, Math.hypot(x, z)), 1.2);
2022
+ const H = (x, z) => {
2023
+ const land = dunes(x, z);
2024
+ const d = dRiver(x, z) - 9 * smoothstep(21, 3, dIsle(x, z)) + 1.3 * noise3(x * 0.11, 0, z * 0.11, 3);
2025
+ let h = lerp(-2.4, land, smoothstep(2.5, 9.5, d));
2026
+ h = Math.max(h, lerp(-9, 1.2 + 0.25 * fbm(x * 0.2, 0, z * 0.2, 2, 2), smoothstep(9, 4.5, dIsle(x, z))));
2027
+ const back = smoothstep(LIP + 2.4, LIP - 2.6, z);
2028
+ if (back > 0) {
2029
+ const d2 = Math.abs(x - 5 - 1.5 * Math.sin(z * 0.08));
2030
+ h = lerp(h, lerp(UPPER - 1.3, 8.2 + land * 0.8, smoothstep(2.6, 7, d2)), back);
2031
+ }
2032
+ return h;
2033
+ };
2034
+ root.add(terrain({
2035
+ size: 460,
2036
+ segments: 130,
2037
+ focus: 0.35,
2038
+ height: H,
2039
+ color: (x, z, y, n, o, blend) => {
2040
+ mixc(o, pal.a, pal.b, smoothstep(-0.4, 0.4, fbm(x * 0.04, 0, z * 0.04, 3, 5)));
2041
+ o.lerp(tmp.set(pal.path), 0.55 * smoothstep(0.92, 0.7, n.y));
2042
+ if (z > LIP - 3) o.lerp(tmp.set(pal.wet), smoothstep(0.8, 0, y) * 0.75);
2043
+ const lawn = smoothstep(9.5, 4.5, dIsle(x, z)) * smoothstep(0.1, 0.8, y);
2044
+ const banks = smoothstep(-0.1, 0.5, fbm(x * 0.05 + 20, 0, z * 0.05, 2, 9)) * smoothstep(0.2, 1.1, y) * smoothstep(3.2, 1.6, y) * smoothstep(38, 22, dRiver(x, z));
2045
+ mixc(blend.tint, 7322943, 10212938, 0.5 + 0.5 * noise3(x * 0.2, 0, z * 0.2, 7));
2046
+ blend.mask = Math.max(lawn, banks);
2047
+ }
2048
+ }));
2049
+ root.add(water({ width: 150, length: 190, segments: 120, x: 4, z: 32, palette: "river", depth: (x, z) => z < LIP - 0.4 ? -1 : -H(x, z), depthRange: 3.2 }));
2050
+ root.add(water({ width: 26, length: 70, segments: 26, x: 5, y: UPPER, z: LIP - 35, palette: "river", depth: (x, z) => UPPER - H(x, Math.min(z, LIP - 2.6)), depthRange: 1.4 }));
2051
+ const fall = waterfall({ seed: 3, width: 6.4, height: UPPER, throwDist: 1.6 });
2052
+ fall.position.set(5, 0, LIP + 1.6);
2053
+ root.add(fall);
2054
+ const b = new Batch(H, new RNG(seed)), front = Math.PI / 2, VIEW = { pos: [7, 8.5, 56], target: [3, 4, -12] };
2055
+ b.keepClear(VIEW.pos, VIEW.target, 7);
2056
+ const formation = (shapes, h, r, sx = [1, 1.6], detail = 1) => (s) => mesa({ seed: s, shape: rng.pick(shapes), height: rng.range(...h), radius: rng.range(...r), sx: rng.range(...sx), sz: rng.range(0.9, 1.2), detail });
2057
+ for (const [x, z, sx, h] of [[-9, -41, 1.5, 17], [20, -41, 1.6, 19], [-27, -44, 1.3, 15], [37, -45, 1.2, 17]]) {
2058
+ const m = mesa({ seed: rng.seed(), shape: "block", height: h, radius: 7.5, sx, sz: 1.1 });
2059
+ m.position.set(x, H(x, z) - 9, z);
2060
+ m.rotation.y = rng.range(-0.2, 0.2);
2061
+ root.add(m);
2062
+ b.claim(x, z, 9);
2063
+ }
2064
+ const span = arch({ seed: 7, span: 38, height: 24, thickness: 6, depth: 9 });
2065
+ span.position.set(5, H(5, -60) + 1, -60);
2066
+ root.add(span);
2067
+ const r1 = ringOf(rng, root, H, { count: 15, r0: 62, r1: 78, sink: 3, skip: (a) => angDist(a, front) < 0.42 || angDist(a, -front) < 0.3, make: formation(["pillar", "block", "butte", "pillar"], [20, 36], [8, 12], [1, 1.6], 0.8) });
2068
+ ringOf(rng, root, H, { count: 9, r0: 109, r1: 138, sink: 6, make: (seed2) => mountain({ seed: seed2, width: rng.range(77, 117), depth: rng.range(42, 64), height: rng.range(34, 57), palette: "desert", detail: 0.45 }) });
2069
+ const mid = [];
2070
+ for (let i = 0; i < 60 && mid.length < 9; i++) {
2071
+ const a = rng.range(0, TAU2), r = rng.range(26, 52), x = Math.cos(a) * r, z = Math.sin(a) * r;
2072
+ if (dRiver(x, z) < 15 || dIsle(x, z) < 24 || z < LIP + 6 || angDist(a, front) < 0.3 || !b.claim(x, z, 8)) continue;
2073
+ const m = mesa({ seed: rng.seed(), shape: rng.pick(["shelf", "hoodoo", "shelf", "pillar"]), height: rng.range(7, 15), radius: rng.range(3, 6), sx: rng.range(1, 1.4) });
2074
+ m.position.set(x, H(x, z) - 1.5, z);
2075
+ m.rotation.y = rng.range(0, TAU2);
2076
+ root.add(m);
2077
+ mid.push(m);
2078
+ }
2079
+ const palms = [palm({ seed: 1, height: 9 }), palm({ seed: 3, height: 6.5, lean: 0.3 }), palm({ seed: 5, height: 4, lean: 0.14, coconuts: false }), palm({ seed: 8, height: 7.5, lean: 0.18 })];
2080
+ const cacti = [saguaro({ seed: 1, height: 5.5, arms: 2 }), saguaro({ seed: 4, height: 4, arms: 3 }), saguaro({ seed: 6, height: 2.8, arms: 1 })];
2081
+ const columns = [columnCactus({ seed: 2, count: 5, height: 3.6 }), columnCactus({ seed: 5, count: 3, height: 2.4 })];
2082
+ const gnarled = [twistedTree({ seed: 2, height: 4.5 }), twistedTree({ seed: 6, height: 3.4 })];
2083
+ for (const m of [...r1, ...mid]) for (const p of onTop(m, rng, rng.int(0, 3))) b.add(rng.pick([...cacti, ...gnarled, palms[2], palms[1]]), p.x, p.z, { y: p.y, sink: 0.15, scale: rng.range(0.8, 1.3) });
2084
+ for (const [dx, dz, k] of [[0, 0, 0], [-2.6, 1.6, 1], [2.2, -2.2, 2], [1.5, 2.8, 2]]) b.add(palms[k], isle[0] + dx, isle[1] + dz, { sink: 0.2, scale: rng.range(0.85, 1.05) });
2085
+ const where = (test, n, fn, tries = 30) => {
2086
+ for (let i = 0, placed = 0; i < n * tries && placed < n; i++) {
2087
+ const a = rng.range(0, TAU2), q = 56 * Math.sqrt(rng.next()), x = Math.cos(a) * q, z = Math.sin(a) * q;
2088
+ if (z < LIP + 3 || !test(x, z, H(x, z))) continue;
2089
+ fn(x, z);
2090
+ placed++;
2091
+ }
2092
+ };
2093
+ const bank = (x, z, y) => y > 0.45 && y < 2.2 && dRiver(x, z) < 30, sand = (x, z, y) => y > 1.4 && dIsle(x, z) > 12;
2094
+ where((x, z, y) => bank(x, z, y) && b.claim(x, z, 2.4), 16, (x, z) => b.add(rng.pick(palms), x, z, { sink: 0.2, scale: rng.range(0.8, 1.15) }));
2095
+ where((x, z, y) => sand(x, z, y) && y > 2 && b.claim(x, z, 2), 18, (x, z) => b.add(rng.pick(cacti), x, z, { scale: rng.range(0.8, 1.3) }));
2096
+ where((x, z, y) => sand(x, z, y) && b.claim(x, z, 2), 12, (x, z) => b.add(rng.pick(columns), x, z, { scale: rng.range(0.8, 1.3) }));
2097
+ where((x, z, y) => sand(x, z, y) && b.claim(x, z, 2.5), 5, (x, z) => b.add(rng.pick(gnarled), x, z, { scale: rng.range(0.9, 1.3) }));
2098
+ const leafy = [fern({ seed: 4, size: 1.1, palette: "palm", serr: 0, wide: 0.3, fronds: 8 }), fern({ seed: 7, size: 0.8, palette: "desert", serr: 0, wide: 0.3, fronds: 7 }), bush({ seed: 3, size: 0.8, palette: "green" })];
2099
+ where(bank, 70, (x, z) => b.add(rng.pick(leafy), x, z, { scale: rng.range(0.8, 1.5) }));
2100
+ where((x, z, y) => dIsle(x, z) < 6.5 && y > 0.6, 14, (x, z) => b.add(rng.pick(leafy), x, z, { scale: rng.range(0.7, 1.2) }), 200);
2101
+ const scrub = [shrub({ seed: 1 }), shrub({ seed: 3, size: 0.6 }), bush({ seed: 6, size: 0.7, palette: "desert" })];
2102
+ where(sand, 90, (x, z) => b.add(rng.pick(scrub), x, z, { scale: rng.range(0.7, 1.4) }));
2103
+ const stones = [boulder({ seed: 2, size: 2, palette: "desert", squash: [1.2, 0.6, 1] }), boulder({ seed: 6, size: 1.2, palette: "desert" }), pebbles({ seed: 3, palette: "desert" }), pebbles({ seed: 8, palette: "desert", count: 8, spread: 2 })];
2104
+ where((x, z, y) => y > 0.2, 46, (x, z) => b.add(rng.pick(stones), x, z, { scale: rng.range(0.6, 1.4) }));
2105
+ const dry = [grassTuft({ seed: 3, palette: "dry", height: 0.45 }), grassTuft({ seed: 4, palette: "dry", height: 0.3, blades: 7 })], green = [grassTuft({ seed: 1 }), grassTuft({ seed: 3, height: 0.4, blades: 9 })];
2106
+ where((x, z, y) => sand(x, z, y) && fbm(x * 0.05, 0, z * 0.05, 2, 12) > -0.05, 2600, (x, z) => b.add(rng.pick(dry), x, z, { scale: rng.range(0.6, 1.1) }), 6);
2107
+ where((x, z, y) => dIsle(x, z) < 7 && y > 0.5 || bank(x, z, y) && y > 0.7 && fbm(x * 0.05 + 20, 0, z * 0.05, 2, 9) > 0.22, 1300, (x, z) => b.add(rng.pick(green), x, z, { scale: rng.range(0.6, 1.1) }), 40);
2108
+ b.build(root);
2109
+ addClouds(root, rng, { count: 11 });
2110
+ return {
2111
+ group: root,
2112
+ view: VIEW,
2113
+ env: { skyTop: 2394346, horizon: 14281983, fog: [15126480, 90, 420], sun: { dir: [-0.5, 0.74, 0.45], color: 16772560, intensity: 3.2 }, hemi: { sky: 13952767, ground: 11107474, intensity: 1.7 }, shadow: 85 }
2114
+ };
2115
+ }
2116
+ var SCENES = { forest: forestScene, snow: snowScene, desert: desertScene };
2117
+ var GRID = 4;
2118
+ var HALF = GRID / 2;
2119
+ var HEIGHT = 3.6;
2120
+ var BLOCK = 0.34;
2121
+ function courses(m, from, to, offset, height, yaw = 0, bottom = 0) {
2122
+ const width = to - from;
2123
+ const rows = Math.max(1, Math.round(height / BLOCK));
2124
+ const rowHeight = height / rows;
2125
+ const sin = Math.sin(yaw);
2126
+ const cos = Math.cos(yaw);
2127
+ m.block(
2128
+ [
2129
+ (from + to) / 2 * cos + offset * sin,
2130
+ bottom + height / 2,
2131
+ -((from + to) / 2) * sin + offset * cos
2132
+ ],
2133
+ [width, height, 0.51],
2134
+ m.palette.joint,
2135
+ "solid",
2136
+ 8e-3,
2137
+ yaw
2138
+ );
2139
+ for (let row = 0; row < rows; row++) {
2140
+ const bricks = Math.max(1, Math.round(width / 0.76));
2141
+ const stagger = row % 2 === 1 && bricks > 2 ? 0.5 : 0;
2142
+ const segments = bricks + Math.ceil(stagger);
2143
+ let left = from;
2144
+ for (let j = 1; j <= segments; j++) {
2145
+ const right = j === segments ? to : from + width * (j - stagger) / bricks + m.rng.range(-0.11, 0.11);
2146
+ const x = (left + right) / 2;
2147
+ const depth = m.rng.range(0.55, 0.63);
2148
+ m.stone(
2149
+ [x * cos + offset * sin, bottom + (row + 0.5) * rowHeight, -x * sin + offset * cos],
2150
+ [right - left - 0.018, rowHeight - 0.02, depth],
2151
+ 0.85 - row / rows * 0.65,
2152
+ 0.035,
2153
+ yaw
2154
+ );
2155
+ left = right;
2156
+ }
2157
+ }
2158
+ }
2159
+ var wall = (m) => courses(m, -HALF, HALF, 0, HEIGHT);
2160
+ var corner = (m) => {
2161
+ courses(m, -0.3, HALF, 0, HEIGHT);
2162
+ courses(m, 0.3, HALF, 0, HEIGHT, -Math.PI / 2);
2163
+ };
2164
+ var tee = (m) => {
2165
+ wall(m);
2166
+ courses(m, 0.3, HALF, 0, HEIGHT, -Math.PI / 2);
2167
+ };
2168
+ var cross = (m) => {
2169
+ tee(m);
2170
+ courses(m, -HALF, -0.3, 0, HEIGHT, -Math.PI / 2);
2171
+ };
2172
+ var collapsedWall = (m) => {
2173
+ const level = [3.3, 2.9, 2.2, 1.32, 0.65];
2174
+ for (const [i, height] of level.entries()) {
2175
+ courses(m, -2 + 0.8 * i, -1.2 + 0.8 * i, 0, height);
2176
+ m.gem(
2177
+ [m.rng.range(-1.8, 1.8), 0.14, m.rng.range(0.39, 0.78)],
2178
+ [0.26, 0.17, 0.24],
2179
+ m.palette.stone,
2180
+ "solid",
2181
+ 0,
2182
+ m.rng.range(0, Math.PI)
2183
+ );
2184
+ }
2185
+ };
2186
+ function masonryWedge(m, inner, outer, a, b, depth, y, z = 0, yaw = 0) {
2187
+ const shape = new Shape();
2188
+ shape.moveTo(inner * Math.cos(a), inner * Math.sin(a) + y);
2189
+ shape.lineTo(outer * Math.cos(a), outer * Math.sin(a) + y);
2190
+ shape.absarc(0, y, outer, a, b, false);
2191
+ shape.lineTo(inner * Math.cos(b), inner * Math.sin(b) + y);
2192
+ shape.absarc(0, y, inner, b, a, true);
2193
+ const stone = new ExtrudeGeometry(shape, {
2194
+ depth: depth - 0.054,
2195
+ curveSegments: 3,
2196
+ bevelSegments: 1,
2197
+ bevelThickness: 0.027,
2198
+ bevelSize: 0.021,
2199
+ steps: 1
2200
+ });
2201
+ stone.translate(0, 0, -depth / 2 + 0.027).rotateY(yaw).translate(0, 0, z);
2202
+ m.add(stone, m.palette.stone, "solid", 0.33, 0.62);
2203
+ }
2204
+ function ring(m, radius, thickness, spring, depth, yaw = 0, missing = -1, z = 0) {
2205
+ const blocks = Math.ceil(radius * 7) | 1;
2206
+ for (let i = 0; i < blocks; i++) {
2207
+ if (i === missing) continue;
2208
+ const start = Math.PI * i / blocks + 0.016;
2209
+ const end = Math.PI * (i + 1) / blocks - 0.016;
2210
+ const top = i === blocks >> 1 ? thickness * 1.15 : thickness * m.rng.range(0.88, 1.08);
2211
+ masonryWedge(
2212
+ m,
2213
+ radius,
2214
+ radius + top,
2215
+ start,
2216
+ end,
2217
+ depth * m.rng.range(0.94, 1.07),
2218
+ spring,
2219
+ z,
2220
+ yaw
2221
+ );
2222
+ }
2223
+ }
2224
+ function arch2(m, width, opening, spring, ruined = false) {
2225
+ const radius = opening / 2;
2226
+ const thickness = (width - opening) / 3.7;
2227
+ const depth = width > GRID ? 0.88 : 0.7;
2228
+ courses(m, -width / 2, -radius, 0, spring);
2229
+ courses(m, radius, width / 2, 0, spring);
2230
+ m.stone([-(width + opening) / 4, spring, 0], [width / 2 - radius, 0.2, depth + 0.11], 0.35);
2231
+ m.stone([(width + opening) / 4, spring, 0], [width / 2 - radius, 0.2, depth + 0.11], 0.35);
2232
+ ring(m, radius, thickness, spring, depth, 0, ruined ? 2 : -1);
2233
+ if (ruined) {
2234
+ for (const x of [0.36, 0.72, 1.14, 1.37])
2235
+ m.gem([x, 0.16, 0.39 + m.rng.next() * 0.47], [0.25, 0.17, 0.24], m.palette.stone);
2236
+ }
2237
+ }
2238
+ var archway = (m) => arch2(m, GRID, 2.52, 1.9);
2239
+ var largeArch = (m) => arch2(m, 6, 4.45, 2.4);
2240
+ var ruinedArch = (m) => arch2(m, GRID, 2.52, 1.9, true);
2241
+ function doorway(m, window) {
2242
+ const open = window ? 1.06 : 1.16;
2243
+ const top = window ? 2.91 : 2.66;
2244
+ for (const side of [-1, 1]) {
2245
+ if (side < 0) courses(m, -HALF, -open, 0, top);
2246
+ else courses(m, open, HALF, 0, top);
2247
+ }
2248
+ courses(m, -HALF, HALF, 0, HEIGHT - top, 0, top);
2249
+ m.stone([0, top + 0.02, 0], [open * 2 + 0.28, 0.28, 0.7], 0.21, 0.02);
2250
+ if (!window) return;
2251
+ courses(m, -open, open, 0, 1.11);
2252
+ m.stone([0, 1.1, 0], [2.39, 0.2, 0.76], 0.4);
2253
+ for (const x of [-0.91, -0.61, -0.3, 0, 0.3, 0.61, 0.91]) {
2254
+ m.rod([x, 1.12, 0], [x, 2.86, 0], 0.041, m.palette.iron, "metal", 0.041, 4);
2255
+ }
2256
+ for (const y of [1.53, 2.03, 2.53])
2257
+ m.block([0, y, 0], [2.12, 0.075, 0.073], m.palette.iron, "metal");
2258
+ }
2259
+ var doorFrame = (m) => doorway(m, false);
2260
+ var barredWindow = (m) => doorway(m, true);
2261
+ function paving(m, moss, cracked = false) {
2262
+ m.block([0, -0.24, 0], [GRID, 0.42, GRID], m.palette.joint, "solid", 0.014);
2263
+ for (let i = 0; i < 4; i++) {
2264
+ for (let j = 0; j < 4; j++) {
2265
+ const x = i - 1.5;
2266
+ const z = j - 1.5;
2267
+ if (cracked && (i + j * 2) % 3 === 0) {
2268
+ for (const shift of [-0.255, 0.255]) {
2269
+ m.stone([x + shift, -0.091, z], [0.455, 0.18, 0.95], moss, 0.045, shift * 0.13);
2270
+ }
2271
+ } else {
2272
+ m.stone([x, -0.09, z], [0.975, 0.18, 0.975], moss, 0.036);
2273
+ }
2274
+ }
2275
+ }
2276
+ }
2277
+ var floor = (m) => paving(m, 0.23);
2278
+ var crackedFloor = (m) => paving(m, 0.5, true);
2279
+ var mossyFloor = (m) => paving(m, 1.05);
2280
+ function grate(m, size = GRID, y = -0.08) {
2281
+ const edge = size / 2 - 0.08;
2282
+ for (const sign of [-1, 1]) {
2283
+ m.block([sign * edge, y, 0], [0.16, 0.17, size], m.palette.iron, "metal");
2284
+ m.block([0, y, sign * edge], [size, 0.17, 0.16], m.palette.iron, "metal");
2285
+ }
2286
+ for (let i = -3; i <= 3; i++) {
2287
+ m.block([i * size / 8, y, 0], [0.087, 0.12, size - 0.22], m.palette.iron, "metal", 0.012);
2288
+ m.block([0, y, i * size / 8], [size - 0.22, 0.086, 0.087], m.palette.iron, "metal", 0.012);
2289
+ }
2290
+ }
2291
+ function tread(m, step, width, yaw, x, z) {
2292
+ const h = (step + 1) * 0.25;
2293
+ const transverse = Math.sin(yaw);
2294
+ const longitudinal = Math.cos(yaw);
2295
+ m.block([x, h / 2, z], [width, h, 0.5], m.palette.joint, "solid", 8e-3, yaw);
2296
+ for (let j = 0; j < width; j++) {
2297
+ const offset = j - (width - 1) / 2;
2298
+ m.stone(
2299
+ [x + offset * longitudinal, h - 0.07, z - offset * transverse],
2300
+ [0.964, 0.14, 0.49],
2301
+ 0.45,
2302
+ 0.041,
2303
+ yaw
2304
+ );
2305
+ }
2306
+ }
2307
+ function stairs(m, cornered = false) {
2308
+ if (!cornered) {
2309
+ for (let i = 0; i < 8; i++) tread(m, i, GRID, 0, 0, 1.75 - i * 0.5);
2310
+ return;
2311
+ }
2312
+ for (let i = 0; i < 4; i++) {
2313
+ tread(m, i, 2, 0, -1, 1.75 - i * 0.5);
2314
+ tread(m, i + 4, 2, Math.PI / 2, 0.25 + i * 0.5, -1);
2315
+ }
2316
+ m.block([-1, 0.48, -1], [2, 0.96, 2], m.palette.joint, "solid", 0.01);
2317
+ for (const x of [-1.5, -0.5])
2318
+ for (const z of [-1.5, -0.5]) m.stone([x, 0.934, z], [0.97, 0.132, 0.97], 0.46);
2319
+ }
2320
+ var ramp = (m) => {
2321
+ const shape = new Shape();
2322
+ shape.moveTo(-HALF, 0);
2323
+ shape.lineTo(HALF, 0);
2324
+ shape.lineTo(HALF, 2);
2325
+ shape.closePath();
2326
+ m.add(
2327
+ new ExtrudeGeometry(shape, { depth: GRID, bevelEnabled: false }).translate(0, 0, -HALF).rotateY(Math.PI / 2),
2328
+ m.palette.joint
2329
+ );
2330
+ const tilt = Math.atan(0.5);
2331
+ for (let i = 0; i < 8; i++)
2332
+ for (let j = 0; j < 4; j++) {
2333
+ const z = 1.75 - i * 0.5;
2334
+ m.stone([j - 1.5, 1 - z * 0.5 - 0.022, z], [0.98, 0.112, 0.543], 0.46, 0.035, 0, tilt);
2335
+ }
2336
+ };
2337
+ var platform = (m) => {
2338
+ for (const x of [-1.53, 1.53])
2339
+ for (const z of [-1.53, 1.53]) {
2340
+ m.stone([x, 0.5, z], [0.65, 1, 0.65], 0.8);
2341
+ m.stone([x, 0.1, z], [0.85, 0.2, 0.85], 0.88);
2342
+ }
2343
+ m.block([0, 0.99, 0], [GRID, 0.24, GRID], m.palette.joint);
2344
+ for (let i = 0; i < 4; i++)
2345
+ for (let j = 0; j < 4; j++) m.stone([i - 1.5, 1.15, j - 1.5], [0.98, 0.16, 0.98], 0.58);
2346
+ };
2347
+ function pillar(m, broken = false) {
2348
+ const cap = broken ? 1.74 : 3.42;
2349
+ m.stone([0, 0.12, 0], [1.25, 0.24, 1.25], 0.87);
2350
+ m.stone([0, 0.31, 0], [1.03, 0.17, 1.03], 0.69);
2351
+ for (let y = 0.54; y < cap; y += 0.36) {
2352
+ m.stone([0, y, 0], [0.74, 0.36, 0.74], 0.63 - y * 0.12, 0.045, m.rng.range(-0.025, 0.025));
2353
+ }
2354
+ if (broken) {
2355
+ m.gem([-0.13, 1.79, 0], [0.39, 0.39, 0.38], m.palette.stone, "solid", 0, 0.55);
2356
+ m.gem([0.62, 0.22, 0.27], [0.26, 0.22, 0.23], m.palette.stone);
2357
+ } else {
2358
+ m.stone([0, 3.37, 0], [1.04, 0.2, 1.04], 0.27);
2359
+ m.stone([0, 3.54, 0], [1.25, 0.16, 1.25], 0.23);
2360
+ }
2361
+ }
2362
+ var vault = (m) => {
2363
+ for (const yaw of [-Math.PI / 4, Math.PI / 4]) ring(m, 2.65, 0.23, 0, 0.22, yaw);
2364
+ for (const z of [-1.92, 1.92]) ring(m, 1.97, 0.27, 0, 0.24, 0, -1, z);
2365
+ for (let row = -3; row <= 3; row++) {
2366
+ const z = row * 0.55;
2367
+ const rise = Math.sqrt(1 - (z / 2.03) ** 2);
2368
+ for (let col2 = -3; col2 <= 3; col2++) {
2369
+ const x = col2 * 0.55;
2370
+ const y = Math.max(rise, Math.sqrt(1 - (x / 2.03) ** 2)) * 1.95;
2371
+ m.stone([x, y, z], [0.55, 0.26, 0.55], 0.31, 0.043);
2372
+ }
2373
+ }
2374
+ };
2375
+ var beam = (m) => {
2376
+ m.block([0, 1.63, 0], [GRID, 0.43, 0.43], m.palette.wood);
2377
+ for (const x of [-1.67, 1.67]) {
2378
+ m.block([x, 0.79, 0], [0.32, 1.6, 0.34], m.palette.wood);
2379
+ m.rod([x, 0.82, 0], [x * 0.57, 1.47, 0], 0.14, m.palette.wood, "solid", 0.14, 4);
2380
+ for (const y of [1.51, 1.76])
2381
+ m.block([x, y, 0.235], [0.11, 0.11, 0.035], m.palette.iron, "metal");
2382
+ }
2383
+ };
2384
+ var BONE = 13218962;
2385
+ function brace(m, x, y, z, width, height) {
2386
+ for (const side of [-1, 1]) {
2387
+ m.rod(
2388
+ [x - width / 2, y - side * height / 2, z],
2389
+ [x + width / 2, y + side * height / 2, z],
2390
+ 0.045,
2391
+ m.palette.wood,
2392
+ "solid",
2393
+ 0.045,
2394
+ 4
2395
+ );
2396
+ }
2397
+ }
2398
+ var crate = (m) => {
2399
+ for (let i = 0; i < 4; i++) {
2400
+ const x = (i - 1.5) * 0.294;
2401
+ m.block([x, 0.56, 0], [0.278, 1.12, 1.15], m.palette.wood);
2402
+ }
2403
+ for (const side of [-1, 1]) {
2404
+ const z = side * 0.607;
2405
+ for (const y of [0.092, 1.03]) m.block([0, y, z], [1.24, 0.12, 0.11], m.palette.wood);
2406
+ for (const x of [-0.555, 0.555]) m.block([x, 0.56, z], [0.095, 1.1, 0.12], m.palette.wood);
2407
+ brace(m, 0, 0.56, z, 1.03, 0.87);
2408
+ for (const x of [-0.55, 0.55])
2409
+ for (const y of [0.09, 1.03])
2410
+ m.gem([x, y, z + side * 0.065], [0.035, 0.035, 0.024], m.palette.iron, "metal");
2411
+ }
2412
+ };
2413
+ var barrel = (m) => {
2414
+ const stations = [0.38, 0.46, 0.49, 0.46, 0.38];
2415
+ for (let i = 0; i < 12; i++) {
2416
+ const a = i * Math.PI / 6;
2417
+ for (let j = 0; j < 4; j++) {
2418
+ const radius = ((stations[j] ?? 0) + (stations[j + 1] ?? 0)) / 2;
2419
+ const stave = [Math.cos(a) * radius, 0.144 + j * 0.28, Math.sin(a) * radius];
2420
+ m.block(stave, [0.116, 0.344, radius * 0.52], m.palette.wood, "solid", 0.025, -a);
2421
+ }
2422
+ }
2423
+ for (const [y, radius] of [
2424
+ [0.17, 0.445],
2425
+ [0.55, 0.516],
2426
+ [0.95, 0.455]
2427
+ ]) {
2428
+ const hoop = new TorusGeometry(radius, 0.037, 4, 24).rotateX(-Math.PI / 2).translate(0, y, 0);
2429
+ m.add(hoop, m.palette.iron, "metal");
2430
+ }
2431
+ m.rod([0, 1.018, 0], [0, 1.08, 0], 0.407, m.palette.wood, "solid", 0.407, 12);
2432
+ for (const z of [-0.19, 0, 0.19]) m.block([0, 1.081, z], [0.72, 0.012, 0.01], m.palette.joint);
2433
+ };
2434
+ var sack = (m) => {
2435
+ m.gem([0, 0.35, 0], [0.43, 0.45, 0.37], 10914402, "solid", 2, 0.23);
2436
+ m.gem([0.02, 0.73, 0], [0.19, 0.13, 0.16], 11506539, "solid", 1);
2437
+ m.rod([0.02, 0.698, 0], [0.02, 0.757, 0], 0.142, 6902589, "solid", 0.141, 10);
2438
+ m.rod([0.13, 0.73, 0.06], [0.15, 0.56, 0.16], 0.025, 9730385);
2439
+ };
2440
+ function chestLid(m, open) {
2441
+ const tilt = open ? -1.83 : 0;
2442
+ function strip(start, end, x, width, strap) {
2443
+ const angles = Array.from({ length: 4 }, (_, i) => start + (end - start) * i / 3);
2444
+ const point = (a, r) => new Vector2(0.32 + 0.382 * r * Math.cos(a), 0.317 * r * Math.sin(a));
2445
+ const outline = [
2446
+ ...angles.map((a) => point(a, strap ? 1.075 : 1)),
2447
+ ...angles.reverse().map((a) => point(a, strap ? 1.018 : 0.862))
2448
+ ];
2449
+ const shape = new Shape(outline);
2450
+ shape.closePath();
2451
+ const piece2 = new ExtrudeGeometry(shape, { depth: width, bevelEnabled: false });
2452
+ piece2.rotateY(-Math.PI / 2).translate(x + width / 2, 0, 0).rotateX(tilt).translate(0, 0.63, -0.32);
2453
+ m.add(piece2, strap ? 6379857 : m.palette.wood, strap ? "metal" : "solid", 0.15);
2454
+ }
2455
+ for (let i = 0; i < 10; i++) {
2456
+ const lo = i * Math.PI / 10;
2457
+ const hi = (i + 1) * Math.PI / 10;
2458
+ strip(lo + 6e-3, hi - 6e-3, 0, 1.16, false);
2459
+ for (const x of [-0.485, 0.485]) strip(lo, hi, x, 0.1, true);
2460
+ }
2461
+ }
2462
+ function chest(m, open = false) {
2463
+ m.block([0, 0.105, 0], [1.17, 0.19, 0.71], m.palette.wood);
2464
+ for (const x of [-0.53, 0.53]) m.block([x, 0.35, 0], [0.14, 0.58, 0.71], m.palette.wood);
2465
+ for (const z of [-0.3, 0.3]) {
2466
+ for (const y of [0.22, 0.4, 0.58]) m.block([0, y, z], [1.12, 0.173, 0.12], m.palette.wood);
2467
+ for (const x of [-0.46, 0.46])
2468
+ m.block([x, 0.363, z * 1.22], [0.075, 0.59, 0.043], m.palette.iron, "metal");
2469
+ }
2470
+ m.block([0, 0.56, 0.383], [0.21, 0.21, 0.065], 10187576, "metal");
2471
+ m.block([0, 0.55, 0.422], [0.03, 0.068, 0.015], m.palette.joint, "metal");
2472
+ chestLid(m, open);
2473
+ if (!open) return;
2474
+ for (let i = 0; i < 31; i++) {
2475
+ const x = m.rng.range(-0.46, 0.46);
2476
+ const z = m.rng.range(-0.22, 0.22);
2477
+ const y = m.rng.range(0.53, 0.66);
2478
+ m.rod(
2479
+ [x, y, z],
2480
+ [x + m.rng.range(-0.022, 0.022), y + 0.025, z],
2481
+ 0.063,
2482
+ i % 3 ? 14984001 : 16767862,
2483
+ "metal",
2484
+ 0.061,
2485
+ 8
2486
+ );
2487
+ }
2488
+ for (const x of [-0.22, 0.25]) m.gem([x, 0.67, 0.04], [0.085, 0.087, 0.082], 12007481, "glow");
2489
+ }
2490
+ var cage = (m) => {
2491
+ const w = 0.95;
2492
+ for (const x of [-w / 2, w / 2])
2493
+ for (const z of [-w / 2, w / 2]) {
2494
+ m.rod([x, 0.02, z], [x, 1.46, z], 0.051, m.palette.iron, "metal", 0.051, 4);
2495
+ m.rod([x, 1.46, z], [0, 1.78, 0], 0.035, m.palette.iron, "metal");
2496
+ }
2497
+ for (const s of [-1, 1]) {
2498
+ for (const n of [-0.25, 0, 0.25]) {
2499
+ m.rod([n, 0.05, s * w / 2], [n, 1.45, s * w / 2], 0.026, m.palette.iron, "metal");
2500
+ m.rod([s * w / 2, 0.05, n], [s * w / 2, 1.45, n], 0.026, m.palette.iron, "metal");
2501
+ }
2502
+ for (const y of [0.06, 0.78, 1.44]) {
2503
+ m.block([0, y, s * w / 2], [w, 0.056, 0.068], m.palette.iron, "metal");
2504
+ m.block([s * w / 2, y, 0], [0.068, 0.056, w], m.palette.iron, "metal");
2505
+ }
2506
+ }
2507
+ m.add(new TorusGeometry(0.095, 0.022, 5, 12).translate(0, 1.85, 0), m.palette.iron, "metal");
2508
+ };
2509
+ function furniture(m, bench = false) {
2510
+ const height = bench ? 0.45 : 0.87;
2511
+ const depth = bench ? 0.5 : 1;
2512
+ const width = bench ? 1.78 : 2.2;
2513
+ for (let i = 0; i < 4; i++) {
2514
+ m.block(
2515
+ [0, height, (i - 1.5) * depth / 4],
2516
+ [width + m.rng.range(-0.09, 0.07), 0.11, depth / 4 - 9e-3],
2517
+ m.palette.wood
2518
+ );
2519
+ }
2520
+ for (const x of [-width * 0.42, width * 0.42])
2521
+ for (const z of [-depth * 0.35, depth * 0.35]) {
2522
+ m.block([x, height / 2, z], [0.11, height, 0.12], m.palette.wood);
2523
+ }
2524
+ for (const z of [-depth * 0.37, depth * 0.37])
2525
+ m.block([0, height * 0.42, z], [width * 0.89, 0.115, 0.08], m.palette.wood);
2526
+ }
2527
+ function vessel(m, x, y, z, scale) {
2528
+ m.gem(
2529
+ [x, y + 0.13 * scale, z],
2530
+ [0.135 * scale, 0.14 * scale, 0.135 * scale],
2531
+ 8806731,
2532
+ "solid",
2533
+ 1
2534
+ );
2535
+ m.rod(
2536
+ [x, y + 0.21 * scale, z],
2537
+ [x, y + 0.31 * scale, z],
2538
+ 0.06 * scale,
2539
+ 10121298,
2540
+ "solid",
2541
+ 0.075 * scale
2542
+ );
2543
+ }
2544
+ var shelves = (m) => {
2545
+ for (const x of [-0.86, 0.86])
2546
+ for (const z of [-0.2, 0.2]) m.block([x, 0.99, z], [0.12, 1.98, 0.13], m.palette.wood);
2547
+ for (const y of [0.21, 0.77, 1.34, 1.94]) {
2548
+ m.block([0, y, 0], [1.79, 0.095, 0.54], m.palette.wood);
2549
+ for (let j = 0; j < 4; j++) {
2550
+ const x = -0.69 + j * 0.45;
2551
+ if (m.rng.next() > 0.24) vessel(m, x, y + 0.05, 0, m.rng.range(0.64, 1.3));
2552
+ }
2553
+ }
2554
+ };
2555
+ function weaponStand(m, rack = false) {
2556
+ const width = rack ? 1.9 : 1.4;
2557
+ for (const x of [-width / 2, width / 2]) {
2558
+ m.block([x, 1.05, 0], [0.14, 2.06, 0.15], m.palette.wood);
2559
+ m.block([x, 0.065, 0], [0.17, 0.13, 0.72], m.palette.wood);
2560
+ }
2561
+ for (const y of rack ? [0.25, 0.88, 1.58] : [0.31, 1.4])
2562
+ m.block([0, y, -0.07], [width, 0.13, 0.13], m.palette.wood);
2563
+ const count = rack ? 6 : 4;
2564
+ for (let i = 0; i < count; i++) {
2565
+ const x = width * ((i + 0.5) / count - 0.5);
2566
+ const base = rack && i % 2 === 0 ? 0.86 : 0.13;
2567
+ const height = rack ? m.rng.range(0.66, 1.13) : m.rng.range(1.48, 1.97);
2568
+ m.rod([x, base, 0.11], [x + 0.09, base + height, 0.01], 0.027, m.palette.wood);
2569
+ m.rod(
2570
+ [x + 0.09, base + height - 0.03, 0.01],
2571
+ [x + 0.115, base + height + 0.24, -0.016],
2572
+ 0.078,
2573
+ 8880522,
2574
+ "metal",
2575
+ 3e-3,
2576
+ 4
2577
+ );
2578
+ if (rack)
2579
+ m.block(
2580
+ [x + 0.07, base + height - 0.13, 0.017],
2581
+ [0.23, 0.049, 0.055],
2582
+ m.palette.iron,
2583
+ "metal"
2584
+ );
2585
+ }
2586
+ }
2587
+ function flag(m, points, color, z) {
2588
+ const path = new Shape();
2589
+ for (const [[x, y], i] of points.map((p, i2) => [p, i2])) {
2590
+ if (i === 0) path.moveTo(x, y);
2591
+ else path.lineTo(x, y);
2592
+ }
2593
+ path.closePath();
2594
+ m.add(new ExtrudeGeometry(path, { depth: 0.025, bevelEnabled: false }).translate(0, 0, z), color);
2595
+ }
2596
+ var banner = (m) => {
2597
+ const gilt = m.palette.pale;
2598
+ flag(
2599
+ m,
2600
+ [
2601
+ [-0.58, 2.66],
2602
+ [0.58, 2.66],
2603
+ [0.57, 0.04],
2604
+ [0, 0.44],
2605
+ [-0.57, 0.04]
2606
+ ],
2607
+ m.palette.cloth,
2608
+ 0
2609
+ );
2610
+ for (const x of [-0.515, 0.515])
2611
+ m.block([x, 1.5, 0.029], [0.027, 2.32, 7e-3], gilt, "solid", 2e-3);
2612
+ flag(
2613
+ m,
2614
+ [
2615
+ [0, 2.16],
2616
+ [0.105, 1.76],
2617
+ [0.38, 1.66],
2618
+ [0.13, 1.59],
2619
+ [0, 1.05],
2620
+ [-0.13, 1.59],
2621
+ [-0.38, 1.66],
2622
+ [-0.105, 1.76]
2623
+ ],
2624
+ gilt,
2625
+ 0.032
2626
+ );
2627
+ m.rod([-0.76, 2.68, 0], [0.76, 2.68, 0], 0.046, m.palette.wood);
2628
+ for (const x of [-0.76, 0.76]) m.gem([x, 2.68, 0], [0.086, 0.077, 0.077], gilt, "metal");
2629
+ };
2630
+ function flame(m, [x, y, z], scale) {
2631
+ for (let i = 0; i < 6; i++) {
2632
+ const dx = i === 0 ? 0 : m.rng.range(-0.19, 0.19) * scale;
2633
+ const dz = i === 0 ? 0.012 : m.rng.range(-0.15, 0.15) * scale;
2634
+ const h = (i === 0 ? 0.94 : m.rng.range(0.26, 0.67)) * scale;
2635
+ const r = m.rng.range(0.073, 0.145) * scale;
2636
+ const points = [
2637
+ [1e-3, 0],
2638
+ [r * 0.9, h * 0.16],
2639
+ [r, h * 0.31],
2640
+ [r * 0.71, h * 0.53],
2641
+ [r * 0.34, h * 0.74],
2642
+ [1e-3, h]
2643
+ ];
2644
+ const tongue = new LatheGeometry(
2645
+ points.map(([radius = 0, height = 0]) => new Vector2(radius, height)),
2646
+ 7
2647
+ );
2648
+ const pos = tongue.getAttribute("position");
2649
+ for (let v = 0; v < pos.count; v++) {
2650
+ const t = pos.getY(v) / h;
2651
+ pos.setX(v, pos.getX(v) + (Math.sin(t * 5 + i) * 0.14 + t * dx) * h);
2652
+ pos.setZ(v, pos.getZ(v) - t * dz * 0.45);
2653
+ }
2654
+ tongue.computeVertexNormals();
2655
+ tongue.translate(x + dx, y, z + dz);
2656
+ m.add(tongue, i === 2 || i === 5 ? m.palette.pale : m.palette.fire, "glow", 0.08, 0, [y, h]);
2657
+ }
2658
+ for (let i = 0; i < 5; i++) {
2659
+ const size = scale * m.rng.range(0.012, 0.026);
2660
+ const ax = x + m.rng.range(-0.34, 0.31) * scale;
2661
+ const ay = y + m.rng.range(0.6, 1.27) * scale;
2662
+ const az = z + m.rng.range(-0.25, 0.25) * scale;
2663
+ m.rod(
2664
+ [ax, ay, az],
2665
+ [ax + size * 0.15, ay + size * 1.9, az],
2666
+ size,
2667
+ m.palette.fire,
2668
+ "glow",
2669
+ 2e-3,
2670
+ 4,
2671
+ [y, scale * 1.25]
2672
+ );
2673
+ }
2674
+ }
2675
+ var wallTorch = (m) => {
2676
+ m.block([0, 0.56, -0.19], [0.24, 0.59, 0.085], m.palette.iron, "metal");
2677
+ m.rod([0, 0.08, -0.03], [0, 0.91, 0.28], 0.07, m.palette.wood, "solid", 0.1, 7);
2678
+ m.rod([0, 0.78, 0.22], [0, 0.96, 0.3], 0.12, m.palette.iron, "metal", 0.16);
2679
+ flame(m, [0, 0.94, 0.3], 0.59);
2680
+ m.fireLight([0, 1.23, 0.47], 21);
2681
+ };
2682
+ var brazier = (m) => {
2683
+ for (let i = 0; i < 3; i++) {
2684
+ const a = i * Math.PI * 2 / 3;
2685
+ m.rod(
2686
+ [Math.cos(a) * 0.48, 0.045, Math.sin(a) * 0.48],
2687
+ [Math.cos(a) * 0.25, 0.69, Math.sin(a) * 0.25],
2688
+ 0.063,
2689
+ m.palette.iron,
2690
+ "metal"
2691
+ );
2692
+ }
2693
+ m.rod([0, 0.6, 0], [0, 0.82, 0], 0.19, m.palette.iron, "metal", 0.54, 14);
2694
+ m.rod([0, 0.82, 0], [0, 0.86, 0], 0.55, 6640986, "metal", 0.52, 14);
2695
+ m.rod([0, 0.86, 0], [0, 0.886, 0], 0.44, m.palette.fire, "glow", 0.44, 14);
2696
+ flame(m, [0, 0.88, 0], 1.25);
2697
+ m.fireLight([0, 1.23, 0], 29);
2698
+ };
2699
+ function candle(m, x, y, z, height) {
2700
+ m.rod([x, y, z], [x, y + height, z], 0.061, BONE, "solid", 0.053, 8);
2701
+ m.rod(
2702
+ [x + 0.04, y + height * 0.49, z + 0.026],
2703
+ [x + 0.04, y + height - 0.02, z + 0.025],
2704
+ 0.013,
2705
+ BONE
2706
+ );
2707
+ m.rod([x, y + height, z], [x, y + height + 0.053, z], 0.015, m.palette.iron);
2708
+ m.rod(
2709
+ [x, y + height + 0.046, z],
2710
+ [x + 0.015, y + height + 0.166, z],
2711
+ 0.034,
2712
+ m.palette.pale,
2713
+ "glow",
2714
+ 3e-3,
2715
+ 6,
2716
+ [y + height, 0.18]
2717
+ );
2718
+ }
2719
+ var candles = (m) => {
2720
+ m.rod([0, 0.025, 0], [0, 0.065, 0], 0.44, m.palette.iron, "metal", 0.44, 12);
2721
+ for (const [x, z, h] of [
2722
+ [0, 0, 0.53],
2723
+ [-0.14, 0.14, 0.34],
2724
+ [0.18, 0.1, 0.25],
2725
+ [0.08, -0.2, 0.37],
2726
+ [0.27, -0.075, 0.21],
2727
+ [-0.2, -0.12, 0.28]
2728
+ ])
2729
+ candle(m, x, 0.065, z, h);
2730
+ m.fireLight([0, 0.59, 0], 6);
2731
+ };
2732
+ var hangingLamp = (m) => {
2733
+ const corners = [-0.26, 0.26];
2734
+ for (const x of corners)
2735
+ for (const z of corners) {
2736
+ m.rod([x, 0.08, z], [x, 0.64, z], 0.038, m.palette.iron, "metal");
2737
+ m.rod([x, 0.64, z], [0, 0.89, 0], 0.035, m.palette.iron, "metal");
2738
+ }
2739
+ for (const y of [0.08, 0.63]) m.block([0, y, 0], [0.56, 0.079, 0.56], m.palette.iron, "metal");
2740
+ candle(m, 0, 0.13, 0, 0.31);
2741
+ m.fireLight([0, 0.5, 0], 7);
2742
+ for (let i = 0; i < 6; i++) {
2743
+ const link = new TorusGeometry(0.075, 0.018, 5, 10).rotateY(i * Math.PI / 2).translate(0, 0.93 + i * 0.106, 0);
2744
+ m.add(link, m.palette.iron, "metal");
2745
+ }
2746
+ };
2747
+ var rubble = (m) => {
2748
+ for (let i = 0; i < 28; i++) {
2749
+ const angle = m.rng.range(0, Math.PI * 2);
2750
+ const r = m.rng.range(0.04, 0.95);
2751
+ const size = m.rng.range(0.1, 0.35) * (1 - r * 0.55);
2752
+ m.gem(
2753
+ [Math.cos(angle) * r, size * 0.58, Math.sin(angle) * r],
2754
+ [size, size * 0.69, size * 0.81],
2755
+ m.palette.stone,
2756
+ "solid",
2757
+ 0,
2758
+ angle
2759
+ );
2760
+ }
2761
+ };
2762
+ var bones = (m) => {
2763
+ const x = -0.27;
2764
+ m.gem([x, 0.25, -0.09], [0.225, 0.235, 0.235], BONE, "solid", 2, 0.2);
2765
+ m.block([x, 0.097, 0.067], [0.25, 0.091, 0.233], BONE);
2766
+ for (const dx of [-0.087, 0.087])
2767
+ m.gem([x + dx, 0.247, 0.11], [0.053, 0.052, 0.048], m.palette.joint, "solid", 1);
2768
+ m.gem([x, 0.192, 0.124], [0.036, 0.044, 0.031], m.palette.joint);
2769
+ for (let i = 0; i < 6; i++)
2770
+ m.block([x + (i - 2.5) * 0.033, 0.144, 0.182], [0.025, 0.048, 0.029], BONE, "solid", 3e-3);
2771
+ for (const [a, b] of [
2772
+ [
2773
+ [0.07, 0.066, 0.29],
2774
+ [0.68, 0.066, -0.04]
2775
+ ],
2776
+ [
2777
+ [0.12, 0.073, -0.17],
2778
+ [0.52, 0.073, 0.29]
2779
+ ],
2780
+ [
2781
+ [-0.51, 0.055, 0.46],
2782
+ [-0.05, 0.055, 0.37]
2783
+ ]
2784
+ ]) {
2785
+ m.rod(a, b, 0.041, BONE);
2786
+ for (const p of [a, b]) m.gem(p, [0.081, 0.062, 0.066], BONE, "solid", 1);
2787
+ }
2788
+ };
2789
+ var TAU3 = Math.PI * 2;
2790
+ var LIMESTONE = 7961458;
2791
+ var SHADE = 5133392;
2792
+ var EDGE = 10066059;
2793
+ function surface(rows, closed = true) {
2794
+ const stride = rows[0]?.length ?? 0;
2795
+ const data = rows.flat(2);
2796
+ const ix = [];
2797
+ const columns = closed ? stride : stride - 1;
2798
+ for (let y = 0; y < rows.length - 1; y++) {
2799
+ for (let x = 0; x < columns; x++) {
2800
+ const a = y * stride + x;
2801
+ const b = y * stride + (x + 1) % stride;
2802
+ ix.push(a, b, a + stride, b, b + stride, a + stride);
2803
+ }
2804
+ }
2805
+ const mesh = new BufferGeometry();
2806
+ mesh.setAttribute("position", new Float32BufferAttribute(data, 3));
2807
+ mesh.setIndex(ix);
2808
+ mesh.computeVertexNormals();
2809
+ return mesh;
2810
+ }
2811
+ function mantle(m) {
2812
+ const layers = [
2813
+ [0.48, 0.53, 0.35, -0.05, 0.5],
2814
+ [0.81, 0.5, 0.33, -0.048, 0.43],
2815
+ [1.23, 0.43, 0.29, -0.031, 0.27],
2816
+ [1.62, 0.34, 0.27, -0.025, 0.16],
2817
+ [1.85, 0.405, 0.27, -0.036, 0.15],
2818
+ [2.13, 0.49, 0.256, -0.046, 0.22],
2819
+ [2.25, 0.37, 0.23, -0.039, 0.3],
2820
+ [2.37, 0.24, 0.199, -0.02, 0.45]
2821
+ ];
2822
+ const rows = layers.map(
2823
+ ([y, w, d, back, opening], layer) => Array.from({ length: 49 }, (_, i) => {
2824
+ const a = opening + i / 48 * (TAU3 - opening * 2);
2825
+ const forward = Math.max(0, Math.cos(a)) ** 2;
2826
+ const drape = Math.cos(a * 11 + y * 2.25) ** 5 * (layer < 4 ? 0.039 : 0.023);
2827
+ const tuck = Math.sin(a * 5 + layer * 0.62) * 0.017;
2828
+ const hem = Math.max(0, 1.36 - y) * forward * 0.79;
2829
+ return [
2830
+ Math.sin(a) * (w + drape + tuck) + Math.sin(y * 1.8) * 0.039,
2831
+ y + hem + tuck + Math.max(0, layer - 4) * Math.sin(a * 2) * 0.012,
2832
+ back + Math.cos(a) * (d + drape + tuck)
2833
+ ];
2834
+ })
2835
+ );
2836
+ m.add(surface(rows, false), LIMESTONE, "solid", 0.128, 0.27);
2837
+ for (const side of [-1, 1]) {
2838
+ const edge = rows.map((row) => side < 0 ? row.at(-1) : row[0]);
2839
+ for (let i = 1; i < edge.length; i++) {
2840
+ const a = edge[i - 1];
2841
+ const b = edge[i];
2842
+ m.rod(a, b, 0.031, EDGE, "solid", 0.029, 7);
2843
+ }
2844
+ }
2845
+ }
2846
+ function hood(m) {
2847
+ const sections2 = [
2848
+ [0.12, 0.157, 0.224, 2.56],
2849
+ [0.256, 0.177, 0.248, 2.58],
2850
+ [0.275, 0.194, 0.29, 2.593],
2851
+ [0.214, 0.237, 0.316, 2.59],
2852
+ [0.07, 0.25, 0.302, 2.57],
2853
+ [-0.082, 0.229, 0.267, 2.53],
2854
+ [-0.187, 0.18, 0.205, 2.5],
2855
+ [-0.237, 0.097, 0.123, 2.489],
2856
+ [-0.263, 6e-3, 6e-3, 2.483]
2857
+ ];
2858
+ const wrap = sections2.map(
2859
+ ([z, w, h, y], index) => Array.from({ length: 40 }, (_, i) => {
2860
+ const a = i / 40 * TAU3;
2861
+ const cos = Math.cos(a);
2862
+ const narrow = 1 - 0.38 * Math.max(0, cos) ** 4 - 0.28 * Math.max(0, -cos) ** 3;
2863
+ const crease = Math.cos(a * 9 + index * 0.63) * 9e-3;
2864
+ return [
2865
+ Math.sin(a) * (w + crease) * narrow,
2866
+ y + h * cos,
2867
+ z + cos * 0.058 + Math.sin(a * 3) * 0.012
2868
+ ];
2869
+ })
2870
+ );
2871
+ m.add(surface(wrap), EDGE, "solid", 0.126, 0.1);
2872
+ m.gem([0, 2.536, 0.047], [0.164, 0.239, 0.126], SHADE, "solid", 2);
2873
+ const face = [
2874
+ [2.412, 0.048, 0.177, 0.189],
2875
+ [2.444, 0.089, 0.173, 0.205],
2876
+ [2.49, 0.108, 0.178, 0.216],
2877
+ [2.54, 0.121, 0.184, 0.205],
2878
+ [2.578, 0.129, 0.174, 0.193],
2879
+ [2.627, 0.13, 0.184, 0.217],
2880
+ [2.684, 0.114, 0.173, 0.214],
2881
+ [2.714, 0.082, 0.156, 0.185]
2882
+ ];
2883
+ m.add(
2884
+ surface(
2885
+ face.map(([y, w, edge, ridge]) => [
2886
+ [-w, y, edge],
2887
+ [-w * 0.53, y, edge * 0.53 + ridge * 0.47],
2888
+ [0, y, ridge],
2889
+ [w * 0.53, y, edge * 0.53 + ridge * 0.47],
2890
+ [w, y, edge]
2891
+ ]),
2892
+ false
2893
+ ),
2894
+ 8750716,
2895
+ "solid",
2896
+ 0.08
2897
+ );
2898
+ m.gem([0, 2.544, 0.216], [0.034, 0.061, 0.027], EDGE, "solid", 0, 0.04);
2899
+ for (const side of [-1, 1]) {
2900
+ const x = side * 0.062;
2901
+ m.rod(
2902
+ [x - 0.039, 2.596, 0.199],
2903
+ [x + 0.039, 2.598 + side * 0.012, 0.206],
2904
+ 0.014,
2905
+ SHADE,
2906
+ "solid",
2907
+ 8e-3,
2908
+ 5
2909
+ );
2910
+ m.rod(
2911
+ [x - 0.041, 2.618, 0.217],
2912
+ [x + 0.041, 2.621 + side * 0.013, 0.222],
2913
+ 0.019,
2914
+ LIMESTONE,
2915
+ "solid",
2916
+ 8e-3,
2917
+ 5
2918
+ );
2919
+ }
2920
+ m.rod([-0.042, 2.46, 0.207], [0.038, 2.46, 0.208], 7e-3, SHADE, "solid", 8e-3, 5);
2921
+ }
2922
+ function sleeve(m, side) {
2923
+ const sections2 = [
2924
+ [0.27, 2.19, -0.04, 0.117],
2925
+ [0.382, 2.13, -7e-3, 0.145],
2926
+ [0.464, 2.012, 0.028, 0.166],
2927
+ [0.49, 1.845, 0.079, 0.149],
2928
+ [0.46, 1.736, 0.14, 0.153],
2929
+ [0.32, 1.687, 0.248, 0.125],
2930
+ [0.175, 1.632, 0.337, 0.101],
2931
+ [0.123, 1.604, 0.381, 0.091]
2932
+ ];
2933
+ const centers = sections2.map(([x, y, z]) => new Vector3(x * side, y, z));
2934
+ const rows = sections2.map(([, , , radius], j) => {
2935
+ const center = centers[j];
2936
+ const before = centers[Math.max(j - 1, 0)];
2937
+ const after = centers[Math.min(j + 1, centers.length - 1)];
2938
+ const axis = before.clone().sub(after).normalize();
2939
+ const u = new Vector3(0, 0, 1).cross(axis).normalize();
2940
+ const v = axis.clone().cross(u);
2941
+ return Array.from({ length: 20 }, (_, i) => {
2942
+ const t = i / 20 * TAU3;
2943
+ const fold = radius * (1 + 0.097 * Math.sin(5 * t + 0.36 * j));
2944
+ return center.clone().addScaledVector(u, Math.cos(t) * fold).addScaledVector(v, -Math.sin(t) * fold).toArray();
2945
+ });
2946
+ });
2947
+ m.add(surface(rows), LIMESTONE, "solid", 0.14);
2948
+ }
2949
+ function limbsAndBlade(m) {
2950
+ for (const side of [-1, 1]) {
2951
+ const x = side * 0.18;
2952
+ m.rod([x, 0.46, 0.021], [x * 1.12, 1.26, 0.055], 0.119, SHADE, "solid", 0.152, 8);
2953
+ m.gem([x, 0.504, 0.17], [0.139, 0.108, 0.23], LIMESTONE, "solid", 1, side * 0.11);
2954
+ sleeve(m, side);
2955
+ m.rod(
2956
+ [side * 0.203, 1.648, 0.317],
2957
+ [side * 0.147, 1.616, 0.358],
2958
+ 0.113,
2959
+ EDGE,
2960
+ "solid",
2961
+ 0.104,
2962
+ 9
2963
+ );
2964
+ m.gem([side * 0.079, 1.596, 0.399], [0.095, 0.077, 0.082], EDGE, "solid", 1, side * 0.22);
2965
+ for (let finger = 0; finger < 3; finger++) {
2966
+ const dx = side * (0.028 + finger * 0.029);
2967
+ m.rod([dx, 1.604, 0.449], [dx * 0.81, 1.553, 0.451], 0.014, LIMESTONE, "solid", 0.012, 5);
2968
+ }
2969
+ }
2970
+ const z = 0.455;
2971
+ m.rod([0, 0.45, z], [0, 1.485, z], 0.015, EDGE, "solid", 0.086, 4);
2972
+ m.block([0, 1.493, z], [0.435, 0.067, 0.085], LIMESTONE, "solid", 0.023);
2973
+ m.rod([0, 1.485, z], [0, 1.699, z], 0.044, SHADE, "solid", 0.039, 8);
2974
+ m.gem([0, 1.707, z], [0.057, 0.041, 0.048], EDGE, "solid", 1);
2975
+ m.gem([0.042, 2.082, 0.253], [0.081, 0.086, 0.041], EDGE, "solid", 1);
2976
+ }
2977
+ var statue = (m) => {
2978
+ m.stone([0, 0.139, 0], [1.44, 0.278, 1.23], 0.55, 0.094);
2979
+ m.stone([0, 0.314, 0], [1.31, 0.13, 1.08], 0.34, 0.055);
2980
+ m.stone([0, 0.417, 0], [1.19, 0.14, 1.01], 0.33, 0.062);
2981
+ m.block([0, 0.154, 0.603], [0.59, 0.146, 0.027], SHADE, "solid", 0.026);
2982
+ for (const x of [-0.13, 0.13])
2983
+ m.rod([x, 0.112, 0.625], [x * 0.31, 0.19, 0.625], 0.013, EDGE, "solid", 0.013, 4);
2984
+ m.gem([0.027, 1.66, -0.057], [0.396, 0.733, 0.271], SHADE, "solid", 2, 0);
2985
+ m.gem([0, 2.31, -0.012], [0.188, 0.196, 0.185], SHADE, "solid", 1);
2986
+ mantle(m);
2987
+ hood(m);
2988
+ limbsAndBlade(m);
2989
+ };
2990
+
2991
+ // src/dungeon/features.ts
2992
+ var portcullis = (m) => {
2993
+ const radius = 1.21;
2994
+ const spring = 1.9;
2995
+ for (let i = -5; i <= 5; i++) {
2996
+ const x = i * 0.216;
2997
+ const y = spring + Math.sqrt(radius * radius - x * x);
2998
+ m.rod([x, 0.16, 0], [x, y, 0], 0.053, m.palette.iron, "metal", 0.044, 4);
2999
+ m.rod([x, 0.16, 0], [x, 5e-3, 0], 0.054, m.palette.iron, "metal", 2e-3, 4);
3000
+ }
3001
+ for (const y of [0.28, 0.82, 1.38, 1.93, 2.45]) {
3002
+ const w = y <= spring ? 2.43 : Math.sqrt(radius * radius - (y - spring) ** 2) * 2;
3003
+ m.block([0, y, 0], [w, 0.103, 0.121], m.palette.iron, "metal", 0.012);
3004
+ for (let x = -1.08; x <= 1.09; x += 0.216) {
3005
+ if (Math.abs(x) < w / 2) m.gem([x, y, 0.078], [0.024, 0.025, 0.019], 9671066, "metal");
3006
+ }
3007
+ }
3008
+ for (const x of [-1.21, 1.21])
3009
+ m.block([x, 1.06, 0], [0.086, 2.12, 0.096], m.palette.iron, "metal");
3010
+ };
3011
+ var lever = (m) => {
3012
+ m.stone([0, 0.15, 0], [0.98, 0.3, 0.85], 0.61);
3013
+ m.stone([0, 0.39, 0], [0.78, 0.22, 0.65], 0.36);
3014
+ for (const x of [-0.18, 0.18]) m.block([x, 0.56, 0], [0.17, 0.29, 0.54], m.palette.iron, "metal");
3015
+ m.rod([-0.3, 0.6, 0], [0.3, 0.6, 0], 0.08, m.palette.iron, "metal", 0.08, 10);
3016
+ for (const z of [-0.09, 0.09]) {
3017
+ m.rod([0, 0.57, z], [0, 1.3, z - 0.3], 0.039, m.palette.iron, "metal", 0.028);
3018
+ }
3019
+ m.rod([-0.21, 1.3, -0.3], [0.21, 1.3, -0.3], 0.075, m.palette.wood, "solid", 0.075, 9);
3020
+ };
3021
+ var spikeTrap = (m) => {
3022
+ grate(m, 2.1, 0.102);
3023
+ m.block([0, 0.075, 0], [2.04, 0.1, 2.04], m.palette.joint, "solid", 0.011);
3024
+ for (const x of [-0.69, -0.35, 0, 0.35, 0.69])
3025
+ for (const z of [-0.69, -0.35, 0, 0.35, 0.69]) {
3026
+ m.rod(
3027
+ [x, 0.16, z],
3028
+ [x + m.rng.range(-0.033, 0.033), m.rng.range(0.48, 0.82), z],
3029
+ 0.085,
3030
+ 7893629,
3031
+ "metal",
3032
+ 1e-3,
3033
+ 5
3034
+ );
3035
+ }
3036
+ };
3037
+ var pressurePlate = (m) => {
3038
+ m.stone([0, 0.075, 0], [1.83, 0.15, 1.83], 0.55);
3039
+ m.block([0, 0.166, 0], [1.52, 0.082, 1.52], m.palette.iron, "metal");
3040
+ m.stone([0, 0.225, 0], [1.32, 0.12, 1.32], 0.18, 0.045);
3041
+ for (const x of [-0.69, 0.69])
3042
+ for (const z of [-0.69, 0.69]) m.gem([x, 0.218, z], [0.041, 0.023, 0.041], 10258012, "metal");
3043
+ };
3044
+ var woodenBridge = (m) => {
3045
+ for (const x of [-0.94, 0.94]) m.block([x, -0.22, 0], [0.18, 0.27, 4], m.palette.wood);
3046
+ for (let i = 0; i < 12; i++)
3047
+ m.block(
3048
+ [0, -0.068, (i - 5.5) / 3],
3049
+ [m.rng.range(2.19, 2.31), 0.14, 0.311],
3050
+ m.palette.wood,
3051
+ "solid",
3052
+ 0.018
3053
+ );
3054
+ for (const x of [-1.06, 1.06]) {
3055
+ const points = [-1.86, 0, 1.86];
3056
+ for (const z of points) {
3057
+ m.rod([x, -0.19, z], [x, 0.83, z], 0.094, m.palette.wood, "solid", 0.083, 6);
3058
+ m.rod([x, 0.58, z], [x, 0.646, z], 0.103, 10189137, "solid", 0.104, 10);
3059
+ }
3060
+ for (let i = 0; i < 16; i++) {
3061
+ const point = (t) => [
3062
+ x,
3063
+ 0.68 - Math.sin(t / 8 * Math.PI % Math.PI) * 0.23,
3064
+ -1.86 + t * 3.72 / 16
3065
+ ];
3066
+ m.rod(point(i), point(i + 1), 0.033, 10189137, "solid", 0.033, 6);
3067
+ }
3068
+ }
3069
+ };
3070
+ var stoneBridge = (m) => {
3071
+ for (let i = 0; i < 8; i++) {
3072
+ const z = i * 0.5 - 1.75;
3073
+ const arch3 = 0.31 + (Math.abs(z) / 1.95) ** 2 * 0.7;
3074
+ for (const x of [-0.81, 0, 0.81]) m.stone([x, -arch3 / 2, z], [0.82, arch3, 0.49], 0.57, 0.046);
3075
+ for (const x of [-1.26, 1.26]) m.stone([x, 0.28, z], [0.26, 0.56, 0.51], 0.52);
3076
+ }
3077
+ for (const x of [-1.28, 1.28])
3078
+ for (const z of [-1.81, 1.81]) {
3079
+ m.stone([x, 0.41, z], [0.4, 0.82, 0.38], 0.7);
3080
+ m.stone([x, 0.9, z], [0.54, 0.18, 0.53], 0.61);
3081
+ }
3082
+ };
3083
+ var altar = (m) => {
3084
+ m.stone([0, 0.14, 0], [2.49, 0.28, 1.24], 0.62);
3085
+ m.stone([0, 0.69, 0], [2.14, 0.86, 0.89], 0.35);
3086
+ for (const x of [-0.825, 0.825]) m.stone([x, 0.66, 0.43], [0.45, 0.83, 0.25], 0.6);
3087
+ m.stone([0, 1.19, 0], [2.56, 0.28, 1.23], 0.32);
3088
+ m.block([0, 1.34, 0.022], [0.75, 0.029, 1.27], m.palette.cloth, "solid", 0.012);
3089
+ m.block([0, 0.97, 0.659], [0.75, 0.74, 0.027], m.palette.cloth, "solid", 0.012);
3090
+ m.block([0, 0.64, 0.675], [0.72, 0.045, 0.018], m.palette.pale, "solid", 6e-3);
3091
+ for (const [x, z, height] of [
3092
+ [-0.93, 0.02, 0.32],
3093
+ [-0.69, -0.25, 0.24],
3094
+ [-0.57, 0.18, 0.18],
3095
+ [0.91, 0.17, 0.4],
3096
+ [0.72, -0.13, 0.21],
3097
+ [0.48, 0.25, 0.23]
3098
+ ])
3099
+ candle(m, x, 1.33, z, height);
3100
+ m.fireLight([0, 1.71, 0.24], 11);
3101
+ };
3102
+ var sarcophagus = (m) => {
3103
+ m.stone([0, 0.11, 0], [2.67, 0.22, 1.37], 0.8);
3104
+ m.stone([0, 0.48, 0], [2.49, 0.62, 1.22], 0.33);
3105
+ m.stone([0, 0.84, 0], [2.65, 0.26, 1.37], 0.31);
3106
+ const lid = new Shape();
3107
+ lid.moveTo(-1.03, -0.48);
3108
+ lid.lineTo(0.83, -0.48);
3109
+ lid.lineTo(1.15, -0.29);
3110
+ lid.lineTo(1.15, 0.29);
3111
+ lid.lineTo(0.83, 0.48);
3112
+ lid.lineTo(-1.03, 0.48);
3113
+ lid.closePath();
3114
+ m.add(
3115
+ new ExtrudeGeometry(lid, {
3116
+ depth: 0.13,
3117
+ bevelSize: 0.051,
3118
+ bevelThickness: 0.04,
3119
+ bevelSegments: 1
3120
+ }).rotateX(-Math.PI / 2).translate(0, 0.98, 0),
3121
+ m.palette.stone
3122
+ );
3123
+ m.gem([0.62, 1.19, 0], [0.185, 0.105, 0.19], m.palette.stone, "solid", 1);
3124
+ m.gem([-0.14, 1.14, 0], [0.65, 0.083, 0.32], m.palette.stone, "solid", 1);
3125
+ for (const side of [-1, 1])
3126
+ for (const x of [-0.92, -0.47, 0, 0.47, 0.92]) {
3127
+ m.stone([x, 0.47, side * 0.62], [0.13, 0.38, 0.09], 0.33);
3128
+ }
3129
+ };
3130
+
3131
+ // src/dungeon/finish.ts
3132
+ var GLSL = `
3133
+ varying vec3 vDungeonPosition;
3134
+ varying vec2 vDungeonSurface;
3135
+
3136
+ float grainHash(vec3 p) {
3137
+ p = fract(p * 0.3183099 + vec3(0.1, 0.3, 0.2)) * 19.0;
3138
+ return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
3139
+ }
3140
+
3141
+ float grain(vec3 p) {
3142
+ vec3 i = floor(p), t = fract(p);
3143
+ t = t * t * (3.0 - 2.0 * t);
3144
+ return mix(
3145
+ mix(mix(grainHash(i), grainHash(i + vec3(1,0,0)), t.x),
3146
+ mix(grainHash(i + vec3(0,1,0)), grainHash(i + vec3(1,1,0)), t.x), t.y),
3147
+ mix(mix(grainHash(i + vec3(0,0,1)), grainHash(i + vec3(1,0,1)), t.x),
3148
+ mix(grainHash(i + vec3(0,1,1)), grainHash(i + vec3(1,1,1)), t.x), t.y), t.z);
3149
+ }
3150
+
3151
+ vec3 dungeonFinish(vec3 p, float kind) {
3152
+ float weather = grain(p * 4.6);
3153
+ if (kind > 1.5) {
3154
+ float fibers = grain(p * vec3(87.0, 17.0, 85.0));
3155
+ return vec3(0.84 + 0.24 * fibers, fibers * 0.0028, 0.84 + 0.12 * weather);
3156
+ }
3157
+ if (kind > 0.5) {
3158
+ // Blend grain axes to keep end grain legible on horizontal, vertical, and cut faces.
3159
+ float vertical = grain(p * vec3(61.0, 2.0, 57.0) + weather * 0.9);
3160
+ float horizontal = grain(p * vec3(2.5, 49.0, 48.0) + weather * 1.3);
3161
+ float bands = mix(vertical, horizontal, smoothstep(0.30, 0.67, weather));
3162
+ return vec3(0.68 + bands * 0.37 + weather * 0.14, bands * 0.006, 0.79 + bands * 0.13);
3163
+ }
3164
+ float gravel = grain(p * 23.0);
3165
+ float chips = abs(grain(p * 51.0) * 2.0 - 1.0);
3166
+ float mineral = smoothstep(0.25, 0.8, 0.57 * weather + 0.43 * gravel);
3167
+ return vec3(0.55 + 0.47 * mineral + 0.16 * chips, gravel * 0.008 + chips * 0.0035, 0.77 + mineral * 0.19);
3168
+ }
3169
+ `;
3170
+ function dungeonFinish(material) {
3171
+ material.onBeforeCompile = (shader) => {
3172
+ shader.vertexShader = shader.vertexShader.replace(
3173
+ "#include <common>",
3174
+ `#include <common>
3175
+ attribute vec2 dungeonSurface;
3176
+ varying vec3 vDungeonPosition;
3177
+ varying vec2 vDungeonSurface;`
3178
+ ).replace(
3179
+ "#include <begin_vertex>",
3180
+ `#include <begin_vertex>
3181
+ vDungeonPosition = position;
3182
+ vDungeonSurface = dungeonSurface;`
3183
+ );
3184
+ shader.fragmentShader = shader.fragmentShader.replace(
3185
+ "#include <common>",
3186
+ `#include <common>
3187
+ ${GLSL}`
3188
+ ).replace(
3189
+ "#include <color_fragment>",
3190
+ `#include <color_fragment>
3191
+ vec3 finish = dungeonFinish(vDungeonPosition + vDungeonSurface.y, vDungeonSurface.x);
3192
+ diffuseColor.rgb *= finish.x;`
3193
+ ).replace(
3194
+ "#include <roughnessmap_fragment>",
3195
+ "#include <roughnessmap_fragment>\nroughnessFactor *= finish.z;"
3196
+ ).replace(
3197
+ "#include <normal_fragment_maps>",
3198
+ `#include <normal_fragment_maps>
3199
+ vec3 dx = dFdx(-vViewPosition), dy = dFdy(-vViewPosition);
3200
+ vec3 row = cross(dy, normal), col = cross(normal, dx);
3201
+ float det = dot(dx, row);
3202
+ vec3 bump = sign(det) * (dFdx(finish.y) * row + dFdy(finish.y) * col);
3203
+ normal = normalize(max(abs(det), 1e-8) * normal - bump);`
3204
+ );
3205
+ };
3206
+ }
3207
+
3208
+ // src/dungeon/mesh.ts
3209
+ var THEME_NAMES = ["Ancient Ruins", "Ice Dungeon", "Lava Depths"];
3210
+ var THEME_BIOMES = ["forest", "tundra", "volcanic"];
3211
+ var PALETTES = [
3212
+ {
3213
+ stone: 7367522,
3214
+ joint: 4077874,
3215
+ growth: 5725741,
3216
+ wood: 7424296,
3217
+ iron: 4473418,
3218
+ cloth: 9187365,
3219
+ fire: 16750124,
3220
+ pale: 16042626
3221
+ },
3222
+ {
3223
+ stone: 6453652,
3224
+ joint: 3360863,
3225
+ growth: 10342377,
3226
+ wood: 5460571,
3227
+ iron: 5596021,
3228
+ cloth: 3427207,
3229
+ fire: 5356799,
3230
+ pale: 13692927
3231
+ },
3232
+ {
3233
+ stone: 5326153,
3234
+ joint: 2695461,
3235
+ growth: 8992811,
3236
+ wood: 6635313,
3237
+ iron: 4798525,
3238
+ cloth: 9318942,
3239
+ fire: 16734745,
3240
+ pale: 16763745
3241
+ }
3242
+ ];
3243
+ var solid = new MeshStandardMaterial({
3244
+ name: "Dungeon painted surfaces",
3245
+ vertexColors: true,
3246
+ roughness: 0.78
3247
+ });
3248
+ var metal = new MeshStandardMaterial({
3249
+ name: "Dungeon worn metal",
3250
+ vertexColors: true,
3251
+ roughness: 0.48,
3252
+ metalness: 0.4
3253
+ });
3254
+ var glow = new MeshBasicMaterial({ name: "Dungeon glow", vertexColors: true, toneMapped: false });
3255
+ glow.onBeforeCompile = (shader) => {
3256
+ shader.uniforms.uDungeonTime = uniforms.uTime;
3257
+ shader.vertexShader = shader.vertexShader.replace(
3258
+ "#include <common>",
3259
+ `#include <common>
3260
+ uniform float uDungeonTime;
3261
+ attribute vec3 dungeonFlame;
3262
+ varying float vFlamePhase;`
3263
+ ).replace(
3264
+ "#include <begin_vertex>",
3265
+ `#include <begin_vertex>
3266
+ float relativeHeight = max(0.0, position.y - dungeonFlame.x);
3267
+ float sway = min(relativeHeight, dungeonFlame.y);
3268
+ vFlamePhase = dungeonFlame.z;
3269
+ float beat = uDungeonTime * 9.2 + vFlamePhase;
3270
+ transformed.x += sway * (0.08 * sin(beat + relativeHeight * 10.2) + 0.04 * cos(beat * 1.61));
3271
+ transformed.z += sway * 0.07 * sin(beat * 0.83 - relativeHeight * 4.8);
3272
+ transformed.y += sway * (0.13 * cos(beat * 0.68) + 0.035 * sin(beat * 2.6));`
3273
+ );
3274
+ shader.fragmentShader = shader.fragmentShader.replace(
3275
+ "#include <common>",
3276
+ "#include <common>\nuniform float uDungeonTime;\nvarying float vFlamePhase;"
3277
+ ).replace(
3278
+ "#include <color_fragment>",
3279
+ "#include <color_fragment>\ndiffuseColor.rgb *= 0.89 + 0.06 * sin(uDungeonTime * 9.1 + vFlamePhase) + 0.05 * cos(uDungeonTime * 13.7 - vFlamePhase);"
3280
+ );
3281
+ };
3282
+ dungeonFinish(solid);
3283
+ dungeonFinish(metal);
3284
+ var finishes = { solid, metal, glow };
3285
+ var dungeonMaterials = { dungeonSolid: solid, dungeonMetal: metal, dungeonGlow: glow };
3286
+ var DungeonMesh = class {
3287
+ constructor(seed, theme) {
3288
+ this.seed = seed;
3289
+ this.theme = theme;
3290
+ this.rng = new RNG(seed);
3291
+ this.palette = PALETTES[theme] ?? PALETTES[0];
3292
+ }
3293
+ seed;
3294
+ theme;
3295
+ rng;
3296
+ palette;
3297
+ parts = { solid: [], metal: [], glow: [] };
3298
+ lights = [];
3299
+ add(shape, color, finish = "solid", wear = 0.22, moss = 0, fire) {
3300
+ const geometry = shape.index ? shape.toNonIndexed() : shape;
3301
+ if (geometry !== shape) shape.dispose();
3302
+ geometry.clearGroups();
3303
+ geometry.deleteAttribute("uv");
3304
+ const positions = geometry.getAttribute("position");
3305
+ const shade = new Color(color).multiplyScalar(this.rng.range(1 - wear, 1 + wear));
3306
+ const mossColor = new Color(this.palette.growth);
3307
+ const vertex = new Color();
3308
+ const colors = new Float32Array(positions.count * 3);
3309
+ const surface2 = new Float32Array(positions.count * 2);
3310
+ const kind = color === this.palette.wood ? 1 : color === this.palette.cloth ? 2 : 0;
3311
+ const materialPhase = this.rng.range(-25, 25);
3312
+ const phase = this.rng.seed();
3313
+ for (let i = 0; i < positions.count; i++) {
3314
+ const x = positions.getX(i);
3315
+ const y = positions.getY(i);
3316
+ const z = positions.getZ(i);
3317
+ const grain = noise3(x * 17, y * 18, z * 19, phase);
3318
+ const lichen = noise3(x * 2.6, y * 2.8, z * 3.1, this.seed);
3319
+ vertex.copy(shade).multiplyScalar(1 + grain * wear * 1.2);
3320
+ vertex.lerp(mossColor, Math.max(0, lichen + 0.42) * moss * 0.72);
3321
+ vertex.toArray(colors, i * 3);
3322
+ surface2[i * 2] = kind;
3323
+ surface2[i * 2 + 1] = materialPhase;
3324
+ }
3325
+ geometry.setAttribute("color", new BufferAttribute(colors, 3));
3326
+ geometry.setAttribute("dungeonSurface", new BufferAttribute(surface2, 2));
3327
+ if (finish === "glow") {
3328
+ const move = new Float32Array(positions.count * 3);
3329
+ if (fire) {
3330
+ const phase2 = this.rng.range(0, 2 * Math.PI);
3331
+ for (let i = 0; i < positions.count; i++) {
3332
+ move[i * 3] = fire[0];
3333
+ move[i * 3 + 1] = fire[1];
3334
+ move[i * 3 + 2] = phase2;
3335
+ }
3336
+ }
3337
+ geometry.setAttribute("dungeonFlame", new BufferAttribute(move, 3));
3338
+ }
3339
+ this.parts[finish].push(geometry);
3340
+ }
3341
+ block(center, [w, h, d], color, finish = "solid", bevel = 0.026, yaw = 0) {
3342
+ const shape = new RoundedBoxGeometry(w, h, d, 1, bevel);
3343
+ if (yaw) shape.rotateY(yaw);
3344
+ shape.translate(...center);
3345
+ this.add(shape, color, finish);
3346
+ }
3347
+ stone(center, [w, h, d], moss = 0.45, bevel = 0.05, yaw = 0, pitch = 0) {
3348
+ const shape = new RoundedBoxGeometry(w, h, d, 1, Math.min(bevel, w * 0.22, h * 0.22, d * 0.22));
3349
+ const positions = shape.getAttribute("position");
3350
+ const grit = this.rng.seed();
3351
+ for (let i = 0; i < positions.count; i++) {
3352
+ const x = positions.getX(i);
3353
+ const y = positions.getY(i);
3354
+ const z = positions.getZ(i);
3355
+ const chip = Math.max(0, noise3(x * 9, y * 12, z * 11, grit) + 0.08) * 0.13;
3356
+ positions.setXYZ(i, x * (1 - chip), y * (1 - chip), z * (1 - chip));
3357
+ }
3358
+ if (pitch) shape.rotateX(pitch);
3359
+ if (yaw) shape.rotateY(yaw);
3360
+ shape.translate(...center);
3361
+ this.add(shape, this.palette.stone, "solid", 0.31, moss);
3362
+ }
3363
+ rod(start, end, r, color, finish = "solid", tip = r, sides = 8, fire) {
3364
+ const a = new Vector3(...start);
3365
+ const b = new Vector3(...end);
3366
+ const direction = b.clone().sub(a);
3367
+ const shape = new CylinderGeometry(tip, r, direction.length(), sides, 1);
3368
+ shape.applyQuaternion(
3369
+ new Quaternion().setFromUnitVectors(new Vector3(0, 1, 0), direction.normalize())
3370
+ );
3371
+ shape.translate(...a.lerp(b, 0.5).toArray());
3372
+ this.add(shape, color, finish, finish === "glow" ? 0.02 : 0.13, 0, fire);
3373
+ }
3374
+ gem(center, scale, color, finish = "solid", detail = 0, yaw = 0) {
3375
+ const shape = new IcosahedronGeometry(1, detail);
3376
+ shape.scale(...scale).rotateY(yaw).translate(...center);
3377
+ this.add(shape, color, finish, 0.16);
3378
+ }
3379
+ fireLight(position, intensity) {
3380
+ const lamp = new PointLight(this.palette.fire, intensity, Math.sqrt(intensity) * 2.05, 2);
3381
+ lamp.name = "Dungeon fire light";
3382
+ lamp.position.set(...position);
3383
+ lamp.userData.fire = { intensity, phase: this.rng.range(0, 2 * Math.PI) };
3384
+ this.lights.push(lamp);
3385
+ }
3386
+ finish(id) {
3387
+ const group = new Group();
3388
+ group.name = `dungeon-${id}`;
3389
+ for (const key of Object.keys(finishes)) {
3390
+ const parts = this.parts[key];
3391
+ if (!parts.length) continue;
3392
+ const merged = mergeGeometries(parts);
3393
+ if (!merged) throw new Error(`Could not merge ${id} ${key} geometry`);
3394
+ const mesh = new Mesh(merged, finishes[key]);
3395
+ mesh.name = `${group.name}:${key}`;
3396
+ mesh.castShadow = key !== "glow";
3397
+ mesh.receiveShadow = key !== "glow";
3398
+ group.add(mesh);
3399
+ for (const part of parts) part.dispose();
3400
+ }
3401
+ if (this.lights.length) group.add(...this.lights);
3402
+ return group;
3403
+ }
3404
+ };
3405
+
3406
+ // src/dungeon/catalog.ts
3407
+ var sections = {
3408
+ Walls: [
3409
+ ["wall", "Straight wall", wall],
3410
+ ["corner", "Corner wall", corner],
3411
+ ["tee", "T-junction wall", tee],
3412
+ ["cross", "Cross-junction wall", cross],
3413
+ ["archway", "Stone archway", archway],
3414
+ ["door-frame", "Door frame", doorFrame],
3415
+ ["barred-window", "Barred window", barredWindow],
3416
+ ["collapsed-wall", "Collapsed wall", collapsedWall]
3417
+ ],
3418
+ "Floors and stairs": [
3419
+ ["floor", "Flagstone floor", floor],
3420
+ ["cracked-floor", "Cracked flagstones", crackedFloor],
3421
+ ["mossy-floor", "Mossy flagstones", mossyFloor],
3422
+ ["grate", "Floor grate", (m) => grate(m)],
3423
+ ["stairs", "Straight stairs", (m) => stairs(m)],
3424
+ ["corner-stairs", "Corner stairs", (m) => stairs(m, true)],
3425
+ ["ramp", "Stone ramp", ramp],
3426
+ ["platform", "Raised platform", platform]
3427
+ ],
3428
+ Structure: [
3429
+ ["pillar", "Pillar", (m) => pillar(m)],
3430
+ ["broken-column", "Broken column", (m) => pillar(m, true)],
3431
+ ["large-arch", "Large arch", largeArch],
3432
+ ["ruined-arch", "Ruined arch", ruinedArch],
3433
+ ["vault", "Vaulted ceiling", vault],
3434
+ ["beam", "Ceiling beam", beam]
3435
+ ],
3436
+ Props: [
3437
+ ["crate", "Crate", crate],
3438
+ ["barrel", "Barrel", barrel],
3439
+ ["sack", "Sack", sack],
3440
+ ["locked-chest", "Locked chest", (m) => chest(m)],
3441
+ ["open-chest", "Open treasure chest", (m) => chest(m, true)],
3442
+ ["cage", "Iron cage", cage],
3443
+ ["table", "Table", (m) => furniture(m)],
3444
+ ["bench", "Bench", (m) => furniture(m, true)],
3445
+ ["shelves", "Shelves", shelves],
3446
+ ["weapon-rack", "Weapon rack", (m) => weaponStand(m, true)],
3447
+ ["weapon-stand", "Weapon stand", (m) => weaponStand(m)],
3448
+ ["banner", "Banner", banner],
3449
+ ["wall-torch", "Wall torch", wallTorch],
3450
+ ["brazier", "Brazier", brazier],
3451
+ ["hanging-lamp", "Hanging lamp", hangingLamp],
3452
+ ["candles", "Candle cluster", candles],
3453
+ ["rubble", "Stone debris", rubble],
3454
+ ["bones", "Bones and skull", bones]
3455
+ ],
3456
+ Features: [
3457
+ ["portcullis", "Portcullis", portcullis],
3458
+ ["lever", "Lever", lever],
3459
+ ["spike-trap", "Floor spike trap", spikeTrap],
3460
+ ["pressure-plate", "Pressure plate", pressurePlate],
3461
+ ["wooden-bridge", "Wooden bridge", woodenBridge],
3462
+ ["stone-bridge", "Stone bridge", stoneBridge],
3463
+ ["statue", "Hooded statue", statue],
3464
+ ["altar", "Candlelit altar", altar],
3465
+ ["sarcophagus", "Sarcophagus", sarcophagus]
3466
+ ]
3467
+ };
3468
+ var dungeonEntries = Object.entries(sections).flatMap(
3469
+ ([category, entries]) => entries.map(([id, name, build2]) => ({ id, name, category, build: build2 }))
3470
+ );
3471
+ var recipes = new Map(dungeonEntries.map(({ id, build: build2 }) => [id, build2]));
3472
+ function buildDungeonPart(id, seed, theme) {
3473
+ const make = recipes.get(id);
3474
+ if (!make) throw new Error(`Unknown dungeon asset "${id}"`);
3475
+ const mesh = new DungeonMesh(seed, theme);
3476
+ make(mesh);
3477
+ return mesh.finish(id);
3478
+ }
3479
+ var dungeonScenes = {
3480
+ courtyard: "The Sunken Court",
3481
+ assembly: "Modular dungeon layout"
3482
+ };
3483
+ function staging(seed, theme) {
3484
+ const rng = new RNG(seed);
3485
+ const group = new Group();
3486
+ const parts = /* @__PURE__ */ new Map();
3487
+ function place(id, x, y, z, yaw = 0, scale = 1) {
3488
+ const variation = rng.int(1, 4);
3489
+ const key = `${id}:${variation}`;
3490
+ let source = parts.get(key);
3491
+ if (!source) {
3492
+ source = buildDungeonPart(id, seed * 97 + variation, theme);
3493
+ parts.set(key, source);
3494
+ }
3495
+ const object = source.clone();
3496
+ object.position.set(x, y, z);
3497
+ object.rotation.y = yaw;
3498
+ object.scale.setScalar(scale);
3499
+ group.add(object);
3500
+ }
3501
+ function light(x, y, z, strength = 21, cool = false) {
3502
+ const color = cool ? 8637183 : [16754264, 5880063, 16740396][theme];
3503
+ const lamp = new PointLight(color, strength, Math.sqrt(strength) * 2.15, 2);
3504
+ lamp.position.set(x, y, z);
3505
+ group.add(lamp);
3506
+ }
3507
+ function brazier2(x, y, z) {
3508
+ place("brazier", x, y, z);
3509
+ }
3510
+ function torch(x, y, z, yaw = 0) {
3511
+ place("wall-torch", x, y, z, yaw);
3512
+ }
3513
+ return { group, place, light, brazier: brazier2, torch, rng };
3514
+ }
3515
+ function pit(stage, seed, theme, x, z) {
3516
+ const m = new DungeonMesh(seed, theme);
3517
+ for (const side of [-1, 1]) {
3518
+ for (const tile of [-1.5, -0.5, 0.5, 1.5]) {
3519
+ m.stone([tile, -1.45, side * 1.84], [0.98, 2.35, 0.32], 0.81);
3520
+ m.stone([side * 1.84, -1.49, tile], [0.32, 2.26, 0.98], 0.75);
3521
+ }
3522
+ }
3523
+ const water2 = [3628644, 4097452, 16208660][theme] ?? 3628644;
3524
+ const liquidLevel = theme === 2 ? -0.72 : -2.62;
3525
+ m.block([0, liquidLevel, 0], [3.8, 0.11, 3.8], water2, theme === 2 ? "glow" : "solid");
3526
+ const foam = [8633268, 9492989, 15706184][theme] ?? 8633268;
3527
+ for (let i = 0; i < 13; i++) {
3528
+ const a = m.rng.range(-1.54, 1.54);
3529
+ const b = m.rng.range(-1.56, 1.56);
3530
+ m.block(
3531
+ [a, liquidLevel + 0.07, b],
3532
+ [m.rng.range(0.16, 0.99), 0.012, 0.02],
3533
+ foam,
3534
+ "glow",
3535
+ 3e-3,
3536
+ m.rng.range(-0.45, 0.45)
3537
+ );
3538
+ }
3539
+ const basin = m.finish("pit");
3540
+ basin.position.set(x, 0, z);
3541
+ stage.group.add(basin);
3542
+ stage.light(x, -1.9, z, 15, theme !== 2);
3543
+ }
3544
+ function courtyardFloor({ place }) {
3545
+ for (const x of [-8, -4, 0, 4, 8]) {
3546
+ for (const z of [-12, -8, -4, 0, 4, 8]) {
3547
+ if (x === 4 && z >= 0 && z < 8) continue;
3548
+ const floor2 = x > 0 ? "cracked-floor" : "floor";
3549
+ place((x + z) % 12 === 0 ? "mossy-floor" : floor2, x, 0, z);
3550
+ }
3551
+ }
3552
+ }
3553
+ function courtyardStructure({ place }) {
3554
+ for (const [index, x] of [-8, -4, 0, 4, 8].entries()) {
3555
+ place(["wall", "barred-window", "wall", "archway", "wall"][index] ?? "wall", x, 0, -14);
3556
+ place(
3557
+ ["collapsed-wall", "wall", "barred-window", "barred-window", "wall"][index] ?? "wall",
3558
+ x,
3559
+ 3.6,
3560
+ -14
3561
+ );
3562
+ }
3563
+ place("portcullis", 4, 0, -13.94);
3564
+ for (const z of [-12, -8, -4, 0]) {
3565
+ place(z === -4 ? "archway" : "wall", -10, 0, z, Math.PI / 2);
3566
+ place(z === 0 ? "collapsed-wall" : "wall", 10, 0, z, -Math.PI / 2);
3567
+ }
3568
+ for (const x of [-7.45, -2.55, 2.55, 7.45]) place("pillar", x, 0, -8.8);
3569
+ place("large-arch", -5, 0, -3.4);
3570
+ place("archway", -5, 0, -11);
3571
+ place("ruined-arch", 1.9, 0, -7.1);
3572
+ place("stairs", 8, 0, -6.02);
3573
+ for (const z of [-10, -14]) place("mossy-floor", 8, 2, z);
3574
+ place("pillar", 4, -1.62, -12.97);
3575
+ place("stone-bridge", 5.98, 2, -13, Math.PI / 2);
3576
+ place("vault", -8, 3.42, -9.3);
3577
+ place("wooden-bridge", 4, 0, 0, Math.PI / 2);
3578
+ }
3579
+ function courtyard(stage) {
3580
+ const { place, brazier: brazier2, torch, light } = stage;
3581
+ courtyardFloor(stage);
3582
+ courtyardStructure(stage);
3583
+ place("altar", -5, 0, -8.2);
3584
+ place("statue", -5, 0.08, -10.35);
3585
+ place("sarcophagus", -7.77, 0, -10.87, 0.24);
3586
+ place("shelves", 8.5, 0, 5.3, -Math.PI / 2);
3587
+ place("locked-chest", 6.62, 0, 6, -0.41);
3588
+ place("open-chest", -4, 0, -4.89, 0.3);
3589
+ place("crate", -7.8, 0, -1.4, 0.25);
3590
+ place("barrel", -7.1, 0, -1.25, 0.41);
3591
+ place("barrel", 8.68, 0, -1.4, 0.23);
3592
+ place("sack", -6.4, 0, -1.49, 0.23);
3593
+ place("bones", -1.43, 0, -0.38, 0.9);
3594
+ place("rubble", -1.6, 0, -6.2);
3595
+ place("broken-column", -8.7, 0, 2.33, 0.35);
3596
+ place("cage", -7.9, 0, -12.9);
3597
+ place("banner", -7.1, 1.52, -3.02);
3598
+ place("banner", -2.9, 1.42, -3.01);
3599
+ place("banner", 2.3, 3.48, -13.6);
3600
+ place("banner", 5.73, 3.48, -13.6);
3601
+ place("hanging-lamp", -6.55, 2.32, -6.77);
3602
+ place("broken-column", -8.1, 0, -5.86, -0.2);
3603
+ brazier2(5.8, 0, 6.1);
3604
+ brazier2(-2.63, 0, 2.15);
3605
+ brazier2(-4.4, 0, -2.55);
3606
+ brazier2(1.9, 0, -8.25);
3607
+ brazier2(7.5, 2, -12.77);
3608
+ torch(-7.6, 1.07, -3.04);
3609
+ torch(-2.5, 1.07, -3.04);
3610
+ torch(-8.95, 1.2, -9.66, Math.PI / 2);
3611
+ torch(6.04, 1.48, -13.55);
3612
+ torch(1.92, 1.4, -13.55);
3613
+ light(-5.22, 2.12, -7.81, 23);
3614
+ light(-3.95, 5.1, -9.84, 41, true);
3615
+ light(1.28, 6.2, -12.4, 35, true);
3616
+ }
3617
+ var PLAN = [
3618
+ "..FFF......",
3619
+ "..FFF..FFF.",
3620
+ "...F...FFF.",
3621
+ ".FFF...FFF.",
3622
+ ".FFFFBFFFF.",
3623
+ ".FFF...FFF.",
3624
+ "...F...FFF.",
3625
+ "..FFF......",
3626
+ "..FFF......"
3627
+ ];
3628
+ function layoutTile({ place, brazier: brazier2 }, tile, row, col2) {
3629
+ if (tile === ".") return;
3630
+ const x = (col2 - 5) * GRID;
3631
+ const z = (row - 4) * GRID;
3632
+ if (tile === "B") {
3633
+ place("wooden-bridge", x, 0, z, Math.PI / 2);
3634
+ return;
3635
+ }
3636
+ place((row + col2) % 4 === 0 ? "mossy-floor" : "floor", x, 0, z);
3637
+ for (const [dr, dc, yaw] of [
3638
+ [-1, 0, 0],
3639
+ [0, -1, Math.PI / 2],
3640
+ [0, 1, Math.PI / 2]
3641
+ ]) {
3642
+ const neighbor = PLAN[row + dr]?.[col2 + dc];
3643
+ if (neighbor === "." || neighbor === void 0)
3644
+ place(row % 2 ? "wall" : "collapsed-wall", x + dc * GRID / 2, 0, z + dr * GRID / 2, yaw);
3645
+ }
3646
+ if ((row * 7 + col2) % 7 === 0) brazier2(x - 1, 0, z - 1);
3647
+ }
3648
+ function layout(stage) {
3649
+ const { place } = stage;
3650
+ for (const [row, line] of PLAN.entries())
3651
+ for (const [col2, tile] of [...line].entries()) layoutTile(stage, tile, row, col2);
3652
+ for (const [x, z] of [
3653
+ [-8, -10],
3654
+ [-8, 6],
3655
+ [12, -2],
3656
+ [8, 6]
3657
+ ])
3658
+ place("archway", x, 0, z);
3659
+ place("altar", -8, 0, -15.25);
3660
+ place("statue", -8, 0, -17.13);
3661
+ place("sarcophagus", 11.3, 0, -9, -0.32);
3662
+ place("spike-trap", 12, 0, 5);
3663
+ place("barrel", -15.11, 0, -0.2);
3664
+ place("open-chest", -8, 0, 16, -0.28);
3665
+ place("lever", 2.84, 0, 1.07);
3666
+ place("pillar", 1.65, 0, -1.25);
3667
+ place("pillar", -1.65, 0, -1.25);
3668
+ stage.light(-8, 2.21, -14.73, 38);
3669
+ }
3670
+ function climate(stage, theme, assembly) {
3671
+ const m = new DungeonMesh(stage.rng.seed(), theme);
3672
+ const placements = assembly ? [
3673
+ [-8, -14],
3674
+ [-14, -3],
3675
+ [6.4, -5],
3676
+ [17, 0],
3677
+ [-9, 15]
3678
+ ] : [
3679
+ [-9.4, -9.8],
3680
+ [-6.2, -13.4],
3681
+ [-0.9, -13.4],
3682
+ [6, -10.8],
3683
+ [8.9, 3.4]
3684
+ ];
3685
+ for (const [x = 0, z = 0] of placements) {
3686
+ for (let i = 0; i < 7; i++) {
3687
+ const dx = m.rng.range(-0.85, 0.85);
3688
+ const dz = m.rng.range(-0.5, 0.5);
3689
+ const y = m.rng.range(0.17, 0.85);
3690
+ if (theme === 1) {
3691
+ m.rod(
3692
+ [x + dx, 0, z + dz],
3693
+ [x + dx * 1.4, y * 2.1, z + dz],
3694
+ y * 0.37,
3695
+ 7776459,
3696
+ "solid",
3697
+ 5e-3,
3698
+ 5
3699
+ );
3700
+ } else {
3701
+ m.gem([x + dx, y * 0.19, z + dz], [0.39, y * 0.33, 0.37], m.palette.growth, "solid", 1);
3702
+ }
3703
+ }
3704
+ }
3705
+ stage.group.add(m.finish("theme-details"));
3706
+ }
3707
+ function hangingFerns(stage) {
3708
+ const fern2 = CATALOG.find(({ id }) => id === "fern");
3709
+ if (!fern2) throw new Error("The courtyard needs the shared fern generator");
3710
+ const ledges = [
3711
+ [-7.39, 3.45, -3.22],
3712
+ [-6.63, 4.06, -3.13],
3713
+ [-5.91, 4.65, -3.11],
3714
+ [-4.88, 4.89, -3.11],
3715
+ [-3.51, 4.29, -3.12],
3716
+ [-1.93, 3.33, -3.1],
3717
+ [-4.85, 6.94, -13.54],
3718
+ [-3.59, 7.05, -13.49],
3719
+ [-2.71, 7.01, -13.43],
3720
+ [-8.74, 2.89, -0.67],
3721
+ [-8.29, 0, 1.44],
3722
+ [6.15, 0, 1.3]
3723
+ ];
3724
+ for (const [x, y, z] of ledges) {
3725
+ const fronds = build(fern2, 0, {
3726
+ size: stage.rng.range(0.98, 1.57),
3727
+ fronds: 8,
3728
+ palette: "deep",
3729
+ seed: stage.rng.seed()
3730
+ });
3731
+ fronds.position.set(x, y, z);
3732
+ fronds.rotation.set(
3733
+ y ? -0.93 : 0,
3734
+ stage.rng.range(-0.8, 0.8),
3735
+ y ? stage.rng.range(1.68, 2.68) : 0
3736
+ );
3737
+ stage.group.add(fronds);
3738
+ }
3739
+ }
3740
+ function frozenArch(stage) {
3741
+ const ice = new DungeonMesh(stage.rng.seed(), 1);
3742
+ for (let i = 0; i < 18; i++) {
3743
+ const u = -2 + i / 17 * 4;
3744
+ const z = -3.13 + ice.rng.range(-0.2, 0.09);
3745
+ const y = 2.4 + Math.sqrt(2.22 ** 2 - u ** 2);
3746
+ const x = -5 + u;
3747
+ const length = ice.rng.range(0.34, 1.34);
3748
+ ice.rod(
3749
+ [x, y + 0.057, z],
3750
+ [x + ice.rng.range(-0.13, 0.13), y - length, z + 0.065],
3751
+ length * 0.155,
3752
+ 11062749,
3753
+ "solid",
3754
+ 5e-3,
3755
+ 5
3756
+ );
3757
+ }
3758
+ for (let i = 0; i < 20; i++) {
3759
+ const x = -5.5 + i * 0.51 + ice.rng.range(-0.11, 0.11);
3760
+ const y = 7.15 - ice.rng.range(0, 0.13);
3761
+ const length = ice.rng.range(0.27, 1.04);
3762
+ ice.rod(
3763
+ [x, y, -13.68],
3764
+ [x + 0.053, y - length, -13.51],
3765
+ length * 0.145,
3766
+ 10735597,
3767
+ "solid",
3768
+ 3e-3,
3769
+ 6
3770
+ );
3771
+ }
3772
+ for (const [x, z] of [
3773
+ [-7.26, 1.68],
3774
+ [6.48, 3.1],
3775
+ [2.05, -2.17]
3776
+ ]) {
3777
+ for (let i = 0; i < 6; i++) {
3778
+ const dx = ice.rng.range(-0.55, 0.55);
3779
+ const dz = ice.rng.range(-0.48, 0.48);
3780
+ const h = ice.rng.range(0.25, 0.91);
3781
+ ice.rod(
3782
+ [x + dx, 0, z + dz],
3783
+ [x + dx * 1.42, h, z + dz * 1.2],
3784
+ h * 0.26,
3785
+ 8634591,
3786
+ "solid",
3787
+ 3e-3,
3788
+ 5
3789
+ );
3790
+ }
3791
+ }
3792
+ stage.group.add(ice.finish("hanging-ice"));
3793
+ }
3794
+ function lavaChasm(stage) {
3795
+ const molten = new DungeonMesh(stage.rng.seed(), 2);
3796
+ for (const x of [2.08, 5.91]) {
3797
+ for (let d = 0; d < 18; d++) {
3798
+ const z = -1.28 + d * 0.42;
3799
+ const floor2 = -0.16 - molten.rng.range(0.02, 0.2);
3800
+ const length = molten.rng.range(0.21, 0.55);
3801
+ molten.rod(
3802
+ [x, floor2, z],
3803
+ [x + (x < 4 ? 1 : -1) * length, floor2 - molten.rng.range(0.13, 0.42), z + 0.16],
3804
+ molten.rng.range(0.03, 0.097),
3805
+ d % 4 ? 15286546 : 16756009,
3806
+ "glow",
3807
+ 0.011,
3808
+ 5
3809
+ );
3810
+ }
3811
+ }
3812
+ stage.group.add(molten.finish("lava-runoff"));
3813
+ stage.light(4.1, -0.38, 3.89, 44);
3814
+ }
3815
+ function buildDungeonScene(id, seed, theme) {
3816
+ const stage = staging(seed, theme);
3817
+ stage.group.name = `dungeon-${id}`;
3818
+ const assembly = id === "assembly";
3819
+ if (assembly) {
3820
+ layout(stage);
3821
+ pit(stage, seed, theme, 0, 0);
3822
+ } else {
3823
+ courtyard(stage);
3824
+ pit(stage, seed, theme, 4, 0);
3825
+ pit(stage, seed + 1, theme, 4, 4);
3826
+ const dressing = [hangingFerns, frozenArch, lavaChasm][theme];
3827
+ dressing?.(stage);
3828
+ }
3829
+ climate(stage, theme, assembly);
3830
+ const sky2 = [2635334, 1783386, 4465186][theme] ?? 2635334;
3831
+ const horizon = [1647409, 1386306, 3152931][theme] ?? 1647409;
3832
+ const view = assembly ? { pos: [25, 33, 34], target: [0, 0, 0] } : { pos: [6.55, 4.15, 10.3], target: [-0.93, 2.15, -7.9] };
3833
+ if (!assembly && theme === 2) {
3834
+ view.pos = [6.9, 7.75, 13.45];
3835
+ view.target = [-0.48, 0.6, -5.2];
3836
+ }
3837
+ return {
3838
+ group: stage.group,
3839
+ view,
3840
+ env: {
3841
+ skyTop: sky2,
3842
+ horizon,
3843
+ fog: [horizon, assembly ? 60 : 20, assembly ? 125 : 67],
3844
+ hemi: { sky: 11717092, ground: 6965827, intensity: assembly ? 1.87 : 1.37 },
3845
+ sun: { dir: [-0.5, 0.71, -0.44], color: 11258100, intensity: assembly ? 2.4 : 2.52 },
3846
+ shadow: assembly ? 29 : 23
3847
+ }
3848
+ };
3849
+ }
3850
+
3851
+ // src/stylized.ts
3852
+ var stylizedMaterials = { ...materials, ...dungeonMaterials };
3853
+ var dungeonRows = [
3854
+ ...Object.entries(dungeonScenes).map(([id, name]) => ({ id, name, category: "Scenes" })),
3855
+ ...dungeonEntries
3856
+ ];
3857
+ var biomeIds = {
3858
+ forest: ["alpine", "forest"],
3859
+ snow: ["alpine", "tundra"],
3860
+ desert: ["desert"],
3861
+ shared: ["alpine", "forest", "tundra", "desert"]
3862
+ };
3863
+ function stylizedAssets() {
3864
+ return [
3865
+ ...Object.keys(SCENES).map(
3866
+ (biome) => ({
3867
+ id: `scene-${biome}`,
3868
+ name: BIOMES[biome] ?? biome,
3869
+ kind: "stylized",
3870
+ category: "Scenes",
3871
+ biomes: biomeIds[biome] ?? [],
3872
+ variants: 1
3873
+ })
3874
+ ),
3875
+ ...CATALOG.map(
3876
+ (entry) => ({
3877
+ id: entry.id,
3878
+ name: entry.name,
3879
+ kind: "stylized",
3880
+ category: BIOMES[entry.biome] ?? entry.biome,
3881
+ biomes: biomeIds[entry.biome] ?? [],
3882
+ variants: entry.variants.length
3883
+ })
3884
+ ),
3885
+ ...dungeonRows.map(
3886
+ ({ id, name, category }) => ({
3887
+ id: `dungeon-${id}`,
3888
+ name,
3889
+ category,
3890
+ kind: "dungeon",
3891
+ biomes: [...THEME_BIOMES],
3892
+ variants: THEME_NAMES.length,
3893
+ variantNames: THEME_NAMES
3894
+ })
3895
+ )
3896
+ ];
3897
+ }
3898
+ function dungeonAsset(id, { variant = 0, seed = 1, overrides }) {
3899
+ if (!Number.isInteger(variant) || variant < 0 || variant >= THEME_NAMES.length)
3900
+ throw new Error(`Invalid dungeon theme variant ${variant}`);
3901
+ if (overrides) throw new Error("Dungeon assets accept a theme variant and seed");
3902
+ const key = id.slice("dungeon-".length);
3903
+ const scene = key === "courtyard" || key === "assembly" ? buildDungeonScene(key, seed, variant) : void 0;
3904
+ const group = scene?.group ?? buildDungeonPart(key, seed, variant);
3905
+ const lights = [];
3906
+ group.traverse((node) => {
3907
+ if (node instanceof PointLight && node.userData.fire) lights.push(node);
3908
+ });
3909
+ const animated = Boolean(scene) || group.children.some(
3910
+ (node) => node instanceof Mesh && node.material === dungeonMaterials.dungeonGlow
3911
+ );
3912
+ const update = lights.length ? (seconds) => {
3913
+ for (const light of lights) {
3914
+ const { intensity, phase } = light.userData.fire;
3915
+ light.intensity = intensity * (0.91 + 0.07 * Math.sin(seconds * 9.1 + phase) + 0.055 * Math.cos(seconds * 13.7 - phase));
3916
+ }
3917
+ } : void 0;
3918
+ return { ...scene, group, animated, update };
3919
+ }
3920
+ function biomeScene(id, { variant = 0, seed, overrides }) {
3921
+ const key = id.slice(6);
3922
+ const scene = id.startsWith("scene-") && Object.hasOwn(SCENES, key) ? SCENES[key] : void 0;
3923
+ if (!scene) throw new Error(`Unknown stylized asset "${id}"`);
3924
+ if (variant !== 0 || overrides) throw new Error("Stylized scenes accept only a seed");
3925
+ return { ...scene({ seed }), animated: true };
3926
+ }
3927
+ function createStylizedAsset(id, options = {}) {
3928
+ const { variant = 0, seed, overrides } = options;
3929
+ if (seed !== void 0 && !Number.isSafeInteger(seed))
3930
+ throw new Error("Stylized seed must be an integer");
3931
+ if (id.startsWith("dungeon-")) return dungeonAsset(id, options);
3932
+ const entry = CATALOG.find((asset) => asset.id === id);
3933
+ if (!entry) return biomeScene(id, options);
3934
+ if (!Number.isInteger(variant) || variant < 0 || variant >= entry.variants.length) {
3935
+ throw new Error(`Invalid variant ${variant} for stylized asset "${id}"`);
3936
+ }
3937
+ const group = build(entry, variant, { ...overrides, ...seed === void 0 ? {} : { seed } });
3938
+ if (entry.fn === "sky") {
3939
+ return { group, animated: false, view: { pos: [0, 0, 1], target: [0, 0.25, 0] } };
3940
+ }
3941
+ return {
3942
+ group,
3943
+ animated: [
3944
+ "water",
3945
+ "waterfall",
3946
+ "grassTuft",
3947
+ "pine",
3948
+ "roundTree",
3949
+ "twistedTree",
3950
+ "palm",
3951
+ "bush",
3952
+ "fern",
3953
+ "shrub",
3954
+ "flowers"
3955
+ ].includes(entry.fn)
3956
+ };
3957
+ }
3958
+
3959
+ export { createStylizedAsset, scatter, sky, stylizedAssets, stylizedMaterials, uniforms as stylizedUniforms, terrain };