无法获得加载json模型的位置

时间:2013-05-28 16:33:44

标签: three.js webgl

这是一个简单矩形框的json文件示例。

{

    "metadata" :
    {
        "formatVersion" : 3.1,
        "sourceFile"    : "BOX.obj",
        "generatedBy"   : "OBJConverter",
        "vertices"      : 8,
        "faces"         : 6,
        "normals"       : 6,
        "colors"        : 0,
        "uvs"           : 4,
        "materials"     : 1
    },

    "scale" : 1.000000,

    "materials": [  {
    "DbgColor" : 15658734,
    "DbgIndex" : 0,
    "DbgName" : "wire_135110008",
    "colorAmbient" : [0.0, 0.0, 0.0],
    "colorDiffuse" : [0.5294, 0.4314, 0.0314],
    "colorSpecular" : [0.35, 0.35, 0.35],
    "illumination" : 2,
    "specularCoef" : 32.0,
    "transparency" : 1.0
    }],

    "vertices": [-0.069100,0.041400,0.069200,-0.069100,0.041400,0.040600,-0.028100,0.041400,0.040600,-0.028100,0.041400,0.069200,-0.069100,0.077200,0.069200,-0.028100,0.077200,0.069200,-0.028100,0.077200,0.040600,-0.069100,0.077200,0.040600],

    "morphTargets": [],

    "morphColors": [],

    "normals": [0,-1,-0,0,1,-0,0,0,1,1,0,-0,0,0,-1,-1,0,-0],

    "colors": [],

    "uvs": [[1,0,1,1,0,1,0,0]],

    "faces": [43,0,1,2,3,0,0,1,2,3,0,0,0,0,43,4,5,6,7,0,3,0,1,2,1,1,1,1,43,0,3,5,4,0,3,0,1,2,2,2,2,2,43,3,2,6,5,0,3,0,1,2,3,3,3,3,43,2,1,7,6,0,3,0,1,2,4,4,4,4,43,1,0,4,7,0,3,0,1,2,5,5,5,5]

}

这是我加载上面方框的代码(没有加载框的设置位置声明):

var jsonLoader = new THREE.BinaryLoader();
jsonLoader.load('models/demo/BOX.js', function (geometry, materials) {
    var faceMaterial = new THREE.MeshFaceMaterial(materials);
    box = new THREE.Mesh(geometry, faceMaterial);

    // Add mesh to the scene
    box.scale.set(10, 10, 10);
    scene.add(box);
});

我试图在加载后得到它原来的位置,我遇到了问题。我使用$('#info').html(box.position.x + ', ' + box.position.y + ', ' + box.position.z);获得的位置始终为0, 0, 0。 (#info是显示结果的div元素。)有人可以在加载后帮助我获取盒子(或任何其他型号)的原始位置而无需手动设置其位置吗?
谢谢!

1 个答案:

答案 0 :(得分:1)

您可以执行box.computeBoundingSphere ();然后box.boundingSphere.center将包含模型的中心。此外,您可以使用computeBoundingBox().