0

I am trying to load a .glb file with react-three-fiber and I get the following error.

Error Unexpected token c in JSON at position 3

I'm not sure what I am doing wrong - it appears the most common solution to this problem is having the .glb file in your public folder (which I do). So I'm at a loss here.

Here is the codesandbox

https://codesandbox.io/s/gltfloader-forked-5nkzl?file=/src/App.js

Here is the code anyways:

import "./styles.css";
import { Suspense } from "react";
import { Canvas, useLoader } from "@react-three/fiber";
import { Environment, OrbitControls } from "@react-three/drei";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";


const Model = () => {
 const gltf = useLoader(GLTFLoader, './scene.glb');
  return (
  <>
  <primitive object={gltf.scene} scale={0.4} />
</>
);
};

export default function App() {
 return (
  <div className="App">
  <Canvas>
    <Suspense fallback={null}>
      <Model />
      <OrbitControls />
      <Environment preset="sunset" background />
    </Suspense>
  </Canvas>
</div>
);
}

Any help would be immensely appreciated.

3 Answers 3

0

./scene.glb is not a valid path. you can't fetch from relative paths in general. either put scene.glb into /public and use /scene.glb or import sceneUrl from "./scene.glb". the unexpected token error you get is because three's loader receives a 404 response.

Sign up to request clarification or add additional context in comments.

1 Comment

i appreciate you responding. I'm not sure what you mean though because I already have scene.glb in my public folder (as you can see in the codesandbox link above).
0

Importing the sceneUrl should work.

import "./styles.css";
import { Suspense } from "react";
import { Canvas, useLoader } from "@react-three/fiber";
import { Environment, OrbitControls } from "@react-three/drei";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

//import sceneUrl
import sceneUrl from './scene.glb'


const Model = () => {
 const gltf = useLoader(GLTFLoader, sceneUrl);
  return (
  <>
  <primitive object={gltf.scene} scale={0.4} />
</>
);
};

export default function App() {
 return (
  <div className="App">
  <Canvas>
    <Suspense fallback={null}>
      <Model />
      <OrbitControls />
      <Environment preset="sunset" background />
    </Suspense>
  </Canvas>
</div>
);
}

Comments

0

I had the same problem for 2days

I fixed it by just using npx gltfjsx [Model.js] [options] that is an npm package

just run the command npx gltfjsx [you model here .glb] it will generate a jsx file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.