@adatechnology/game-terrain-three 0.1.1 → 0.1.2

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.
@@ -125,18 +125,33 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
125
125
  shade[1] = terrain.scorch[index];
126
126
  return patternIndex(broken ? "soil" : material.pattern);
127
127
  }
128
- function buildChunkGeometry(THREE, terrain, chunk, step) {
128
+ function chunkVertexCount(terrain, chunk, step) {
129
+ const lastColumn = Math.min(terrain.columns, chunk.column + chunk.cells);
130
+ const lastRow = Math.min(terrain.rows, chunk.row + chunk.cells);
131
+ return (Math.ceil((lastColumn - chunk.column) / step) + 1) * (Math.ceil((lastRow - chunk.row) / step) + 1);
132
+ }
133
+ function attributeArray(geometry, name) {
134
+ return geometry.getAttribute(name).array;
135
+ }
136
+ function refreshAttributes(geometry, names) {
137
+ for (const name of names) geometry.getAttribute(name).needsUpdate = true;
138
+ geometry.boundingSphere = null;
139
+ geometry.boundingBox = null;
140
+ }
141
+ var LOOK_ATTRIBUTES = ["position", "normal", "aBase", "aShade", "aPattern"];
142
+ function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
129
143
  const [x0, z0] = terrain.bounds;
130
144
  const { cellSize, columns, rows } = terrain;
131
145
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
132
146
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
133
147
  const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
134
148
  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);
149
+ const target = reuse && reuse.getAttribute("position").count === across * down ? reuse : void 0;
150
+ const positions = target ? attributeArray(target, "position") : new Float32Array(across * down * 3);
151
+ const normals = target ? attributeArray(target, "normal") : new Float32Array(across * down * 3);
152
+ const bases = target ? attributeArray(target, "aBase") : new Float32Array(across * down * 3);
153
+ const shades = target ? attributeArray(target, "aShade") : new Float32Array(across * down * 2);
154
+ const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(across * down);
140
155
  const base = [0, 0, 0];
141
156
  const shade = [1, 0];
142
157
  let at = 0;
@@ -165,6 +180,10 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
165
180
  at++;
166
181
  }
167
182
  }
183
+ if (target) {
184
+ refreshAttributes(target, LOOK_ATTRIBUTES);
185
+ return target;
186
+ }
168
187
  const indices = new Uint32Array((across - 1) * (down - 1) * 6);
169
188
  let cursor = 0;
170
189
  for (let j = 0; j < down - 1; j++) {
@@ -190,17 +209,19 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
190
209
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
191
210
  return geometry;
192
211
  }
193
- function buildPristineGeometry(THREE, terrain, chunks, skirt) {
212
+ function buildPristineGeometry(THREE, terrain, chunks, skirt, reuse) {
194
213
  const [x0, z0] = terrain.bounds;
195
214
  const { cellSize, columns, rows } = terrain;
196
215
  const [, , x1, z1] = terrain.bounds;
197
216
  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);
217
+ const capacity = terrain.chunksX * terrain.chunksZ + SKIRT_QUADS;
218
+ const target = reuse && reuse.getAttribute("position").count === capacity * 4 ? reuse : void 0;
219
+ const positions = target ? attributeArray(target, "position") : new Float32Array(capacity * 12);
220
+ const normals = target ? attributeArray(target, "normal") : new Float32Array(capacity * 12);
221
+ const bases = target ? attributeArray(target, "aBase") : new Float32Array(capacity * 12);
222
+ const shades = target ? attributeArray(target, "aShade") : new Float32Array(capacity * 8);
223
+ const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(capacity * 4);
224
+ const indices = target ? target.getIndex().array : new Uint32Array(capacity * 6);
204
225
  const base = [0, 0, 0];
205
226
  const shade = [1, 0];
206
227
  chunks.forEach((chunk, n) => {
@@ -234,6 +255,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
234
255
  corners.forEach(([x, z], corner) => {
235
256
  const at = n * 4 + corner;
236
257
  positions[at * 3] = x;
258
+ positions[at * 3 + 1] = 0;
237
259
  positions[at * 3 + 2] = z;
238
260
  normals[at * 3 + 1] = 1;
239
261
  bases.set(surface.color, at * 3);
@@ -243,13 +265,19 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
243
265
  const first = n * 4;
244
266
  indices.set([first, first + 2, first + 1, first + 2, first + 3, first + 1], n * 6);
245
267
  });
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));
268
+ const geometry = target ?? new THREE.BufferGeometry();
269
+ if (target) {
270
+ refreshAttributes(target, LOOK_ATTRIBUTES);
271
+ target.getIndex().needsUpdate = true;
272
+ } else {
273
+ geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
274
+ geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
275
+ geometry.setAttribute("aBase", new THREE.BufferAttribute(bases, 3));
276
+ geometry.setAttribute("aShade", new THREE.BufferAttribute(shades, 2));
277
+ geometry.setAttribute("aPattern", new THREE.BufferAttribute(patterns, 1));
278
+ geometry.setIndex(new THREE.BufferAttribute(indices, 1));
279
+ }
280
+ geometry.setDrawRange(0, quads * 6);
253
281
  return geometry;
254
282
  }
