Preloading Obj+Mtl Objects in Three.js

旧城冷巷雨未停 提交于 2020-02-08 07:01:52

问题


I need to preload some obj+mtl files with Three.js (It's not the same file) and I need call another function when all the objects have been loaded.

I tried putting a boolean variable that changes when every obj has been loaded and doing a function that confirms if all the objects have been loaded but it didn't work, for some reason the page crash.

I have all the obj and mtl paths in an array.

This is an example of what I'm doing. http://pastie.org/10297027

I tried to put the load function in a for statement but it didn't work well

Can you help me?


回答1:


Use a three.js loading manager for this task, here is how to do it.

Create a manager:

var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
    // this gets called after an object has been loaded
};
manager.onLoad = function () {
    // everything is loaded
    // call your other function
};

Create a loader using the manager and load items:

var OBJMTLLoader = new THREE.OBJMTLLoader( manager );

OBJMTLLoader.load(urlsOBJ[0], urlsMTL[0], function(object) {

    // stuff you do in your callback
    var newObject = object.clone();
    newObject.position.set(140, 10, 10);
    newObject.rotation.x = -99;
    scene.add(newObject);
    objectArray.push(newObject);

});

Tested in three.js r71.




回答2:


new THREE.MTLLoader()
                    .setPath( 'models/obj/male02/' )
                    .load( 'male02_dds.mtl', function ( materials ) {

                        materials.preload();

                        new THREE.OBJLoader()
                            .setMaterials( materials )
                            .setPath( 'models/obj/male02/' )
                            .load( 'male02.obj', function ( object ) {

                                object.position.y = - 95;
                                scene.add( object );

                            }, onProgress, onError );

                    } );

OBJMTLLoader is depricated. You should use OBJLoader and MTLLoader in combination.

Here is an example



来源:https://stackoverflow.com/questions/31457314/preloading-objmtl-objects-in-three-js

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