@adatechnology/game-terrain-three 0.6.14 → 0.6.16

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;