Search code examples
javascriptthree.jsaframewebvr

How to get bounding box information from a 3D object in aframe?


I am working on an aframe project that involves loading 3D objects of unknown sizes into my scene. Naturally I would want to resize the object to a certain size (like fixed height) before I put it in the scene. But how do I extract information like width, height and depth from the object's bounding box?


Solution

  • You'll need to use A-Frame's underlying three.js APIs here. That answer has been posted for three.js before, but here's an A-Frame version:

    // get three.js object from aframe entity
    var el = document.querySelector('#my-element');
    var object = el.getObject3D('mesh');
    
    // compute bounding box
    var bbox = new THREE.Box3().setFromObject(obj);
    console.log(bbox.min, bbox.max)