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

问题描述:

如何在三个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://*.com/questions/750486/javascript-closure-inside-loops-simple-practical-example)或[this](http://*.com/questions/29456674 /三JS-架回调的问题在环只用接收,最后值) – 2pha

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