roamwright 0.61.12 → 0.61.14

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