I'm trying to load a gltf model in react-three-fiber (R3F) and I am having a nightmare. I have tried looking for the answer and there have been people with a similar problem but I was not able to solve my issue.
I keep getting this in the console:
at JSON.parse (<anonymous>)
at GLTFLoader.parse (GLTFLoader.js:213)
at Object.onLoad (GLTFLoader.js:145)
at XMLHttpRequest.<anonymous> (three.module.js:35829)
As far as I am aware, there is nothing wrong with my code. I've tried writing it loading in the model in in many different ways. The creator of react-three-fiber loaded in a model to his codesandbox a few days ago like this: https://codesandbox.io/s/r3f-gltf-useloader-8nb5i - so I feel it can't be an issue with R3F. I noticed that he was using a glb rather than a gltf file so I went and found a glb model to check if that would make a difference. I realised I also have to have my model in the public folder, which I have also done. Unfortunately, this did not make a difference and I still continue to get the issue.
I have tried like this:
import React, { Suspense } from "react";
import { Canvas, useLoader } from "react-three-fiber";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import * as THREE from "three";
import Box from "./Components/Sphere.Component";
import Plane from "./Components/Plane.Component";
import Controls from "./Components/Controls.Component";
import "./App.css";
const Chair = () => {
const { nodes } = useLoader(GLTFLoader, "../public/mindbreaker.glb");
return (
<mesh geometry={nodes.Cube.geometry}>
<meshStandardMaterial attach="material" color="lightblue" />
</mesh>
);
};
function App() {
return (
<Canvas
camera={{ position: [0, 0, 5] }}
onCreated={({ gl }) => {
gl.shadowMap.enabled = true;
gl.shadowMap.type = THREE.PCFSoftShadowMap;
}}
>
<fog attach="fog" args={["pink", 5, 15]} />
<Plane />
<Controls />
<Box />
<Suspense fallback={null}>
<Chair />
</Suspense>
</Canvas>
);
}
export default App;
Here is a person that had a similar issue: https://discourse.threejs.org/t/json-or-gltf-loader-for-three-js-in-react/3898/10 but I still don't really understand how to solve this problem.
Any help would be greatly appreciated, I was so looking forward to having a play with R3F but I seem to have fallen at the first hurdle. Someone please save me from this headache! haha.
Thank you!