我是three.js和@react-fiber/three的新手,我创建了一个带有3个平面的简单盒子,使用了一个spotLight。我试图在飞机上投下盒子的阴影,但它不起作用。
让我给你看我的密码:
Test.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;在src//中的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;如您所见,我已经将castShadow添加到以下内容中:
和receiveShadow到飞机上。
令我惊讶的是,它还是不起作用。
发布于 2021-06-27 14:26:59
反作用-三纤维(@react-three/fiber)显示画布组件的属性:shadowMaps没有按预期工作,但在研究(也称为googling)之后,我找到了这个视频。
<Canvas
shadows
>
</Canvas>shadows属性是我缺少的东西。
发布于 2021-06-30 08:02:41
而不是使用meshPhongMaterial为您的飞机使用meshStandardMaterial,它将修复这个问题。您可以在这里看到活生生的例子,https://codesandbox.io/s/nice-cloud-rb6tj?file=/src/App.js
https://stackoverflow.com/questions/68150285
复制相似问题