0

I have problem with three.js obj + mtl loader. obj still is without texture. any help? :(

    var loader1 = new THREE.OBJLoader();    
    var loader2 = new THREE.MTLLoader();
    loader2,load("models/house1.mtl"), function (materials){
       loader1.load("models/house1.obj ", function(obj) {
          object=obj; 
          object.materials.set( materials );
          object.scale.set(4,4,4); 
          object.position.set(-60,0,30); 
          object.rotation.set(0,0,0); 
          scene.add(object);
       })
   }
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 2
    You have what appears to be at least one syntax error (I believe loader1.load() is missing the closing parenthesis). I suspect you've got other coding errors, too. SUGGESTION: check your Javascript in Chrome Developer Tools (F12): https://developers.google.com/web/tools/chrome-devtools/javascript/ – paulsm4 May 12 '18 at 21:43

1 Answers1

0

I think you missed to preload the material resources. The MTLLoader will return an instance of "MaterialCreator" after parsing the mtl file. You can use this object to preload and prepare the resources required to create the materials. For more details, refer the code :

Also as @paulsm4 suggested, you are not using the proper syntax for loading an OBJ + MTL model.

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath( "models/" ); //Give the path upto the mtl file
mtlLoader.load( "house1.mtl", function( materials ){

    materials.preload();//We can preload the material resources like this.

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials( materials );//Set the materials for the objects using OBJLoader's setMaterials method
    objLoader.setPath( "models/" ); //Give the path upto the obj file
    objLoader.load( "house1.obj", function( object ){

        object.scale.set( 4, 4, 4 ); 
        object.position.set( -60, 0, 30 ); 
        object.rotation.set( 0, 0, 0 ); 
        scene.add( object );

    }, onProgress, onError );

} );

You can also refer this answer

HariV
  • 687
  • 1
  • 10
  • 17
  • @EvelinaDiksaitė, [This answer](https://stackoverflow.com/questions/17712857/how-to-load-textures-from-objmtl-files-in-three-js) already explains how to load an OBJ+MTL model. I have added this answer just to highlight what you have missed. – HariV May 14 '18 at 06:32