@adatechnology/game-terrain-three 0.1.1 → 0.2.0

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/dist/index.d.ts CHANGED
@@ -26,6 +26,7 @@ interface TerrainRenderer {
26
26
  readonly pristineChunks: number;
27
27
  readonly marksDrawn: number;
28
28
  readonly debrisDrawn: number;
29
+ readonly crackQuads: number;
29
30
  };
30
31
  dispose(): void;
31
32
  }
@@ -47,4 +48,7 @@ declare function createTerrainTestModule(options?: {
47
48
  readonly locale?: string;
48
49
  }): TestableModule;
49
50
 
50
- export { TERRAIN_AREA_GROUND, TERRAIN_STRIPS, type TerrainRenderer, type TerrainRendererOptions, createSurfaceMaterial, createTerrainRenderer, createTerrainTestModule };
51
+ /** The name of a ground profile in the locale (`en` when the locale is unknown); a profile a game added without a name answers its id. */
52
+ declare function terrainProfileName(profileId: string, localeId: string): string;
53
+
54
+ export { TERRAIN_AREA_GROUND, TERRAIN_STRIPS, type TerrainRenderer, type TerrainRendererOptions, createSurfaceMaterial, createTerrainRenderer, createTerrainTestModule, terrainProfileName };
@@ -25,7 +25,8 @@ var GameTerrainThree = (() => {
25
25
  TERRAIN_STRIPS: () => TERRAIN_STRIPS,
26
26
  createSurfaceMaterial: () => createSurfaceMaterial,
27
27
  createTerrainRenderer: () => createTerrainRenderer,
28
- createTerrainTestModule: () => createTerrainTestModule
28
+ createTerrainTestModule: () => createTerrainTestModule,
29
+ terrainProfileName: () => terrainProfileName
29
30
  });
30
31
 
31
32
  // ../game-contracts/dist/index.js
@@ -38,6 +39,47 @@ var GameTerrainThree = (() => {
38
39
  }
39
40
 
40
41
  // ../game-terrain/dist/index.js
42
+ function createRandom(seed) {
43
+ let state = seed | 0;
44
+ return () => {
45
+ state = state + 1831565813 | 0;
46
+ let mixed = Math.imul(state ^ state >>> 15, 1 | state);
47
+ mixed = mixed + Math.imul(mixed ^ mixed >>> 7, 61 | mixed) ^ mixed;
48
+ return ((mixed ^ mixed >>> 14) >>> 0) / 4294967296;
49
+ };
50
+ }
51
+ var HAIRLINE = 15e-4;
52
+ var SEGMENTS = 4;
53
+ var BRANCH_CHANCE = 0.45;
54
+ function crackLines(crack) {
55
+ const random = createRandom(crack.seed);
56
+ const segments = [];
57
+ const widthAt = (distance) => Math.max(HAIRLINE, crack.width * (1 - distance / crack.reach));
58
+ for (let ray = 0; ray < crack.rays; ray++) {
59
+ let angle = ray / crack.rays * Math.PI * 2 + (random() - 0.5) * 0.6;
60
+ const length = crack.reach * (0.45 + 0.55 * random());
61
+ let x = crack.x;
62
+ let z = crack.z;
63
+ let run = 0;
64
+ for (let segment = 0; segment < SEGMENTS; segment++) {
65
+ const step = length / SEGMENTS * (0.75 + 0.5 * random());
66
+ angle += (random() - 0.5) * 0.5;
67
+ const nx = x + Math.cos(angle) * step;
68
+ const nz = z + Math.sin(angle) * step;
69
+ segments.push({ x0: x, z0: z, x1: nx, z1: nz, w0: widthAt(run), w1: widthAt(run + step) });
70
+ if (segment > 0 && random() < BRANCH_CHANCE) {
71
+ const side = angle + (random() < 0.5 ? -1 : 1) * (0.45 + random() * 0.5);
72
+ const branch = step * (0.5 + 0.6 * random());
73
+ const w = widthAt(run + step) * 0.6;
74
+ segments.push({ x0: nx, z0: nz, x1: nx + Math.cos(side) * branch, z1: nz + Math.sin(side) * branch, w0: Math.max(HAIRLINE, w), w1: HAIRLINE });
75
+ }
76
+ run += step;
77
+ x = nx;
78
+ z = nz;
79
+ }
80
+ }
81
+ return segments;
82
+ }
41
83
  var TERRAIN_PATTERNS = ["plain", "grass", "soil", "sand", "asphalt", "concrete", "tile", "planks", "stone"];
42
84
 
43
85
  // src/surface-material.ts
@@ -125,18 +167,33 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
125
167
  shade[1] = terrain.scorch[index];
126
168
  return patternIndex(broken ? "soil" : material.pattern);
127
169
  }
