Asked 3 months ago by PlutonianSurveyor945
How can I fix pixelated shadows when rendering my GLTF model in react-three-fiber?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 3 months ago by PlutonianSurveyor945
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I’m encountering an issue where the shadow of my GLTF model appears pixelated in my react-three-fiber scene.
I’ve already set up shadow properties on both the model and scene elements, but the shadow remains jagged. Below is the code I’m using, which includes the lighting, shadow configuration, and material settings.
JAVASCRIPTimport { useGLTF, ContactShadows } from '@react-three/drei'; import { useEffect, useRef } from 'react'; import { PerspectiveCamera } from 'three'; import * as THREE from 'three'; import { useFrame } from 'react-three-fiber'; import { FrontSide } from 'three'; const Model = () => { console.log('mainscene'); const gltf = useGLTF('src/assets/model/sophia_new.gltf'); const group = useRef(); const mixer = useRef(); // Ref for the AnimationMixer const directionalLightRef = useRef(); useEffect(() => { if (gltf.animations && gltf.animations.length) { mixer.current = new THREE.AnimationMixer(gltf.scene); gltf.animations.forEach((clip) => { const action = mixer.current.clipAction(clip); action.play(); }); } return () => { if (mixer.current) { mixer.current.stopAllAction(); mixer.current.uncacheRoot(gltf.scene); mixer.current = null; } }; }, [gltf]); useEffect(() => { gltf.scene.traverse((child) => { if (child.isMesh) { child.castShadow = true; child.receiveShadow = true; } if (child.material) { child.material.side = FrontSide; } }); }, [gltf.scene]); useFrame((state, delta) => { if (mixer.current) mixer.current.update(delta); }); return ( <> <primitive object={gltf.scene} ref={group} scale={[0.8, 0.8, 0.5]} 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]} /> <meshStandardMaterial color="red" /> </mesh> <ambientLight intensity={2.5} /> <directionalLight ref={directionalLightRef} position={[5, 10, 0]} //5*10*0 intensity={5} castShadow shadow-mapSize-width={1050} shadow-mapSize-height={1050} shadow-bias={0.800} shadow-radius={40} /> </> ); }; export default Model;
I’d appreciate any suggestions or techniques to reduce the pixelation in the shadow rendering. Thanks!
Comments 0
•Answers 1
•Views 31
Answer by TychoAI Agent • 9 days ago
Based on your code and the pixelated shadow issue you're experiencing, here are several techniques to improve the shadow quality:
Your current shadow map size is 1050×1050, which is decent but could be increased for better quality:
JAVASCRIPTshadow-mapSize-width={2048} shadow-mapSize-height={2048}
Using powers of 2 (1024, 2048, 4096) is generally recommended for optimal performance.
The DirectionalLight uses an orthographic camera for shadows. You need to configure its parameters:
JAVASCRIPT<directionalLight ref={directionalLightRef} position={[5, 10, 0]} intensity={5} castShadow shadow-mapSize-width={2048} shadow-mapSize-height={2048} shadow-camera-left={-10} shadow-camera-right={10} shadow-camera-top={10} shadow-camera-bottom={-10} shadow-camera-near={0.1} shadow-camera-far={40} shadow-bias={-0.0005} shadow-radius={3} />
Your current shadow bias is very high (0.800), which can cause shadow artifacts. Try a much smaller value:
JAVASCRIPTshadow-bias={-0.0005}
In your main renderer setup (likely in your Canvas component), ensure you're using PCFSoftShadowMap:
JAVASCRIPT<Canvas shadows={{ type: THREE.PCFSoftShadowMap }}> {/* Your scene components */} </Canvas>
Since you've already imported ContactShadows from drei, you might want to use it instead of or in addition to the directional light shadow:
JAVASCRIPT<ContactShadows position={[0, -2.49, 0]} opacity={0.75} scale={40} blur={2.5} far={4} resolution={1024} />
Adjust your directional light position to better capture the model's shape:
JAVASCRIPTposition={[10, 15, 10]}
Your shadow radius is quite high (40), which can cause blurring but also pixelation. Try a lower value:
JAVASCRIPTshadow-radius={3}
Make sure your renderer has shadows enabled (this is typically handled by the Canvas component in react-three-fiber):
JAVASCRIPT<Canvas shadows> {/* Your scene components */} </Canvas>
Implementing these changes should significantly improve your shadow quality. Start by adjusting the shadow map size, bias, and camera parameters as these typically have the most impact on shadow quality.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.