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

时间:2021-06-27 22:58:55
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 );   

}

I am trying to calculate bounding box of 3d json models. Right now I'm just using a sphere. When I try to get bounding box it says null. How can i get bounding box so that even if I load complex model it will calculate bounding box of whole model.

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

1 个解决方案

#1


0  

Okay I wrote this method to get maximum and minimum values. I will try to work with this for now. However I would appreciate if anybody would answer why boundingBox is not working.

好吧,我写了这个方法来获得最大值和最小值。我现在会尝试使用它。但是,如果有人会回答为什么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);
    }
}

#1


0  

Okay I wrote this method to get maximum and minimum values. I will try to work with this for now. However I would appreciate if anybody would answer why boundingBox is not working.

好吧,我写了这个方法来获得最大值和最小值。我现在会尝试使用它。但是,如果有人会回答为什么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);
    }
}