Turn on shadow PCF, move the directional light position, and the shape of the shadow changes

Turn on shadow PCF, move the directional light position, and the shape of the shadow changes.
How can I keep the shape of the shadow unchanged?

Looking forward to your reply.

Hello and welcome!

I believe that we have the same shadow behaviour in real life - when the light source is moving, shadow shape is also changing.
There is the tool in Inspector (which you can call in any PG with Inspector button) to see the position and direction of light, and also to tune Shadow generator.

1 Like

Welcome aboard!

This part of the shadow doc may help you find the problem:

1 Like

Thank you for your reply.
Can The Directional Light used here be treated as sunlight at an infinite distance?
If it is infinite sunlight, should usePercentageCloserFiltering be turned on for shadows, and should not affect the shape of the shadows?
Is there a problem with the effect here?

Hi and welcome to the Community,
Shadows is a complex topic and you should read the section, check out the featured PG and use ‘the Inspector’ tool to make real-time changes to get a better understanding on it.
Since shadow is cast from a light, you will also need to get a good understanding of lights.

To try answer your question:

as explained in the light section of the doc (link above), the directional already casts on an infinite distance. Now, that’s for the light part. Using a shadowGenerator with a dir light gives you more possibilites of controlling the distance (of the shadow). You can set maxZ and minZ on a shadowGenerator, i.e. using useContactHardeningShadow.

Filtering of the shadow only makes for improving the ‘aspect’ of the shadow/projection, AND SO…

Filtering has no incidence on the shape of the shadow. The shape of the shadow is defined by the light source and the direction of the light only.

In conclusion: If you want the shape of your shadow to remain fixed, you will have to work with 2 (or more) lights in the scene. Where the light you use for the shadows will not move, or move differently (just very slightly in direction) from the other lights. Once you have your multiple lights base set-up, there are actually a number of tricks you can use. I.E. you can actually cast a shadow from below the ground, using negative values on a dir light and stop the light/shadow at just the upper or lower part of your shadowed mesh (or group of meshes). You can also make darker and more precise shadows by pushing the distance/direction of the light much further (i.e at -300 on Y, like the light targets a point that is set at minus 300 below the ground). You can make softer shadows in a number of ways. Depending on the chosen type of light, you can use i.e. the light angle so that the light does not directly hit the surface. There’s already a good level of information in the doc but there would be a lot more to it.

Here just two quick examples for you:

  1. Uses a contactHardeningShadow. You can see the projection of the shadow (shape) change as the light changes direction.
    https://playground.babylonjs.com/#B48X7G#219
  2. The second uses two sources of light, where the light used for shadows does not move (so the shape doesn’t change).
    https://playground.babylonjs.com/#B48X7G#220

I hope this won’t discoureage you. Actually, I found studying shadows a rather cool topic (hard but fun).
I hope you are enjoying your time with BJS and will soon be able to teach me how I can master better shadows :smiley: :student: Meanwhile, have a great day :sunglasses:

3 Likes