In the world of three.js, calculating shadows for directional lights involves setting a range based on a bounding box that extends from the light source. This means that if I want to limit how far shadows are rendered, I need to adjust the bounding box geometry. For instance:
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 50, 50, 50 );
directionalLight.intensity = 0.5;
directionalLight.castShadow = true;
directionalLight.shadowCameraLeft = -10;
directionalLight.shadowCameraRight = 10;
directionalLight.shadowCameraTop = 10;
directionalLight.shadowCameraBottom = -10;
The shadowCamera parameters can be modified to determine the distance at which shadows are generated from the light source's field of view. My goal is to have this bounding box follow the user camera so that shadows are visible up to a certain distance away from the camera.
However, since the bounding box's geometry is tied to the light's position relative to the scene's origin or target, simply moving the bounding box to match the camera's location won't achieve the desired effect.
I tried moving the light source along with the camera in the hopes that the bounding geometry would adapt to the camera's position. Unfortunately, this approach failed because I couldn't set a target for the directional light – it always aimed towards the origin, leading to changes in its direction whenever I adjusted the light's position.
Even attempting something like the following had no effect:
directionalLight.target.position.set(100,100,100);
If this method had worked, I could theoretically maintain a constant angle between the directional light's position and target by adjusting them relative to each other, potentially achieving my objective. But, as mentioned, it didn't work.
Do you have any suggestions on how to establish a shadow range or insight into why the above approach was unsuccessful?