# Adding additional MeshInstances to Model

**URL:** <https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661>\
**Category:** Help & Support\
**Created:** [March 25, 2017, 12:59pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661 "2017-03-25T12:59:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 25, 2017, 12:59pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/1 "2017-03-25T12:59:59Z")

</div>

Hello,

Is it possible to update the graph of a model during runtime, adding additional mesh instances?

Tried the following code, though it didn’t work:

> ```
> var box = pc.createBox(this.app.graphicsDevice);
> var node = new pc.GraphNode();
> 
> ```

> ```
> node.setPosition(
> pc.math.random(-this.boxDimensions, this.boxDimensions),
> pc.math.random(-this.boxDimensions, this.boxDimensions),
> pc.math.random(-this.boxDimensions, this.boxDimensions)
> );
> 
> var material = this.material.resource;
> var meshInstance = new pc.MeshInstance(node, box, material);
> 
> ```

> ```
> this.parentEntity.model.model.graph.addChild( node );
> this.parentEntity.model.model.meshInstances.push(meshInstance);
> 
> this.app.scene.update();
> 
> ```

---

<div class="post-metadata">

**Author:** ![max](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/max/32/477_2.png) [@max](https://forum.playcanvas.com/u/max)\
**Post date:** [March 27, 2017, 10:12am UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/2 "2017-03-27T10:12:30Z")

</div>

Hm… It might be a bit of a problem doing that way. Why you simply not add it as another child entity with model component?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 27, 2017, 12:29pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/3 "2017-03-27T12:29:50Z")

</div>

You are right it works that way. I was doing some stress tests to see the performance impact of entities, graphs, models etc and find the best way to reduce draw calls / memory allocation for static geometry. A way to merge geometry/models together.

---

<div class="post-metadata">

**Author:** ![max](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/max/32/477_2.png) [@max](https://forum.playcanvas.com/u/max)\
**Post date:** [March 27, 2017, 2:55pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/4 "2017-03-27T14:55:08Z")

</div>

Each meshInstance - is a draw call actually. So by adding another mesh instance is same as having another entity with own mesh instance, in terms of rendering performance. There will be tiny more overhead indeed, but we talking here tiny micro-optimization in most cases. In most cases 99% of CPU pressure will come from other things and actual game logic.

Dev tools have Profiles tab, with CPU profiling - really useful tool to see what is actually taking most performance out.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 27, 2017, 3:24pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/5 "2017-03-27T15:24:43Z")

</div>

@max each mesh instance is a draw call, even if two mesh instances share the exact same material?

---

<div class="post-metadata">

**Author:** ![max](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/max/32/477_2.png) [@max](https://forum.playcanvas.com/u/max)\
**Post date:** [March 27, 2017, 3:51pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/6 "2017-03-27T15:51:23Z")

</div>

If they share same message, it still need to dispatch a “draw” with some uniforms that are unique per each mesh instance, like their world matrix (transformation). If they are same, then they will share same vertex and index buffer and more likely avoid a lot of state changes including less changes of uniforms for program (shader) as they share material.

In order to avoid a draw call, either they have to be joined into single mesh instance by merging index buffer. Or by using instancing. But instancing only worth if you’ve got large number of same entities - then it can really help to reduce number of CPU \> GPU instructions. But this is subject to case to case, and requires actual testing to identify if it will worth it.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [March 27, 2017, 9:09pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/7 "2017-03-27T21:09:40Z")

</div>

@max is there a PlayCanvas API for instancing?

---

<div class="post-metadata">

**Author:** ![max](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/max/32/477_2.png) [@max](https://forum.playcanvas.com/u/max)\
**Post date:** [March 28, 2017, 11:09am UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/8 "2017-03-28T11:09:19Z")

</div>

There are two approaches, but we endup not recommending both of them as through our tests performance was not changed in positive way.

One way is to enable simple auto instancing, and that is naive approach. All needed is to enable `enableAutoInstancing` on `GraphicsDevice`:  
`app.graphicsDevice.enableAutoInstancing = true`

But again, this very likely not going to improve performance.

And other way is to manually control instancing, by managing array of uniforms to be passed to the instancing shader, but current implementation is not in usable state and will be removed/changed to a better API.

---

<div class="post-metadata">

**Author:** ![yqu](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yqu/32/3103_2.png) [@yqu](https://forum.playcanvas.com/u/yqu)\
**Post date:** [May 14, 2019, 9:10pm UTC](https://forum.playcanvas.com/t/adding-additional-meshinstances-to-model/3661/9 "2019-05-14T21:10:50Z")

</div>

Can it be possible to add additional mesh instances if they are skinned? The key problem with adding another entity is animation component not playing the animation on all the mesh instances, but instead you would need a model and animation component per entity.
