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

Here is a Playground test case: Babylon.js Playground

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:

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.

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:

1 Like

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

Documentation kind of indicated that using includedOnlyMeshes should be enough.

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

rootMesh.getChildMeshes((mesh) => {

should be:

rootMesh.getChildMeshes(false).forEach((mesh) => {

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. :slight_smile: