How to reduce the time for `mesh.instantiateHierarchy`?

Hi, I’m implementing an endless runner game and I have a glb model which has some stage templates on it. I load the model into an asset container and I randomly pick one of these templates and call the instantiateHierarchy method on them but it takes between 20 to 30 milliseconds. I’m using deterministic lockstep and this is causing lag and synchronization issues. is there a workaround for this?

The simplest method is to instantiate all, disable and then enable by demand.

4 Likes

Indeed. Did you also notice that using setEnabled(true) for the first time on a bunch of meshes/instances creates this n-ms delay. While, after enabling it once it the scene, setEnable(true/false) is nearly instant. I didn’t find a workaround for this (except for force enabling/disabling in the Init sequence). Of course, I’m speaking about a number of (meshes, clones, instances)… like a few hundreds, not just a couple.

Edit: Forgot to mention that this isn’t a question. I got my answer. Partly from you and from sebavan. It’s because the first time you enable a mesh it needs to compile the materials. While disabled meshes skip parts of the computing at an early stage (understandably and logically). So enabling them on load and disabling them at some point in the background during the Init sequence clearly does the trick (It did for me :smile:).