128
- function buildChunkGeometry(THREE, terrain, chunk, step) {
170
+ function chunkVertexCount(terrain, chunk, step) {
171
+ const lastColumn = Math.min(terrain.columns, chunk.column + chunk.cells);
172
+ const lastRow = Math.min(terrain.rows, chunk.row + chunk.cells);
173
+ return (Math.ceil((lastColumn - chunk.column) / step) + 1) * (Math.ceil((lastRow - chunk.row) / step) + 1);
174
+ }
175
+ function attributeArray(geometry, name) {
176
+ return geometry.getAttribute(name).array;
177
+ }
178
+ function refreshAttributes(geometry, names) {
179
+ for (const name of names) geometry.getAttribute(name).needsUpdate = true;
180
+ geometry.boundingSphere = null;
181
+ geometry.boundingBox = null;
182
+ }
183
+ var LOOK_ATTRIBUTES = ["position", "normal", "aBase", "aShade", "aPattern"];
184
+ function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
129
185
  const [x0, z0] = terrain.bounds;
130
186
  const { cellSize, columns, rows } = terrain;
131
187
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
132
188
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
133
189
  const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
134
190
  const down = Math.ceil((lastRow - chunk.row) / step) + 1;
135
- const positions = new Float32Array(across * down * 3);
136
- const normals = new Float32Array(across * down * 3);
137
- const bases = new Float32Array(across * down * 3);
138
- const shades = new Float32Array(across * down * 2);
139
- const patterns = new Float32Array(across * down);
191
+ const target = reuse && reuse.getAttribute("position").count === across * down ? reuse : void 0;
192
+ const positions = target ? attributeArray(target, "position") : new Float32Array(across * down * 3);
193
+ const normals = target ? attributeArray(target, "normal") : new Float32Array(across * down * 3);
194
+ const bases = target ? attributeArray(target, "aBase") : new Float32Array(across * down * 3);
195
+ const shades = target ? attributeArray(target, "aShade") : new Float32Array(across * down * 2);
196
+ const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(across * down);
140
197
  const base = [0, 0, 0];
141
198
  const shade = [1, 0];
142
199
  let at = 0;
@@ -165,6 +222,10 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
165
222
  at++;
166
223
  }
167
224
  }
225
+ if (target) {
226
+ refreshAttributes(target, LOOK_ATTRIBUTES);
227
+ return target;
228
+ }
168
229
  const indices = new Uint32Array((across - 1) * (down - 1) * 6);
169
230
  let cursor = 0;
170
231
  for (let j = 0; j < down - 1; j++) {
@@ -190,17 +251,19 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
190
251
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
191
252
  return geometry;
192
253
  }
