Skip to content

Commit

Permalink
Reuse one array instead of creating many
Browse files Browse the repository at this point in the history
  • Loading branch information
Elettrotecnica committed Apr 24, 2024
1 parent 97d2a61 commit bdc0d89
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ const _finishCollisionShape = function(collisionShape, options, scale) {

export const iterateGeometries = (function() {
const inverse = new THREE.Matrix4();
const vertices = [];
return function(root, options, cb) {
inverse.copy(root.matrixWorld).invert();
const scale = new THREE.Vector3();
Expand All @@ -689,7 +690,7 @@ export const iterateGeometries = (function() {
// todo: might want to return null xform if this is the root so that callers can avoid multiplying
// things by the identity matrix

let vertices;
let unInterleavedVertices;
if (mesh.geometry.isBufferGeometry) {
const verticesAttribute = mesh.geometry.attributes.position;
if (verticesAttribute.isInterleavedBufferAttribute) {
Expand All @@ -699,21 +700,22 @@ export const iterateGeometries = (function() {
// regular array here to not carry this logic around in
// the shape api.
//
vertices = [];
vertices.length = 0;
for (let i = 0; i < verticesAttribute.count; i += 3) {
vertices.push(verticesAttribute.getX(i));
vertices.push(verticesAttribute.getY(i));
vertices.push(verticesAttribute.getZ(i));
}
unInterleavedVertices = vertices;
} else {
vertices = verticesAttribute.array;
unInterleavedVertices = verticesAttribute.array;
}
} else {
vertices = mesh.geometry.vertices;
unInterleavedVertices = mesh.geometry.vertices;
}

cb(
vertices,
unInterleavedVertices,
transform.elements,
mesh.geometry.index ? mesh.geometry.index.array : null
);
Expand Down

0 comments on commit bdc0d89

Please # to comment.