首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应-光纤-三投阴影不起作用

反应-光纤-三投阴影不起作用
EN

Stack Overflow用户
提问于 2021-06-27 10:42:28
回答 2查看 1K关注 0票数 2

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

让我给你看我的密码:

Test.js

以下代码是在App.js中导入的

代码语言:javascript
复制
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;

在src//中的Scene.js

下面的代码是在Test.js中导入的(上面)

代码语言:javascript
复制
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;

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

  • spotLight

和receiveShadow到飞机上。

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

EN

回答 2

Stack Overflow用户

发布于 2021-06-27 14:26:59

反作用-三纤维(@react-three/fiber)显示画布组件的属性:shadowMaps没有按预期工作,但在研究(也称为googling)之后,我找到了这个视频

代码语言:javascript
复制
<Canvas
    shadows
>
</Canvas>

shadows属性是我缺少的东西。

票数 3
EN

Stack Overflow用户

发布于 2021-06-30 08:02:41

而不是使用meshPhongMaterial为您的飞机使用meshStandardMaterial,它将修复这个问题。您可以在这里看到活生生的例子,https://codesandbox.io/s/nice-cloud-rb6tj?file=/src/App.js

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

https://stackoverflow.com/questions/68150285

复制
相关文章

相似问题

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