@adatechnology/game-terrain-three 0.6.13 → 0.6.15

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.
@@ -343,51 +343,43 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
343
343
  geometry.boundingBox = null;
344
344
  }
345
345
  var LOOK_ATTRIBUTES = ["position", "normal", "aBase", "aShade", "aPattern"];
346
- function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
346
+ var vertexBase = [0, 0, 0];
347
+ var vertexShade = [1, 0];
348
+ function fillChunk(terrain, chunk, step, arrays) {
347
349
  const [x0, z0] = terrain.bounds;
348
350
  const { cellSize, columns, rows } = terrain;
351
+ const { positions, normals, bases, shades, patterns } = arrays;
349
352
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
350
353
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
351
354
  const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
352
355
  const down = Math.ceil((lastRow - chunk.row) / step) + 1;
353
- const target = reuse && reuse.getAttribute("position").count === across * down ? reuse : void 0;
354
- const positions = target ? attributeArray(target, "position") : new Float32Array(across * down * 3);
355
- const normals = target ? attributeArray(target, "normal") : new Float32Array(across * down * 3);
356
- const bases = target ? attributeArray(target, "aBase") : new Float32Array(across * down * 3);
357
- const shades = target ? attributeArray(target, "aShade") : new Float32Array(across * down * 2);
358
- const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(across * down);
359
- const base = [0, 0, 0];
360
- const shade = [1, 0];
361
356
  let at = 0;
362
357
  for (let j = 0; j < down; j++) {
363
358
  const row = Math.min(lastRow, chunk.row + j * step);
364
359
  for (let i = 0; i < across; i++) {
365
360
  const column = Math.min(lastColumn, chunk.column + i * step);
366
- const height = terrain.vertexHeight(column, row);
367
361
  positions[at * 3] = x0 + column * cellSize;
368
- positions[at * 3 + 1] = height;
362
+ positions[at * 3 + 1] = terrain.vertexHeight(column, row);
369
363
  positions[at * 3 + 2] = z0 + row * cellSize;
370
- const left = terrain.vertexHeight(Math.max(0, column - 1), row);
371
- const right = terrain.vertexHeight(Math.min(columns, column + 1), row);
372
- const back = terrain.vertexHeight(column, Math.max(0, row - 1));
373
- const front = terrain.vertexHeight(column, Math.min(rows, row + 1));
374
- const nx = left - right;
364
+ const nx = terrain.vertexHeight(Math.max(0, column - 1), row) - terrain.vertexHeight(Math.min(columns, column + 1), row);
375
365
  const ny = 2 * cellSize;
376
- const nz = back - front;
377
- const length = Math.hypot(nx, ny, nz);
366
+ const nz = terrain.vertexHeight(column, Math.max(0, row - 1)) - terrain.vertexHeight(column, Math.min(rows, row + 1));
367
+ const length = Math.sqrt(nx * nx + ny * ny + nz * nz);
378
368
  normals[at * 3] = nx / length;
379
369
  normals[at * 3 + 1] = ny / length;
380
370
  normals[at * 3 + 2] = nz / length;
381
- patterns[at] = vertexLook(terrain, column, row, base, shade);
382
- bases.set(base, at * 3);
383
- shades.set(shade, at * 2);
371
+ patterns[at] = vertexLook(terrain, column, row, vertexBase, vertexShade);
372
+ bases[at * 3] = vertexBase[0];
373
+ bases[at * 3 + 1] = vertexBase[1];
374
+ bases[at * 3 + 2] = vertexBase[2];
375
+ shades[at * 2] = vertexShade[0];
376
+ shades[at * 2 + 1] = vertexShade[1];
384
377
  at++;
385
378
  }
386
379
  }
387
- if (target) {
388
- refreshAttributes(target, LOOK_ATTRIBUTES);
389
- return target;
390
- }
380
+ }
381
+ function newChunkGeometry(THREE, across, down) {
382
+ const count = across * down;
391
383
  const indices = new Uint32Array((across - 1) * (down - 1) * 6);
392
384
  let cursor = 0;
393
385
  for (let j = 0; j < down - 1; j++) {
@@ -405,14 +397,31 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
405
397
  }
406
398
  }
407
399
  const geometry = new THREE.BufferGeometry();
