【问题标题】:@react-three/drei stuff not working with next.js@react-three/drei 东西不适用于 next.js
【发布时间】:2021-07-11 21:25:55
【问题描述】:

我无法使用 drei 材质和其他东西,例如 MeshWobbleMaterial 或 MeshDistortMaterial 甚至 ContactShadows,我总是收到如下错误:

react-three-fiber.esm.js:1383 Uncaught TypeError: Cannot read property 'getState' of null 在 useFrame (react-three-fiber.esm.js:1383) 在 MeshDistortMaterial.js:72 (在 MeshDistortMaterial 的情况下)

它所指的这个 null 是 react-three-fiber 上的 useFrame 模块内的一个 'useContext(context)'。 我认为这与我也在使用下一个版本 10.1.3 的事实有关。

这是完整的代码:

import { useRef, useState, useMemo } from 'react'
import { Canvas } from 'react-three-fiber'
import { Box, MeshDistortMaterial } from '@react-three/drei'
import * as THREE from 'three'
import MyCamera from '../three/MyCamera'

//HERES WHERE THE MATERIAL IS USED
const MyBox = function (props) {
    const mesh = useRef()
  
    const [hovered, setHover] = useState(false)
    const [active, setActive] = useState(false)
  
    return (
      <Box
        args={[1, 1, 1]}
        {...props}
        ref={mesh}
        scale={active ? [6, 6, 6] : [5, 5, 5]}
        onClick={() => setActive(!active)}
        onPointerOver={() => setHover(true)}
        onPointerOut={() => setHover(false)}
        castShadow
      >
      
        <MeshDistortMaterial
          attach="material"
          distort={1} // Strength, 0 disables the effect (default=1)
          speed={10} // Speed (default=1)
        />

      </Box>
    )
}

//HERES JUST THE CANVAS STUFF
export default function Main() { 

  const dirLight = useMemo(()=>{
  
    const light = new THREE.DirectionalLight('white');
    light.castShadow=true;
    //Set up shadow properties for the light
    light.shadow.mapSize.width = 10240 // default
    light.shadow.mapSize.height = 10240 // default
    light.shadow.camera.near = 0.1 // default
    light.shadow.camera.far = 5000 // default
    light.shadow.camera.top = -100 // default
    light.shadow.camera.right = 100 // default
    light.shadow.camera.left = -100 // default
    light.shadow.camera.bottom = 100 // default
    return light
  
  },[])

  return (
    <div className="canvasContainer">
    <Canvas 
      linear = "true"
      frameloop="demand" 
      shadows = "true"
      shadowMap
    >
      
      <MyCamera position={[0, 0, 30]} infLimit={-1000} supLimit ={0} />
      
      <ambientLight intensity={0.2}/>

      <primitive object={dirLight} position={[30, 0, 30]} />
      <primitive object={dirLight.target} position={[0, 0, 0]}  />
      
      <mesh receiveShadow position={[0,0,-2]}>
        <planeBufferGeometry attach="geometry" args={[1000, 1000]} />
        <meshStandardMaterial attach="material" color="gray" />
      </mesh>
      
      <MyBox position={[8,0,0]}/>

    </Canvas>
    </div>
  )
}

有办法解决吗?

我知道使用 next 你需要安装 next-transpile-modules 并创建下一个配置文件。我做到了,这是我的 next.config.js

const withTM = require('next-transpile-modules')(['three', '@react-three/drei'])

module.exports = withTM()

非常感谢您的帮助,使用 next.js 解决这些问题非常困难。

【问题讨论】:

    标签: javascript reactjs three.js next.js react-three-fiber


    【解决方案1】:

    您是否尝试将您的 useMemo 设置为 Canvas 的子级?

    我的猜测是您从场景上下文中实例化了 DirectionalLight。 这可能会导致此错误。

    【讨论】:

      【解决方案2】:

      澄清一下,我走在正确的道路上,但是一些软件包导致了这个错误发生,一段时间后无法解决这个问题,我卸载并安装了所有依赖项,其中很多在期间更新这一次,它工作正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 2021-07-02
        • 2021-12-23
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        • 2022-12-16
        相关资源
        最近更新 更多