255
283
 
@@ -292,6 +320,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
292
320
  mesh.frustumCulled = false;
293
321
  mesh.receiveShadow = true;
294
322
  mesh.renderOrder = 1;
323
+ mesh.setColorAt(0, new THREE.Color(1, 1, 1));
295
324
  const matrix = new THREE.Matrix4();
296
325
  const rotation = new THREE.Matrix4();
297
326
  const scale = new THREE.Matrix4();
@@ -344,6 +373,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
344
373
  mesh.count = 0;
345
374
  mesh.castShadow = true;
346
375
  mesh.frustumCulled = false;
376
+ mesh.setColorAt(0, new THREE.Color(1, 1, 1));
347
377
  const matrix = new THREE.Matrix4();
348
378
  const position = new THREE.Vector3();
349
379
  const scale = new THREE.Vector3();
@@ -384,7 +414,13 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
384
414
  var BAR_RADIUS = 6e-3;
385
415
  var EXPOSED_MARGIN = 2e-3;
386
416
  var MAX_SAG = 0.07;
387
- function buildRebarGeometry(THREE, terrain, chunk) {
417
+ var MIN_BAR_CAPACITY = 32;
418
+ function barCapacity(count) {
419
+ let capacity = MIN_BAR_CAPACITY;
420
+ while (capacity < count) capacity *= 2;
421
+ return capacity;
422
+ }
423
+ function buildRebarGeometry(THREE, terrain, chunk, reuse) {
388
424
  const [x0, z0] = terrain.bounds;
389
425
  const { cellSize, columns, rows, stride } = terrain;
390
426
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
@@ -421,9 +457,11 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
421
457
  }
422
458
  if (boxes.length === 0) return null;
423
459
  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);
460
+ const target = reuse && reuse.getAttribute("position").count >= count * 24 ? reuse : void 0;
461
+ const capacity = target ? target.getAttribute("position").count / 24 : barCapacity(count);
462
+ const positions = target ? target.getAttribute("position").array : new Float32Array(capacity * 24 * 3);
463
+ const normals = target ? target.getAttribute("normal").array : new Float32Array(capacity * 24 * 3);
464
+ const indices = target ? target.getIndex().array : new Uint32Array(capacity * 36);
427
465
  const FACES = [
428
466
  [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
429
467
  [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
@@ -449,10 +487,20 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
449
487
  cursor += 6;
450
488
  }
451
489
  }
490
+ if (target) {
491
+ target.getAttribute("position").needsUpdate = true;
492
+ target.getAttribute("normal").needsUpdate = true;
493
+ target.getIndex().needsUpdate = true;
494
+ target.boundingSphere = null;
495
+ target.boundingBox = null;
496
+ target.setDrawRange(0, count * 36);
497
+ return target;
498
+ }
452
499
  const geometry = new THREE.BufferGeometry();
453
500
  geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
454
501
  geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
455
502
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
503
+ geometry.setDrawRange(0, count * 36);
456
504
  return geometry;
457
505
  }
458
506
 
@@ -462,6 +510,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
462
510
  var DEFAULT_SKIRT = 300;
463
511
  var LOD_CHECK_METRES = 4;
464
512
  var REBAR_COLOUR = 4863272;
513
+ var MAX_SPARE_GEOMETRIES = 24;
465
514
  function createTerrainRenderer(THREE, options) {
466
515
  const { terrain } = options;
467
516
  const lodDistance = options.lodDistance ?? DEFAULT_LOD_DISTANCE;
@@ -480,6 +529,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
480
529
  const debris = createDebrisLayer(THREE, terrain);
481
530
  group.add(marks.mesh, debris.mesh);
482
531
  let pristineStale = true;
532
+ let pristineBuilt = false;
483
533
  let pristineCount = 0;
484
534
  let lodX = Infinity;
485
535
  let lodZ = Infinity;
@@ -491,31 +541,62 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
491
541
  return Math.hypot(centreX - camera.x, centreZ - camera.z);
492
542
  };
