roamwright 0.61.17 → 0.61.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/examples/ruins.js DELETED
@@ -1,327 +0,0 @@
1
- import * as THREE from 'three';
2
- import {
3
- buildHillRuin,
4
- buildMoundRuins,
5
- buildRubbleHeap,
6
- createDaylightSky,
7
- createOrbitCamera,
8
- createPavedGroundMaterial,
9
- createRockShapes,
10
- GeometryBuilder,
11
- geometryOf,
12
- layPavedRoad,
13
- mulberry32,
14
- scatterRubble,
15
- } from 'roamwright';
16
- import { instanceArrays, instancedMesh, putInstance } from 'roamwright/geometry/instances.js';
17
- import { C, TRS } from 'roamwright/geometry/primitives.js';
18
- import { sstep, TAU } from 'roamwright/math/scalar.js';
19
- import { createHeightfieldMesh } from 'roamwright/geometry/heightfield.js';
20
- import { initExample } from './stage.js';
21
-
22
- const { canvas, renderer, camera, scene, worldGroup, seed, noise, hash, shapes } = initExample({
23
- fallbackSeed: 42,
24
- fov: 40,
25
- });
26
- const { fbm, vnoise } = noise;
27
-
28
- const HILL = { x: -50, z: -42, r: 36, h: 11.5 };
29
- const MOUND = { x: 54, z: -38, r: 36, h: 9.5 };
30
- const PLAZA = { x: 0, z: 16 };
31
-
32
- // Flat-topped hill on the west, buried-town mound on the east, level highway corridor and plaza in between.
33
- function plateauRise(dx, dz, r, h) {
34
- const d = Math.hypot(dx, dz) / r;
35
- if (d >= 1.25) return 0;
36
- return h * (1 - sstep(0.68, 1.18, d));
37
- }
38
-
39
- function height(x, z) {
40
- const corridor = sstep(16, 44, Math.abs(x)) * sstep(22, 46, Math.hypot(x - PLAZA.x, z - PLAZA.z));
41
- const undulation = (fbm(x * 0.015, z * 0.015, 3, 17) - 0.5) * 4.5 * corridor;
42
- const hill = plateauRise(x - HILL.x, z - HILL.z, HILL.r, HILL.h);
43
- const mound = plateauRise(x - MOUND.x, z - MOUND.z, MOUND.r, MOUND.h);
44
- return 0.4 + undulation + hill + mound;
45
- }
46
-
47
- const earthTone = new THREE.Color(0xcab791);
48
- const scrubTone = new THREE.Color(0x9a9168);
49
- const rockTone = new THREE.Color(0xb6a382);
50
- const hillTone = new THREE.Color(0xc2b296);
51
-
52
- const terrain = createHeightfieldMesh({
53
- size: 360,
54
- segs: 180,
55
- heightAt: height,
56
- colorAt: (x, y, z, up, out) => {
57
- const n = fbm(x * 0.04, z * 0.04, 3, 29);
58
- out.copy(earthTone).lerp(scrubTone, sstep(0.45, 0.78, n) * 0.55);
59
- out.lerp(rockTone, 1 - sstep(0.74, 0.94, up));
60
- const onHill = 1 - sstep(HILL.r * 0.75, HILL.r * 1.15, Math.hypot(x - HILL.x, z - HILL.z));
61
- out.lerp(hillTone, onHill * 0.6);
62
- out.multiplyScalar(0.88 + 0.24 * vnoise(x * 0.2, z * 0.2, 31));
63
- },
64
- });
65
- worldGroup.add(terrain);
66
-
67
- // Procedural paved plaza at the highway head: concentric flagstone rings, coursed strips, cobbles and optional rain sheen.
68
- const plazaGeo = new THREE.PlaneGeometry(54, 48, 24, 24).rotateX(-Math.PI / 2);
69
- const plazaPos = plazaGeo.attributes.position;
70
- const plazaCols = new Float32Array(plazaPos.count * 3);
71
- const plazaTint = new THREE.Color();
72
- for (let i = 0; i < plazaPos.count; i++) {
73
- const wx = PLAZA.x + plazaPos.getX(i);
74
- const wz = PLAZA.z + plazaPos.getZ(i);
75
- plazaPos.setXYZ(i, wx, height(wx, wz) + 0.07, wz);
76
- const g = 0.86 + 0.1 * vnoise(wx * 0.08, wz * 0.08, 43);
77
- plazaTint.setRGB(0.8 * g, 0.76 * g, 0.67 * g);
78
- plazaTint.toArray(plazaCols, i * 3);
79
- }
80
- plazaGeo.computeVertexNormals();
81
- plazaGeo.setAttribute('color', new THREE.BufferAttribute(plazaCols, 3));
82
-
83
- let wetState = 0;
84
- function makePlazaMaterial(wet) {
85
- const mat = createPavedGroundMaterial({
86
- origin: PLAZA,
87
- rings: [{ x: 0, z: 0, r: 14, ring: 1.45 }],
88
- strips: [
89
- { x0: -27, z0: -4.2, x1: 27, z1: 4.2, len: 1.25, course: 0.95, along: 'x', wear: 2.2 },
90
- { x0: -5.4, z0: -24, x1: 5.4, z1: 24, len: 1.3, course: 1.05, along: 'z', wear: 2.8 },
91
- ],
92
- cobble: 0.2,
93
- earth: 0.18,
94
- wet,
95
- pools: [
96
- [-6, 7, 5.5, 4],
97
- [7, -5, 6.2, 4.2],
98
- [2, 11, 4.8, 3.6],
99
- [-9, -8, 5, 3.8],
100
- ],
101
- });
102
- mat.polygonOffset = true;
103
- mat.polygonOffsetFactor = mat.polygonOffsetUnits = -2;
104
- return mat;
105
- }
106
- const plazaMesh = new THREE.Mesh(plazaGeo, makePlazaMaterial(0));
107
- plazaMesh.receiveShadow = true;
108
- worldGroup.add(plazaMesh);
109
-
110
- // Shared weathered stone, rock and pool materials.
111
- const stoneMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.9, metalness: 0.02 });
112
- const rockMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.94, flatShading: true });
113
- const poolMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.16, metalness: 0.18 });
114
- const mudWallMat = new THREE.MeshStandardMaterial({ color: 0xc2ab87, roughness: 0.96 });
115
-
116
- // Hilltop ruin: tumbled blocks, leaning tower remnants, wall-stump rooms, flank masonry, scree and draped hollows.
117
- const hillKit = {
118
- hill: { u: HILL.x, v: -HILL.z, r: HILL.r },
119
- stone: new GeometryBuilder(),
120
- rock: new GeometryBuilder(),
121
- orders: [],
122
- Y: (u, v) => height(u, -v),
123
- G: (u, v) => height(u, -v),
124
- solid: () => {},
125
- };
126
- buildHillRuin(hillKit, mulberry32(seed * 17 + 3), {
127
- shapes,
128
- shape: { tumbled: 68, rooms: 9, flank: 220, scree: 900, oldWalls: 110, pools: 4, sinkholes: 3 },
129
- });
130
- // Fallen rubble heaps flanking the plaza edges.
131
- buildRubbleHeap(hillKit.stone, mulberry32(seed * 23 + 7), {
132
- x: -20,
133
- z: 28,
134
- radius: 4.5,
135
- count: 20,
136
- ground: height,
137
- rock: shapes.rock,
138
- });
139
- scatterRubble(hillKit.rock, mulberry32(seed * 29 + 11), {
140
- x: 22,
141
- z: 24,
142
- radius: 5.5,
143
- count: 24,
144
- ground: height,
145
- hue: 0xc8b89a,
146
- scale: 1.15,
147
- rock: shapes.rock,
148
- });
149
- hillKit.stone.mesh(stoneMat, worldGroup);
150
- hillKit.rock.mesh(rockMat, worldGroup);
151
- for (const order of hillKit.orders) {
152
- if (!order.mesh) continue;
153
- order.mesh.mesh(order.mat === 'pool' ? poolMat : rockMat, worldGroup, Boolean(order.shadow));
154
- }
155
-
156
- // Buried-town mound: eroded mud-brick wall stumps and strewn potsherds.
157
- const moundBuilt = buildMoundRuins(MOUND, seed * 31 + 19, false, height, {
158
- noise: vnoise,
159
- shape: { sherds: 520, wallsReach: 34, standing: 0.78 },
160
- });
161
- if (moundBuilt.walls.index.length) {
162
- const g = new THREE.BufferGeometry();
163
- g.setAttribute('position', new THREE.BufferAttribute(moundBuilt.walls.position, 3));
164
- g.setAttribute('normal', new THREE.BufferAttribute(moundBuilt.walls.normal, 3));
165
- g.setAttribute('uv', new THREE.BufferAttribute(moundBuilt.walls.uv, 2));
166
- g.setIndex(new THREE.BufferAttribute(moundBuilt.walls.index, 1));
167
- const mesh = new THREE.Mesh(g, mudWallMat);
168
- mesh.castShadow = mesh.receiveShadow = true;
169
- worldGroup.add(mesh);
170
- }
171
- if (moundBuilt.sherds?.position.length) {
172
- const sherdMesh = new THREE.Mesh(geometryOf(moundBuilt.sherds), rockMat);
173
- sherdMesh.receiveShadow = true;
174
- worldGroup.add(sherdMesh);
175
- }
176
-
177
- // Paved stone highway with kerbs, milestones, spanning arch and a roadside avenue of cedars.
178
- const roadBuilt = layPavedRoad({
179
- road: {
180
- pts: [
181
- [0, 152],
182
- [0, 102],
183
- [0, 56],
184
- [0, 34],
185
- ],
186
- w: 9.2,
187
- trees: 24,
188
- },
189
- ground: height,
190
- parent: worldGroup,
191
- random: mulberry32(seed * 37 + 5),
192
- stoneMaterial: stoneMat,
193
- pavingReach: { value: 180 },
194
- pavingBand: 24,
195
- arch: true,
196
- avenueOffset: 9.2,
197
- cedarSeed: seed * 41 + 9,
198
- hash,
199
- camera,
200
- getReach: () => 140,
201
- });
202
-
203
- // Bedded strata outcrops and wind-worn boulders instanced around the hill foot, mound and road verges.
204
- const rockLib = createRockShapes({ noise: vnoise });
205
- const outcrops = rockLib.outcropShapes();
206
- const outcropBatches = outcrops.map(() => ({ copies: instanceArrays(30), count: 0 }));
207
- const stoneBatches = rockLib.stones.map(() => ({ copies: instanceArrays(70), count: 0 }));
208
- const rockPalette = [0xcfb590, 0xbfa282, 0xd8c19e, 0xb89674];
209
- const rockRng = mulberry32(seed * 53 + 13);
210
-
211
- for (let i = 0; i < 64; i++) {
212
- const x = (rockRng() - 0.5) * 200;
213
- const z = (rockRng() - 0.5) * 180;
214
- if (Math.abs(x) < 15 && z > 20) continue; // keep highway clear
215
- if (Math.hypot(x - PLAZA.x, z - PLAZA.z) < 28) continue;
216
- if (Math.hypot(x - HILL.x, z - HILL.z) < HILL.r * 0.85) continue;
217
- if (Math.hypot(x - MOUND.x, z - MOUND.z) < MOUND.r * 0.8) continue;
218
- const s = 1.6 + rockRng() * 2.6;
219
- const col = C(rockPalette[i % rockPalette.length], 0.08, rockRng);
220
- const batch = outcropBatches[i % outcropBatches.length];
221
- if (batch.count < 30)
222
- putInstance(batch.copies, batch.count++, TRS(x, height(x, z), z, s, s * (0.85 + rockRng() * 0.4), s, rockRng() * TAU), ...col);
223
- }
224
- for (let i = 0; i < 160; i++) {
225
- const x = (rockRng() - 0.5) * 190;
226
- const z = (rockRng() - 0.5) * 170;
227
- if (Math.abs(x) < 11 && z > 24) continue;
228
- if (Math.hypot(x - PLAZA.x, z - PLAZA.z) < 23) continue;
229
- const s = 0.45 + rockRng() * 1.1;
230
- const col = C(rockPalette[i % rockPalette.length], 0.1, rockRng);
231
- const batch = stoneBatches[i % stoneBatches.length];
232
- if (batch.count < 70)
233
- putInstance(
234
- batch.copies,
235
- batch.count++,
236
- TRS(x, height(x, z) + s * 0.12, z, s, s * (0.7 + rockRng() * 0.35), s, rockRng() * TAU, (rockRng() - 0.5) * 0.4),
237
- ...col,
238
- );
239
- }
240
- const tintedRockMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.93 });
241
- outcropBatches.forEach(({ copies, count }, i) => {
242
- if (!count) return;
243
- const m = instancedMesh(outcrops[i], tintedRockMat, {
244
- matrices: copies.matrices.subarray(0, count * 16),
245
- colours: copies.colours.subarray(0, count * 3),
246
- });
247
- m.castShadow = m.receiveShadow = true;
248
- worldGroup.add(m);
249
- });
250
- stoneBatches.forEach(({ copies, count }, i) => {
251
- if (!count) return;
252
- const m = instancedMesh(rockLib.stones[i], tintedRockMat, {
253
- matrices: copies.matrices.subarray(0, count * 16),
254
- colours: copies.colours.subarray(0, count * 3),
255
- });
256
- m.castShadow = false;
257
- m.receiveShadow = true;
258
- worldGroup.add(m);
259
- });
260
-
261
- // Warm sunlit sky and directional shadows.
262
- createDaylightSky({
263
- scene,
264
- renderer,
265
- noise,
266
- sky: {
267
- zenith: [0.22, 0.46, 0.72],
268
- horizon: [0.89, 0.86, 0.78],
269
- sunDir: [0.68, 0.42, 0.58],
270
- },
271
- sunPosition: [120, 74, 102],
272
- sunIntensity: 2.8,
273
- sunColor: 0xfff2d8,
274
- shadowRadius: 125,
275
- shadowFar: 360,
276
- normalBias: 0.05,
277
- fogColor: 0xe2d8c5,
278
- fogDensity: 0.0012,
279
- });
280
-
281
- const VIEWS = {
282
- overview: { eye: [52, 32, 92], target: [-2, 6, -4] },
283
- hill: { eye: [-10, 26, -6], target: [HILL.x, HILL.h + 1, HILL.z] },
284
- arch: { eye: [3, 6.5, 92], target: [0, 6, 42] },
285
- plaza: { eye: [22, 13, 38], target: [PLAZA.x, 1, PLAZA.z] },
286
- mound: { eye: [22, 22, 2], target: [MOUND.x, MOUND.h, MOUND.z] },
287
- };
288
-
289
- const rig = createOrbitCamera({
290
- camera,
291
- canvas,
292
- renderer,
293
- scene,
294
- views: VIEWS,
295
- initialView: 'overview',
296
- groundAt: height,
297
- minClearance: 1.5,
298
- minDistance: 8,
299
- maxDistance: 240,
300
- onDraw: () => {},
301
- });
302
-
303
- // Snap cedar LODs immediately for the initial view before the first frame renders.
304
- roadBuilt.hand?.(1);
305
-
306
- for (const btn of document.querySelectorAll('[data-wet]')) {
307
- btn.addEventListener('click', () => {
308
- const next = Number(btn.dataset.wet);
309
- if (next === wetState) return;
310
- wetState = next;
311
- const old = plazaMesh.material;
312
- plazaMesh.material = makePlazaMaterial(wetState);
313
- old.dispose();
314
- for (const b of document.querySelectorAll('[data-wet]')) b.setAttribute('aria-pressed', String(Number(b.dataset.wet) === wetState));
315
- });
316
- }
317
-
318
- const clock = new THREE.Timer();
319
- function animate() {
320
- clock.update();
321
- const dt = Math.min(0.1, clock.getDelta());
322
- rig.clampCamera();
323
- roadBuilt.hand?.(dt);
324
- renderer.render(scene, camera);
325
- requestAnimationFrame(animate);
326
- }
327
- animate();