简体   繁体   English

Three.js GLTFLoader 未正确读取 .glb 文件

[英]Three.js GLTFLoader is not reading .glb file correctly

I created a simple React app that has a scene initialized with three.js. Im getting the following error when loading a model with GLTFLoader.我创建了一个简单的 React 应用程序,它有一个用 three.js 初始化的场景。我在使用 GLTFLoader 加载 model 时收到以下错误。

GLTFLoader.js:192 SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
at JSON.parse (<anonymous>)
at GLTFLoader.parse (GLTFLoader.js:317:1)
at Object.onLoad (GLTFLoader.js:212:1)
at three.module.js:40464:1

I imported the following at the top of my App.js component我在 App.js 组件的顶部导入了以下内容

import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';

and in my function App() component I did the following:在我的 function App() 组件中,我执行了以下操作:

useEffect(() =>{
    const test = new SceneInit('myCanvas');
    test.initialize();
    test.animate();

    // Load model
    const glftLoader = new GLTFLoader();
    glftLoader.load('./football.glb', (gltfscene) => {
      test.scene.add(gltfscene.scene);
    })

  }, []);

I am positive that the file path to my.glb model is correct and am confused on why the GLTFLoader is not able to read the contents of the file.我确信 my.glb model 的文件路径是正确的,并且对 GLTFLoader 无法读取文件内容的原因感到困惑。 If I change the file path I get the same exact error.如果我更改文件路径,我会得到完全相同的错误。 The SceneInit class sets up a basic three.js scene and works correctly with a regular sphere. SceneInit class 设置了一个基本的 three.js 场景,并且可以正确地使用常规球体。

Folder Directory文件夹目录

I was able to resolve this by simply putting the model inside the public folder directory.我只需将 model 放入公用文件夹目录即可解决此问题。 No idea why three.js can't just read the file path directly.不知道为什么 three.js 不能直接读取文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM