# Most efficient way to update vertex buffer?

**URL:** <https://forum.babylonjs.com/t/most-efficient-way-to-update-vertex-buffer/27802>\
**Category:** Questions\
**Created:** [February 19, 2022, 8:40am UTC](https://forum.babylonjs.com/t/most-efficient-way-to-update-vertex-buffer/27802 "2022-02-19T08:40:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrlooi](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mrlooi/32/18406_2.png) [@mrlooi](https://forum.babylonjs.com/u/mrlooi)\
**Post date:** [February 19, 2022, 8:40am UTC](https://forum.babylonjs.com/t/most-efficient-way-to-update-vertex-buffer/27802/1 "2022-02-19T08:40:18Z")

</div>

Hey ya’ll,

I’m trying to update small sections of the vertex buffer in real-time (60fps). Applications include vertex editing + weight painting.  
Was wondering what is the most efficient way to update vertex buffer positions/weights each frame? I only see updateVerticesData/setVerticesData, but this does not seem efficient, esp if I only need to update one single vertex.

Would be great to know if there is an API for this, to avoid a deep dive into the source code i.e. Buffer class etc 🙂

---

<div class="post-metadata">

**Author:** ![mrlooi](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mrlooi/32/18406_2.png) [@mrlooi](https://forum.babylonjs.com/u/mrlooi)\
**Post date:** [February 19, 2022, 8:44am UTC](https://forum.babylonjs.com/t/most-efficient-way-to-update-vertex-buffer/27802/2 "2022-02-19T08:44:11Z")

</div>

I see something in this thread @Deltakosh  
Perhaps that’s my answer? 😀

> [@Partially update a vertexbuffer or split buffers into smaller parts](https://forum.babylonjs.com/t/partially-update-a-vertexbuffer-or-split-buffers-into-smaller-parts/9400/2):
>
> This is actually supported slight_smile You can get the vertexbuffer you want and be able to call: [https://doc.babylonjs.com/api/classes/babylon.vertexbuffer#updatedirectly](https://doc.babylonjs.com/api/classes/babylon.vertexbuffer#updatedirectly)

In which case, question

1. Does it have to be set to updatable first on creation? From [line](https://github.com/BabylonJS/Babylon.js/blob/master/src/Buffers/buffer.ts#L174) it seems to be the case
2. If I want to update e.g. 10 non-contiguous vertices, do I loop the vertices and run updateDirectly 10 times (one per vertex) and would this affect performance

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [February 19, 2022, 9:25am UTC](https://forum.babylonjs.com/t/most-efficient-way-to-update-vertex-buffer/27802/3 "2022-02-19T09:25:02Z")

</div>

Hello!

> [@mrlooi](#):
>
> Perhaps that’s my answer? 😀

Yes, it is. If you have a look at the example PG it logs the timings. `updateDirectly` is faster.

> [@mrlooi](#):
>
> If I want to update e.g. 10 non-contiguous vertices, do I loop the vertices and run updateDirectly 10 times (one per vertex) and would this affect performance

This results only in one draw call at the end so it’s ok. But you should do your own tests. It’s very easy using `console.time()`. It depends on your scenario. Maybe it’s worth to update the whole buffer so you don’t have to slice the buffer array and save some CPU cycles.

> [@mrlooi](#):
>
> Does it have to be set to updatable first on creation?

Yes.

🖖
