reactjs 用React Three Fiber和Gltf添加阴影

ogq8wdun  于 2023-04-29  发布在  React
关注(0)|答案(1)|浏览(141)

我正在使用react three fiber和vectary的gltf,我试图在我的<planeBufferGeometry/〉上添加gltf的阴影。它不工作,我不知道我做错了什么。
代码如下:

softShadows();

const ArcCircleGltf = () => {
   const gltf = useLoader(GLTFLoader, '/assets/gltf/portfolio.gltf');
   return (
      <Suspense fallback={null}>
         <primitive object={gltf.scene} />
      </Suspense>
   );
};

const ArcCircle = () => {
   const reactGltf= useRef<three.Mesh>();

   useFrame(() => {
      reactGltf.current!.rotation.x += 0.003;
      reactGltf.current!.rotation.y += 0.003;
   });

   return (
      <mesh ref={reactGltf} position={[0, 0, 0]} castShadow>
         <ArcCircleGltf />
      </mesh>
   );
};

const Plane = () => {
   const plane = useRef<three.Mesh>(null);

   return (
      <mesh ref={plane} rotation={[-Math.PI / 2, 0, 0]} position={[0, -4, 0]} receiveShadow>
         <planeBufferGeometry attach='geometry' args={[100, 100]} />
         <shadowMaterial attach='material' color='white' opacity={0.2} />
      </mesh>
   );
};

const CanvasComp = () => {
   return (
      <Canvas shadows camera={{ position: [5, 0, 5], fov: 5 }}>
         <ambientLight intensity={0.1} />
         <directionalLight
            position={[5, 5, 4]}
            intensity={1}
            castShadow
            shadow-mapSize-height={512}
            shadow-mapSize-width={512}
            shadow-camera-far={50}
            shadow-camera-left={-10}
         />
         <pointLight position={[1, 1, 0]} intensity={1} />
         <ArcCircle />
         <Plane />
         <OrbitControls />
      </Canvas>
   );
};

关于所有的教程和论坛上,它看起来像我做的一切都好。我认为planeBufferGeometry设置不正确,但我显然不确定。

kuarbcqp

kuarbcqp1#

嗨,我不确定这是否仍然与你有关,但我也无法让我的gltfs投下阴影,直到我添加了这行代码。

gltf.scene.traverse( function( node ) {
        if ( node.isMesh ) { node.castShadow = true; }
    } );

显然,您需要将castShadow属性添加到组成gltf模型的所有子网格。
How to cast a shadow with a gltf model in three.js?

相关问题