It is definitely possible to optimize the shadow mapping process by flagging meshes that do not require updates when updating the shadow map in Babylon.js.
One way to achieve this is by adding a custom property to each mesh, such as “isStatic” or “doesNotCastShadows”, and then using this property to filter out meshes that do not need to be updated when updating the shadow map. For example, you can use the mesh.isVisible property to check if a mesh is currently visible in the scene, and skip updating its shadow if it is not.
Here’s an example implementation using the shadowGenerator.getShadowMap().renderList property:
// Add all meshes that cast shadows to the render list
shadowGenerator.getShadowMap().renderList = scene.meshes.filter(mesh => mesh.receiveShadows);
// Filter out static meshes that don't need to update their shadows
shadowGenerator.getShadowMap().renderList = shadowGenerator.getShadowMap().renderList.filter(mesh => !mesh.isStatic || mesh.isVisible);
In this example, we first add all meshes that cast shadows to the render list, and then filter out any meshes that have the isStatic property set to true and are currently not visible.
Implementing this feature should not be difficult in Babylon.js, and it can significantly improve the performance of shadow mapping in large scenes with many static meshes.
Yes, you are right, shaderbytes. The shadow of the “static” meshes should be buffered in any way. The solution of chatGPT and my modification is garbage. I should have thought more deeply about it before publishing. Thank you pointing it out.
We don’t want to filter meshes from the shadowMap’s render list. Instead, I’m looking for a solution to add shadow casters to a shadowGenerator and flag meshes that don’t need to update their shadows in order to improve performance.
I am unsure if this conflicts with the nature of how shadow maps are generated.
It can only work if all shadow generator meshes are static. In that case, you only need to set the refresh frequency to 0 so that the shadow map is not regenerated each frame. But you can’t have static + non static shadow generator meshes in a shadow generator render list.
Yes, I understand. In fact, I did this before in one of my test scenes. Problem with baking shadows is I don’t think @webgrid can really do that. I guess you know his game (at least a little). I don’t see how he could easily bake shadows within his map generator. It would by any means require a lot of work.
I experimented with two shadow generators, one dynamic and one static, as you mentioned and that worked, but it consumed may a little much CPU. I cannot bake the shadows since my landscape can change and is semi-procedural.
My solution involved using performance “friendly settings” for the shadow generator and removing small 3D objects that wouldn’t be that noticeable to the player.
My next step may be to improve the segmentation of the landscape so that I can further reduce the render list.
Shadows are soo cool, but consumes too much resources
Don’t remember where I found this static shadowing of a mesh, but this is also worth to mention if your light sources are always on the same place, since the shadows will not change their positions in this case!
That would be a nice improvement, but you’d also have to bootstrap the zbuffer. I don’t know if it’s easy to do in WebGL (in WebGPU, the zbuffer is a normal texture, so we should be able to read/write to it, even if I think there are restrictions on the operations we can do with a zbuffer texture)…