ShadowGenerator exclude offscreen objects from shadow map

Hey guys.

I am working on a project with a locked camera perspective with a slightly angled topdown view. A sort of action exploration click to move click to move game.

I have a simple DirectionalLight with a ShadowGenerator creating shadows.

I notice that offscreen meshes are getting culled automatically and my draw calls do decrease based on their bounding boxes and the view frustrum.

But the offscreen meshes are still getting drawn again for the shadow map and included in it.

Is there any built in way to exclude non-drawn offscreen meshes from casting shadows?
I know I can compare them all by distance and disable them manually but I thought there may already be some option/setting I am missing!

Please let me know if you need me to make a playground example.


There’s no built-in way to do it: the shadow generator will generate to the shadow map the meshes from its renderList.

If you only want the visible meshes to cast a shadow, you can try to set the shadow map renderList property to the list of active meshes of the scene:

The list of active meshes is the list of meshes that are inside the camera frustum.

Hey again Evgeni_Popov. Thank you for your response!

Yes I see now what you mean about the shadow map using it’s own render list.

Nice example. I tried it in my own code but unfortunately it results in every active mesh casting a shadow.

I had a good result with something like

scene.onBeforeRenderObservable.add(() => {
	for (var i = 0; i < scene.meshes.length; i++)
		var mesh = scene.meshes[i];
		if ( scene.activeCamera.isInFrustum(mesh) && mesh.isVisible && ( == "Clone of Penguin.001") )
			shadowGenerator.addShadowCaster(mesh, true);
			shadowGenerator.removeShadowCaster(mesh, true);

It results in a lot of calls to scene.activeCamera.isInFrustum(mesh) PER mesh. Not sure if that matters performance-wise for a couple hundred meshes.

Maybe I could just update the shadow map render list when the camera moves or when a mesh moves?

What would you recommend?
Thanks again for your help.

Not sure isInFrustum will be the bottleneck, but the best way to know is to check. If your camera / meshes don’t move often, only updating the list when they do could be worthwhile. But I would not make any of these optimizations first and see if they are really needed after testing.

1 Like

Thank you Evgeni_Popov for sharing your Babylon wisdom!

Yes agreed, sometimes I can’t help it but to optimize every last detail. But I think this will do just fine.

For now I am just going to update the shadows at a set interval until my scenes get much larger.

I present to you some dapper penguins who cast shadows how I had hoped:

1 Like