无法在threeJS场景上加载blender模型未定义

时间:2017-12-09 11:16:28

标签: javascript json three.js blender

我已经按照SO上的一些问题解决了这个问题,但我仍然无法加载外部对象(blender)。

基本上我将它导出为ThreeJS JSON文件,这是我的JSON文件:

{
    "textures":[],
    "animations":[{
        "fps":24,
        "tracks":[],
        "name":"default"
    }],
    "geometries":[{
        "materials":[{
            "colorEmissive":[0,0,0],
            "DbgName":"Material.113",
            "DbgIndex":0,
            "shading":"phong",
            "specularCoef":50,
            "transparent":false,
            "visible":true,
            "blending":1,
            "depthWrite":true,
            "DbgColor":15658734,
            "doubleSided":false,
            "wireframe":false,
            "colorSpecular":[0.5,0.5,0.5],
            "opacity":1,
            "depthTest":true,
            "colorDiffuse":[0.0396404,0.0396404,0.0396404]
        },{
            "colorEmissive":[0,0,0],
            "DbgName":"Material.112",
            "DbgIndex":1,
            "shading":"lambert",
            "transparent":false,
            "visible":true,
            "blending":1,
            "depthWrite":true,
            "DbgColor":15597568,
            "doubleSided":false,
            "wireframe":false,
            "opacity":1,
            "depthTest":true,
            "colorDiffuse":[0.8,0.443066,0.182712]
        },{
            "colorEmissive":[0,0,0],
            "DbgName":"Material.114",
            "DbgIndex":2,
            "shading":"phong",
            "specularCoef":50,
            "transparent":false,
            "visible":true,
            "blending":1,
            "depthWrite":true,
            "DbgColor":60928,
            "doubleSided":false,
            "wireframe":false,
            "colorSpecular":[0.5,0.5,0.5],
            "opacity":1,
            "depthTest":true,
            "colorDiffuse":[0.8,0.614231,0.407028]
        }],
        "data":{
            "uvs":[],
            "normals":[-0,0,1,-1,-0,0,1,-0,-0,-0,-1,0,-1,4.76837e-07,0,-1,-0,0,-2.38419e-07,-1,2.38419e-07,-2.38419e-07,-1,2.38419e-07,-2.38419e-07,-1,2.38419e-07 },
        "type":"Geometry",
        "uuid":"639CD2C2-56FB-4D8F-B53B-B8C3E040803C",
        "name":"Cube.111Geometry.4"
    }],
    "object":{
        "children":[{
            "name":"Cube.001",
            "uuid":"1213E565-1F93-497F-B4F0-843C1F17BCAD",
            "matrix":[-0.6,0,0,0,0,0,0.6,0,0,0.6,0,0,-0.866864,0.6,2.6259,1],
            "visible":true,
            "type":"Mesh",
            "material":["87C3E87D-CD72-4A6F-963A-84474DFE4059","FABD8A0F-02B0-4665-A21E-5CD1E4DDD518","E4331AC2-6CAC-4BCE-A5BD-89A811DD4D76"],
            "castShadow":true,
            "receiveShadow":true,
            "geometry":"639CD2C2-56FB-4D8F-B53B-B8C3E040803C"
        }],
        "matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],
        "type":"Scene",
        "uuid":"23736546-1486-4089-967C-6DA05CB3F49C"
    },
    "materials":[{
        "vertexColors":0,
        "color":13398062,
        "uuid":"FABD8A0F-02B0-4665-A21E-5CD1E4DDD518",
        "depthWrite":true,
        "depthTest":true,
        "type":"MeshLambertMaterial",
        "blending":1,
        "emissive":0,
        "name":"Material.112"
    },{
        "vertexColors":0,
        "shininess":50,
        "color":657930,
        "uuid":"87C3E87D-CD72-4A6F-963A-84474DFE4059",
        "specular":8355711,
        "depthWrite":true,
        "depthTest":true,
        "type":"MeshPhongMaterial",
        "blending":1,
        "emissive":0,
        "name":"Material.113"
    },{
        "vertexColors":0,
        "shininess":50,
        "color":13409383,
        "uuid":"E4331AC2-6CAC-4BCE-A5BD-89A811DD4D76",
        "specular":8355711,
        "depthWrite":true,
        "depthTest":true,
        "type":"MeshPhongMaterial",
        "blending":1,
        "emissive":0,
        "name":"Material.114"
    }],
    "metadata":{
        "generator":"io_three",
        "sourceFile":"DAMA1.blend",
        "version":4.4,
        "type":"Object"
    },
    "images":[]
}

我在normals数组上省略了一些数据,但基本上我尝试像这样加载它:

var objLoader =new THREE.ObjectLoader();
objLoader.load('assets/dama.json', function (geometry) {
  this.scene.add(geometry);
});

它找到了该文件,但我收到如下错误:无法读取未定义的属性'scene'

也许它与导出的设置有关,但我已经将缓冲区测量更改为几何体并添加了场景复选框,我做错了什么?:S

1 个答案:

答案 0 :(得分:0)

我不确定。但你可以试试这个,

var scope = this;
var objLoader =new THREE.ObjectLoader();
objLoader.load('assets/dama.json', function (geometry) { 
    scope.scene.add(geometry); 
});

我希望您的场景是对象的属性,这可能就是您将其称为this.scene的原因。这个问题是,当您在加载函数中调用 this 时,它将引用 THREE.ObjectLoader 。因此,如果您需要使用 this 引用基础对象,则应首先复制它,然后再使用它。

如果您没有使用对象来保存场景,只需拨打scene.add(geometry)而不是scope.scene.add()this.scene.add()