加载three.js Collada文件时出错:第一个对象正常工作,后续对象失败

时间:2015-10-19 06:20:46

标签: three.js blender collada

我在three.js上加载Collada文件时遇到问题。我加载的第一个对象正常工作:enter image description here

但是当我从场景中删除它并加载另一个对象时(从场景中删除并处理资源后),几何体显示为乱码:

enter image description here

可能是什么问题?

以下是使用的代码: (特别感谢@gaitat的处理功能!最初回答here

var renderer = null;
var scene = null;
var camera = null;
var controls = null;
var dae = null;
//var loader = null;

function init() {


    renderer = new THREE.WebGLRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
    renderer.setSize( 800, 600 );

    var elem = $('.main3d')[0];
    elem.appendChild( renderer.domElement );

    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera( 20, 800/600, 1, 1000 );
    camera.position.set( 0, -100, 50 );
    //camera.lookAt( scene.position );
    controls = new THREE.TrackballControls( camera, renderer.domElement );

    var light = new THREE.AmbientLight( 0xffffff ); // soft white light
    scene.add( light );

    threeAnimate();


}

function load(url) {
    loader = new THREE.ColladaLoader();

        loader.load(url, function (collada) {
            dae = collada.scene;
            scene.add(dae);

        });

}

var animFrame = null;
function animate()  {

    animFrame = requestAnimationFrame( threeAnimate );
    renderer.render( scene, camera );
    controls.update();

}

// special thanks to @gaitat for this function!
// originally answered here https://stackoverflow.com/questions/33152132/three-js-collada-whats-the-proper-way-to-dispose-and-release-memory-garbag/33199591
function disposeNode (node)
{
    if (node instanceof THREE.Camera)
    {
        node = undefined;
    }
    else if (node instanceof THREE.Light)
    {
        node.dispose ();
        node = undefined;
    }
    else if (node instanceof THREE.Mesh)
    {
        if (node.geometry)
        {
            node.geometry.dispose ();
            node.geometry = undefined;
        }

        if (node.material)
        {
            if (node.material instanceof THREE.MeshFaceMaterial)
            {
                $.each (node.material.materials, function (idx, mtrl)
                {
                    if (mtrl.map)           mtrl.map.dispose ();
                    if (mtrl.lightMap)      mtrl.lightMap.dispose ();
                    if (mtrl.bumpMap)       mtrl.bumpMap.dispose ();
                    if (mtrl.normalMap)     mtrl.normalMap.dispose ();
                    if (mtrl.specularMap)   mtrl.specularMap.dispose ();
                    if (mtrl.envMap)        mtrl.envMap.dispose ();

                    mtrl.dispose ();    // disposes any programs associated with the material
                    mtrl = undefined;
                });
            }
            else
            {
                if (node.material.map)          node.material.map.dispose ();
                if (node.material.lightMap)     node.material.lightMap.dispose ();
                if (node.material.bumpMap)      node.material.bumpMap.dispose ();
                if (node.material.normalMap)    node.material.normalMap.dispose ();
                if (node.material.specularMap)  node.material.specularMap.dispose ();
                if (node.material.envMap)       node.material.envMap.dispose ();

                node.material.dispose ();   // disposes any programs associated with the material
                node.material = undefined;
            }
        }

        node = undefined;
    }
    else if (node instanceof THREE.Object3D)
    {
        node = undefined;
    }
}   // disposeNode

function disposeHierarchy (node, callback)
{
    for (var i = node.children.length - 1; i >= 0; i--)
    {
        var child = node.children[i];
        disposeHierarchy (child, callback);
        callback (child);
    }
}

0 个答案:

没有答案