未捕获的TypeError:THREE.MTLLoader不是构造函数

时间:2017-10-28 11:44:00

标签: three.js

我是三个新手,我试图在我的框架中包含一个obj模型witl mtl - 我通过键入以下内容安装了三个obj-loader:

npm install --save three-obj-loader

然后我可以加载一个模型,如下所示:

import * as THREE from 'three'
import * as objlibrary from 'three-obj-loader'
let OBJLoader = objlibrary(THREE)

let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / 
window.innerHeight, 0.1, 50000);

let renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xB1B1B1, 0);
renderer.domElement.id = 'view3d'

document.body.appendChild(renderer.domElement);

//load obj-model
var loader = new THREE.OBJLoader();
loader.load("http://blabla/object.obj" , function ( object ) {
var material = new THREE.MeshLambertMaterial( { color: 0x006400 } );
object.traverse( function ( child ) {

    if ( child instanceof THREE.Mesh ) {

        child.material = material;

    }

} ); 
object.scale.x = 0.01;
object.scale.y = 0.01;
object.scale.z = 0.01;
scene.add( object );

} );

但我希望包含对象mtl-file。我再次使用npm install安装了三个mtl-loader,并在代码中包含以下内容:

import * as mtllibrary from 'three-mtl-loader'
let MTLLoader = mtllibrary(THREE)

然后我尝试使用与此类似的代码:https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html

var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("http://blabla/object.obj.mtl", function(materials) {
  materials.preload();
  var objLoader = new THREE.OBJLoader();
  objLoader.setMaterials(materials);
  objLoader.load("http://blabla/object.obj", function(object) {
    object.scale.x = 0.01;
    object.scale.y = 0.01;
    object.scale.z = 0.01;
    scene.add(object);
  });
});

但是会抛出以下错误:' Uncaught TypeError:THREE.MTLLoader不是构造函数',我不太明白,当三者没有发生同样的情况时-obj装载机。 (我也尝试安装objmtllloader,导致同样的错误) - 非常感谢任何帮助:)

修改: 正如评论中所提到的那样,三mtl-loader看起来并不像三个obj-loader那样将THREE作为输入。我想也许你可以改变它(我不知道我是否正确地做到了),我试着这样做: https://www.dropbox.com/s/gbefq8x7roqwhww/index.js?dl=0

现在没有错误,但对象根本就没有显示。

1 个答案:

答案 0 :(得分:1)

实际上将mtl-loader更改为:https://www.dropbox.com/s/gbefq8x7roqwhww/index.js?dl=0,确实解决了问题:D