首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@react 3/drei不使用next.js的东西

@react 3/drei不使用next.js的东西
EN

Stack Overflow用户
提问于 2021-04-16 21:09:16
回答 2查看 1.5K关注 0票数 1

我不能使用MeshWobbleMaterial或MeshDistortMaterial,甚至ContactShadows之类的材料和其他东西,我总是会收到一个错误,例如:

TypeError:无法读取属性'getState‘of null at useFrame (react tri-fiber.esm.js:1383) at MeshDistortMaterial.js:72 (以MeshDistortMaterial为例)

它所指的这个空值是一个“useContext(上下文)”,它位于react上的useFrame模块中--三个光纤。我认为这与我正在使用的next 10.1.3版本有关。

以下是整个代码:

代码语言:javascript
复制
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-模块并创建下一个配置文件。我做到了,这是我的next.config.js

代码语言:javascript
复制
const withTM = require('next-transpile-modules')(['three', '@react-three/drei'])

module.exports = withTM()

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-14 16:19:53

为了澄清,我走上了正确的道路,但是一些包导致了这个错误的发生,在一段时间无法解决这个问题之后,我对所有的依赖程序进行了隔离和安装,其中很多都在这段时间内进行了更新,然后它就正常工作了。

票数 0
EN

Stack Overflow用户

发布于 2021-04-22 09:11:00

你试过在画布的孩子中设置你的useMemo吗?

我的猜测是,您在场景上下文之外实例化一个DirectionalLight。这可能会导致这个错误。

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

https://stackoverflow.com/questions/67132225

复制
相关文章

相似问题

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