# Multiple ShadowGenerators for one light source

**URL:** <https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236>\
**Category:** Questions\
**Created:** [June 10, 2022, 12:15pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236 "2022-06-10T12:15:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sairens](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sairens](https://forum.babylonjs.com/u/sairens)\
**Post date:** [June 10, 2022, 12:15pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/1 "2022-06-10T12:15:23Z")

</div>

Shadows are really affecting my game’s performance, so I’ve been having a look at improving it.

I had 1 `DirectionalLight` in my scene, and pretty much all the meshes in it are shadow casters. The good news is that the majority of my meshes are static buildings, so I want to freeze the shadows for them by setting the `refreshRate = BABYLON.RenderTargetTexture.REFRESHRATE_RENDER_ONCE`.

However, the singular light in my scene is also responsible for casting shadows off the player meshes, and obviously players can move around, so I don’t want their shadows to be frozen. There doesn’t seem to be support for this currently in the `ShadowGenerator`.

I tried creating multiple `ShadowGenerator`s, however, if the same light is used for multiple `ShadowGenerator`s, only the last-created generator seems to cast any shadows at all. Additionally, creating a clone of the light in my scene isn’t a good solution either, because that results in the shadows “stacking” when they overlap, as opposed to for a singular light source, where “stacked” shadows don’t make the shadow-receiving-area darker.

Is there another way I can solve this problem currently? If not, could we add support for partial freezing of shadows in the `ShadowGenerator`? The ideal change, though, would be to make a single light work for multiple shadow generators - that way it would be possible to have differing qualities of shadows for each generator.

Playground link where I’m doing 2 lights (2nd is a clone) and 2 `ShadowGenerator`s, demonstrating the overlapping dark shadows: [https://playground.babylonjs.com/#4CDJPH#2](https://playground.babylonjs.com/#4CDJPH#2)

![shadows_issue](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/9/196a4c78f28eca21020a1278cd2cf22279236d8f.png)

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [June 10, 2022, 6:50pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/2 "2022-06-10T18:50:58Z")

</div>

Cc @sebavan and I can take a look too when I’m in a PC 😃

---

<div class="post-metadata">

**Author:** ![jelster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jelster/32/25881_2.png) [@jelster](https://forum.babylonjs.com/u/jelster)\
**Post date:** [June 10, 2022, 10:53pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/3 "2022-06-10T22:53:52Z")

</div>

I’m on my iPad so no PG right now, but there are two things that occur to me:

1. Create a clone of the light and set it via includedMeshes (or is it exclude…?) to only affect the buildings. Add your shadow caster with render once

2. Pre-bake the static shadows cast by the buildings into a texture (would this properly be an AO texture?)

🤓3. Switch to a physical-based rendering and let the shaders figure out shadows

HTH!

---

<div class="post-metadata">

**Author:** ![sairens](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sairens](https://forum.babylonjs.com/u/sairens)\
**Post date:** [June 11, 2022, 12:04am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/4 "2022-06-11T00:04:44Z")

</div>

1. I’ve updated the PG a bit to do exactly this [https://playground.babylonjs.com/#4CDJPH#2](https://playground.babylonjs.com/#4CDJPH#2) (there doesn’t seem to be any field called `included/excludedMeshes`, I’ve instead added the static mesh as a shadow caster to the static shadow generator only, and the moving mesh as a shadow caster to the dynamic shadow caster only) - the issue here is when the shadows overlap, the resulting area is very dark, and that doesn’t make sense if the goal is to emulate 1 light source (ie. 1 sun) in the scene

2. That makes sense, however there are a very large number of buildings (and future areas in my game will also contain some of the same buildings, in different positions/orientations relative to the light source), so I would really love a way to generate and freeze the shadows programmatically rather than baking as textures

3. Would you be able to elaborate on this one a little more? Thanks!

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 11, 2022, 12:07am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/5 "2022-06-11T00:07:56Z")

</div>

Maybe @Evgeni_Popov can have a look as he is a shadow guru 🙂

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 11, 2022, 9:28am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/6 "2022-06-11T09:28:25Z")

</div>

I’m very much interested in reading this answer. Did a number of attemps with no success. I hope @Evgeni_Popov will impress me once more 😉 Already bookmarked for later reference 🤔

Edit: I’m quite sure that if there is a solution, it must be a twist. It feels not natural, since light only adds to light (and shadows to shadows). I mean I have been able to do something similar but not with casting two shadows from light (one static and one dynamic) on a same mesh (in this case, the ground). I know of how to cast a single shadow on the ground and another shadow contact hardening with maxZ and minZ limits on other objects or on self, but the above really leaves me wondering…

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 11, 2022, 9:33am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/7 "2022-06-11T09:33:29Z")

</div>

> [@sebavan](#):
>
> Maybe @Evgeni_Popov can have a look as he is a shadow guru 🙂

Vile flatterer! 😉

The only way I can see to do it is to modify the shadow code to not darken the color further when handling the 2nd light if the pixel is already in shadow from the 1st light.

Here’s a way to do it with a material plugin (new in 5.0 - doc: [https://doc.babylonjs.com/divingDeeper/materials/using/materialPlugins](https://doc.babylonjs.com/divingDeeper/materials/using/materialPlugins)):

> **[Babylon.js Playground](https://playground.babylonjs.com/#4CDJPH%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

For this to work:

- 1st and 2nd lights must be directional lights and have the same intensity
- you must not use any shadow filtering method

Note that the plugin works for both the standard and the PBR materials.

If you change the type of the light (point, spot) and/or the shadow filtering method (PCF, poisson, …), the regular expression lines 106/112 must be changed accordingly. It’s a bit cumbersome to do, so I have created a PR to allow reusing regular expression groups in the injected code:

> <https://github.com/BabylonJS/Babylon.js/pull/12642>
>
> See https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source…/31236/6

When this PR is merged, this PG will work:

> **[Babylon.js Playground](https://playground.babylonjs.com/#4CDJPH%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

It allows you to use any type of lights and any type of shadow filtering methods.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 11, 2022, 9:39am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/8 "2022-06-11T09:39:10Z")

</div>

> [@Evgeni\_Popov](#):
>
> It’s a bit cumbersome to do, so I have created a PR to allow reusing regular expression groups in the injected code

WOW! Do that and become my hero (well, you already are). OK, so become my super…outmost…spectacular hero 🦸 😁

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 11, 2022, 9:49am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/9 "2022-06-11T09:49:00Z")

</div>

> [@Evgeni\_Popov](#):
>
> Vile flatterer! 😉

Yeah, I know, I’m a bad ass 🙈 I’m only here to soak-up the knowledge and efforts of others. 😆  
Just don’t tell the others 😁

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 11, 2022, 10:08am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/10 "2022-06-11T10:08:03Z")

</div>

Well, in fact it was a reply to @sebavan post 🙂

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 11, 2022, 10:10am UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/11 "2022-06-11T10:10:28Z")

</div>

LOL, no worries. I can take it for me aswell 😉

---

<div class="post-metadata">

**Author:** ![jelster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jelster/32/25881_2.png) [@jelster](https://forum.babylonjs.com/u/jelster)\
**Post date:** [June 11, 2022, 3:35pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/12 "2022-06-11T15:35:59Z")

</div>

> [@sairens](#):
>
> Would you be able to elaborate on this one a little more? Thanks!

I think the approach that ya’all settled on for this makes the most sense for your use case as I see it. A PBR approach would be very heavy and be overkill I think!

Basically, PBR is when your materials use a realistic set of simulated properties that define how light reflects from it, which includes occlusion (shadows) as a emergent property of the PBR system.

Much more complicated and powerful, and probably not what you’re looking for 🙂🕶

---

<div class="post-metadata">

**Author:** ![sairens](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sairens](https://forum.babylonjs.com/u/sairens)\
**Post date:** [June 12, 2022, 12:14pm UTC](https://forum.babylonjs.com/t/multiple-shadowgenerators-for-one-light-source/31236/13 "2022-06-12T12:14:00Z")

</div>

Hey @Evgeni_Popov, that solution works great, I’ve now implemented it and it’s working perfectly - many thanks!!!

Additionally, thanks to everyone else who’s popped by this thread and given their input as well. This community is honestly one of the most supportive and helpful forums I’ve come across, I haven’t come across any other open source projects that are as well-maintained and supported as BabylonJS. 🎉
