javascript - 如何在打字稿中导入three.js GLTFExporter?
问题描述
我正在尝试GLTFExporter
在 three.js 打字稿项目中使用。我在https://github.com/pinqy520/three-typescript-starter使用示例启动器,并且只将这些行添加到render
函数中:
console.log(`type of exporter = ${typeof(THREE.GLTFExporter)}`)
let exporter = new THREE.GLTFExporter()
exporter.parse(scene, (gltf) => {console.log("Parsed scene!"); }, {});
但是当我npm start
在浏览器中运行它时,它THREE.GLTFExporter
是未定义的,所以它“不是构造函数”(当然)。我怀疑这是因为 typescript 类型GLTFExporter
是在中导出的,three-gltfexporter.d.ts
但实际的类实际上并没有从它的源中导出node_modules/three/examples/js/exports/GLTFExporter.js
(那里没有export
关键字)。但是我对打字稿还是很陌生,所以可能是我不理解某些东西。但是 three.js 的所有“常规”部分都可以很好地导入,所以我怀疑仅在 .js 中GLTFExporter
定义的事实examples/
。
解决方案
看起来您需要放入THREE
全局范围,然后导入 GLTFExporter,因为 GLTFExporter 文件希望THREE
在全局范围中找到以便添加:
(<any>window).THREE = THREE;
import "three/examples/js/exporters/GLTFExporter";
最好@types/three
有一个单独的声明文件,其中three
每个可选扩展都有一个模块扩充,GLTFExporter
而不是无条件地声明所有可选扩展,这样 TypeScript 就能够在编译时警告你这个问题。考虑在DefinitiveTyped上提交一个错误。
推荐阅读
- reactjs - 基于子组件有条件地渲染包装代码
- javascript - 是否可以在 Windows 10 的 Windows 网络上远程获取触摸屏设备的当前坐标 X、Y?
- python - 用于 juputer notebook 的 Handcalcs 插件中的平方根问题
- javascript - Rijndael encryption VB to Javascript
- wordpress - 基于 ACF 转发器子字段自定义分类的查询帖子
- audio - 有没有办法在 C# 中创建虚拟扬声器或麦克风?
- python - 在对数据集进行预处理时,机器学习中的 Standard Scaler 和 MinMax Scaler 之间的主要区别是什么?
- c++ - 检测到“RuntimeLibrary”不匹配:值“MD_DynamicRelease”与应用程序 obj 中的值“MTd_Static debug”不匹配
- javascript - 我的侧导航仍然在右侧可见
- ios - 通过XIB创建自定义视图时如何消除多余的contentView?