If we use the normal shadow map without the cascade, the above code works.
I figured out this helps to refresh the shadowcaster:
shadowGenerator.autoCalcDepthBounds = false;
shadowGenerator.autoCalcDepthBounds = true;
This example is a bit artificial but we are encountering this problem in larger project where we do lots of async mesh loading all the time dynamically.
You need to set a sensible value for the camera far value for CSM to work properly. See Cascaded Shadow Maps | Babylon.js Documentation
Thanks! I will investigate with this in mind - something a bit weird still going on in our main project. I try to isolate a playground example later if it still seems like a real bug.
One thing is at least that we try make everything automatic - in your ok working solution the autoCalcZ is turned off.
This version is closer to what we are aiming for:
I am able to cure the error behaviour by creating the camera -before- the shadowgenerator.
autoCalcDepthBounds = true only works if there’s an active camera in the scene as it needs one to create the depth renderer.
autoCalcDepthBounds = true is more taxing on the GPU than
autoCalcDepthBounds = false, so the choice may depend on the targets you want to support (mobiles or not, for eg) and the complexity of your scene.
I have been trying out different things and for now only quaranteed way is to create new CSM shadow generator every 200 frames and re-add all the shadowcasting meshes. This way at least every 200 frames, the whole scene starts getting proper shadows without any missing shadowcasters. (This has a caveat that after about 5 minutes Chrome crashes on “hardware texture creation failed”)
If I try to init shadowcasting only once it seems to always miss some meshes after ImportMeshAsyncs - seems like scene have to be rendered at least once to get things ready for CSM to init properly? If I put a 1000ms timeout to every ImportMeshAsync => CSM problem goes away…
I have no idea yet why all this is happening, I will examine the ShadowGenerator codes next and try to come up with a PG that reproduces this situation.
Here is now an example of using dynamic scene with CascadedShadowGenerator:
This is the best version we have been able to implement this far… I would be happy to get some feedback if there is any smarter way of keeping the shadowscaster updated with meshes in the scene. And is this what I am doing now maybe causing some bad performance caveat?
One other question rose while doing this: Why is shadowgenerator using readPixels? Can it be avoided? It causes bad performance penalty on some devices…
Removing and adding directly into the
It’s because you use
autoCalcDepthBounds = true. Set it to
false if you don’t want to incur the
readPixels penality as well as a depth rendering + min/max reduction:
autoCalcDepthBounds = true is quite taxing on performances.
Would you have time to describe any other method for automatically calculating those? Is there maybe a way to get good approximate from frustum or scene bounding box?
I moved this to Questions btw… not a bug
I am thinking of trying to calculate minz and maxz from the bounding boxes of objects visible on the current camera frustum.
Yes, it’s a way to do it but it will be less precise (meaning you will compute a minZ that will be lower and a maxZ that will be higher than the values computed by the min/max reducer) so you will need to see if it works for you. Thinking of it, I’m not sure it will work well, at least for minZ, as you will most probably end up having minZ = 0 because some objects (the ground) will be partly behind the camera…