代码之家  ›  专栏  ›  技术社区  ›  GaryO

如何导入三.jsGLTFExporter在typescript中?

  •  1
  • GaryO  · 技术社区  · 7 年前

    我试着用 GLTFExporter 在一个三.jstypescript项目。 我在用样品起动器 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 是未定义的,所以它“不是构造函数”(当然)。 我怀疑这是因为 GLTF导出器 在中导出 three-gltfexporter.d.ts node_modules/three/examples/js/exports/GLTFExporter.js (没有 export 关键字)。但我对打字还是很陌生,所以可能是我不懂什么。但所有的“平常”部分三.js进口的很好,所以我怀疑 GLTF导出器 examples/ .

    1 回复  |  直到 7 年前
        1
  •  1
  •   Matt McCutchen    7 年前

    THREE 然后导入GLTFExporter,因为GLTFExporter文件需要找到 在全局范围中添加:

    (<any>window).THREE = THREE;
    import "three/examples/js/exporters/GLTFExporter";
    

    @types/three 有一个单独的声明文件,其模块扩展为 three GLTFExporter 而不是无条件地声明所有可选扩展,这样TypeScript就可以在编译时警告您这个问题。考虑在上提交一个bug DefinitelyTyped