How to get the shadow map to follow you around?

I’m working on a game where you have a large map and your character walks around it. What I want to do is to add shadows for the world and have the shadow map follow you around so that everything around you has shadows, but when you walk away, those things which are are no longer in view are removed from the shadow map, so they no longer take up performance.

One of the main reasons I want to do this is illustrated here:

In this playground, you see that the shadows are pixelated. It seems to be because the shadow map is small, but it tries to stretch to cover all the meshes added, which makes the shadows blocky. If you remove it from the shadow map, or make the shadow map size bigger:
the shadows are not nearly as pixelated.

So, my question is how to not get the shadow map to stretch. I created this playground to illustrate the issue:
The shadow map tries to stretch to cover all of the meshes in one direction, even though I added light.autoUpdateExtends = false. If you don’t add that:
It automatically extends to cover it in another direction, which is what I don’t want.

Without adding the second sphere into the shadow map:
It isn’t pixelated.

@Evgeni_Popov is amazing with shadows :slight_smile:

What you want is to use light.shadowFrustumSize. If you set a value, the light frustum will use this value for the X and Y dimensions of the frustum, and either activeCamera.minZ/maxZ or shadowMinZ/shadowMaxZ (if you provide them) for the Z dimension:

Note that the directional light helper does not work when using shadowFrustumSize: you can create an issue about it in the repo to keep track of that.

1 Like

Thanks! That works perfectly!

1 Like