Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OBJLoader and MTLLoader in THREE.js r74 and later

Tags:

three.js

It seems that OBJMTLLoader has been removed recently (r74?), but I cannot track down any documentation on how to use the two replacement classes. Here is the current code I have (adapted from Three.js Cookbook):

<script src="../libs/three.r74.js"></script>
<script src="../libs/MTLLoader.js"></script>
<script src="../libs/OBJMTLLoader.js"></script>

<script>
var legoManMesh = null;
function init(){ /* Create my scene here */ }

var loader = new THREE.OBJMTLLoader();
loader.load("../assets/models/lego.obj", "../assets/models/lego.mtl",
  function (obj) {
    legoManMesh = obj;
    init();
    }
  );
</script>

(BTW, when moving from r69 to r74 the above code fails with "TypeError: loader.setCrossOrigin is not a function")


ADDITIONAL:

The sample lego.mtl file here references a texture png using a relative path.

# Blender MTL File: 'LEGO Minifigure - Blendswap.blend'
# Material Count: 2

newmtl Cap
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.990000 0.120000 0.120000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.00000
illum 2

newmtl Minifig
Ns 874.999998
Ka 0.000000 0.000000 0.000000
Kd 0.800000 0.800000 0.800000
Ks 0.200000 0.200000 0.200000
Ni 1.000000
d 1.000000
illum 2
map_Kd ../textures/Mini-tex.png
like image 209
Darren Cook Avatar asked Feb 13 '16 13:02

Darren Cook


1 Answers

Here is code that demonstrates how to load .obj and .mtl files in r74:

var mesh = null;

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath( "https://threejs.org/examples/models/obj/walt/" );
mtlLoader.load( 'WaltHead.mtl', function( materials ) {

  materials.preload();

  var objLoader = new THREE.OBJLoader();
  objLoader.setMaterials( materials );
  objLoader.setPath( "https://threejs.org/examples/models/obj/walt/" );
  objLoader.load( 'WaltHead.obj', function ( object ) {

    mesh = object;
    mesh.position.y = -50;
    scene.add( mesh );

  } );

} );

Fiddle at: http://jsfiddle.net/g2evz0q5/

Updated answer to reflect correction for completeness.

like image 111
gaitat Avatar answered Oct 24 '22 00:10

gaitat