【问题标题】:React three fiber Shadow material not working反应三纤维阴影材料不起作用
【发布时间】:2021-08-11 02:08:31
【问题描述】:

以下是代码,旋转网格不会在阴影材质上投射任何阴影。 planeBufferGeometry 似乎在那里。 -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------

import React from 'react';
import './App.scss';
import {Canvas, useFrame} from 'react-three-fiber';


const SpinningMesh = ({position, args, color}) => {

  const mesh = React.useRef(null);
  useFrame(()=> (mesh.current.rotation.x = mesh.current.rotation.y += 0.01))

  return(
        <mesh castShadow ref={mesh} position={position}> 
          <boxBufferGeometry attach='geometry' args={args}/>
          <meshStandardMaterial attach='material' color={color}/>
        </mesh>
  );
}

function App() {

  return (
    <>
      <Canvas shadowMap colorManagement camera={{position:[-5,2,10], fov:60}}>
        <ambientLight intensity={0.3}/>
        <directionalLight
          castShadow
          position={[0,10,0]}
          intensity={1.5}
          shadow-mapSize-width={1024}
          shadow-mapSize-height={1024}
          shadow-camera-far={50}
          shadow-camera-left = {-10}
          shadow-camera-right = {10}
          shadow-camera-top = {10}
          shadow-camera-bottom = {-10}
        />
        <pointLight position={[-10,0,-20]} intensity={.5}/>
        <pointLight position={[0,-10,0]} intensity={1.5}/>

        <group>
          <mesh receiveShadow rotation={[-Math.PI / 2 , 0 , 0]} position={[0,-3,0]}>
            <planeBufferGeometry attach='geometry' args={[100,100]}/>
            <shadowMaterial attach='material' opacity={0.3} color="blue"/>
          </mesh>
        </group>


        <SpinningMesh position={[0,1,0]} args={[3,2,1]} color='lightblue'/>
        <SpinningMesh position={[-2,1,-5]} color='pink'/>
        <SpinningMesh position={[5,1,-2]} color='pink'/>
      </Canvas>
    </>
  );
}

export default App;

output

【问题讨论】:

  • hmm 有趣,react 调试工具可能会有所帮助
  • 命名空间是 @react-three/fiber 而不是 react-three-fiber。阴影的道具称为“阴影”。网格需要 castShadow 和可选的 receiveShadow 道具,材质为 receiveShadow 道具。这是一个例子:codesandbox.io/s/gltf-animations-re-used-k8phr

标签: reactjs three.js shadow react-three-fiber


【解决方案1】:

&lt;Canvas shadows colorManagement camera={{position:[-5,2,10], fov:60}}&gt;

在 Canvas 中使用 shadows 属性而不是使用 shadowMap

【讨论】:

  • 还是不行,怎么回事?反应三纤维 = 7.0.17
【解决方案2】:

如果您希望旋转网格进行任何阴影投射,请不要忘记您还必须将 castShadow 设置为 true:

<SpinningMesh castShadow position={[0,1,0]} args={[3,2,1]} color='lightblue'/>

See here for a quick example in the docs

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2021-07-11
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2022-12-23
    • 2021-05-09
    • 2021-04-18
    相关资源
    最近更新 更多