# NME: Shadow output of 'Lights' node doesn't work with multiple lights

**URL:** <https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573>\
**Category:** Questions\
**Tags:** lighting, material, nodematerial\
**Created:** [August 25, 2023, 12:59pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573 "2023-08-25T12:59:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![WirelessKiwi](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wirelesskiwi/32/19420_2.png) [@WirelessKiwi](https://forum.babylonjs.com/u/WirelessKiwi)\
**Post date:** [August 25, 2023, 12:59pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/1 "2023-08-25T12:59:50Z")

</div>

Hello,

I’m trying to recreate the ShadowOnlyMaterial with the NodeMaterialEditor because I need to control the shadow opacity. Here’s is what I have came up with:

[https://nme.babylonjs.com/#GA2RPP](https://nme.babylonjs.com/#GA2RPP)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/d/0dc4620696a097640dbc84eef75b90c8a2cf26cb.png)

Every is working great as long as there is only 1 light in the scene.  
If I have multiple lights, only the shadows of the last light added into the scene show up.

Here is a Playground that reproduce the issue: [https://playground.babylonjs.com/#VM49TX#2](https://playground.babylonjs.com/#VM49TX#2)

Am I doing something wrong or is there some sort of limitations ?  
If I cannot get the shadows from multiple lights, is there a way to at least choose wich light will be used by the node ? Relying on the creation order isn’t a very robust solution…

---

<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:** [August 25, 2023, 3:15pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/2 "2023-08-25T15:15:57Z")

</div>

Assigned @Evgeni\_Popov on August 25, 2023, 3:15pm UTC



---

<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:** [August 25, 2023, 5:02pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/3 "2023-08-25T17:02:44Z")

</div>

The `Lights.shadow` output gives you the value of the last light in the scene, indeed. So, if this light has no shadow generator, you’ll get 1. I think this output was mainly intended for use in the case of a single light.

The `ShadowOnly` material also has this problem, which is why there’s an `activeLight` property: when you set a value, it rearranges the lights to ensure that the right light will be used by the shader.

To overcome this problem and handle the multi-light scenario, I wonder if we could add a new output like “AggregatedShadow” that would give you the % for the current pixel not in shadow, regardless of the number of lights (or even change “shadow” if we consider it to be a bug)…

---

<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:** [August 25, 2023, 5:28pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/4 "2023-08-25T17:28:17Z")

</div>

I ended up creating a PR, because I think the way it currently works is buggy :

> <https://github.com/BabylonJS/Babylon.js/pull/14207>
>
> See https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-w…ith-multiple-lights/43573
> 
> I tagged it as a bug, as I think it does not behave as expected when there are multiple lights: depending on the position of the light that casts shadows in the light array, it will or will not generate the right value for the "shadow" output. Also, the current code does not work when multiple lights cast shadow.
> 
> In the case of a single light casting shadows, the code from the PR gives the same output than the current code, so it's not a breaking change in this regard.

---

<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:** [August 25, 2023, 5:29pm UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/5 "2023-08-25T17:29:18Z")

</div>

Unassigned @Evgeni\_Popov on August 25, 2023, 5:29pm UTC



---

<div class="post-metadata">

**Author:** ![WirelessKiwi](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wirelesskiwi/32/19420_2.png) [@WirelessKiwi](https://forum.babylonjs.com/u/WirelessKiwi)\
**Post date:** [August 28, 2023, 7:41am UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/6 "2023-08-28T07:41:26Z")

</div>

Thank you for your quick response ! 🙂

Once the PR has been merged, what’s the best way for me to know when the fix is available in the next package version ?

---

<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:** [August 28, 2023, 9:58am UTC](https://forum.babylonjs.com/t/nme-shadow-output-of-lights-node-doesnt-work-with-multiple-lights/43573/7 "2023-08-28T09:58:49Z")

</div>

It will be available on the Playground the next day it is merged, and the next thursday morning (european time) for the npm packages.
