Skip to content

Commit

Permalink
Hopefully avoids more GC churn by giving BJS typed arrays (which shou…
Browse files Browse the repository at this point in the history
…ldn't be copied?)
  • Loading branch information
fenomas committed Apr 28, 2023
1 parent d71247c commit 5bdb9e4
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/lib/terrainMesher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5bdb9e4

Please sign in to comment.