threejs加载动画对象无法解码

threejs loading animated object cannot decode

大家好,我是 threejs 的新手,我正在尝试导入我从网上下载的这个动画恐龙。我正在尝试在我的网站中加载和导入 运行,但遇到了一些错误。对于文件结构,material_diffuse.png 和 material_specularGlossiness.png 以及 scene.gltf 和 scene.bin 都在与 index.html.

相同的文件夹中

列出了我遇到的一些错误。

textures/Material_diffuse.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)

textures/Material_specularGlossiness.png:1 Failed to load resource: the server responded with a status of 404 (Not Found)

GLTFLoader.js:127 DOMException: The source image could not be decoded.

这是我的代码

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>3d model</title>
    <style>
      body {
        margin: 0;
      }
      canvas {
        display: block;
      }
    </style>
  </head>
  <body>
    <script src="three.js"></script>
    <script type="module" src="GLTFLoader.js"></script>
    <script type="module">
      import { GLTFLoader } from "./GLTFLoader.js";

      var scene = new THREE.Scene();

      var camera = new THREE.PerspectiveCamera(
        75,
        window.innerWidth / window.innerHeight,
        0.01,
        1000
      );
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      var loader = new GLTFLoader();

      var obj;
      loader.load("scene.gltf", function (gltf) {
        obj = gltf.scene;
        scene.add(gltf.scene);
      });
      scene.background = new THREE.Color(0xffffff);
      var light = new THREE.HemisphereLight(0xffffff, 0x444444);
      scene.add(light);
      // camera.position.set(0, 5, 30);
      camera.position.set(0, 5, 8);

      function animate() {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
      }
      animate(); 

      function rotateFunction() {
        obj.rotation.y += 0.01;
      }

     document.addEventListener('scroll', function(e) { rotateFunction() });
    </script>
    <div>
      
    </div>
  </body>
</html>

提前致谢。

根据报告的错误,文件结构应如下所示:

+-- scene.gltf
+-- scene.bin
+-- textures
|   +-- Material_diffuse.png
|   +-- Material_specularGlossiness.png

意味着您必须将纹理放入 textures 目录。

此外,您的应用导入错误。在您的应用运行之前暂时使用这些导入:

<script type="module">
import * as THREE from 'https://cdn.skypack.dev/three@0.135.0/build/three.module.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.135.0/examples/jsm/loaders/GLTFLoader.js';