Varun Pandey
Varun Pandey

Reputation: 78

React three fiber Shadow material not working

The following is the code, the spinning mesh are not casting any shadows on the shadow material. The planeBufferGeometry seems to be there. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

Upvotes: 2

Views: 6912

Answers (3)

Reiko Ochiai
Reiko Ochiai

Reputation: 11

In addition to Shivam and hpalu' answers, you can fix this error for with the combination of changing following:

<Canvas **shadows** colorManagement camera={{position:[-5,2,10], fov:60}}>

Use @react-three/fiber, instead of react-three-fiber

Upvotes: 1

Shivam
Shivam

Reputation: 96

<Canvas shadows colorManagement camera={{position:[-5,2,10], fov:60}}>

Use shadows attribute rather than using shadowMap in Canvas

Upvotes: 8

M -
M -

Reputation: 28507

Don't forget you have to also set castShadow to true if you want your spinning meshes to do any shadow casting:

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

See here for a quick example in the docs

Upvotes: 1

Related Questions