【问题标题】:Having trouble with react three fiber: THREE.WebGLRenderer: Context Lost反应三纤维时遇到问题:THREE.WebGLRenderer: Context Lost
【发布时间】:2022-01-07 15:11:36
【问题描述】:

我的骰子出现错误

THREE.WebGLRenderer:上下文丢失。

我不确定,但我认为这与从 use-cannon 切换到 @react-three/cannon 有关。当我尝试渲染 D4 时,我得到了错误。然而,这个盒子似乎工作得很好,这就是为什么我对接下来应该尝试什么来解决这个问题有点迷茫。


import * as THREE from 'three'
import ReactDOM from 'react-dom'
import React, { Suspense, useMemo, useState, useEffect } from 'react'
import { Canvas, useFrame, useLoader } from '@react-three/fiber'
import { Physics, useBox, usePlane, useSphere, useConvexPolyhedron } from '@react-three/cannon'
import niceColors from 'nice-color-palettes'
import './styles.css'
// import { useConvexPolyhedron } from '@react-three/cannon';
import { Tetrahedron, RoundedBox } from '@react-three/drei';

import { TestDice } from './TestDice.js'





const DiceOverview = (props) => {

  // console.log('trigger check');
  // console.log(dice, 'something odd');
  //radius and the rest are all passed in so likely radius adjusts these values.



  function Plane({ color, ...props }) {
    const [ref, api] = usePlane(() => ({ ...props }))
    return (
      <mesh ref={ref} receiveShadow >
        <planeBufferGeometry onClick={() => api.applyImpulse([0, 20, 0], [0, 0, 0])} attach="geometry" args={[1000, 1000]} />
        <meshPhongMaterial attach="material" color={color} />
      </mesh>
    )
  }


  function Box() {
    const [ref, api] = useBox(() => ({ mass: 1, args: [4, 4, 4], isKinematic: true }))
    useFrame(state => {
      const t = state.clock.getElapsedTime()
    })
    return (
      <mesh ref={ref} castShadow receiveShadow>
        <boxBufferGeometry attach="geometry" args={[4, 4, 4]} />
        <meshLambertMaterial attach="material" color={'#3F00FF'} side={THREE.DoubleSide} />
      </mesh>
    )
  }


  const D4 = (props) => {
    console.log(props, 'what is D4 getting?')
    console.log(Tetrahedron, 'this is tetrahedron');
    console.log('hello');
    const radius = 1;
    const tetrahedronGeometry = new THREE.TetrahedronGeometry(radius);
    const [ref, api] = useConvexPolyhedron(() => {
      return {
        args: tetrahedronGeometry,
        mass: 1,
        ...props,
      };
    });
    console.log(ref, api, 'are these triggering');

    return (
  <Tetrahedron
    args={radius}
    ref={ref}
    onClick={() => api.applyImpulse([0, 20, 40], [0, 0, 0])}
    castShadow
    receiveShadow
  >
        <meshNormalMaterial attach="material" />
      </Tetrahedron>
    );
  };


  const Dtest = (props) => {


    return (
      <RoundedBox
        args={[1, 1, 1]} // Width, Height and Depth of the box
        radius={0.05} // Border-Radius of the box
        smoothness={4} // Optional, number of subdivisions
        position={[0, 0, 10]}
      >
        <meshPhongMaterial attach="material" color="#f3f3f3" wireframe />
      </RoundedBox>

    );
  };



  const glProps = { alpha: false };
  const cameraProps = { position: [0, -12, 16] };
  // let keys = Object.keys(props)

  // console.log('what is this color', niceColors[10][4])
  return (
    <>
      <button onClick={() => { console.log('click') }}>button</button>
      <button>toss</button>
      <TestDice />
      <div style={{ position: "relative", width: 500, height: 500 }}>
        <Canvas id="myCanvas" concurrent shadowMap sRGB gl={glProps} camera={cameraProps} width={100}>
          <hemisphereLight intensity={0.35} />
          <spotLight position={[30, 0, 30]} angle={0.3} penumbra={1} intensity={2} castShadow shadow-mapSize-width={256} shadow-mapSize-height={256} />
          <pointLight position={[-30, 0, -30]} intensity={0.5} />

          <Physics className='physics' gravity={[0, 0, -60]}>
            <Plane color={niceColors[10][4]} />
            <Plane color={niceColors[10][1]} position={[-6, 0, 0]} rotation={[0, 0.9, 0]} />
            <Plane color={niceColors[10][2]} position={[6, 0, 0]} rotation={[0, -0.9, 0]} />
            <Plane color={niceColors[10][3]} position={[0, 6, 0]} rotation={[0.9, 0, 0]} />
            <Plane color={niceColors[10][0]} position={[0, -6, 0]} rotation={[-0.9, 0, 0]} />

            <D4 position={[-4, 0, 2]} rotation={[0, 1, 0]} />

            <Dtest />
            {/* {promise} */}
            <Box />
          </Physics>
        </Canvas>
      </div>
    </>
  );
};

export default DiceOverview;

这是我的package.JSON 文件:

{
  "name": "basictemplate",
  "version": "1.0.0",
  "description": "template to use in future projects",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode development --watch"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/jessemchung/template.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/jessemchung/template/issues"
  },
  "homepage": "https://github.com/jessemchung/template#readme",
  "devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/preset-env": "^7.15.0",
    "@babel/preset-react": "^7.14.5",
    "babel-loader": "^8.2.2",
    "css-loader": "^6.2.0",
    "style-loader": "^3.2.1",
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "@react-three/cannon": "^3.0.1",
    "@react-three/drei": "^7.5.0",
    "@react-three/fiber": "^7.0.6",
    "nice-color-palettes": "^3.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "three": "^0.132.2"
  }
}

经过更多测试,我相信useConvexPolyhedron 是给我带来麻烦的原因。具体来说,我认为问题与args 值有关。更新版本中可能发生了某些变化,因为它与 use-cannon 一起使用(现已弃用)。

大家长话短说,我给github负责人发了消息,他们给了很好的解释。

https://github.com/pmndrs/use-cannon/issues/274

基本上,这已被弃用,他们现在的做法有所不同

【问题讨论】:

  • 欢迎来到 Stack Overflow! Context lost 是您遇到的唯一错误吗?它没有任何堆栈跟踪吗?如果有,请edit您的问题包含完整的错误消息。谢谢!
  • 对不起。这就是控制台日志所说的全部内容。我不知道堆栈跟踪是什么。话虽如此,我确实找出了问题所在。实际上是因为版本的变化导致它被弃用。
  • 嘿 Amilar,我也遇到同样的错误,你能帮我解决吗?很紧急
  • 嘿,长话短说,使用此功能的功能已被弃用。在与 Github 上的人交谈后,他们基本上指导我查看他们的示例内容。 github.com/pmndrs/use-cannon/issues/274这基本上是我的讨论。

标签: three.js react-three-fiber


【解决方案1】:

添加恢复上下文功能

restoreContext() {
    const canvas = renderer.domElement;
    canvas.addEventListener(
      'webglcontextlost',
      function (event) {
        event.preventDefault();
        setTimeout(function () {
          renderer.forceContextRestore();
        }, 1);
      },
      false
    );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 2021-04-18
    • 2021-07-11
    • 2021-05-09
    • 2021-06-08
    • 2021-08-09
    • 2021-03-15
    相关资源
    最近更新 更多