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

It seems that OBJMTLLoader (r74?) Has recently been removed , but I can not trace the documentation on how to use the two replacement classes. Here is the current code that 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 switching from r69 to r74, the above code failed using "TypeError: loader.setCrossOrigin is not a function")


ADDITIONALLY:

The sample lego.mtl file here refers to the png texture 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
+4
source share
1 answer

Here is the code demonstrating how to upload .obj and .mtl files to r74:

var mesh = null;

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

  materials.preload();

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

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

  } );

} );

Fiddle at: http://jsfiddle.net/dw62cxpy/2/

, .

+7

Source: https://habr.com/ru/post/1628636/


All Articles