Jump to content

Shadow Resolution Calculation Method


Recommended Posts

Hi Everyone! Can someone explain how BabylonJS calculates the resolution of the shadow map? I think I understand it but just want to be sure because I'm having difficulties getting sharp shadows in one of our projects. 

First off see the overall 3D area of the game/project, I pointed out the dark spots are baked shadows, so ignore those. Also, the area you see in the camera (where most of the objects are located) is circled. Also, the items marked #1 and #2 are two spheres i made that were moved out to the far edges of the area. 

Next, you see what it looks like in the BJS camera if the spheres at the edges (1 and 2) are NOT casting shadows. The shadow generator (set to 4k shadow map size) looks nice and crisp. (These are realtime shadows, not baked)


However, the minute I turn enable casting shadows for those two sphere's at the edge of the map, the same exact shadow generator now produces this:

Obviously this looks less than desirable. So initial assumption is that the "resolution" of objects on the shadow map (which I'm assuming is one single dyanmic texture) is calculated by the bounding boxes of the objects casting shadows? Where the edge of the texture goes to the edge of bounding box on the furthest object from the directional lights field of view? 



Link to comment
Share on other sites

Yep, this is a limitation in the shadows system at the moment... As I understand its just represented with one image. To fit in the new stuff you add, it has to sort of "zoom out" everything else to make space, and if those things are far away, it has to zoom out even further, resulting in everything getting more pixelated.
@Deltakosh has a solution planned for this which is to implement CSM shadows (which hopefully is coming for the 2.5 release) which will allow Babylon to give more detail to the nearby shadows and less detail to the ones far away.

You can work around this limit a bit for now though by:

a) Increase the shadowmap size to the absolute maximum you can get away with

b) Write a little script which simply adds things to the shadow map when the player gets within 50m or so, and removes them again when they're outside that radius (far away things won't be shadowed, but I think it's a better solution than having ugly shadows). Combine that with baked shadows and distance fog and you can get away with it.

Link to comment
Share on other sites


@Dal is correct. If you are using a directional light, babylon.js try to compute the perfect point of view to embrace all objects in your shadow map (https://github.com/BabylonJS/Babylon.js/blob/master/src/Lights/babylon.directionalLight.ts#L45)

You can turn this behavior off by using light.autoUpdateExtends = false

But you can also consider having various directional lights: one for the closed range objects and one for the far objects.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...