Stick directional light (shadow) to the camera?

Hello,

I’m working on a project with a terrain and top down camera in a fix angle.

There’s a directional light (like sun) casting shadows on the terrain.

For a big terrain I have to set a large shadow camera view (left-, right-, top-, bottom value) and for detailed shadows a big shadow map width, height. This will way heavy on the performance as far as i know. So it’s better to set a smaller view and smaller shadow map but then just a part of the terrain has shadows.

I know of the cascading shadow map solution but i thought of a other solution.

In the update loop i stick the directional light to the camera like this:

directionallight.target.position.set( camera.position.x, 0, camera.position.z ); //change target position to keep the same angle of the shadow;

directionallight.target.updateMatrixWorld(); //update the target… performance?

directionallight.position.set( camera.position.x, 0, camera.position.z ).add( 10, 10, 5 ); //the update the lights pos

After this implementation i only have to set the shadow camera view as big as the camera view. and when i move around the map there are always shadows. So, it works but my question is: is it a good idea/wise to do this? will it perform better?

Thanks for any answers/thoughts.

Edit: aka Screen Space Shadows

.add() accepts just one parameter of THREE.Vector3.

sorry i had old code there, .add( new THREE.Vector3( 10, 10, 5 ) )

From the docs about .target:

=========================
Note : For the target’s position to be changed to anything other than the default, it must be added to the scene using

scene.add( light.target );

This is so that the target’s matrixWorld gets automatically updated each frame.

=========================

So, after instantiate the light you can add its target to the scene, also setting a vector of shift for the light
let lightShift = new THREE.Vector3(10, 10, 5); // for re-use
And then, in animation loop do:

directionallight.target.position.set( camera.position.x, 0, camera.position.z );
directionallight.position.copy( directionallight.target.position ).add( lightShift );
1 Like

Thanks for the quick reply. I implemented it successfully.

You’re welcome :beers:

What if we want to take in consideration the sun position? This is very similar to what I need to do, but I would like to cast the shadows based on the sun direction.

Thanks in advance