Skip to content

Instantly share code, notes, and snippets.

@Daniel-Wiedemann
Created September 28, 2014 21:04
Show Gist options
  • Save Daniel-Wiedemann/11b13235be38e3750c4d to your computer and use it in GitHub Desktop.
Save Daniel-Wiedemann/11b13235be38e3750c4d to your computer and use it in GitHub Desktop.
Compute Boundingbox from a group of Objects
/**
* Calculates the bounding box of grouped 3d Objects, with Objects that contains geometries
* @param {Object3d} objectgroup A Object3d that contains other Object3d objects like Mesh.
* @return {Object} Returns a Object with two parameter min{THREE.Vector3} and max{THREE.Vector3}
*/
function computeBoundingBoxGroupObjects(objectgroup){
var minX = 0, minY = 0, minZ = 0, maxX = 0, maxY = 0, maxZ = 0;
function getBox(item){
if(item.geometry){
item.geometry.computeBoundingBox();
minX = Math.min(minX,item.geometry.boundingBox.min.x);
maxX = Math.max(maxX,item.geometry.boundingBox.max.x);
minY = Math.min(minY,item.geometry.boundingBox.min.y);
maxY = Math.max(maxY,item.geometry.boundingBox.max.y);
minZ = Math.min(minZ,item.geometry.boundingBox.min.z);
maxZ = Math.max(maxZ,item.geometry.boundingBox.max.z);
}
}
if( objectgroup instanceof THREE.Object3D){
objectgroup.traverse(function(_item){
getBox(_item);
});
}
if( typeof objectgroup === 'object' && typeof objectgroup.slice === 'function'){
for(var i = 0; i < objectgroup.length; i++){
getBox(objectgroup[i]);
}
}
return {
min: new THREE.Vector3(minX, minY, minZ),
max: new THREE.Vector3(maxX, maxY, maxZ)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment