Search code examples
javascriptthree.jsbounding-box

three.js: bounding box strange behaviour


I am using three.js and I noticed something that does not work as I would expect. In my application I defined a cube and its bounding box:

var scene = new THREE.Scene();
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshPhongMaterial({color: 0xbaf5e8, flatShading: true});
var cube = new THREE.Mesh( geometry, material );
cube.receiveShadow = true;
scene.add(cube);
var helper_bbox = new THREE.BoxHelper(cube);
helper_bbox.update();
scene.add(helper_bbox);

When I tried to access the computed bounding box (helper_bbox.geometry.boundingBox) I noticed that its value is null, although it is perfectly rendered in the screen. Nevertheless, the bounding sphere (helper_bbox.geometry.boundingSphere) is accesible.

I cannot figure out why is this happening. Does anyone have any idea on this? Is there any method that I should call explicitly to retrieve the coordinates of the points of the bounding box? Thanks.


Solution

  • Geometry bounding boxes are not computed automatically, unless they are required for another internal method. You can force the box to be computed -- and inspect it -- like so:

    object.geometry.computeBoundingBox();
    
    console.log( object.geometry.boundingBox );
    

    three.js r.87