如何在 TypeScript 中使用 Three.js 加载 OBJ 模型 [英] How to load OBJ model with three.js in TypeScript
问题描述
我正在使用 TypeScript 和 three.d.ts 来自 绝对输入.我使用 THREE.JSONLoader 没有问题,但是我如何使用 此处 在 TypeScript 项目中.我可能需要创建一个 OBJLoader.d.ts 文件,但我不知道该怎么做以及如何使用创建的定义.我试图简单地复制 THREE.JSONLoader 定义并将其重命名为 OBJLoader,但这不起作用.
I am using TypeScript and three.d.ts from definitely typed. I have no problems using THREE.JSONLoader, but how do I use an OBJLoader from here in a TypeScript project. I probably need to create an OBJLoader.d.ts file, but I have no idea how to do it and then how to use the created definition. I tried to simply copy the THREE.JSONLoader definition and rename it to OBJLoader, but that didn't work.
推荐答案
这个答案在发布时是正确的,但现在在 2019 年已经过时了.请参阅下面@trusktr 的回复以获取今天更好的解决方案.
查看了 OBJLoader 的源码 此处,(并参考 three.d.ts) 一个简单的 objloader.d.ts
文件可能如下所示:
Having looked at the source of the OBJLoader here, (and with reference to three.d.ts) a simple objloader.d.ts
file might look like this:
/// <reference path="three.d.ts" />
export class OBJLoader extends EventDispatcher {
constructor();
load(url: string, callback?: (response:any) => any): void;
parse(data:any):any; // Not sure if the return value can be typed. Seems to be a group but I can't find a definition for that in three.d.ts?
}
警告:这很快就被破解了,没有经过测试,但可以帮助您入门.
Caveat: this is quickly hacked together and not tested, but may help you to get started.
然后,您将按照当前使用 three.d.ts
的相同方式引用您的 objloader.d.ts
.不要忘记在您的 html 页面中同时包含 three.js
和 OBJLoader.js
文件,或者如果您正在使用外部模块,请导入它们.
You would then reference your objloader.d.ts
in the same way you are currently using three.d.ts
. Don't forget to include both the three.js
and OBJLoader.js
files in your html page, or import them if you are working with external modules.
这篇关于如何在 TypeScript 中使用 Three.js 加载 OBJ 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!