@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.
package/dist/index.js CHANGED
@@ -176,51 +176,43 @@ function refreshAttributes(geometry, names) {
176
176
  geometry.boundingBox = null;
177
177
  }
178
178
  var LOOK_ATTRIBUTES = ["position", "normal", "aBase", "aShade", "aPattern"];
179
- function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
179
+ var vertexBase = [0, 0, 0];
180
+ var vertexShade = [1, 0];
181
+ function fillChunk(terrain, chunk, step, arrays) {
180
182
  const [x0, z0] = terrain.bounds;
181
183
  const { cellSize, columns, rows } = terrain;
184
+ const { positions, normals, bases, shades, patterns } = arrays;
182
185
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
183
186
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
184
187
  const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
185
188
  const down = Math.ceil((lastRow - chunk.row) / step) + 1;
186
- const target = reuse && reuse.getAttribute("position").count === across * down ? reuse : void 0;
187
- const positions = target ? attributeArray(target, "position") : new Float32Array(across * down * 3);
188
- const normals = target ? attributeArray(target, "normal") : new Float32Array(across * down * 3);
189
- const bases = target ? attributeArray(target, "aBase") : new Float32Array(across * down * 3);
190
- const shades = target ? attributeArray(target, "aShade") : new Float32Array(across * down * 2);
191
- const patterns = target ? attributeArray(target, "aPattern") : new Float32Array(across * down);
192
- const base = [0, 0, 0];
193
- const shade = [1, 0];
194
189
  let at = 0;
195
190
  for (let j = 0; j < down; j++) {
196
191
  const row = Math.min(lastRow, chunk.row + j * step);
197
192
  for (let i = 0; i < across; i++) {
198
193
  const column = Math.min(lastColumn, chunk.column + i * step);
199
- const height = terrain.vertexHeight(column, row);
200
194
  positions[at * 3] = x0 + column * cellSize;
201
- positions[at * 3 + 1] = height;
195
+ positions[at * 3 + 1] = terrain.vertexHeight(column, row);
202
196
  positions[at * 3 + 2] = z0 + row * cellSize;
203
- const left = terrain.vertexHeight(Math.max(0, column - 1), row);
204
- const right = terrain.vertexHeight(Math.min(columns, column + 1), row);
205
- const back = terrain.vertexHeight(column, Math.max(0, row - 1));
206
- const front = terrain.vertexHeight(column, Math.min(rows, row + 1));
207
- const nx = left - right;
197
+ const nx = terrain.vertexHeight(Math.max(0, column - 1), row) - terrain.vertexHeight(Math.min(columns, column + 1), row);
208
198
  const ny = 2 * cellSize;
209
- const nz = back - front;
210
- const length = Math.hypot(nx, ny, nz);
199
+ const nz = terrain.vertexHeight(column, Math.max(0, row - 1)) - terrain.vertexHeight(column, Math.min(rows, row + 1));
200
+ const length = Math.sqrt(nx * nx + ny * ny + nz * nz);
211
201
  normals[at * 3] = nx / length;
212
202
  normals[at * 3 + 1] = ny / length;
213
203
  normals[at * 3 + 2] = nz / length;
214
- patterns[at] = vertexLook(terrain, column, row, base, shade);
215
- bases.set(base, at * 3);
216
- shades.set(shade, at * 2);
204
+ patterns[at] = vertexLook(terrain, column, row, vertexBase, vertexShade);
205
+ bases[at * 3] = vertexBase[0];
206
+ bases[at * 3 + 1] = vertexBase[1];
207
+ bases[at * 3 + 2] = vertexBase[2];
208
+ shades[at * 2] = vertexShade[0];
209
+ shades[at * 2 + 1] = vertexShade[1];
217
210
  at++;
218
211
  }
219
212
  }
220
- if (target) {
221
- refreshAttributes(target, LOOK_ATTRIBUTES);
222
- return target;
223
- }
213
+ }
214
+ function newChunkGeometry(THREE, across, down) {
215
+ const count = across * down;
224
216
  const indices = new Uint32Array((across - 1) * (down - 1) * 6);
225
217
  let cursor = 0;
226
218
  for (let j = 0; j < down - 1; j++) {
@@ -238,14 +230,31 @@ function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
238
230
  }
239
231
  }
240
232
  const geometry = new THREE.BufferGeometry();
241
- geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
242
- geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
243
- geometry.setAttribute("aBase", new THREE.BufferAttribute(bases, 3));
244
- geometry.setAttribute("aShade", new THREE.BufferAttribute(shades, 2));
245
- geometry.setAttribute("aPattern", new THREE.BufferAttribute(patterns, 1));
233
+ geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
234
+ geometry.setAttribute("normal", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
235
+ geometry.setAttribute("aBase", new THREE.BufferAttribute(new Float32Array(count * 3), 3));
236
+ geometry.setAttribute("aShade", new THREE.BufferAttribute(new Float32Array(count * 2), 2));
237
+ geometry.setAttribute("aPattern", new THREE.BufferAttribute(new Float32Array(count), 1));
246
238
  geometry.setIndex(new THREE.BufferAttribute(indices, 1));
247
239
  return geometry;
248
240
  }
