[英]Importing useGLTF from '@react-three/drei/useGLTF' cause Module not found:
[英]@react-three/drei stuff not working with next.js
我無法使用 drei 材質和其他東西,例如 MeshWobbleMaterial 或 MeshDistortMaterial 甚至 ContactShadows,我總是收到錯誤消息,例如:
react-three-fiber.esm.js:1383 Uncaught TypeError: Cannot read property 'getState' of null at useFrame (react-three-fiber.esm.js:1383) at 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 很難解決這些問題。
您是否嘗試在 Canvas 的子代中設置您的 useMemo?
我的猜測是您從場景上下文中實例化了 DirectionalLight。 這可能會導致這個錯誤。
澄清一下,我走在正確的道路上,但是一些軟件包導致了這個錯誤,在一段時間無法解決這個問題之后,我卸載並安裝了所有依賴項,並且在此期間更新了很多依賴項,然后它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.