I think there are some problems here.
1) THREE.MTLLoader .load behaves like a "non-blocking" function.
Thus, you download OBJ files when MTL files are not yet fully loaded. You need to load the OBJ files inside the callback function passed as the argument to mtlLoader.load (). Take a look at the example you mentioned.
2) About the mtlLoader.load callback function passed as an argument:
His argument, materials, is of type THREE.MTLLoader.MaterialCreator . As a result, loadMaterials is an array of THREE.MTLLoader.MaterialCreator elements and to create THREE.MultiMaterial , you need an array of THREE.Material elements.
In addition, MultiMaterial is used to assign many materials to one object (one material on the face of the object), and not to "optionally" select material for the object ( Example MultiMaterial Cube ).
One approach for this (code NOT ):
First we need the LoadingManager :
var manager = new THREE.LoadingManager();
This means that all MTLs were loaded before OBJ loading:
manager.onLoad = function() { if (loadedMaterials.length > 0) { var objLoader = new THREE.OBJLoader(); objLoader.setPath(resources); objLoader.setMaterials(loadedMaterials[0]);
And we load all MTL:
var mtlLoader = new THREE.MTLLoader(manager); mtlLoader.setPath(resources); for (var i = 0; i < materialsToLoad.length; i++) { mtlLoader.load(materialsToLoad[i], function(materials) { materials.preload(); loadedMaterials.push(materials); }); }
Hope this helps!
source share