Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is three.js cast shadow not working on a 3D model

I have 3D models as such:

model

I want to add a cast shadow similar to this:

shadow

And I have the following piece of code responsible for the model:

var ambLight = new THREE.AmbientLight( 0x404040 );
this.scene.add(ambLight)

var loader = new THREE.GLTFLoader();
loader.load(path,function (gltf) {
 gltf.scene.traverse( function( model ) {
  if (model.isMesh){ 
   model.castShadow = true; 
  }
 });
 this.scene.add(gltf.scene);
}

I added the castSHadow part as seen in this StackOverflow post.

I've tried model.castShadow = true and I've tried removing the if condition and just leave the castShadow but that doesn't work either. Am I missing a step? The full custom layer code is here if it helps.

like image 621
TiagoRibeiro Avatar asked Jan 27 '26 00:01

TiagoRibeiro


1 Answers

  • You only have an instance of AmbientLight in your scene which is no shadow-casting light.
  • 3D objects can only receive shadow if they set Object3D.receiveShadow to true and if the material is not unlit. Meaning MeshBasicMaterial would not work as the ground's material.
  • You have to globally enable shadows via: renderer.shadowMap.enabled = true;

I suggest you have a closer look to the shadow setup of this official example.

like image 78
Mugen87 Avatar answered Jan 28 '26 12:01

Mugen87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!