我们如何在threejs中计算对象的边界框

时间:2015-11-04 06:29:15

标签: javascript 3d three.js

function loadModel(){
    switchCamera("orthographic");
    var modelLoader = new THREE.JSONLoader();
    modelLoader.load("models/sphere.json", callBack);

}

function callBack(object3D_geometry){ 
    var material = new THREE.MeshLambertMaterial( { color:0x000000 } );
    var model = new THREE.Mesh( object3D_geometry, material);
    scene.add(model);

    console.log(object3D_geometry);
    //console.log(object3D_geometry.boundingBox); 
    //model.geometry.computeBoundingBox;
    modelBoundingBox = model.geometry.boundingBox;

    console.log("model Bounding box");
    console.log(modelBoundingBox);

    renderer.render( scene, camera );   

}

我正在尝试计算3d json模型的边界框。现在我只是使用一个球体。当我试图获得边界框时,它表示null。我怎么能得到边界框,这样即使我加载复杂模型,它也会计算出整个模型的边界框。

1 个答案:

答案 0 :(得分:0)

好的,我写了这个方法来获取最大值和最小值。我现在会尝试使用它。但是如果有人会回答为什么boundingBox无法正常工作,我将不胜感激。

function calculate_XYZ_Max_Min(modelVertices){
    var min_X = modelVertices[0].x;
    var max_X = modelVertices[0].x;
    var min_Y = modelVertices[0].y;
    var max_Y = modelVertices[0].y;
    var min_Z = modelVertices[0].z;
    var max_Z = modelVertices[0].z;


    for (var i=1; i< modelVertices.length; i++){

        if (modelVertices[i].x > max_X)     max_X = modelVertices[i].x;
        if (modelVertices[i].x < min_X)     min_X = modelVertices[i].x;
        if (modelVertices[i].y > max_Y)     max_Y = modelVertices[i].y;
        if (modelVertices[i].y < min_Y)     min_Y = modelVertices[i].y;
        if (modelVertices[i].z > max_Z)     max_Z = modelVertices[i].z;
        if (modelVertices[i].z < min_Z)     min_Z = modelVertices[i].z;
    }

    return{
        xMin: Math.floor(min_X);
        xMax: Math.ceil(max_X); 
        yMin: Math.floor(min_Y);
        yMax: Math.ceil(max_Y);
        zMin: Math.floor(min_Z);
        zMax: Math.ceil(max_Z);
    }
}