@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
42 lines (41 loc) • 1.05 kB
JavaScript
;
import { Box3, Vector3 } from "three";
function traverseAndInitBoundingBox(object) {
let bbox;
object.traverse((childObject) => {
if (!bbox) {
const geometry = childObject.geometry;
if (geometry) {
childObject.updateWorldMatrix(true, false);
geometry.computeBoundingBox();
if (geometry.boundingBox) {
bbox = geometry.boundingBox.clone();
bbox.applyMatrix4(childObject.matrixWorld);
}
}
}
});
return bbox;
}
export function computeBoundingBoxFromObject3D(object) {
const bbox = traverseAndInitBoundingBox(object);
if (bbox) {
bbox.expandByObject(object);
}
return bbox;
}
export function computeBoundingBoxFromObject3Ds(objects) {
let bbox;
for (const object of objects) {
computeBoundingBoxFromObject3D(object);
}
if (bbox) {
for (const object of objects) {
if (bbox) {
bbox.expandByObject(object);
}
}
}
bbox = bbox || new Box3(new Vector3(-1, -1, -1), new Vector3(1, 1, 1));
return bbox;
}