Three.js未被捕获的TypeError:加载3D对象时无法读取未定义的属性“ 0”

时间:2019-03-19 17:02:28

标签: json animation three.js loading

    ob = JSON.stringify( that.char_elements[0].anim_manager.anim_list[0].obj.toJSON(), null, 3 );
    require("fs").writeFile( "./src/data/obj.pr", ob , 'utf8', function(err) {
        console.log(err);
    });

这是我编写.json文件以稍后加载的方式。而且导入似乎没有任何问题,因为生成的文件具有以下格式:

{
   "metadata": {
      "version": 4.5,
      "type": "Object",
      "generator": "Object3D.toJSON"
   },
   "geometries": [
      {
         "uuid": "447BF091-8882-4377-97C0-6E5102476B59",
         "type": "BufferGeometry",
         "name": "pCylinder2.000",
         "data": {...

但是当我稍后尝试使用THREE.ObjectLoader加载它时,当我尝试以这种方式加载它时,我会从这段代码的标题中看到这个奇怪的错误:

    var f2 = require('fs').readFileSync("./src/data/project.pr", 'utf8', function(err) {
        console.log(err);
    });

    char.anim_manager.loader_helper.js_loader.load( f2, char.anim_manager.loader_helper.true_load);
// gives this error: Three.js Uncaught TypeError: Cannot read property '0' of 
// undefined when loading 3D object...at XMLHttpRequest.<anonymous> (three.js:34342)

加载功能:

    function load_init( anim ) {
    viewer.mixer = new THREE.AnimationMixer( anim );

    if(viewer.cur_anim)
        viewer.scene.remove(viewer.cur_anim);

    cur_anim.anim = anim;
    viewer.cur_anim = cur_anim.anim;

    var that = this;
    anim.name = "animation_name";

    var mat_dict = {};

    anim.traverse( function ( child ) {
        if ( child.isMesh ) {

            //anim.mat_manager.mat_list
            const oldMat = child.material;

            if(oldMat.length == undefined)//only one material in the mesh
            {
                if(!mat_dict[oldMat.name])
                {
                    child.material = new THREE.MeshToonMaterial( {  
                        color: oldMat.color,
                        skinning: true,
                        name: oldMat.name,
                        side: THREE.DoubleSide,
                        shininess: 0,
                        specular: 0,
                        reflectivity: 0,
                    } ); 

                    mat_dict[oldMat.name] = child.material;
                    cur_anim.mat_manager.mat_list.push(child.material);
                }
                else
                    child.material = mat_dict[oldMat.name];
            }
            else//multiple materials
            {
                for(var i = 0; i < oldMat.length; i++)
                {
                    if(!mat_dict[oldMat[i].name])
                    {
                        child.material[i] = new THREE.MeshToonMaterial( {  
                        color: oldMat[i].color,
                        skinning: true,
                        name: oldMat[i].name,
                        side: THREE.DoubleSide,
                        shininess: 0,
                        specular: 0,
                        reflectivity: 0,
                        } );

                        mat_dict[oldMat[i].name] = child.material[i];
                        cur_anim.mat_manager.mat_list.push(child.material[i]);
                    }
                    else
                        child.material[i] = mat_dict[oldMat[i].name];
                }
            }
        }
    } );

    var action = viewer.mixer.clipAction( anim.animations[ 0 ] );
    action.play();

    viewer.scene.add( cur_anim.anim );
} 

但是我使用绑定来避免使用 this 错误。叫做true_load

var true_load = load_init.bind(this);

0 个答案:

没有答案