Three.js:使用THREE.JSONLoader.parse而不是THREE.JSONLoader.load时出现异常

时间:2013-06-13 16:44:22

标签: json parsing three.js

当我创建一个JSON模型并使用THREE.JSONLoader.load加载它时,它加载时没有任何问题,但是当我尝试从中创建变量并在主脚本中包含数据时THREE.JSONLoader.parse调用抛出异常。

var lh_model = {
    "metadata" : { "formatVersion" : 3.1, ..., "morphTargets"  : 0 },
    "vertices": [
          0.001000,    0.001000,  -0.001000,
        102.616974,  -61.974983,  19.303007,
        102.108978,  -62.482986,  -7.620989,
        123.952972,  -49.274994,  -3.048992,
        123.952972,  -48.766994,  14.223002
    ],
    "uvs": [0.9458,0.5134,0.9419,0.5177,0.9513,0.5177,........],
    "faces": [ 0, 4, 1, 2 ],
    "morphTargets": []
}

var loader = new THREE.JSONLoader();
var geometry = loader.parse(lh_model);

load.parse导致

Uncaught TypeError: Cannot read property 'length' of undefined

错误在Three.js内部的某处:

THREE.Mesh.prototype.updateMorphTargets = function () {

    if ( this.geometry.morphTargets.length > 0 ) {

当我采用其他人(http://stemkoski.github.io/Three.js/Model.html)制作的工作示例时也会发生同样的情况。

我正在使用Three.js r.58

1 个答案:

答案 0 :(得分:4)

这样做

var loader = new THREE.JSONLoader();
var model = loader.parse( lh_model );

var mesh = new THREE.Mesh( model.geometry, new THREE.MeshBasicMaterial() );
scene.add( mesh );

如果您在模型中添加材料,则可以执行以下操作

var mesh = new THREE.Mesh( model.geometry, materials[ 0 ] );

three.js r.58