确保 html 文件中的 Three.js 和 GLTFLoader 的导入放置在您自己的脚本之前。我喜欢将自己的脚本放在 html 文件的最底部。
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r121/three.min.js" integrity="sha512-yNJzAsg5JyP91u+sLHlUDULMBd3hmEiVkYeeN1cQBKaLZ7EyT6oH2u5THNIRM2Fu6VKcZJv+F/QAp1h/qzy9Ow==" crossorigin="anonymous"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/loaders/GLTFLoader.js"></script>
更新:上面的 cdn 链接将始终指向最新的 GLTFLoader,该链接可能不向后兼容。使用特定标签代替:
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r126/three.min.js" integrity="sha512-n8IpKWzDnBOcBhRlHirMZOUvEq2bLRMuJGjuVqbzUJwtTsgwOgK5aS0c1JA647XWYfqvXve8k3PtZdzpipFjgg==" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected] /cdn-cgi/l/email-protection/examples/js/loaders/GLTFLoader.js"></script>
在您的脚本中,您不需要额外的导入,只需调用加载器
const gltfLoader = new THREE.GLTFLoader();
换句话说,如果您使用上面提供的 html 导入,以下代码是多余的。
import * as THREE from "https://cdn.jsdelivr.net/npm/[email protected] /cdn-cgi/l/email-protection/build/three.module.js";
import { GLTFLoader } from "https://cdn.jsdelivr.net/npm/[email protected] /cdn-cgi/l/email-protection/examples/jsm/loaders/GLTFLoader.js";
工作示例:
索引.html
<!DOCTYPE html>
<html>
<head>
<!-- CSS imports-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r126/three.min.js" integrity="sha512-n8IpKWzDnBOcBhRlHirMZOUvEq2bLRMuJGjuVqbzUJwtTsgwOgK5aS0c1JA647XWYfqvXve8k3PtZdzpipFjgg==" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected] /cdn-cgi/l/email-protection/examples/js/loaders/GLTFLoader.js">
</head>
<body>
<!-- body -->
</body>
<script type="text/javascript" src="/static/myscript.js"></script>
</html>
myscript.js
const gltfLoader = new THREE.GLTFLoader();