简体   繁体   English

Three.js:如何返回对象的大小?

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

Does anyone know how to return the size of a Three.js object? 有谁知道如何返回Three.js对象的大小? 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 three.js r.64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM