2016-03-08 48 views
0

如何在三個js .obj文件中加載兩個以上的對象。我嘗試了通過在obj和mtl的加載器文件上應用for循環,但它不工作,而是隻顯示最後一個對象?請如果有人能幫忙。如何使用用戶輸入中的three.js在場景中加載兩個或更多.obj對象?

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); 
      }); 
     }); 
    } 
} 
+0

請參見[this](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example)或[this](http://stackoverflow.com/questions/29456674 /三JS-架回調的問題在環只用接收,最後值) – 2pha

回答

0

加載模型沒有for循環,它看起來你的變量持有obj覆蓋。

相關問題