如何使用用户输入中的 three.js 在场景中加载两个或多个 .obj 对象



如何在 .obj 文件的三个 js 中加载两个以上的对象。我尝试在 obj 和 mtl 的加载器文件中应用 for 循环,但它不起作用,而是只显示最后一个对象?如果有人可以帮忙,请。

function init() {   
    container = document.createElement( 'div' );
    document.body.appendChild( container );
    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
    camera.position.z = 1000;
    // scene
    scene = new THREE.Scene();
    var ambient = new THREE.AmbientLight( 0x444444 );
    scene.add( ambient );
    var directionalLight = new THREE.DirectionalLight( 0xffeedd );
    directionalLight.position.set( 0, 0, 1 ).normalize();
    scene.add( directionalLight );
    // model
    THREE.Loader.Handlers.add( /.dds$/i, new THREE.DDSLoader() );
    var setBaseUrl1 = 'newObj/';
    var setPath1 = 'newObj/';
    var i = 0;
    var mltLoad1,objLoad1;
    var mtlLoader = new Array(res.length); 
    var objLoader = new Array(res.length); 
    for(i=0;i<res.length;i++)
    {
        mtlLoad1 = String(res[i].concat(".mtl"));
        objLoad1 = String(res[i].concat(".obj"));       
        mtlLoader[i] = new THREE.MTLLoader();
        mtlLoader[i].setBaseUrl( setBaseUrl1 );
        mtlLoader[i].setPath( String(setPath1) );
        mtlLoader[i].load( mtlLoad1, function( materials ) {
            materials.preload();
            objLoader[i] = new THREE.OBJLoader();
            objLoader[i].setMaterials( materials );
            objLoader[i].setPath( setPath1 );
            objLoader[i].load( objLoad1, function(object) {
                object.position.y = -95;
                object.position.z = 500;
                camera.position.x = 500;
                scene.add( object );
            });
        });
    }
}

没有for循环的加载模型,看起来你的变量持有obj覆盖。

最新更新