1

I am trying to create a little solar system but found a bug... or a feature. I'd like for all of my planets to be able to cast and receive shadows from all other planets. However, it seems as if it depends on instancing order if shadows are cast or not. Code for the light and shadows:

const sunLight = new THREE.PointLight(0xffffff, 3, 100);
sunLight.position.set(0, 0, 0);
sunLight.castShadow = true
scene.add(sunLight);

//Set up shadow properties for the light
sunLight.shadow.mapSize.width = 512; // default
sunLight.shadow.mapSize.height = 512; // default
sunLight.shadow.camera.near = 0.5; // default
sunLight.shadow.camera.far = 500; // default

const sphereSize = 1;
const pointLightHelper = new THREE.PointLightHelper(sunLight, sphereSize);
scene.add(pointLightHelper);
const shadowHelper = new THREE.CameraHelper( sunLight.shadow.camera );
scene.add( shadowHelper );

Basic code for the code objects:

var earth = new THREE.Mesh(
  new THREE.SphereGeometry(1, 32, 16),
  new THREE.MeshStandardMaterial({
    map: tLoader.load("/textures/nasa-world.jpg"),
    bumpMap: tLoader.load("/textures/nasa-jpl-world-bump.png"),
    bumpScale: 0.01,
  }));
  earth.castShadow = true
  earth.receiveShadow = true
  // position goes here
  scene.add(earth);

var mars = new THREE.Mesh(
  new THREE.SphereGeometry(0.53, 32, 16),
  new THREE.MeshStandardMaterial({
    map: tLoader.load("/textures/nasa-mars.jpg"),
    bumpMap: tLoader.load("/textures/nasa-mars-bump.png"),
    bumpScale: 0.01,
  }))
  mars.castShadow = true
  mars.receiveShadow = true
  //position goes here
  scene.add(mars);

Case 1 (working shadow):

earth.position.x = 18
mars.position.x = 15

(https://ibb.co/gS26Sfz)

Case 2 (not working):

earth.position.x = 15
mars.position.x = 18

(https://ibb.co/PZrh2wS)

Case 3 (not sure why, but it works): When I switch around the instancing (I first instance mars, then earth, Case 2 DOES work). (https://ibb.co/pRz06b1)

It does seem to me that only objects that are instanced BEFORE the objects that drop shadows can actually receive shadows. I cannot imagine, though, that this is truly a limitation, I am probably doing something wrong. Please help me, how can I make both objects cast and receive shadows from one another?

VictoriaStudios
  • 135
  • 1
  • 9
  • 1
    I think you're right and it's related to draw order. I experienced something similar with transparency: https://stackoverflow.com/questions/43502415/transparency-within-instanced-shapes – TheJim01 Mar 02 '22 at 17:00
  • Many thanks... I'll keep experimenting... – VictoriaStudios Mar 03 '22 at 01:51

1 Answers1

0

After playing around and reading some more documentation, it seems as if this problem is hard coded. The instancing order does seem to determine what can cast and receive shadows. In other words: the meshes that are to receive the shadow must be instanced before the objects casting the shadows. This is quite a limitation, in some ways.

VictoriaStudios
  • 135
  • 1
  • 9