How to Override Materials

It works properly, but there seems to be a large bottleneck yet, even with the instanced shader version. I see drops below 60fps even with this very trivial scene, apparently due to isReadyForSubmesh calls on https://www.babylonjs-playground.com/#BYYJ4A#14 which calls _prepareEffect. Here’s a Chrome profile if you want to check. Profile-BYYJ4A-14.zip (1.0 MB)

To get back to the original problem: what is an approach that works for most scenes that has good performance? Let’s consider a scene that has static objects, animated objects and instances as a definition of “most scenes”. As I understood from this thread it seems that cloning objects and setting their materials only once would be the best answer, as the expensive material checking calls are avoided.

I made another PoC, with clones: https://www.babylonjs-playground.com/#BYYJ4A#18. It seems that reloading the object is the best choice for cloning right now (How to clone a GLB model and play seperate animation on each clone?).

There are still two problems with this approach:

  • I can’t get the main scene to render only the original objects. The RTT renders correctly. setEnabled on scene.onBeforeRender seems not to do what I think it would.
  • It doesn’t seem trivial to synchronize transformations and animations on the original object and the clone. Apparently in my demo the animations are in sync by pure chance that the loader is fast enough, and they’d need a deterministic lockstep. I suppose I could make a façade object that clones the methods of the original Mesh, forwarding any calls to the methods on both meshes. It would work, but it’s ugly and I don’t think it’s very trivial to do.

The new playground doesn’t have instances, but I am assuming that any instances can also be duplicated from the cloned base objects.

BTW, if we reach some form of consensus on something that would be interesting to add to BJS to help with multiple passes I could submit a PR at a later point, or perhaps add a module to another repo. The howto I’m writing is almost finished, but since I keeping finding new problems to bother you guys… :smiley: thanks @Deltakosh and @Evgeni_Popov for all the help.