5

Does anyone know how to return the size of a Three.js object? What I'm doing is loading objects from a file. What I want to do is be able to detect the size of the object and set the scale ... or camera position accordingly so the entire object fits in the frame. Since I will be loading different files and objects I will need to detect the size:

Here is some code:

    var group;

    var loader = new THREE.STLLoader();
    var group = new THREE.Object3D();
    loader.load("stl_file.stl", function (geometry) {
        console.log(geometry);
        var mat = new THREE.MeshLambertMaterial({color: 0x999999});
        group = new THREE.Mesh(geometry, mat);

        group.scale.set(0.02, 0.02, 0.02);
        scene.add(group); 

    });

So somewhere in this code I'd want to try to detect the size of the object in the file before setting the scale. Or, if that can't be done I can adjust the camera postition later in the script. What do you think?

Jay

jligda
  • 180
  • 3
  • 18

1 Answers1

10

You can use either of two approaches:

geometry.computeBoundingBox();
var box = geometry.boundingBox;

or

var box = new THREE.Box3().setFromObject( object );

The second one is appropriate for an object that has child objects.

three.js r.64

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thanks! And I was able to extract the values with the following: var sceneWidthMax = box.max.x; var sceneHeightMax = box.max.y; var sceneDepthMax = box.max.z;var sceneWidthMin = box.min.x; var sceneHeightMin = box.min.y; var sceneDepthMim = box.min.z; But it is the Max values that I need. Now to figure how to use this information to set the scale. I'll post the answer once I find it. – jligda Jan 01 '14 at 16:36
  • See http://stackoverflow.com/questions/14614252/how-to-fit-camera-to-object/14614736#14614736 – WestLangley Jan 01 '14 at 22:09