408
- geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
409
- geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
410
- geometry.setAttribute("aBase", new THREE.BufferAttribute(bases, 3));
411
- geometry.setAttribute("aShade", new THREE.BufferAttribute(shades, 2));
412
- geometry.setAttribute("aPattern", new THREE.BufferAttribute(patterns, 1));
400
+ geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
401
+ geometry.setAttribute("normal", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
402
+ geometry.setAttribute("aBase", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
403
+ geometry.setAttribute("aShade", new THREE.BufferAttribute(new Float32Array(count * 2), 2));
404
+ geometry.setAttribute("aPattern", new THREE.BufferAttribute(new Float32Array(count), 1));
413
405
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
414
406
  return geometry;
415
407
  }
408
+ function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
409
+ const lastColumn = Math.min(terrain.columns, chunk.column + chunk.cells);
410
+ const lastRow = Math.min(terrain.rows, chunk.row + chunk.cells);
411
+ const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
412
+ const down = Math.ceil((lastRow - chunk.row) / step) + 1;
413
+ const reused = reuse !== void 0 && reuse.getAttribute("position").count === across * down;
414
+ const geometry = reused ? reuse : newChunkGeometry(THREE, across, down);
415
+ fillChunk(terrain, chunk, step, {
416
+ positions: attributeArray(geometry, "position"),
417
+ normals: attributeArray(geometry, "normal"),
418
+ bases: attributeArray(geometry, "aBase"),
419
+ shades: attributeArray(geometry, "aShade"),
420
+ patterns: attributeArray(geometry, "aPattern")
421
+ });
422
+ if (reused) refreshAttributes(geometry, LOOK_ATTRIBUTES);
423
+ return geometry;
424
+ }
416
425
  function buildPristineGeometry(THREE, terrain, chunks, skirt, reuse) {
417
426
  const [x0, z0] = terrain.bounds;
418
427
  const { cellSize, columns, rows } = terrain;
@@ -736,9 +745,12 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
736
745
  const quaternion = new THREE.Quaternion();
737
746
  const euler = new THREE.Euler();
738
747
  const colour = new THREE.Color();
748
+ let seenVersion = -1;
739
749
  return {
740
750
  mesh,
741
751
  sync() {
752
+ if (terrain.debrisVersion === seenVersion) return;
753
+ seenVersion = terrain.debrisVersion;
742
754
  const pieces = terrain.debris();
743
755
  let count = 0;
744
756
  for (const piece of pieces) {
@@ -977,24 +989,43 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
977
989
  while (capacity < count) capacity *= 2;
978
990
  return capacity;
979
991
  }
992
+ var FACES = [
993
+ [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
994
+ [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
995
+ [[0, 1, 5, 3, 1, 5, 3, 4, 5, 0, 4, 5], [0, 0, 1]],
996
+ [[3, 1, 2, 0, 1, 2, 0, 4, 2, 3, 4, 2], [0, 0, -1]],
997
+ [[3, 1, 5, 3, 1, 2, 3, 4, 2, 3, 4, 5], [1, 0, 0]],
998
+ [[0, 1, 2, 0, 1, 5, 0, 4, 5, 0, 4, 2], [-1, 0, 0]]
999
+ ];
1000
+ var BOX = 6;
1001
+ var barsByProfile = /* @__PURE__ */ new WeakMap();
1002
+ var barsOf = (profile) => {
1003
+ const known = barsByProfile.get(profile);
1004
+ if (known) return known;
1005
+ let top = 0;
1006
+ let depth = Number.NaN;
1007
+ let spacing = Number.NaN;
1008
+ for (const stratum of profile.strata) {
1009
+ if (stratum.reinforcement && Number.isNaN(depth)) {
1010
+ depth = top + stratum.reinforcement.cover;
1011
+ spacing = stratum.reinforcement.spacing;
1012
+ }
1013
+ top += stratum.thickness;
1014
+ }
1015
+ const bars = { depth, spacing, limit: top };
1016
+ barsByProfile.set(profile, bars);
1017
+ return bars;
1018
+ };
1019
+ var boxes = [];
980
1020
  function buildRebarGeometry(THREE, terrain, chunk, reuse) {
981
1021
  const [x0, z0] = terrain.bounds;
982
1022
  const { cellSize, columns, rows, stride } = terrain;
983
1023
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
984
1024
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
985
- const boxes = [];
986
- const coverOf = (index) => {
987
- let top = 0;
988
- for (const stratum of terrain.profileList[terrain.profileIndex[index]].strata) {
989
- if (stratum.reinforcement) return { depth: top + stratum.reinforcement.cover, spacing: stratum.reinforcement.spacing };
990
- top += stratum.thickness;
991
- }
992
- return null;
993
- };
1025
+ boxes.length = 0;
994
1026
  const showing = (index, depth) => {
995
- const profile = terrain.profileList[terrain.profileIndex[index]];
996
- const limit = profile.strata.reduce((sum, stratum) => sum + stratum.thickness, 0);
997
- return terrain.dug[index] > depth + EXPOSED_MARGIN && terrain.dug[index] <= limit + EXPOSED_MARGIN;
1027
+ const dug = terrain.dug[index];
1028
+ return dug > depth + EXPOSED_MARGIN && dug <= barsOf(terrain.profileList[terrain.profileIndex[index]]).limit + EXPOSED_MARGIN;
998
1029
  };
999
1030
  const onBar = (coordinate, spacing) => Math.floor(coordinate / spacing) !== Math.floor((coordinate + cellSize) / spacing);
1000
1031
  const addBox = (ax, az, bx, bz, y) => {
@@ -1003,44 +1034,43 @@ ${SURFACE_GLSL}`).replace("#include <color_fragment>", "#include <color_fragment
1003
1034
  for (let row = chunk.row; row <= lastRow; row++) {
1004
1035
  for (let column = chunk.column; column <= lastColumn; column++) {
1005
1036
  const index = row * stride + column;
1006
- const cover = coverOf(index);
1007
- if (!cover || !showing(index, cover.depth)) continue;
1037
+ const { depth, spacing } = barsOf(terrain.profileList[terrain.profileIndex[index]]);
1038
+ if (Number.isNaN(depth) || !showing(index, depth)) continue;
1008
1039
  const x = x0 + column * cellSize;
1009
1040
  const z = z0 + row * cellSize;
1010
- const y = -cover.depth - BAR_RADIUS - Math.min(1, terrain.barBend[index]) * MAX_SAG;
1011
- if (onBar(z, cover.spacing) && column < lastColumn && showing(index + 1, cover.depth)) addBox(x, z, x + cellSize, z, y);
1012
- if (onBar(x, cover.spacing) && row < lastRow && showing(index + stride, cover.depth)) addBox(x, z, x, z + cellSize, y);
1041
+ const y = -depth - BAR_RADIUS - Math.min(1, terrain.barBend[index]) * MAX_SAG;
1042
+ if (onBar(z, spacing) && column < lastColumn && showing(index + 1, depth)) addBox(x, z, x + cellSize, z, y);
1043
+ if (onBar(x, spacing) && row < lastRow && showing(index + stride, depth)) addBox(x, z, x, z + cellSize, y);
1013
1044
  }
1014
1045
  }
1015
1046
  if (boxes.length === 0) return null;
1016
- const count = boxes.length / 6;
1047
+ const count = boxes.length / BOX;
1017
1048
  const target = reuse && reuse.getAttribute("position").count >= count * 24 ? reuse : void 0;
1018
1049
  const capacity = target ? target.getAttribute("position").count / 24 : barCapacity(count);
1019
1050
  const positions = target ? target.getAttribute("position").array : new Float32Array(capacity * 24 * 3);
1020
1051
  const normals = target ? target.getAttribute("normal").array : new Float32Array(capacity * 24 * 3);
1021
1052
  const indices = target ? target.getIndex().array : new Uint32Array(capacity * 36);
1022
- const FACES = [
1023
- [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
1024
- [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
1025
- [[0, 1, 5, 3, 1, 5, 3, 4, 5, 0, 4, 5], [0, 0, 1]],
1026
- [[3, 1, 2, 0, 1, 2, 0, 4, 2, 3, 4, 2], [0, 0, -1]],
1027
- [[3, 1, 5, 3, 1, 2, 3, 4, 2, 3, 4, 5], [1, 0, 0]],
1028
- [[0, 1, 2, 0, 1, 5, 0, 4, 5, 0, 4, 2], [-1, 0, 0]]
1029
- ];
1030
1053
  let vertex = 0;
1031
1054
  let cursor = 0;
1032
1055
  for (let n = 0; n < count; n++) {
1033
- const box = boxes.slice(n * 6, n * 6 + 6);
1056
+ const at = n * BOX;
1034
1057
  for (const [corners, normal] of FACES) {
1035
1058
  const first = vertex;
1036
1059
  for (let c = 0; c < 4; c++) {
1037
- positions[vertex * 3] = box[corners[c * 3]];
1038
- positions[vertex * 3 + 1] = box[corners[c * 3 + 1]];
1039
- positions[vertex * 3 + 2] = box[corners[c * 3 + 2]];
1040
- normals.set(normal, vertex * 3);
1060
+ positions[vertex * 3] = boxes[at + corners[c * 3]];
1061
+ positions[vertex * 3 + 1] = boxes[at + corners[c * 3 + 1]];
1062
+ positions[vertex * 3 + 2] = boxes[at + corners[c * 3 + 2]];
1063
+ normals[vertex * 3] = normal[0];
1064
+ normals[vertex * 3 + 1] = normal[1];
1065
+ normals[vertex * 3 + 2] = normal[2];
1041
1066
  vertex++;
1042
1067
  }
1043
- indices.set([first, first + 1, first + 2, first, first + 2, first + 3], cursor);
1068
+ indices[cursor] = first;
1069
+ indices[cursor + 1] = first + 1;
1070
+ indices[cursor + 2] = first + 2;
1071
+ indices[cursor + 3] = first;
1072
+ indices[cursor + 4] = first + 2;
1073
+ indices[cursor + 5] = first + 3;
1044
1074
  cursor += 6;
1045
1075
  }
1046
1076
  }