Three.js load Multiple obj files one after another

試著忘記壹切 提交于 2019-12-12 03:42:16

问题


I'm using the obj/mtl loader provided by three.js to load several obj files including mtl. Now I need to load multiple objs. But I need to load them one after another. I already used THREE.DefaultLoadingManager.onProgress to add a "Loading Screen". But how can I check for loaded === total within the loop for adding new objs. Or should I use a recursive function?

Hope you can help me. Thanks


回答1:


There is a callback function that is called when an object is loaded. There you can trigger the next loading step.

var index = 0;
var files = ['file1.obj','file2.obj'];

var objLoader = new THREE.OBJLoader();

function loadNextFile() {

  if (index > files.length - 1) return;

  objLoader.load(files[index], function(object) {

    scene.add(object);

    index++;
    loadNextFile();

  });

}

loadNextFile();

This basic code needs to be extended to load materials.




回答2:


You could check THREE.DefaultLoadingManager.onLoad.
That should do the Job, without trying.




回答3:


@brakebein's answer helped me to get this solution right, so I thought I would chime in with my full code that worked for me (fwiw) - THANKS BRAKEBEIN! :)

// Texture and OBJ loader
let OBJfiles = ['love3','rose'];
let _MTLLoader = new THREE.MTLLoader().setPath( 'models/' );

// this function will load the next MTL and OBJ file in the queue
function loadNextMTL () {

    if (index > OBJfiles.length - 1) return;

    _MTLLoader.load( OBJfiles[index]+'.mtl', function ( materials ) {
        materials.preload();
        new THREE.OBJLoader()
            .setMaterials( materials )
            .setPath( 'models/' )
            .load( OBJfiles[index]+'.obj', function ( group ) {
                mesh = group.children[0];
                mesh.material.side = THREE.DoubleSide;
                mesh.position.y = 0.25;
                mesh.scale.set(0.02,0.02,0.02);
                markerRoot[index].add(mesh);

                index++; // incrememnt count and load the next OBJ
                loadNextMTL();

            });
            //, onProgress, onError > These can be used to keep track of the loads
        });

}

loadNextMTL (); // kick off the preloading routine


来源:https://stackoverflow.com/questions/39993833/three-js-load-multiple-obj-files-one-after-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!