193
- function buildPristineGeometry(THREE, terrain, chunks, skirt) {
254
+ function buildPristineGeometry(THREE, terrain, chunks, skirt, reuse) {
194
255
  const [x0, z0] = terrain.bounds;
195
256
  const { cellSize, columns, rows } = terrain;
196
257
  const [, , x1, z1] = terrain.bounds;
197
258
  const quads = chunks.length + SKIRT_QUADS;
198
- const positions = new Float32Array(quads * 12);
199
- const normals = new Float32Array(quads * 12);
200
- const bases = new Float32Array(quads * 12);
201
- const shades = new Float32Array(quads * 8);
202
- const patterns = new Float32Array(quads * 4);
203
- const indices = new Uint32Array(quads * 6);
259
+ const capacity = terrain.chunksX * terrain.chunksZ + SKIRT_QUADS;
260
+ const target = reuse && reuse.getAttribute("position").count === capacity * 4 ? reuse : void 0;
261
+ const positions = target ? attributeArray(target, "position") : new Float32Array(capacity * 12);
262
+ const normals = target ? attributeArray(target, "normal") : new Float32Array(capacity * 12);
263
+ const bases = target ? attributeArray(target, "aBase") : new Float32Array(capacity * 12);
264
+ const shades = target ? attributeArray(target, "aShade") : new Float32Array(capacity * 8);
265
+ const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(capacity * 4);
266
+ const indices = target ? target.getIndex().array : new Uint32Array(capacity * 6);
204
267
  const base = [0, 0, 0];
205
268
  const shade = [1, 0];
206
269
  chunks.forEach((chunk, n) => {
@@ -234,6 +297,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
234
297
  corners.forEach(([x, z], corner) => {
235
298
  const at = n * 4 + corner;
236
299
  positions[at * 3] = x;
300
+ positions[at * 3 + 1] = 0;
237
301
  positions[at * 3 + 2] = z;
238
302
  normals[at * 3 + 1] = 1;
239
303
  bases.set(surface.color, at * 3);
@@ -243,13 +307,19 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
243
307
  const first = n * 4;
244
308
  indices.set([first, first + 2, first + 1, first + 2, first + 3, first + 1], n * 6);
245
309
  });
246
- const geometry = new THREE.BufferGeometry();
247
- geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
248
- geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
249
- geometry.setAttribute("aBase", new THREE.BufferAttribute(bases, 3));
250
- geometry.setAttribute("aShade", new THREE.BufferAttribute(shades, 2));
251
- geometry.setAttribute("aPattern", new THREE.BufferAttribute(patterns, 1));
252
- geometry.setIndex(new THREE.BufferAttribute(indices, 1));
310
+ const geometry = target ?? new THREE.BufferGeometry();
311
+ if (target) {
312
+ refreshAttributes(target, LOOK_ATTRIBUTES);
313
+ target.getIndex().needsUpdate = true;
314
+ } else {
315
+ geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
316
+ geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
317
+ geometry.setAttribute("aBase", new THREE.BufferAttribute(bases, 3));
318
+ geometry.setAttribute("aShade", new THREE.BufferAttribute(shades, 2));
319
+ geometry.setAttribute("aPattern", new THREE.BufferAttribute(patterns, 1));
320
+ geometry.setIndex(new THREE.BufferAttribute(indices, 1));
321
+ }
322
+ geometry.setDrawRange(0, quads * 6);
253
323
  return geometry;
254
324
  }
255
325
 
@@ -292,6 +362,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
292
362
  mesh.frustumCulled = false;
293
363
  mesh.receiveShadow = true;
294
364
  mesh.renderOrder = 1;
365
+ mesh.setColorAt(0, new THREE.Color(1, 1, 1));
295
366
  const matrix = new THREE.Matrix4();
296
367
  const rotation = new THREE.Matrix4();
297
368
  const scale = new THREE.Matrix4();
@@ -344,6 +415,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
344
415
  mesh.count = 0;
345
416
  mesh.castShadow = true;
346
417
  mesh.frustumCulled = false;
418
+ mesh.setColorAt(0, new THREE.Color(1, 1, 1));
347
419
  const matrix = new THREE.Matrix4();
348
420
  const position = new THREE.Vector3();
349
421
  const scale = new THREE.Vector3();
@@ -379,12 +451,104 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
379
451
  }
380
452
  };
381
453
  }
