Loaded obj + mtl not casting shadow

Even though I’ve tried castShadow = true and receiveShadow = true for the appropriate objects, a model I imported as an obj + mil file will not cast a shadow on anything else in the scene, not even on a plane that is successfully receiving a shadow from another object. Here’s the code ( the “pin” is not casting a shadow on the pedestal [“boxMesh”] ):

			var boxGeometry = new THREE.BoxGeometry( 0.7, 1.7, 0.7 );
            
			var boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
			boxMesh.position.set( -1.3, 0.85, 0.3 );
			boxMesh.castShadow = true;
            boxMesh.needsUpdate = true;
			scene.add( boxMesh );
            
            // load pin
            
            var loader = new THREE.MTLLoader()
				loader.setPath( '/dentoncontemporary/models/' )
				loader.load( 'pinGreen.mtl', function ( materials ) {

					materials.preload();

					var objLoader = new THREE.OBJLoader()
						objLoader.setMaterials( materials )
						objLoader.setPath( '/dentoncontemporary/models/' )
						objLoader.load( 'pinGreen.obj', function ( pin ) {

							pin.scale.x = pin.scale.y = pin.scale.z = 0.04;
                            pin.position.y = 1.65;
                            pin.position.x = -1.6;
                            pin.position.z = 0.3;
                            pin.rotation.x = 0.15;
                            pin.castShadow = true;

                            // this traverse didn't work either
                            /*
                            pin.traverse( function ( child )
                            {
                                child.castShadow = true;
                                
                            });*/
							scene.add( pin );
						});

				} );

Because the non-casting obj in question (pin) has 2 materials, I tried traversing it like the commented-out part above, but it didn’t work.

Here’s the web page

Any light-shedding advice I can get would be great! Thx!

1 Like

If you console.log(scene); you can take a look at your object hierarchy.

If you look at object 2 (the box) and 3 (the pin), you will see that the box has receiveShadow: false and the pin has castShadow: false (image below)

You will need to change both of these to true to get the pin casting a shadow onto the box. You are currently setting castShadow = true on the parent of the actual pin mesh in your code. instead of pin.castShadow = true; try pin.traverse(function(child){child.castShadow = true;}); to iterate over all children and set the cast Shadow flag.
With boxMesh, you will need to set boxMesh.receieveShadow = true;

image

Thx calrk! Super thorough answer! I’ll give it a try and report back later today.

Yep, that fixed it. Very nice!