ThreeJS - JSONLoader“无法读取未定义的属性'制服'

时间:2013-06-30 22:48:24

标签: json 3d three.js blender

我正在尝试使用Blender中的纹理导出对象。导出的JSON如下所示:

 {

    "metadata" :
    {
        "formatVersion" : 3.1,
        "generatedBy"   : "Blender 2.66 Exporter",
        "vertices"      : 8,
        "faces"         : 6,
        "normals"       : 8,
        "colors"        : 0,
        "uvs"           : [],
        "materials"     : 1,
        "morphTargets"  : 0,
        "bones"         : 0
    },

    "scale" : 1.000000,

    "materials" : [ {
        "DbgColor" : 15658734,
        "DbgIndex" : 0,
        "DbgName" : "Material",
        "blending" : "NormalBlending",
        "colorAmbient" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
        "colorDiffuse" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
        "colorSpecular" : [0.0, 0.0, 0.0],
        "depthTest" : true,
        "depthWrite" : true,
        "mapDiffuse" : "unicorn.jpg",
        "mapDiffuseWrap" : ["repeat", "repeat"],
        "shading" : "Lambert",
        "specularCoef" : 50,
        "transparency" : 1.0,
        "transparent" : false,
        "vertexColors" : false
    }],

    "vertices" : [1,-1,-1,1,-1,1,-1,-1,1,-0.999999,-1,-1,1,1,-0.999999,0.999999,1,1,-1,1,0.999999,-1,1,-1],

    "morphTargets" : [],

    "normals" : [0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,-0.577349,-0.577349,-0.577349,0.577349,0.577349,-0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,0.577349,0.577349,0.577349],

    "colors" : [],

    "uvs" : [],

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

    "bones" : [],

    "skinIndices" : [],

    "skinWeights" : [],

    "animation" : {}


}

我的Json装载机:

 var object;
var loader = new THREE.JSONLoader();          

loader.load( "models/texturecube.js", function(geometry, materials) {
    var material = new THREE.MeshFaceMaterial(materials);
    object = new THREE.Mesh(geometry, materials); // throws

    object.scale.set(1, 1, 1);
    scene.add(object)

});

加载程序抛出“无法读取属性'未定义的”制服“异常。我看到其他人必须修改导出的JSON才能使其正常工作。有谁知道'制服'属性用于什么?我应该在我的JSON中定义吗?

谢谢!

2 个答案:

答案 0 :(得分:6)

您的material变量未被使用。这样做:

object = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );

此外,如果您希望能够应用纹理,则几何体将需要UV。


编辑:有一种新的模式。如果您有一组材质,现在可以将材质数组直接传递到Mesh构造函数中,如下所示:

object = new THREE.Mesh( geometry, materials );

three.js r.87

答案 1 :(得分:0)

您只是将一种材料传递给该行中的对象:

var material = new THREE.MeshFaceMaterial(materials[0]);

相反,您需要传递完整的材料数组:

var material = new THREE.MeshFaceMaterial(materials);

基本上导致错误的原因是,当您的模型尝试访问除第一个之外的任何材料时,它会抛出undefined错误,因为材料不存在,因为您尚未将其传递给对象构造函数