454
+ var CRACK_LIFT = 4e-3;
455
+ var MAX_CRACK_QUADS = 8192;
456
+ var CRACK_SHADE = 0.22;
457
+ var DARK_SURFACE = 0.1;
458
+ var DUST_LIFT = 2;
459
+ var DUST_GREY = 0.12;
460
+ function createCrackLayer(THREE, terrain) {
461
+ const positions = new Float32Array(MAX_CRACK_QUADS * 4 * 3);
462
+ const colours = new Float32Array(MAX_CRACK_QUADS * 4 * 3);
463
+ const indices = new Uint32Array(MAX_CRACK_QUADS * 6);
464
+ for (let quad = 0; quad < MAX_CRACK_QUADS; quad++) {
465
+ const first = quad * 4;
466
+ indices.set([first, first + 2, first + 1, first + 1, first + 2, first + 3], quad * 6);
467
+ }
468
+ const geometry = new THREE.BufferGeometry();
469
+ const position = new THREE.BufferAttribute(positions, 3);
470
+ const colour = new THREE.BufferAttribute(colours, 3);
471
+ position.setUsage(THREE.DynamicDrawUsage);
472
+ colour.setUsage(THREE.DynamicDrawUsage);
473
+ geometry.setAttribute("position", position);
474
+ geometry.setAttribute("color", colour);
475
+ geometry.setIndex(new THREE.BufferAttribute(indices, 1));
476
+ geometry.setDrawRange(0, 0);
477
+ const material = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.9, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -3, polygonOffsetUnits: -3 });
478
+ const mesh = new THREE.Mesh(geometry, material);
479
+ mesh.name = "terrain-cracks";
480
+ mesh.frustumCulled = false;
481
+ mesh.renderOrder = 2;
482
+ let seen = -1;
483
+ let groundMoved = false;
484
+ const stopListening = terrain.onDeform(() => {
485
+ groundMoved = true;
486
+ });
487
+ const lift = (x, z) => terrain.heightAt(x, z) + CRACK_LIFT;
488
+ return {
489
+ mesh,
490
+ sync() {
491
+ const cracks = terrain.cracks();
492
+ if (seen === terrain.cracksVersion && !(groundMoved && cracks.length > 0)) return;
493
+ seen = terrain.cracksVersion;
494
+ groundMoved = false;
495
+ let quad = 0;
496
+ for (let n = cracks.length - 1; n >= 0 && quad < MAX_CRACK_QUADS; n--) {
497
+ const crack = cracks[n];
498
+ const tone = terrain.material(crack.material).color;
499
+ const dark = tone[0] * 0.3 + tone[1] * 0.59 + tone[2] * 0.11 < DARK_SURFACE;
500
+ const k = dark ? DUST_LIFT : CRACK_SHADE;
501
+ const r = dark ? Math.min(1, tone[0] * k + DUST_GREY) : tone[0] * k;
502
+ const g = dark ? Math.min(1, tone[1] * k + DUST_GREY) : tone[1] * k;
503
+ const b = dark ? Math.min(1, tone[2] * k + DUST_GREY) : tone[2] * k;
504
+ for (const s of crackLines(crack)) {
505
+ if (quad >= MAX_CRACK_QUADS) break;
506
+ if (!terrain.contains(s.x0, s.z0) || !terrain.contains(s.x1, s.z1)) continue;
507
+ const length = Math.hypot(s.x1 - s.x0, s.z1 - s.z0);
508
+ if (length < 1e-5) continue;
509
+ const ax = -(s.z1 - s.z0) / length;
510
+ const az = (s.x1 - s.x0) / length;
511
+ const corners = [
512
+ [s.x0 + ax * s.w0 * 0.5, s.z0 + az * s.w0 * 0.5],
513
+ [s.x0 - ax * s.w0 * 0.5, s.z0 - az * s.w0 * 0.5],
514
+ [s.x1 + ax * s.w1 * 0.5, s.z1 + az * s.w1 * 0.5],
515
+ [s.x1 - ax * s.w1 * 0.5, s.z1 - az * s.w1 * 0.5]
516
+ ];
517
+ corners.forEach(([x, z], corner) => {
518
+ const at = (quad * 4 + corner) * 3;
519
+ positions[at] = x;
520
+ positions[at + 1] = lift(x, z);
521
+ positions[at + 2] = z;
522
+ colours[at] = r;
523
+ colours[at + 1] = g;
524
+ colours[at + 2] = b;
525
+ });
526
+ quad++;
527
+ }
528
+ }
529
+ position.needsUpdate = true;
530
+ colour.needsUpdate = true;
531
+ geometry.setDrawRange(0, quad * 6);
532
+ },
533
+ dispose() {
534
+ stopListening();
535
+ geometry.dispose();
536
+ material.dispose();
537
+ }
538
+ };
539
+ }
382
540
 
