1

在使用 OBJLoader 加载模型后,我在尝试获取模型几何图形的边界框时遇到了问题。到目前为止,我有:

var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {

for ( var i = 0, l = object.children.length; i < l; i ++ ) {
    geometry = object.children[0].geometry;         
    bBox = geometry.computeBoundingBox();
    console.log("have a box of "+bBox);
}
//...rest of function

但是当我将 bBox 写入控制台时,它是未定义的。在这种情况下,几何不是访问的正确属性吗?

4

4 回答 4

4

我认为这样的事情应该可以解决问题:

var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {

    THREE.SceneUtils.traverseHierarchy( object, function ( child ) {

        if ( child instanceof THREE.Mesh ) {

            child.geometry.computeBoundingBox();

        }

    }

}
于 2012-08-03T01:40:56.800 回答
3

这个答案结合了最新版本 THREE.js 的最后一个

var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
object.traverse(function ( child ) {
    if ( child instanceof THREE.Mesh ) {
        child.geometry.computeBoundingBox();
        object.bBox = child.geometry.boundingBox;//<-- Actually get the variable
    }
});
};

现在,您可以通过键入 object.bBox.max.x 来访问网格的尺寸,例如获取 x。

于 2014-02-20T20:22:24.997 回答
0

@mrdoob's answer was correct but due to three.js API changes (as of r52), it should look like this now:

var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
    object.traverse(function ( child ) {
        if ( child instanceof THREE.Mesh ) {
            child.geometry.computeBoundingBox();
        }
    });
};
于 2013-06-27T21:19:44.150 回答
0

基于 mrdoob 的 traverse 函数,我能够得到一个具有以下内容的边界框:

var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
    THREE.SceneUtils.traverseHierarchy(object, function(child){
    if(child instanceof THREE.Mesh){
        for (var i in child) {
            if(i == "geometry"){
                var geo = child[i];
                    geo.computeBoundingBox();
                    var bBox = geo.boundingBox;
                }
             }
         }
      });
      ///rest of loading function here
 });
于 2012-08-03T03:51:52.563 回答