【问题标题】:Bounding Box of a stl object in three.jsthree.js 中 stl 对象的边界框
【发布时间】:2015-10-26 21:24:23
【问题描述】:

大家好,

我遇到过几篇关于此主题的帖子,但没有任何解决方案可行。我有一个在 STLLoader 的帮助下在 three.js 中加载的对象,我想为其获取一个边界框。

    // Add stl objects and a name
function addSTLObject(url, name) {
    var loader = new THREE.STLLoader();
    loader.load(url, function (geometry) {

        var material = new THREE.MeshPhongMaterial({ color: 0xff5533 });
        var mesh = new THREE.Mesh(geometry, material);
        // To scale element, use:
        // mesh.scale.set(0.01, 0.01, 0.01);
        // Add a name to the object to find it if it needs to be removed
        mesh.name = name;
        mesh.position.x = 0;
        mesh.position.y = 0;
        mesh.position.z = 0;

        scene.add(mesh);
    });
}

我正在按如下方式加载这个对象:

addSTLObject('model/cases/iphone5.stl', 'phone-model');
var phoneModelAdded = scene.getObjectByName('phone-model', true);

现在我已经尝试了这里提供的解决方案:https://github.com/mrdoob/three.js/issues/3471 和这里Any way to get a bounding box from a three.js Object3D?

var bbox = new THREE.Box3().setFromObject(phoneModelAdded);

var geometry = phoneModel.children[0].children[0].geometry;
geometry.computeBoundingBox();

虽然第一个解决方案给我一个错误说“无法读取未定义的属性'updateMatrixWorld'”,但第二个解决方案根本没有给我任何错误,但什么也不做,如果我尝试访问“几何”属性,它说它没有'不存在。

有人有可行的解决方案吗? 任何帮助表示赞赏。

祝你有美好的一天!

编辑

        // Add stl objects and a name
function addSTLObject(url, name) {
    var bbox;
    var loader = new THREE.STLLoader();
    loader.load(url, function (geometry) {

        var material = new THREE.MeshPhongMaterial({ color: 0xff5533 });
        var mesh = new THREE.Mesh(geometry, material);
        // To scale element, use:
        // mesh.scale.set(0.01, 0.01, 0.01);
        // Add a name to the object to find it if it needs to be removed
        mesh.name = name;
        mesh.position.x = 0;
        mesh.position.y = 0;
        mesh.position.z = 0;

        bbox = new THREE.Box3().setFromObject(mesh);

        scene.add(mesh);

        return bbox;
    });
}

以后

var bbox = addSTLObject('model/cases/iphone5.stl', 'phone-model');
    scene.add(bbox);

错误:“THREE.Object3D.add:对象不是 THREE.Object3D 的实例”

编辑 2:

var bbox, bboxComputed = false;

    function addSTLObject(url, name) {
    var bbox;
    var loader = new THREE.STLLoader();
    loader.load(url, function (geometry) {

        var material = new THREE.MeshPhongMaterial({ color: 0xff5533 });
        var mesh = new THREE.Mesh(geometry, material);
        // To scale element, use:
        // mesh.scale.set(0.01, 0.01, 0.01);
        // Add a name to the object to find it if it needs to be removed
        mesh.name = name;
        mesh.position.x = 0;
        mesh.position.y = 0;
        mesh.position.z = 0;

        bbox = new THREE.BoundingBoxHelper(mesh);
        bbox.update();
        bboxComputed = true;

        scene.add(mesh);
    });

}

addSTLObject('model/cases/iphone5.stl', 'phone-model');
    var myInterval = setInterval( function(){
        if( bboxComputed ) {
            alert( bbox.box.min, bbox.box.max, bbox.box.size() );
            scene.add(bbox);
            clearInterval( myInterval );
            bboxComputed = false;
        }
    }, 100 );

这行不通。

