在弄清楚如何让GLTFLoader在THREE.js中工作时,我遇到了一些问题。我不知道如何使用cdn站点来托管文件。我试着使用网络上的例子链接,但这对我来说并不管用。我在另一篇文章中读到,GLTFLoader文件必须与我正在使用的core THREE文件(r121)的版本相同。
我想我可以转到mrdoob github (不熟悉如何使用github),点击raw file,然后在githack这样的网站上使用这个链接来生成一个cdn链接,并将其作为脚本添加到我的html中,或者将其导入到我的js文件中,但这并不起作用。
如果这是一种方法,那么它就不起作用了。在我输入的代码中:
let loader = new GLTFLoader(); //from the docs
//or
let loader = new THREE.GLTFLoader(); //not from the docs
我得到以下两个错误中的一个:未捕获ReferenceError: GLTFLoader未定义或未捕获TypeError: THREE.GLTFLoader不是构造函数
我已经做了几个小时了,完全不知道该怎么做。
CodePenhttps://codepen.io/jfirestorm44/pen/RwRPJda?editors=0010
如果重要的话,我将遵循以下教程:https://tympanus.net/codrops/2019/10/14/how-to-create-an-interactive-3d-character-with-three-js/
发布于 2020-11-14 01:59:45
确保您在html文件中导入的three.js和GLRFLoader放在您自己的脚本之前。我喜欢把我自己的脚本放在html文件的最底部。
更新:上面的cdn链接将始终指向最新的GLTFLoader,这可能不是向后兼容的。改用特定的标签:
在您的脚本中,您不需要额外的导入,只需调用加载器即可
const gltfLoader = new THREE.GLTFLoader();
换句话说,如果您使用上面提供的html导入,则以下代码是多余的。
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.121.1/build/three.module.js";
import { GLTFLoader } from "https://cdn.jsdelivr.net/npm/three@0.121.1/examples/jsm/loaders/GLTFLoader.js";
工作示例:
index.html
</head>
<body>
<!-- body -->
</body>
<script type="text/javascript" src="/static/myscript.js">
我的脚本.js
const gltfLoader = new THREE.GLTFLoader();
https://stackoverflow.com/questions/64409605
复制相似问题