Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js: how can I return the size of an object?

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

like image 375
jligda Avatar asked Feb 15 '23 06:02

jligda


1 Answers

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

like image 92
WestLangley Avatar answered Feb 17 '23 01:02

WestLangley