Crash from BJS 4.2 onwards using CascadedShadowGenerator

Hi folks,

Today I tried upgrading my app from 4.1 to 4.2 and faced a crash. This is also reproducible with the latest preview, v5.0.0-alpha.15.

The error I get:

Uncaught TypeError: can't access property "meshes", _this.getScene() is null

Code level stack trace:

From my debugging, this happens when I dispose the last mesh in my scene, and only if the scene uses the CascadedShadowGenerator, with non-default numCascades (4 is fine, 2 and 3 crash). No issue if I don’t have this specific setup.

I have tried to create a repro case in the PG but did not manage.

Tagging @Evgeni_Popov as I’ve seen that you’ve done significant work on shadows in 4.2, so this might ring a bell to you ;).

I’m happy to help debug this more, but so far that’s all I could come up with. Also pointing out that this is not a major issue for me since I can simply put 4 cascades to fix it.

Thank you.

That does not ring any bell unfortunately.

Also, I can’t find _this.getScene() in the Babylon.js source code. I would definitely need a repro to be able to help.

_this.getScene() is the transpiled version I believe, I am using the ES6 packages for tree shaking. The corresponding line in the source code is here:

I’ve tried all I could to create a repro but did not manage unfortunately.

Are you disposing of the shadow generator before disposing of the meshes? If yes, you should do the reverse.

It seems the dispose function of the shadow generator RTT has been called before you get the error, as this.getScene() will return null only if RTT.dispose has been called.

I am not disposing of the shadow generator at all, I clear all meshes of the scene to replace them by new ones that will use the same shadow generator. Also, this used to work before up to v4.1.

That’s strange, looking at the code it seems the only way to have _scene = null is by calling dispose() on the RTT, dispose is the only location I have found that sets this._scene = null.

I guess a repro would be the only way to move further.

Thanks for investigating. I’ll try to repro again and post an update if I manage to, or if I find anything additional that may help.

Not sure why I can’t repro, my app’s code is fairly complex so there may be additional factors that affect which I haven’t identified.