reactjsthree.jsnext.jsreact-three-fiberreact-three-drei

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


Solution

  • 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"
      }
    }