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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With