Hi I have this piece of code here where I am loading two different gltf models. I have enabled shadows on all the meshes in each of them. The canvas shadows have also been enabled. Also enabled meshes and added cast shadow and receive shadow properties to the lights as well. But nothing seems to be working. The models are showing up perfectly but without the shadows. This makes the overall scene look extremely flat. Following is my code:
import {OrbitControls,useGLTF,Environment,Html,ContactShadows} from "@react-three/drei"
import {useLoader} from "@react-three/fiber"
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
import { useEffect, useRef } from "react"
import { Group,PerspectiveCamera, SpotLight ,DirectionalLight} from "three"
export default function Object(){
// When loading the GLTF model
const gltf = useLoader(GLTFLoader, '/scene.gltf')
const bg=useLoader(GLTFLoader,'/bg.gltf')
const group = useRef()
const camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
useEffect(() => {
gltf.scene.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
}
});
}, [gltf.scene]);
useEffect(() => {
bg.scene.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
}
});
}, [bg.scene]);
useEffect(() => {
camera.position.set(5,5,5)
camera.lookAt(0,-3.5,-0.2)
}, [camera])
return<>
<OrbitControls
enableZoom={true}
maxDistance={15}
minDistance={1}
minAzimuthAngle={-Math.PI / 8} // Limit to 45 degrees to the left
maxAzimuthAngle={Math.PI / 8}
/>
<primitive object={bg.scene} ref={group} scale={[0.8, 0.8, 0.8]} position={[0,-2,-0.16]} rotation={[0, -Math.PI / 2, 0]} castShadow/>
<primitive object={gltf.scene} ref={group} scale={[0.8, 0.8, 0.8]} position={[0,-2,-0.16]} rotation={[0, -Math.PI / 2, 0]} castShadow/>
<mesh position={[0,-2.5,-0.16]} rotation={[-Math.PI/2,0,0]} receiveShadow>
<planeGeometry args={[50,50]}/>
<meshBasicMaterial color="lightblue"/>
</mesh>
<directionalLight position={[2,3,-0.16]} intensity={5} castShadow shadow-mapSize-width={1024}
shadow-mapSize-height={1024}
/>
<ambientLight intensity={5}/>
<ContactShadows frames={10} position={[0,-2,-0.16]} rotation={[0,-Math.PI/2,0]} scale={0.8} opacity={0.1} blur={0.5} color='black'/>
</>
}