I revisited this topic today and I was able to achieve animated shadows on thin instances with a ShaderMaterial by adding my customized bonesVertex to shadowMapVertex.
Now my problem is: I have custom attributes added to the original PBRCustomMaterial used for the model. How can I pass the attribute used for each instanced model to the custom ShaderMaterial I pass to ShadowDepthWrapper?
Another question is about performance. It is much better than using PBRCustomMaterial itself in ShadowDepthWrapper, but FPS dropped to 40 with 200 animated units. Without shadow, I am able to maintain 600 units without FPS drops, and I believe I can have more units eventually when I replace the testing model with low-poly ones. But shadow seems to be the bottleneck. I am wondering how game like total war can support shadows of thousands units?
Came here to post a similar question, and hopefully it’s on topic enough for this thread. I’m setting the existing color attribute on a PBRCustomMaterial, but the alpha channel of that color doesn’t seem to affect the transparency of the produced shadow with or without the ShadowDepthWrapper. The instance fades in via animation, but the shadow appears abruptly at full strength.
Should that be working with instances based on the alpha channel of the color attribute, or would this require a custom shader for the shadow?
Vertex color alpha is not supported in the soft transparent shadow mode.
So you would need to write a custom shadow depth generator (standalone ShadowDepthWrapper, see Shadows | Babylon.js Documentation) to generate the shadow map.
Thanks. I missed the definition of my custom attribute “animControl” when creating the ShaderMaterial instance. I already did it for uniforms but forgot the attribute.
Everything in my game is thin instance except the singleton terrain and water. Shadow on static models also seem to be fine. The performance issue is only for adding shadows for animated models.
but animated models if there are thinInstances or instances should all be done in one draw call so it should not impact perf much to have 100 or 200 ???
You can try to use 2 cascades instead of 3 (which is the default). You should also monitor where the time is spent using the Performance tab of the browser, to have a clearer view of the spots that take time in the program.
This is all I have in my scene (see screenshot below) with 100 troll units. The Nodes sections lists all the meshes I use. The two root folders, one is for the troll model, the other is the asset pack for all the plants, rocks, and buildings. All these meshes are listed once since thin instances won’t be listed.
Similarly for the materials, a troll has 4 meshes, hair, body, weapon, and tanga. So I created 4 ShaderMaterial for each.
The GPU frame time keeps going up with more troll units if shadow is turned on. The draw call 65 is good. I have the troll and ~20 building/plants/rocks, terrain, and water material loaded into the scene.
I followed your instruction to profile the app. There is nothing significant on JS side. I notice some small GC overhead and corrected it. Also changed number of cascades to 2. These fixed helped a little bit.
I then think my issue is GPU bound. Some observations:
Inter frame time is always close to GPU frame time when I manipulate the number of units.
I tried to remove the body of the troll model, keeping the smaller parts like hair and the skirt. FPS is back to 60 again even when I have 1000 units. It seems to indicate I have very large active faces and vertices on the troll model. Removing the largest part (body) significantly reduced the GPU frame time. Other than the body mesh makes 70% of this model’s total polygons, it has not difference compare to other meshes of the model. In my code, I looped through the troll meshes, and applied all the customization for baked animation and shader material for ShadowDepthWrapper inside the loop.
I also tried to remove/disable everything else in my scene, and I divided the total number of active faces in babylon inspector by the number of units to calculate the number of active faces of a single unit. Then I found the result is very strange: I have over 25000 faces per unit. But if I load the model into babylon sandbox, the active faces is 10479. Even if I remove the body, I still have 6000+ faces per unit when loaded into my scene, compared with < 3000 faces in sandbox. I don’t write any code that adding vertices and faces (that would be too hard for me), so it feels very strange this happens. I will keep investigating. But I want to let you know and see if this behaviour ring a bell to you by any chance.
Now I have a strong feeling if I can fix this issue, I will be able to render 1000+ units for a unit that has <= 5000 polygons.
Yes, it is expected: the number of active faces is the number of faces that are drawn. When using shadows, each face is drawn two times, once in the shadow map and another time for the regular rendering. With CSM, it can even be drawn more than two times because a face could be drawn in several cascades.
As the time spent is GPU time, I fear it will be difficult to optimize except by reducing the face count. Also, if you have a live link it could help to diagnose better.
With CSM, it can even be drawn more than two times because a face could be drawn in several cascades.
Thanks for explaining. After my last post, I also noticed the faces are exactly doubled in my PG, but the faces are ~2.7 times in my scene that uses CSM. Now I understand the reason.
As the time spent is GPU time, I fear it will be difficult to optimize except by reducing the face count.
I looked the models on sketchfab, low poly warrior models with 1000 faces should be good enough for me. Compare to the testing model with 10k faces now, I should be able to rendering 2000 units. I will probably only need 1000 - 1500 units and leave some buffer for castle and town building blocks.