编辑 3: 我试图建立一个函数,它拥有我需要的一切,并返回一个包含所有计算信息的对象:

    function calculateSTLProperties(url, name) {
    var STLObject, STLbbox, STLComputed = false, STLGeometry;

    var loader = new THREE.STLLoader();
    loader.load(url, function (geometry) {

        var material = new THREE.MeshPhongMaterial({ color: 0xff5533 });
        var mesh = new THREE.Mesh(geometry, material);
        // To scale element, use:
        // mesh.scale.set(0.01, 0.01, 0.01);
        // Add a name to the object to find it if it needs to be removed
        mesh.name = name;
        mesh.position.x = 0;
        mesh.position.y = 0;
        mesh.position.z = 0;

        // Compute a bounding box for the element
        STLbbox = new THREE.BoundingBoxHelper(mesh);
        STLbbox.update();

        // Get the geometry of the case
        STLGeometry = geometry;
        STLComputed = true;
    });

    // Set an interval to wait for the corresponding bounding box and geometry to be computed
    var myInterval = setInterval( function(){
        if( STLComputed ) {
        STLObject = {
            "geometry" : STLGeometry,
            "bbox" : STLbbox,
            "x" : STLbbox.box.size().x,
            "y" : STLbbox.box.size().y,
            "z" : STLbbox.box.size().z
        };

        clearInterval( myInterval );
        bboxComputed = false;

        }
    }, 100 );

    return STLObject;
}

不幸的是,不知何故该对象没有通过,我在尝试保存它时最终得到一个“未定义”:

var STLObjectLoaded = calculateSTLProperties('model/cases/iphone5.stl', 'phone-model');
    console.log(STLObjectLoaded);

我错过了什么?

【问题讨论】:

  • 在调用scene.getObjectByName()之前怎么知道对象已经实际加载了。加载是异步的。

标签: javascript three.js bounding-box


【解决方案1】:

模型加载是异步的,因此所有计算都应该在模型加载后进行。添加一个回调,它在模型加载时调用,并在那里添加对 bbox 的调用。您使用scene.getObjectByName() 调用的方式是使用您还验证的空对象进行的(“如果我尝试访问“几何”属性,它会说它不存在”)

更新:

用途:

var bbox = new THREE.BoundingBoxHelper( mesh ); bbox.update();
scene.add( bbox );

loader.load() 函数内部。

setFromObject() 调用仅创建边界框,但不创建边界框的几何图形。

更新二

如果您希望 bbox 在您的 loader.load() 函数之外可用,则需要使用全局变量

var bboxComputed = false;

在计算 loader.load() 函数中的 bbox 后立即设置为 true:

scene.add( bbox );
bboxComputed = true;

然后在你的主要而不是使用:

console.log( bbox.box.min, bbox.box.max, bbox.box.size() );

你会使用类似的东西:

var myInterval = setInterval( function(){
    if( bboxComputed ) {
        console.log( bbox.box.min, bbox.box.max, bbox.box.size() );
        clearInterval( myInterval );
    }
}, 100 );

我设置了 0.1 秒的延迟,当最终计算出 bbox 时,我清除了间隔,这样它就不会永远运行。

【讨论】:

  • 不确定你的意思。我调用我的函数,调用.getObjectByName(),然后调用var bbox = new THREE.Box3().setFromObject(phoneModelAdded);。所以这应该是正确的顺序,不是吗?如何添加“回调”?
  • 是的,但在 loader.load() 回调中调用它们。
  • 我用我所做的更改以及由此产生的错误更改了我的帖子。请帮忙!
  • return bboxloader.load() 返回,而不是从 addSTLObject() 返回
  • 不幸的是还是同样的错误。也许看看我的设置会清除一些事情:sebastianjvf.github.io/index2.html
【解决方案2】:

从 three.js R125 开始,推荐的方法是使用 loadAsync 方法,该方法现在是 three.js 的原生方法:

https://threejs.org/docs/#api/en/loaders/Loader.loadAsync

该方法返回一个承诺。然后,您可以使用“then”来获取 STL 的几何形状并创建网格。您也可以像之前的答案一样使用传统的回调,或者使用 async/await 结构,但我认为下面使用本机 three.js 方法的示例是最简单的方法。该示例展示了如何在 promise 被解析并加载 STL 文件后将几何图形获取到全局变量:

// Global variables for bounding boxes
let bbox;

const loader = new STLLoader();
const promise = loader.loadAsync('model1.stl');
promise.then(function ( geometry ) {
  const material = new THREE.MeshPhongMaterial();
  const mesh = new THREE.Mesh( geometry, material );
  mesh.geometry.computeBoundingBox();
  bbox = mesh.geometry.boundingBox;
  scene.add( mesh );
  buildScene();
  console.log('STL file loaded!');
}).catch(failureCallback);

function failureCallback(){
  console.log('Could not load STL file!');
}

function buildScene() {
  console.log('STL file is loaded, so now build the scene');
  // !VA bounding box of the STL mesh accessible now
  console.log(bbox);
  // Build the rest of your scene...
}

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2013-04-16
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多