From 155d01db5b5aba80ec23699ec6a5c4ff23237ebe Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 8 Jan 2022 16:39:28 -0500 Subject: [PATCH] use bounds caching to speed up rotate & scale operations --- src/mesh/api.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/mesh/api.js b/src/mesh/api.js index 524a7903..2da7eedf 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -298,6 +298,7 @@ let api = mesh.api = { }; let deferFn = []; +let boundsCache = {}; let util = mesh.util = { @@ -381,9 +382,19 @@ let util = mesh.util = { if (geometry) { let matrix = object.matrixWorld; - let position = geometry.attributes.position.clone(); - position.applyMatrix4(new THREE.Matrix4().extractRotation(matrix)); - let bounds = new THREE.Box3().setFromBufferAttribute(position); + let bkey = [matrix.elements.map(v => v.round(5))].join(',') + let cached = boundsCache[object.id]; + let bounds; + if (!cached || cached.bkey !== bkey) { + let position = geometry.attributes.position.clone(); + position.applyMatrix4(new THREE.Matrix4().extractRotation(matrix)); + bounds = new THREE.Box3().setFromBufferAttribute(position); + cached = boundsCache[object.id] = { + bkey, + bounds + }; + } + bounds = cached.bounds.clone(); let bt = new THREE.Box3().copy(bounds); let m4 = new THREE.Matrix4(); m4.setPosition(new THREE.Vector3().setFromMatrixPosition(object.matrixWorld));