493
543
  const stepFor = (distance) => distance > lodDistance * 2 ? 4 : distance > lodDistance ? 2 : 1;
544
+ const spareGeometries = [];
545
+ function keepSpare(geometry) {
546
+ if (spareGeometries.length < MAX_SPARE_GEOMETRIES) spareGeometries.push(geometry);
547
+ else geometry.dispose();
548
+ }
549
+ function takeSpare(chunk, step) {
550
+ const count = chunkVertexCount(terrain, chunk, step);
551
+ const found = spareGeometries.findIndex((geometry) => geometry.getAttribute("position").count === count);
552
+ return found < 0 ? void 0 : spareGeometries.splice(found, 1)[0];
553
+ }
494
554
  function drop(index) {
495
555
  const entry = drawn.get(index);
496
556
  if (!entry) return;
497
557
  group.remove(entry.mesh);
498
- entry.mesh.geometry.dispose();
558
+ keepSpare(entry.mesh.geometry);
499
559
  if (entry.rebar) {
500
560
  group.remove(entry.rebar);
501
561
  entry.rebar.geometry.dispose();
502
562
  }
503
563
  drawn.delete(index);
504
564
  }
565
+ function drawRebar(chunk, entry) {
566
+ const bars = buildRebarGeometry(THREE, terrain, chunk, entry.rebar?.geometry);
567
+ if (!bars) {
568
+ if (entry.rebar) entry.rebar.visible = false;
569
+ return;
570
+ }
571
+ if (entry.rebar && entry.rebar.geometry === bars) {
572
+ entry.rebar.visible = true;
573
+ return;
574
+ }
575
+ if (entry.rebar) {
576
+ group.remove(entry.rebar);
577
+ entry.rebar.geometry.dispose();
578
+ }
579
+ entry.rebar = new THREE.Mesh(bars, rebarMaterial);
580
+ entry.rebar.castShadow = true;
581
+ group.add(entry.rebar);
582
+ }
505
583
  function draw(chunk, camera) {
506
- drop(chunk.index);
507
584
  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);
585
+ let entry = drawn.get(chunk.index);
586
+ if (!entry) {
587
+ const mesh = new THREE.Mesh(buildChunkGeometry(THREE, terrain, chunk, step, takeSpare(chunk, step)), surface);
588
+ mesh.receiveShadow = true;
589
+ group.add(mesh);
590
+ entry = { mesh, rebar: null, step };
591
+ drawn.set(chunk.index, entry);
592
+ } else {
593
+ const previous = entry.mesh.geometry;
594
+ const sameDetail = previous.getAttribute("position").count === chunkVertexCount(terrain, chunk, step);
595
+ entry.mesh.geometry = buildChunkGeometry(THREE, terrain, chunk, step, sameDetail ? previous : takeSpare(chunk, step));
596
+ if (entry.mesh.geometry !== previous) keepSpare(previous);
597
+ entry.step = step;
517
598
  }
518
- drawn.set(chunk.index, { mesh, rebar, step });
599
+ drawRebar(chunk, entry);
519
600
  }
520
601
  function rebuildPristine() {
521
602
  const chunks = [];
@@ -523,8 +604,9 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
523
604
  const chunk = terrain.chunk(index);
524
605
  if (chunk.pristine) chunks.push(chunk);
525
606
  }
526
- pristineMesh.geometry.dispose();
527
- pristineMesh.geometry = buildPristineGeometry(THREE, terrain, chunks, skirt);
607
+ if (!pristineBuilt) pristineMesh.geometry.dispose();
608
+ pristineMesh.geometry = buildPristineGeometry(THREE, terrain, chunks, skirt, pristineBuilt ? pristineMesh.geometry : void 0);
609
+ pristineBuilt = true;
528
610
  pristineCount = chunks.length;
529
611
  pristineStale = false;
530
612
  }
@@ -557,6 +639,7 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
557
639
  stats: () => ({ chunkMeshes: drawn.size, pristineChunks: pristineCount, marksDrawn: marks.mesh.count, debrisDrawn: debris.mesh.count }),
558
640
  dispose() {
559
641
  for (const index of [...drawn.keys()]) drop(index);
642
+ for (const geometry of spareGeometries) geometry.dispose();
560
643
  pristineMesh.geometry.dispose();
561
644
  surface.dispose();
562
645
  rebarMaterial.dispose();