首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@react-three/drei时useGLTF崩溃场景

使用@react-three/drei时useGLTF崩溃场景
EN

Stack Overflow用户
提问于 2021-09-01 14:34:14
回答 1查看 584关注 0票数 1

大家好,我这样做有点让我自己发疯,但我正在尝试设置一个简单的three.js场景,但是当我尝试使用@react-three/drei中的useGLTF函数时,场景崩溃了,我的控制台说:

Warning: meshopt_decoder is using experimental SIMD support

代码语言:javascript
复制
The above error occurred in the <ForwardRef(Canvas)> component:

    at Canvas (http://localhost:3000/_next/static/chunks/pages/index.js?ts=1630506543315:10124:3)
    at div
    at Layout (http://localhost:3000/_next/static/chunks/pages/index.js?ts=1630506543315:502:23)
    at Home
    at wrappedComponent (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1630506543315:457:73)
    at wrappedComponent (http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1630506543315:457:73)
    at ErrorBoundary (http://localhost:3000/_next/static/chunks/main.js?ts=1630506543315:764:47)
    at ReactDevOverlay (http://localhost:3000/_next/static/chunks/main.js?ts=1630506543315:880:23)
    at Container (http://localhost:3000/_next/static/chunks/main.js?ts=1630506543315:8865:5)
    at AppContainer (http://localhost:3000/_next/static/chunks/main.js?ts=1630506543315:9361:24)
    at Root (http://localhost:3000/_next/static/chunks/main.js?ts=1630506543315:9500:25)

React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.
代码语言:javascript
复制
THREE.WebGLRenderer: Context Lost.

我的react场景如下所示:

代码语言:javascript
复制
import React, { Suspense } from 'react'
import { Canvas } from '@react-three/fiber'
import Layout from '../components/layout'
import { Sky, OrbitControls, useGLTF } from '@react-three/drei'

const Skull = () => {
  const { nodes, materials  } = useGLTF('skull.gltf')
  console.log(nodes)

  return (
    <Suspense fallback={null}>
      <mesh geometry={nodes.mesh_0.geometry} />
    </Suspense>
  )
}

export default function Home() {
  return (
      <Layout>
        <Canvas>
          <Sky sunPosition={[0, 1, 0]} />
          <Skull />
          <OrbitControls />
        </Canvas>
      </Layout>
  )
}

和我的包:

代码语言:javascript
复制
{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@material-ui/core": "^4.12.3",
    "@material-ui/lab": "^4.0.0-alpha.60",
    "@react-three/drei": "^7.5.0",
    "@react-three/fiber": "^7.0.6",
    "add": "^2.0.6",
    "axios": "^0.21.1",
    "mobx": "^6.3.2",
    "mobx-react": "^7.2.0",
    "next": "11.1.2",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "sass": "^1.38.2",
    "three": "^0.132.2",
    "three-stdlib": "^2.4.0",
    "yarn": "^1.22.11"
  },
  "devDependencies": {
    "esbuild-loader": "^2.15.1",
    "eslint": "7.32.0",
    "eslint-config-next": "11.1.2"
  }
}

我不知道这是怎么回事,控制台错误的描述性不够,让我无法理解。有没有什么明显的步骤我在这里漏掉了?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-09-02 13:36:14

我最终找到了一种让一切正常工作的方法。我必须添加一个调用来预加载gltf文件,然后一切都运行正常。我更新了我的默认组件,如下所示:

代码语言:javascript
复制
export default function Home() {
  useGLTF.preload("skull.gltf")
  return (
      <Layout>
        <Canvas>
          <Sky sunPosition={[0, 1, 0]} />
          <Skull />
          <OrbitControls />
        </Canvas>
      </Layout>
  )
}

在此之后,我在加载场景时没有遇到错误,一切都按预期进行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69015790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档