From 5bdb9e405fa035e949a1381c5db8c51e912580b3 Mon Sep 17 00:00:00 2001 From: andyhall Date: Fri, 28 Apr 2023 14:50:28 +0900 Subject: [PATCH] Hopefully avoids more GC churn by giving BJS typed arrays (which shouldn't be copied?) --- src/lib/terrainMesher.js | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/lib/terrainMesher.js b/src/lib/terrainMesher.js index a04f3d9..fa6d66d 100644 --- a/src/lib/terrainMesher.js +++ b/src/lib/terrainMesher.js @@ -594,12 +594,6 @@ var faceDataPool = (() => { /** @param {import('../index').Engine} noa */ function MeshBuilder(noa, terrainMatManager) { - var cache_arr_positions = [] - var cache_arr_normals = [] - var cache_arr_colors = [] - var cache_arr_uvs = [] - var cache_arr_atlasIndexes = [] - /** * Consume the intermediate FaceData struct and produce * actual mesehes the 3D engine can render @@ -634,22 +628,14 @@ function MeshBuilder(noa, terrainMatManager) { } // build the necessary arrays - var indices = [] - var positions = cache_arr_positions - var normals = cache_arr_normals - var colors = cache_arr_colors - var uvs = cache_arr_uvs - var atlasIndexes = cache_arr_atlasIndexes - - // trim size of reused arrays if needed var nf = faceData.numFaces - if (positions.length > nf * 12) { - positions.length = nf * 12 - normals.length = nf * 12 - colors.length = nf * 16 - uvs.length = nf * 8 - if (usesAtlas) atlasIndexes.length = nf * 4 - } + var indices = new Uint16Array(nf * 6) + var positions = new Float32Array(nf * 12) + var normals = new Float32Array(nf * 12) + var colors = new Float32Array(nf * 16) + var uvs = new Float32Array(nf * 8) + var atlasIndexes + if (usesAtlas) atlasIndexes = new Float32Array(nf * 4) // scan all faces in the struct, creating data for each for (var f = 0; f < faceData.numFaces; f++) { @@ -700,6 +686,11 @@ function MeshBuilder(noa, terrainMatManager) { var mesh = new Mesh(name, scene) var vdat = new VertexData() + // clobber babylon's bounding internals + mesh.doNotSyncBoundingInfo = true + mesh._updateBoundingInfo = () => mesh + mesh._refreshBoundingInfo = () => mesh + vdat.positions = positions vdat.indices = indices vdat.normals = normals