0

How to load more than two objects in three js of .obj file . I tried by applying a for loop across the loader file of obj and mtl but it is not working instead it shows only the last object ?Please if any one can help.

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 );
            });
        });
    }
}
Gene R
  • 3,684
  • 2
  • 17
  • 27
  • see [this](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) or [this](http://stackoverflow.com/questions/29456674/three-js-loader-callback-issues-in-loop-only-receive-last-value) – 2pha Mar 08 '16 at 10:17

1 Answers1

0

load model without for loop ,it looks your variable holds obj override .

Nukes
  • 284
  • 2
  • 8