241
+ function buildChunkGeometry(THREE, terrain, chunk, step, reuse) {
242
+ const lastColumn = Math.min(terrain.columns, chunk.column + chunk.cells);
243
+ const lastRow = Math.min(terrain.rows, chunk.row + chunk.cells);
244
+ const across = Math.ceil((lastColumn - chunk.column) / step) + 1;
245
+ const down = Math.ceil((lastRow - chunk.row) / step) + 1;
246
+ const reused = reuse !== void 0 && reuse.getAttribute("position").count === across * down;
247
+ const geometry = reused ? reuse : newChunkGeometry(THREE, across, down);
248
+ fillChunk(terrain, chunk, step, {
249
+ positions: attributeArray(geometry, "position"),
250
+ normals: attributeArray(geometry, "normal"),
251
+ bases: attributeArray(geometry, "aBase"),
252
+ shades: attributeArray(geometry, "aShade"),
253
+ patterns: attributeArray(geometry, "aPattern")
254
+ });
255
+ if (reused) refreshAttributes(geometry, LOOK_ATTRIBUTES);
256
+ return geometry;
257
+ }
249
258
  function buildPristineGeometry(THREE, terrain, chunks, skirt, reuse) {
250
259
  const [x0, z0] = terrain.bounds;
251
260
  const { cellSize, columns, rows } = terrain;
@@ -570,9 +579,12 @@ function createDebrisLayer(THREE, terrain) {
570
579
  const quaternion = new THREE.Quaternion();
571
580
  const euler = new THREE.Euler();
572
581
  const colour = new THREE.Color();
582
+ let seenVersion = -1;
573
583
  return {
574
584
  mesh,
575
585
  sync() {
586
+ if (terrain.debrisVersion === seenVersion) return;
587
+ seenVersion = terrain.debrisVersion;
576
588
  const pieces = terrain.debris();
577
589
  let count = 0;
578
590
  for (const piece of pieces) {
@@ -812,24 +824,43 @@ function barCapacity(count) {
812
824
  while (capacity < count) capacity *= 2;
813
825
  return capacity;
814
826
  }
827
+ var FACES = [
828
+ [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
829
+ [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
830
+ [[0, 1, 5, 3, 1, 5, 3, 4, 5, 0, 4, 5], [0, 0, 1]],
831
+ [[3, 1, 2, 0, 1, 2, 0, 4, 2, 3, 4, 2], [0, 0, -1]],
832
+ [[3, 1, 5, 3, 1, 2, 3, 4, 2, 3, 4, 5], [1, 0, 0]],
833
+ [[0, 1, 2, 0, 1, 5, 0, 4, 5, 0, 4, 2], [-1, 0, 0]]
834
+ ];
835
+ var BOX = 6;
836
+ var barsByProfile = /* @__PURE__ */ new WeakMap();
837
+ var barsOf = (profile) => {
838
+ const known = barsByProfile.get(profile);
839
+ if (known) return known;
840
+ let top = 0;
841
+ let depth = Number.NaN;
842
+ let spacing = Number.NaN;
843
+ for (const stratum of profile.strata) {
844
+ if (stratum.reinforcement && Number.isNaN(depth)) {
845
+ depth = top + stratum.reinforcement.cover;
846
+ spacing = stratum.reinforcement.spacing;
847
+ }
848
+ top += stratum.thickness;
849
+ }
850
+ const bars = { depth, spacing, limit: top };
851
+ barsByProfile.set(profile, bars);
852
+ return bars;
853
+ };
854
+ var boxes = [];
815
855
  function buildRebarGeometry(THREE, terrain, chunk, reuse) {
816
856
  const [x0, z0] = terrain.bounds;
817
857
  const { cellSize, columns, rows, stride } = terrain;
818
858
  const lastColumn = Math.min(columns, chunk.column + chunk.cells);
819
859
  const lastRow = Math.min(rows, chunk.row + chunk.cells);
820
- const boxes = [];
821
- const coverOf = (index) => {
822
- let top = 0;
823
- for (const stratum of terrain.profileList[terrain.profileIndex[index]].strata) {
824
- if (stratum.reinforcement) return { depth: top + stratum.reinforcement.cover, spacing: stratum.reinforcement.spacing };
825
- top += stratum.thickness;
826
- }
827
- return null;
828
- };
860
+ boxes.length = 0;
829
861
  const showing = (index, depth) => {
830
- const profile = terrain.profileList[terrain.profileIndex[index]];
831
- const limit = profile.strata.reduce((sum, stratum) => sum + stratum.thickness, 0);
832
- return terrain.dug[index] > depth + EXPOSED_MARGIN && terrain.dug[index] <= limit + EXPOSED_MARGIN;
862
+ const dug = terrain.dug[index];
863
+ return dug > depth + EXPOSED_MARGIN && dug <= barsOf(terrain.profileList[terrain.profileIndex[index]]).limit + EXPOSED_MARGIN;
833
864
  };
834
865
  const onBar = (coordinate, spacing) => Math.floor(coordinate / spacing) !== Math.floor((coordinate + cellSize) / spacing);
835
866
  const addBox = (ax, az, bx, bz, y) => {
@@ -838,44 +869,43 @@ function buildRebarGeometry(THREE, terrain, chunk, reuse) {
838
869
  for (let row = chunk.row; row <= lastRow; row++) {
839
870
  for (let column = chunk.column; column <= lastColumn; column++) {
840
871
  const index = row * stride + column;
841
- const cover = coverOf(index);
842
- if (!cover || !showing(index, cover.depth)) continue;
872
+ const { depth, spacing } = barsOf(terrain.profileList[terrain.profileIndex[index]]);
873
+ if (Number.isNaN(depth) || !showing(index, depth)) continue;
843
874
  const x = x0 + column * cellSize;
844
875
  const z = z0 + row * cellSize;
845
- const y = -cover.depth - BAR_RADIUS - Math.min(1, terrain.barBend[index]) * MAX_SAG;
846
- if (onBar(z, cover.spacing) && column < lastColumn && showing(index + 1, cover.depth)) addBox(x, z, x + cellSize, z, y);
847
- if (onBar(x, cover.spacing) && row < lastRow && showing(index + stride, cover.depth)) addBox(x, z, x, z + cellSize, y);
876
+ const y = -depth - BAR_RADIUS - Math.min(1, terrain.barBend[index]) * MAX_SAG;
877
+ if (onBar(z, spacing) && column < lastColumn && showing(index + 1, depth)) addBox(x, z, x + cellSize, z, y);
878
+ if (onBar(x, spacing) && row < lastRow && showing(index + stride, depth)) addBox(x, z, x, z + cellSize, y);
848
879
  }
849
880
  }
850
881
  if (boxes.length === 0) return null;
851
- const count = boxes.length / 6;
882
+ const count = boxes.length / BOX;
852
883
  const target = reuse && reuse.getAttribute("position").count >= count * 24 ? reuse : void 0;
853
884
  const capacity = target ? target.getAttribute("position").count / 24 : barCapacity(count);
854
885
  const positions = target ? target.getAttribute("position").array : new Float32Array(capacity * 24 * 3);
855
886
  const normals = target ? target.getAttribute("normal").array : new Float32Array(capacity * 24 * 3);
856
887
  const indices = target ? target.getIndex().array : new Uint32Array(capacity * 36);
857
- const FACES = [
858
- [[0, 4, 2, 0, 4, 5, 3, 4, 5, 3, 4, 2], [0, 1, 0]],
859
- [[0, 1, 5, 0, 1, 2, 3, 1, 2, 3, 1, 5], [0, -1, 0]],
860
- [[0, 1, 5, 3, 1, 5, 3, 4, 5, 0, 4, 5], [0, 0, 1]],
861
- [[3, 1, 2, 0, 1, 2, 0, 4, 2, 3, 4, 2], [0, 0, -1]],
862
- [[3, 1, 5, 3, 1, 2, 3, 4, 2, 3, 4, 5], [1, 0, 0]],
863
- [[0, 1, 2, 0, 1, 5, 0, 4, 5, 0, 4, 2], [-1, 0, 0]]
864
- ];
865
888
  let vertex = 0;
866
889
  let cursor = 0;
867
890
  for (let n = 0; n < count; n++) {
868
- const box = boxes.slice(n * 6, n * 6 + 6);
891
+ const at = n * BOX;
869
892
  for (const [corners, normal] of FACES) {
870
893
  const first = vertex;
871
894
  for (let c = 0; c < 4; c++) {
872
- positions[vertex * 3] = box[corners[c * 3]];
873
- positions[vertex * 3 + 1] = box[corners[c * 3 + 1]];
874
- positions[vertex * 3 + 2] = box[corners[c * 3 + 2]];
875
- normals.set(normal, vertex * 3);
895
+ positions[vertex * 3] = boxes[at + corners[c * 3]];
896
+ positions[vertex * 3 + 1] = boxes[at + corners[c * 3 + 1]];
897
+ positions[vertex * 3 + 2] = boxes[at + corners[c * 3 + 2]];
898
+ normals[vertex * 3] = normal[0];
899
+ normals[vertex * 3 + 1] = normal[1];
900
+ normals[vertex * 3 + 2] = normal[2];
876
901
  vertex++;
877
902
  }
878
- indices.set([first, first + 1, first + 2, first, first + 2, first + 3], cursor);
903
+ indices[cursor] = first;
904
+ indices[cursor + 1] = first + 1;
905
+ indices[cursor + 2] = first + 2;
906
+ indices[cursor + 3] = first;
907
+ indices[cursor + 4] = first + 2;
908
+ indices[cursor + 5] = first + 3;
879
909
  cursor += 6;
880
910
  }
881
911
  }