use bounds caching to speed up rotate & scale operations

This commit is contained in:
Stewart Allen 2022-01-08 16:39:28 -05:00
commit 155d01db5b

View file

@ -298,6 +298,7 @@ let api = mesh.api = {
}; };
let deferFn = []; let deferFn = [];
let boundsCache = {};
let util = mesh.util = { let util = mesh.util = {
@ -381,9 +382,19 @@ let util = mesh.util = {
if (geometry) { if (geometry) {
let matrix = object.matrixWorld; let matrix = object.matrixWorld;
let position = geometry.attributes.position.clone(); let bkey = [matrix.elements.map(v => v.round(5))].join(',')
position.applyMatrix4(new THREE.Matrix4().extractRotation(matrix)); let cached = boundsCache[object.id];
let bounds = new THREE.Box3().setFromBufferAttribute(position); 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 bt = new THREE.Box3().copy(bounds);
let m4 = new THREE.Matrix4(); let m4 = new THREE.Matrix4();
m4.setPosition(new THREE.Vector3().setFromMatrixPosition(object.matrixWorld)); m4.setPosition(new THREE.Vector3().setFromMatrixPosition(object.matrixWorld));