Bill O
Bill O

Reputation: 400

How do I properly use drei useGLTF

I think I'm following the react-three docs and many other examples, but cannot get drei useGLTF to work as others have.

I have a simple, from-scratch, Next|React|react-three/fiber project. I'm simply trying to load the example astronaut and display it.

I believe the following code actually worked perfectly for a while. After some minor changes and undo's I think I arrived back at the same code, which now doesn't work. (I've tried with and without Suspense.)

import { Canvas, Suspense } from '@react-three/fiber'
import { useGLTF } from '@react-three/drei'

function Model() {
  const gltf = useGLTF('https://thinkuldeep.com/modelviewer/Astronaut.glb')
  return (<primitive object={gltf.scene} />)
}

export default function Home() {
  return (
    <div>
      {/* <Suspense> */}
        <Canvas>
          <ambientLight />
          <Model />
        </Canvas>
      {/* </Suspense> */}
    </div>
  )
}

Here is the console message: enter image description here

Here is my package.json:

{
  "name": "cfgnext",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@react-three/drei": "^8.18.10",
    "@react-three/fiber": "^7.0.26",
    "next": "12.1.0",
    "react": "17.0.2",
    "react-dom": "^17.0.2",
    "styled-components": "^5.3.3"
  },
  "devDependencies": {
    "eslint": "8.11.0",
    "eslint-config-next": "12.1.0"
  }
}

From everything I've read this should be quite simple. Can someone please tell me where I'm going wrong.

tia, Bill

Upvotes: 3

Views: 14331

Answers (2)

Bill O
Bill O

Reputation: 400

I updated react, fiber and drei to the latest versions. That caused my original posted code to work (using Suspense from react). Here it is with the change re: Suspense.

import { Suspense } from 'react'
import { Canvas } from '@react-three/fiber'
import { useGLTF } from '@react-three/drei'

function Model() {
  const gltf = useGLTF('https://thinkuldeep.com/modelviewer/Astronaut.glb')
  return (<primitive object={gltf.scene} />)
}

export default function Home() {
  return (
    <div>
      <Suspense>
        <Canvas>
          <ambientLight />
          <Model />
        </Canvas>
      </Suspense>
    </div>
  )
}

Here are the versions that I used:

{
  "name": "cfgnext-updated",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@react-three/drei": "^9.0.1",
    "@react-three/fiber": "^8.0.6",
    "next": "12.1.4",
    "react": "18.0.0",
    "react-dom": "18.0.0"
  },
  "devDependencies": {
    "eslint": "8.12.0",
    "eslint-config-next": "12.1.4"
  }
}

Upvotes: 6

irzhy
irzhy

Reputation: 1137

mine is quite similar but I rather use Suspense differently

<Suspense fallback={null}>
  <Model src={src} containerRef={containerRef.current} />
</Suspense>

Also I added an error boundary to prevent any crash due to rendering

import { withErrorBoundary } from 'react-error-boundary';

// ...

export default withErrorBoundary(ModelViewer, {
  FallbackComponent: () => (<div>An error occured</div>),
  onError: (err: Error, info: {componentStack: string}) => console.error(err, info),
});

Upvotes: 1

Related Questions