383
541
  // src/rebar.ts
384
542
  var BAR_RADIUS = 6e-3;
385
543
  var EXPOSED_MARGIN = 2e-3;
386
544
  var MAX_SAG = 0.07;
387
- function buildRebarGeometry(THREE, terrain, chunk) {
545
+ var MIN_BAR_CAPACITY = 32;
546
+ function barCapacity(count) {
547
+ let capacity = MIN_BAR_CAPACITY;
548
+ while (capacity < count) capacity *= 2;
549
+ return capacity;
550
+ }
551
+ function buildRebarGeometry(THREE, terrain, chunk, reuse) {
388
552
  const [x0, z0] = terrain.bounds;
389
553
  const { cellSize, columns, rows, stride } = terrain;
390
554
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
@@ -421,9 +585,11 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
421
585
  }
422
586
  if (boxes.length === 0) return null;
423
587
  const count = boxes.length / 6;
424
- const positions = new Float32Array(count * 24 * 3);
425
- const normals = new Float32Array(count * 24 * 3);
426
- const indices = new Uint32Array(count * 36);
588
+ const target = reuse && reuse.getAttribute("position").count >= count * 24 ? reuse : void 0;
589
+ const capacity = target ? target.getAttribute("position").count / 24 : barCapacity(count);
590
+ const positions = target ? target.getAttribute("position").array : new Float32Array(capacity * 24 * 3);
591
+ const normals = target ? target.getAttribute("normal").array : new Float32Array(capacity * 24 * 3);
592
+ const indices = target ? target.getIndex().array : new Uint32Array(capacity * 36);
427
593
  const FACES = [
428
594
  [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
429
595
  [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
@@ -449,10 +615,20 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
449
615
  cursor += 6;
450
616
  }
451
617
  }
618
+ if (target) {
619
+ target.getAttribute("position").needsUpdate = true;
620
+ target.getAttribute("normal").needsUpdate = true;
621
+ target.getIndex().needsUpdate = true;
622
+ target.boundingSphere = null;
623
+ target.boundingBox = null;
624
+ target.setDrawRange(0, count * 36);
625
+ return target;
626
+ }
452
627
  const geometry = new THREE.BufferGeometry();
453
628
  geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
454
629
  geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
455
630
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
631
+ geometry.setDrawRange(0, count * 36);
456
632
  return geometry;
457
633
  }
458
634
 
@@ -462,6 +638,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
462
638
  var DEFAULT_SKIRT = 300;
463
639
  var LOD_CHECK_METRES = 4;
464
640
  var REBAR_COLOUR = 4863272;
641
+ var MAX_SPARE_GEOMETRIES = 24;
465
642
  function createTerrainRenderer(THREE, options) {
466
643
  const { terrain } = options;
467
644
  const lodDistance = options.lodDistance ?? DEFAULT_LOD_DISTANCE;
@@ -478,8 +655,10 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
478
655
  const drawn = /* @__PURE__ */ new Map();
479
656
  const marks = createMarkLayer(THREE, terrain, options.markDistance ?? DEFAULT_MARK_DISTANCE);
480
657
  const debris = createDebrisLayer(THREE, terrain);
481
- group.add(marks.mesh, debris.mesh);
658
+ const cracks = createCrackLayer(THREE, terrain);
659
+ group.add(marks.mesh, cracks.mesh, debris.mesh);
482
660
  let pristineStale = true;
661
+ let pristineBuilt = false;
483
662
  let pristineCount = 0;
484
663
  let lodX = Infinity;
485
664
  let lodZ = Infinity;
@@ -491,31 +670,62 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
491
670
  return Math.hypot(centreX - camera.x, centreZ - camera.z);
492
671
  };
493
672
  const stepFor = (distance) => distance > lodDistance * 2 ? 4 : distance > lodDistance ? 2 : 1;
673
+ const spareGeometries = [];
674
+ function keepSpare(geometry) {
675
+ if (spareGeometries.length < MAX_SPARE_GEOMETRIES) spareGeometries.push(geometry);
676
+ else geometry.dispose();
677
+ }
678
+ function takeSpare(chunk, step) {
679
+ const count = chunkVertexCount(terrain, chunk, step);
680
+ const found = spareGeometries.findIndex((geometry) => geometry.getAttribute("position").count === count);
681
+ return found < 0 ? void 0 : spareGeometries.splice(found, 1)[0];
682
+ }
494
683
  function drop(index) {
495
684
  const entry = drawn.get(index);
496
685
  if (!entry) return;
497
686
  group.remove(entry.mesh);
498
- entry.mesh.geometry.dispose();
687
+ keepSpare(entry.mesh.geometry);
499
688
  if (entry.rebar) {
500
689
  group.remove(entry.rebar);
501
690
  entry.rebar.geometry.dispose();
502
691
  }
503
692
  drawn.delete(index);
504
693
  }
694
+ function drawRebar(chunk, entry) {
695
+ const bars = buildRebarGeometry(THREE, terrain, chunk, entry.rebar?.geometry);
696
+ if (!bars) {
697
+ if (entry.rebar) entry.rebar.visible = false;
698
+ return;
699
+ }
700
+ if (entry.rebar && entry.rebar.geometry === bars) {
701
+ entry.rebar.visible = true;
702
+ return;
703
+ }
704
+ if (entry.rebar) {
705
+ group.remove(entry.rebar);
706
+ entry.rebar.geometry.dispose();
707
+ }
708
+ entry.rebar = new THREE.Mesh(bars, rebarMaterial);
709
+ entry.rebar.castShadow = true;
710
+ group.add(entry.rebar);
711
+ }
505
712
  function draw(chunk, camera) {
506
- drop(chunk.index);
507
713
  const step = stepFor(distanceTo(chunk, camera));
508
- const mesh = new THREE.Mesh(buildChunkGeometry(THREE, terrain, chunk, step), surface);
509
- mesh.receiveShadow = true;
510
- group.add(mesh);
511
- const bars = buildRebarGeometry(THREE, terrain, chunk);
512
- let rebar = null;
513
- if (bars) {
514
- rebar = new THREE.Mesh(bars, rebarMaterial);
515
- rebar.castShadow = true;
516
- group.add(rebar);
714
+ let entry = drawn.get(chunk.index);
715
+ if (!entry) {
716
+ const mesh = new THREE.Mesh(buildChunkGeometry(THREE, terrain, chunk, step, takeSpare(chunk, step)), surface);
717
+ mesh.receiveShadow = true;
718
+ group.add(mesh);
719
+ entry = { mesh, rebar: null, step };
720
+ drawn.set(chunk.index, entry);
721
+ } else {
722
+ const previous = entry.mesh.geometry;
723
+ const sameDetail = previous.getAttribute("position").count === chunkVertexCount(terrain, chunk, step);
724
+ entry.mesh.geometry = buildChunkGeometry(THREE, terrain, chunk, step, sameDetail ? previous : takeSpare(chunk, step));
725
+ if (entry.mesh.geometry !== previous) keepSpare(previous);
726
+ entry.step = step;
517
727
  }
518
- drawn.set(chunk.index, { mesh, rebar, step });
728
+ drawRebar(chunk, entry);
519
729
  }
520
730
  function rebuildPristine() {
521
731
  const chunks = [];
@@ -523,15 +733,17 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
523
733
  const chunk = terrain.chunk(index);
524
734
  if (chunk.pristine) chunks.push(chunk);
525
735
  }
526
- pristineMesh.geometry.dispose();
527
- pristineMesh.geometry = buildPristineGeometry(THREE, terrain, chunks, skirt);
736
+ if (!pristineBuilt) pristineMesh.geometry.dispose();
737
+ pristineMesh.geometry = buildPristineGeometry(THREE, terrain, chunks, skirt, pristineBuilt ? pristineMesh.geometry : void 0);
738
+ pristineBuilt = true;
528
739
  pristineCount = chunks.length;
529
740
  pristineStale = false;
530
741
  }
531
742
  return {
532
743
  group,
533
744
  sync(camera) {
534
- const limit = firstSync ? Infinity : void 0;
745
+ const laidAnew = terrain.dirtyChunkCount >= terrain.chunksX * terrain.chunksZ;
746
+ const limit = firstSync || laidAnew ? Infinity : void 0;
535
747
  firstSync = false;
536
748
  for (const chunk of terrain.drainDirtyChunks(limit)) {
537
749
  if (chunk.pristine) {
@@ -552,15 +764,18 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
552
764
  }
553
765
  }
554
766
  marks.sync(camera);
767
+ cracks.sync();
555
768
  debris.sync();
556
769
  },
557
- stats: () => ({ chunkMeshes: drawn.size, pristineChunks: pristineCount, marksDrawn: marks.mesh.count, debrisDrawn: debris.mesh.count }),
770
+ stats: () => ({ chunkMeshes: drawn.size, pristineChunks: pristineCount, marksDrawn: marks.mesh.count, debrisDrawn: debris.mesh.count, crackQuads: cracks.mesh.geometry.drawRange.count / 6 }),
558
771
  dispose() {
559
772
  for (const index of [...drawn.keys()]) drop(index);
773
+ for (const geometry of spareGeometries) geometry.dispose();
560
774
  pristineMesh.geometry.dispose();
561
775
  surface.dispose();
562
776
  rebarMaterial.dispose();
563
777
  marks.dispose();
778
+ cracks.dispose();
564
779
  debris.dispose();
565
780
  group.removeFromParent();
566
781
  }
@@ -568,6 +783,13 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
568
783
  }
569
784
 
570
785
  // src/locale.ts
786
+ var PROFILE_NAMES = {
787
+ en: { lawn: "Lawn", dirt: "Dirt", sand: "Sand", road: "Asphalt road", concrete_slab: "Concrete slab", suspended_slab: "Slab over a void", tile_floor: "Tile floor", wood_floor: "Wood floor", stone_paving: "Stone paving" },
788
+ "pt-BR": { lawn: "Grama", dirt: "Terra", sand: "Areia", road: "Asfalto", concrete_slab: "Laje de concreto", suspended_slab: "Laje sobre v\xE3o", tile_floor: "Piso cer\xE2mico", wood_floor: "Assoalho", stone_paving: "Cal\xE7amento de pedra" }
789
+ };
790
+ function terrainProfileName(profileId, localeId) {
791
+ return (PROFILE_NAMES[localeId] ?? PROFILE_NAMES.en)[profileId] ?? profileId;
792
+ }
571
793
  var LOCALES = {
572
794
  en: {
573
795
  name: "Terrain",