未捕获的类型错误:THREE.MTLLoader 不是构造函数 2.0 [英] Uncaught TypeError: THREE.MTLLoader is not a constructor 2.0

查看:43
本文介绍了未捕获的类型错误:THREE.MTLLoader 不是构造函数 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在这里问过这个问题:未捕获的类型错误:三.MTLLoader 不是构造函数

I earlier asked this question in here: Uncaught TypeError: THREE.MTLLoader is not a constructor

我通过更改三 mtl-loader 文件使其工作的地方.但由于我稍后将我的工作上传到 github,我必须在不更改这些文件的情况下创建一个解决方案.

Where I got it to work by changing the three-mtl-loader file. But as I am going to upload my work to github later, I have to create a solution without changing these files.

因此,我尝试使用经典示例中使用的加载器加载 obj 和 mtl 文件:https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html

I am therefore instead trying to load in the obj and mtl files using the loaders used in the classic example: https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html

但我仍然收到错误'Uncaught TypeError: THREE.MTLLoader is not a constructor'

But I still get the error 'Uncaught TypeError: THREE.MTLLoader is not a constructor'

我有点不确定如何在这些加载器中加载,但现在我的代码如下所示:

I am a bit unsure about how I would load in these loaders, but right now my code looks like this:

import * as THREE from 'three'
import {OBJLoader} from 'three'
import {MTLLoader} from 'three'


var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("http://blabla.obj.mtl", function(materials) {
   materials.preload();
   var objLoader = new THREE.OBJLoader();
   objLoader.setMaterials(materials);
   objLoader.load("http://blabla.obj", function(object) {

       object.scale.x = scale;
       object.scale.y = scale;
       object.scale.z = scale;

       scene.add(object)

  });
});

我插入的src文件夹中的three.js文件中的位置:

Where in the three.js file in the src folder I have inserted:

export { OBJLoader } from './loaders/OBJLoader.js'
export { MTLLoader} from './loaders/MTLLoader.js'

编辑

我使用 NPM 安装,错误消失了.不幸的是,发生了其他问题 - 但我在另一个问题中提出了这些问题:三 mtl-loader 错误:THREE.MeshPhongMaterial:.shading 已被删除 ->对象不可见

I installed using NPM, and the error disappears. Unfortunately other issues occur - but I have asked these in a different question: three-mtl-loader error: THREE.MeshPhongMaterial: .shading has been removed -> object not visible

推荐答案

Three.js 文档具有误导性.尽管 MTLLoader 在主文档中,但它实际上不是 Three.js 的一部分核心库.它包含在 Three.js 的 examples/ 文件夹中.

The Three.js docs are misleading. Even though the MTLLoader is in the main documentation, it's actually not part of Three.js's core library. It's included in the examples/ folder of Three.js.

如果您使用 ES6 模块语法,您有两个选择.

If you're using ES6 module syntax, you have two options.

通常人们会向 NPM 发布这样的内容,因为 Threejs 的创建者不使用现代 JS 开发实践.在这种情况下,有一个 three-mtl-loader npm 包 包.

Usually people publish things like this to NPM because the creators of Threejs don't use modern JS development practices. In this case, there is a three-mtl-loader npm package package.

npm install --save three-mtl-loader

然后

import MTLLoader from 'three-mtl-loader';

选项 2:从 Three 的源代码中复制 MTLLoader.

您可以复制MTLLoader.js 文件添加到您的项目中,但如果您使用的是现代 ES6 模块,则将第三方库复制到您的本地项目中并不是一个好习惯.

Option 2: Copy MTLLoader from Three's source.

You can copy the MTLLoader.js file into your project, but it's not good practice to copy third party libraries into your local project if you're using modern ES6 modules.

这篇关于未捕获的类型错误:THREE.MTLLoader 不是构造函数 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