反应纤维三投射阴影不起作用

Jar*_*seg 3 three.js reactjs react-three-fiber

three.js我是和的新手@react-fiber/three,我创建了一个带有 3 个平面的简单盒子,使用了聚光灯。我试图将盒子的阴影投射到飞机上,但它不起作用。

让我向你展示我的代码:

测试.js

App.js中导入以下代码

import React from "react";
import { Canvas } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
import Scene from "./Scenes/Scene";

const Test = () => {
  return (
    <Canvas
      colorManagement
      shadowMap
      camera={{ position: [15, 15, 15], fov: 60 }}
    >
      {/* <ambientLight color="#ffffff" intensity={0.1} /> */}
      <spotLight
        position={[2, 5, 2]}
        color="#ffffff"
        intensity={2.5}
        shadow-mapSize-height={1024}
        shadow-mapSize-width={1024}
        shadow-camera-far={50}
        shadow-camera-left={-10}
        shadow-camera-right={10}
        shadow-camera-top={10}
        shadow-camera-bottom={-10}
        castShadow
      />

      <Scene />
      <OrbitControls enablePan={true} enableZoom={true} enableRotate={true} />
    </Canvas>
  );
};

export default Test;

Run Code Online (Sandbox Code Playgroud)

src/Scenes/ 中的 Scene.js

在Test.js中导入以下代码(上图)


import React, { useRef } from "react";
import { useFrame } from "@react-three/fiber";
import { Box, Plane } from "@react-three/drei";

const Scene = () => {
  const boxRef = useRef();
  useFrame(() => {
    boxRef.current.rotation.y += 0.001;
    boxRef.current.rotation.x += 0.001;
    boxRef.current.rotation.z += 0.001;
  });
  return (
    <group>
      <Box
        ref={boxRef}
        castShadow
        receiveShadow
        position={[0, 0.5, 0]}
        args={[3, 3, 3]}
      >
        <meshStandardMaterial attach="material" color="#C42727" />
      </Box>

      <Plane
        receiveShadow
        rotation={[-Math.PI / 2, 0, 0]}
        position={[0, -2, 0]}
        args={[15, 15]}
      >
        <meshPhongMaterial attach="material" color="#ffffff" />
      </Plane>

      <Plane
        receiveShadow
        rotation={[0, 0, 0]}
        position={[0, 5.5, -7.5]}
        args={[15, 15]}
      >
        <meshPhongMaterial attach="material" color="#ffffff" />
      </Plane>

      <Plane
        receiveShadow
        rotation={[0, Math.PI / 2, 0]}
        position={[-7.5, 5.5, 0]}
        args={[15, 15]}
      >
        <meshPhongMaterial attach="material" color="#ffffff" />
      </Plane>
    </group>
  );
};

export default Scene;
Run Code Online (Sandbox Code Playgroud)

如您所见,我已将castShadow 添加到以下内容:

  • 聚光灯
  • 盒子

并接收位面之影。

令我惊讶的是,它仍然不起作用。

Jar*_*seg 5

事实证明,react- Three-Fiber ( @react-three/fiber)的 Canvas 组件的属性shadowMaps没有按预期工作,但经过一番研究(又名谷歌搜索)后,我发现了这个视频

<Canvas
    shadows
>
</Canvas>
Run Code Online (Sandbox Code Playgroud)

shadows财产是我所缺少的。