# Shadow doesn't work if another light is created before shadow casting light?

**URL:** <https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852>\
**Category:** Bugs\
**Created:** [April 13, 2023, 11:06am UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852 "2023-04-13T11:06:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [April 13, 2023, 11:06am UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852/1 "2023-04-13T11:06:42Z")

</div>

Here is a Playground test case: [Babylon.js Playground](https://playground.babylonjs.com/#5M7BY5)

It has a `ShadowOnlyMaterial` mesh to receive a shadow from a `DirectionalLight` that only casts the shadow. If scene has another `DirectionalLight` created before the shadow setup, the shadow doesn’t show up.

However it does show up if you click on the Inspector → Nodes → BaseLight → “Turn on/off the light” toggle. And it stays visible ever afterwards:

 ![babylonjs-shadow-bug](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/2/2/2274d048af41ada352267ddc849481ba737175f3.png)

I tried to figure out what is happening but still don’t have any ideas. Couldn’t even figure out a viable workaround other than the inspector toggle.

---

<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:** [April 13, 2023, 11:40am UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852/2 "2023-04-13T11:40:37Z")

</div>

It’s “by design”, the `ShadowOnlyMaterial` assumes it should use the first light of the light list.

You can set the light you want to use by setting `ShadowOnlyMaterial.activeLight`, and the code will make sure this light will be the first of the list:

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

---

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [April 13, 2023, 11:53am UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852/3 "2023-04-13T11:53:20Z")

</div>

Nice, that would have been good to know couple of hours ago. 🙂

Documentation kind of indicated that using `includedOnlyMeshes` should be enough.

---

<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:** [April 13, 2023, 1:19pm UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852/4 "2023-04-13T13:19:42Z")

</div>

`includedOnlyMeshes` does work, but you made a mistake in your PG:

```javascript
rootMesh.getChildMeshes((mesh) => {
    includeOnly.push(mesh);
});

```

should be:

```auto
rootMesh.getChildMeshes(false).forEach((mesh) => {
    includeOnly.push(mesh);
});

```

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

---

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [April 13, 2023, 1:50pm UTC](https://forum.babylonjs.com/t/shadow-doesnt-work-if-another-light-is-created-before-shadow-casting-light/39852/5 "2023-04-13T13:50:28Z")

</div>

Thanks! This is interesting. In the original codebase it looks like `includedOnlyMeshes` was set to an empty array in the case where I discovered this problem and that apparenly doesn’t disable said lights but rather makes them active for all meshes again.

I guess setting `activeLight` makes sense as a precaution regardless, to make sure it is never replaced by unintented light even when scene doesn’t at that time have light using meshes. And probably also best that I setEnabled(false) to lights when the include mesh list is empty. 🙂
