Accessing the bounding box is a pretty straightforward thing to do from a standard Three.js model:
var bb = mesh.geometry.boundingBox
However optimisations in our custom renderer makes it a little bit less intuitive. Below are two snippets that illustrate how to compute bounding box for the selected component. Typically when you select a component visually, the Autodesk.Viewing.SELECTION_CHANGED_EVENT returns an object that contains the list of fragmentIds, which you need to pass to those methods (see full sample further down):
1 //returns bounding box as it appears in the viewer 2 // (transformations could be applied) 3 function getModifiedWorldBoundingBox(fragIds, fragList) { 4 5 var fragbBox = new THREE.Box3(); 6 var nodebBox = new THREE.Box3(); 7 8 fragIds.forEach(function(fragId) { 9 10 fragList.getWorldBounds(fragId, fragbBox); 11 nodebBox.union(fragbBox); 12 }); 13 14 return nodebBox; 15 } 16 17 // Returns bounding box as loaded in the file 18 // (no explosion nor transformation) 19 function getOriginalWorldBoundingBox(fragIds, fragList) { 20 21 var fragBoundingBox = new THREE.Box3(); 22 var nodeBoundingBox = new THREE.Box3(); 23 24 var fragmentBoxes = fragList.boxes; 25 26 fragIds.forEach(function(fragId) { 27 28 var boffset = fragId * 6; 29 30 fragBoundingBox.min.x = fragmentBoxes[boffset]; 31 fragBoundingBox.min.y = fragmentBoxes[boffset+1]; 32 fragBoundingBox.min.z = fragmentBoxes[boffset+2]; 33 fragBoundingBox.max.x = fragmentBoxes[boffset+3]; 34 fragBoundingBox.max.y = fragmentBoxes[boffset+4]; 35 fragBoundingBox.max.z = fragmentBoxes[boffset+5]; 36 37 nodeBoundingBox.union(fragBoundingBox); 38 }); 39 40 return nodeBoundingBox; 41 }Here is the result of the extension code provided below. You can test a live version here:
Comments