Every shape possesses a boundingBox
property within its geometry object. This property contains a THREE.Box3
object, which is made up of two THREE.Vector3
objects, namely min
and max
.
let geometry = new THREE.CylinderGeometry(...);
let material = new THREE.LineBasicMaterial(...);
let mesh = new THREE.Mesh(geometry, material);
let boundingBox = mesh.geometry.boundingBox.clone();
alert('bounding box coordinates: (' + boundingBox.min.x + ', ' + boundingBox.min.y + ', ' + boundingBox.min.z + '), (' + boundingBox.max.x + ', ' + boundingBox.max.y + ', ' + boundingBox.max.z + ')');
When dealing with more intricate shapes, such as those imported from JSON Object files, the bounding box property is not defined by default. It needs to be explicitly calculated.
let loader = new THREE.ObjectLoader();
loader.load(imagePath, function(object) {
geometry = object.children[0].children[0].geometry; // Replace this with the path to your geometry
geometry.computeBoundingBox(); // Otherwise, geometry.boundingBox will remain undefined
let boundingBox = geometry.boundingBox.clone();
alert('bounding box coordinates: (' + boundingBox.min.x + ', ' + boundingBox.min.y + ', ' + boundingBox.min.z + '), (' + boundingBox.max.x + ', ' + boundingBox.max.y + ', ' + boundingBox.max.z + ')');
}