# Optimizing cloned/instanced meshes playing separate animations

**URL:** <https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101>\
**Category:** Questions\
**Tags:** instances, animations, clones\
**Created:** [January 4, 2023, 8:49pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101 "2023-01-04T20:49:58Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [January 4, 2023, 8:49pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/1 "2023-01-04T20:49:58Z")

</div>

Hi all 🙂

I was playing around with getting a group cloned/instanced meshes to play their animations separately, and in general trying to see if one approach works better than another in terms of performance. Are there any other tricks I could be using to squeeze out even more performance (fps, lower memory usage) in either case of using clones vs instances?

> **[Babylon.js Playground](https://playground.babylonjs.com/#85SWEQ%2318)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Panuchka](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/panuchka/32/12054_2.png) [@Panuchka](https://forum.babylonjs.com/u/Panuchka)\
**Post date:** [January 4, 2023, 11:07pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/2 "2023-01-04T23:07:49Z")

</div>

I wondered this a bit in the past as well. It is a bit complicated topic and I think depends on your use case…

Check out this thread:

> [@Animations and performance, tips and tricks?](https://forum.babylonjs.com/t/animations-and-performance-tips-and-tricks/20107):
>
> Hi everyone! I have a pretty generic question about animations and performance. I am making a game where I have these different characters that cannot be instantiated; they need to have separate meshes, textures and animations. I also made a small stress test illustrating the situation, take a look: [https://playground.babylonjs.com/#S7E00P#51](https://playground.babylonjs.com/#S7E00P#51) Now, the framerate drops quite significantly if there are tens of models in the screen at one time. I can of course see some obvious optimizations, like …

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [January 4, 2023, 11:59pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/3 "2023-01-04T23:59:48Z")

</div>

Thanks for sharing!

This random variable that doesn’t get used is cracking me up right now 😆

> const meshTask = assetsManager.addMeshTask(‘Very nice task’, ‘’, path, fileName)

This is definitely useful for exploring the curve of n-entities vs fps for both instances and clones as you scale up the number of entities with animations playing.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 5, 2023, 10:01am UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/4 "2023-01-05T10:01:00Z")

</div>

Note that by default `instantiateModelsToScene` will always clone meshes if they have a skeleton, even if you set `doNotInstantiate = false`. If you want to force instanciation in this case, you should pass `() => false` for `doNotInstantiate`.

However, doing so will prevent you to use a different animation for each instance: all instances of a mesh share the same animation.

Since 5.0, we added Baked Texture Animation, if that can help:

> **[Baked Texture Animations | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/animation/baked_texture_animations)**
>
> Efficient animations on the GPU.

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [January 5, 2023, 5:34pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/5 "2023-01-05T17:34:16Z")

</div>

That’s super interesting to learn. So it sounds like in the playground I set up, those birds are always cloned? Yet I thought I had seen a consistent difference in the fps as measured by the babylonjs inspector.

I tried it out as you said, `opts = { doNotInstantiate: () => false }` and observed all of the instances animations play as one.

> A limitation of the current VAT implementation is that you cannot [blend animations to play simultaneously](https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#animation-blending). Also, the animations may not be as smooth as when not using BVA because there is no interpolation between frames. The “smoothness” will depend on the number of frames of the animation.

So it sounds like if I wanted to do a large flock of birds in the distance this could work but otherwise is too rigid to work in a game setting where birds close to the player need to be able dynamically stop and go… is it possible to use both baked animations and regular animations on the same model/instance?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 5, 2023, 7:19pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/6 "2023-01-05T19:19:26Z")

</div>

> [@arcman7](#):
>
> So it sounds like in the playground I set up, those birds are always cloned?

Yes indeed.

> [@arcman7](#):
>
> is it possible to use both baked animations and regular animations on the same model/instance?

I think it should be possible, but not at the same time, as there’s a `BakedVertexAnimationManager` property on the mesh that you can disable by doing `mesh.bakedVertexAnimationManager.isEnabled = false`.

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [January 5, 2023, 7:57pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/7 "2023-01-05T19:57:23Z")

</div>

Wow, if that’s the case, that makes some really cool stuff possible. Say for example we have a flock of ducks, animated as flying and moving forward on a given axis at 3m/s:

d0 d2 d4 d6 d7 … d500 … dn-1  
d1 d3 d5 … d300 … dn ~~~\> 3 m/s

If the game allowed it, the player could click at random any duck in the flock, and that particular duck instance could then be switched over from a baked animation, to the regular more dynamic animation system and do something to display different behavior from the flock?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 5, 2023, 8:48pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/8 "2023-01-05T20:48:26Z")

</div>

Yes, it seems it could be work, but you will really know once you test it 😉

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [January 5, 2023, 9:15pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/9 "2023-01-05T21:15:14Z")

</div>

Finishing up some code, and there’s a thread I need to respond on first, but very soon I think I can set up a cool pg to demo it. I’ll also have to learn about baking animations first, but that looks easy enough 🙂

---

<div class="post-metadata">

**Author:** ![Panuchka](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/panuchka/32/12054_2.png) [@Panuchka](https://forum.babylonjs.com/u/Panuchka)\
**Post date:** [February 2, 2023, 9:10pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/10 "2023-02-02T21:10:52Z")

</div>

Really interested seeing your pg!

Also wondering if WebGPU could provide any help in this matter… 🤔

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [February 2, 2023, 9:22pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/11 "2023-02-02T21:22:11Z")

</div>

WebGPU will change the game completely 🙂  
drawcalls will no longer be such a huge limitation it is today,  
and you’ll be able to just have 100’s of clones playing separate animations

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [February 2, 2023, 9:32pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/12 "2023-02-02T21:32:19Z")

</div>

I remember seeing @Deltakosh showing this in a video once,  
this version was just what i found from a quick google 🙂  
i believe it’s 10000 static clones in each scene.

[![](https://img.youtube.com/vi/gKTQ3VWn0cU/maxresdefault.jpg "WebGL vs WebGPU: BabylonJS") ](https://www.youtube.com/watch?v=gKTQ3VWn0cU)

---

<div class="post-metadata">

**Author:** ![Panuchka](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/panuchka/32/12054_2.png) [@Panuchka](https://forum.babylonjs.com/u/Panuchka)\
**Post date:** [February 2, 2023, 9:37pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/13 "2023-02-02T21:37:56Z")

</div>

I wish it could be validated already… Trying to run my example from the other thread gives bazillion errors in google chrome canary with webgpu turned on 🤪

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [February 2, 2023, 9:42pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/14 "2023-02-02T21:42:55Z")

</div>

This could be useful, too

> [@Different animation of the instances](https://forum.babylonjs.com/t/different-animation-of-the-instances/36885/7):
>
> I actually created a POC some time ago:
> 
> [Babylon.js Playground](https://playground.babylonjs.com/#3PVNQ1%239)
> 
> It let’s you\* play different animations on instances, as well as allows you do move, scale and rotate bones dynamically. My arachnid is a bad test case as it has 110 bones(loads of helpers from Max’ IK)  
> It’s only a very basic POC and could probably do with quite a bit of rewriting. For instance, meshes not in the camera’s frustum are still calculated. I just did a quick test using the VAT shader

Bone matrices are still calculated on the CPU though, so the only win is fewer drawcalls

---

<div class="post-metadata">

**Author:** ![Panuchka](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/panuchka/32/12054_2.png) [@Panuchka](https://forum.babylonjs.com/u/Panuchka)\
**Post date:** [February 2, 2023, 9:59pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/15 "2023-02-02T21:59:31Z")

</div>

Pretty cool!

There is additional requirement at least in my case, since I need some specific trickery with lighting as well and that sorcery only works with clones…

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [February 2, 2023, 10:10pm UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/16 "2023-02-02T22:10:22Z")

</div>

Hypothetically would it make sense to have bone calculations done in a wasm module and it would just write the results to a shared buffer array that babylon.js would internally reading from?

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [February 3, 2023, 7:08am UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/17 "2023-02-03T07:08:59Z")

</div>

> Really interested seeing your pg!

Yeah, getting around to it soon-ish, lol, I just keep getting side tracked by things that I need to do first.

---

<div class="post-metadata">

**Author:** ![Panuchka](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/panuchka/32/12054_2.png) [@Panuchka](https://forum.babylonjs.com/u/Panuchka)\
**Post date:** [February 3, 2023, 7:14am UTC](https://forum.babylonjs.com/t/optimizing-cloned-instanced-meshes-playing-separate-animations/37101/18 "2023-02-03T07:14:51Z")

</div>

> [@arcman7](#):
>
> I just keep getting side tracked by things that I need to do first.

Feeling you 🤣
