# How to update the vertices of a mesh?

**URL:** <https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867>\
**Category:** Questions\
**Created:** [November 8, 2019, 10:42am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867 "2019-11-08T10:42:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheng](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@cheng](https://forum.babylonjs.com/u/cheng)\
**Post date:** [November 8, 2019, 10:42am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/1 "2019-11-08T10:42:15Z")

</div>

I created a mesh using BABYLON.PolygonMeshBuilder and I want to update its vertices in the scene.registerBeforeRender(function ()) method. Here is the code:

```
    var poly = [];
    this.lenghtBox = 10;

    if (this.isMobile) {
        this.lenghtBox = 20;
    }
    this.wid = this.lenghtBox * 4;
    this.hei = this.lenghtBox * 2;

    var z = -30;
    poly.push(new BABYLON.Vector3(this.lenghtBox * 2, 0, z));
    poly.push(new BABYLON.Vector3(-this.lenghtBox * 2, 0, z));
    poly.push(new BABYLON.Vector3(-this.lenghtBox * 2, this.lenghtBox * 2, z));
    poly.push(new BABYLON.Vector3(this.lenghtBox * 2, this.lenghtBox * 2, z));
    var tag = new BABYLON.PolygonMeshBuilder("test", poly, this.scene).build(true);

scene.registerBeforeRender(function () {
   var positions =tag.getVerticesData(BABYLON.VertexBuffer.PositionKind);
   var UpdatePos = [];

       var wor = [];
               //Get world position from window coordinate.
                wor.push(cam.GetWorldPoint(new BABYLON.Vector3(10, 20, 0))); 
                wor.push(cam.GetWorldPoint(new BABYLON.Vector3(10, 40, 0)));
                wor.push(cam.GetWorldPoint(new BABYLON.Vector3(40, 40, 0)));
                wor.push(cam.GetWorldPoint(new BABYLON.Vector3(40, 20, 0)));

                for (var i = 0; i < win.length; i++) {
                    UpdatePos.push(win[i].x);
                    UpdatePos.push(win[i].y);
                    UpdatePos.push(10);
                }

         tag.setVerticesData(BABYLON.VertexBuffer.PositionKind, UpdatePos);
});

```

I check the mesh vertices with getVerticesData and the return positions reflected the changes but the problem is the mesh (tag) didn’t update to the new position in the screen.

---

<div class="post-metadata">

**Author:** ![Darcey](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darcey/32/5539_2.png) [@Darcey](https://forum.babylonjs.com/u/Darcey)\
**Post date:** [November 8, 2019, 12:43pm UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/2 "2019-11-08T12:43:48Z")

</div>

When I modify a Mesh I use:

```
this.sphere.bakeCurrentTransformIntoVertices();

```

[https://doc.babylonjs.com/resources/baking\_transformations](https://doc.babylonjs.com/resources/baking_transformations)

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [November 8, 2019, 1:14pm UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/3 "2019-11-08T13:14:45Z")

</div>

Hey Cheng… I made a little “starter” playground… [https://www.babylonjs-playground.com/#1TGOMH#9](https://www.babylonjs-playground.com/#1TGOMH#9)

It is mostly your code, except where noted.

I dunno what cam.GetWorldPoint() is supposed to do, but I just made a little do-nothing function at line 41. Modify at-will, and re-save/re-run, whatever.

Also, you use a data structure named _win_ in lines 62-64… but I don’t know what _win_ is. If you could create a ‘win’ in that playground FOR US, then maybe we could activate this playground fully.

Then we can see what is problem with `tag.setVerticesData()` maybe.

Help us get this test-playground working, if you please. Make edits, tests, more saves, then paste us URL’s to the interesting save-versions… and we’ll talk more.

It is SO SO much easier for helpers to help… when we have a playground demo that shows the problem. Thanks for your help with our testing playground.

PS: Notice line 38. Perhaps vertices of polygon mesh… are not set ‘updatable’ by default. Maybe we need to tell them to be-so. hmm. 🙂 Not sure.

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [November 8, 2019, 1:42pm UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/4 "2019-11-08T13:42:27Z")

</div>

Update:

[https://www.babylonjs-playground.com/#1TGOMH#10](https://www.babylonjs-playground.com/#1TGOMH#10)

After activating showNormals tool on previous playground, I realized I still had overlapping vertices. So I made some more adjustments in lines 28-31… to get a “plane” created. Then I gave it a material and a color… for fun. 🙂

Cheng… perhaps some issues in lines 28-31… made it APPEAR that the vertices were not updating. Not sure. Now we have a good-looking polygonMesh to play-with and test-upon, anyway. Nicely colored with good lighting normals on each corner.

---

<div class="post-metadata">

**Author:** ![cheng](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@cheng](https://forum.babylonjs.com/u/cheng)\
**Post date:** [November 9, 2019, 7:43am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/5 "2019-11-09T07:43:14Z")

</div>

Hi @Wingnut @Darcey , it is complicated, actually what I want to achieve is to create a few rectangles in fixed size at the top left position of the client area (A contour legends) . GetWorldPoint is a method that take in window coordinate and return you a world coordinate, so by passing four corner of the rectangle in window coordinate it will return you the 4 corners of points in world coordinate. I know we maybe able to update the mesh position by mesh.bakeCurrentTransformIntoVertices(), or mesh.position or mesh.scaling but in this case I want to update the mesh vertices directly by replacing vertices with the values I calculate myself.

---

<div class="post-metadata">

**Author:** ![Darcey](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/darcey/32/5539_2.png) [@Darcey](https://forum.babylonjs.com/u/Darcey)\
**Post date:** [November 9, 2019, 8:11am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/6 "2019-11-09T08:11:04Z")

</div>

Wouldn’t that be somewhere along the lines of:  
[https://doc.babylonjs.com/how\_to/custom](https://doc.babylonjs.com/how_to/custom)

```
var customMesh = new BABYLON.Mesh("custom", scene);

var positions = [-5, 2, -3, -7, -2, -3, -3, -2, -3, 5, 2, 3, 7, -2, 3, 3, -2, 3];
var indices = [0, 1, 2, 3, 4, 5];

var vertexData = new BABYLON.VertexData();

vertexData.positions = positions;
vertexData.indices = indices;    

vertexData.applyToMesh(customMesh);

```

Link at bottom of page:  
[https://doc.babylonjs.com/how\_to/updating\_vertices](https://doc.babylonjs.com/how_to/updating_vertices)

[https://www.babylonjs-playground.com/#VE6GP#2](https://www.babylonjs-playground.com/#VE6GP#2)

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [November 9, 2019, 9:38am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/7 "2019-11-09T09:38:06Z")

</div>

Hi darcey. If you look at [line 89](https://www.babylonjs-playground.com/#1TGOMH#10) (yet disabled, but maybe soon enabled), Cheng is setting positions-kind data there… but not on a blank/custom mesh. Cheng is doing it on a pre-made updatable polygon call _tag_. ONLY positions. No need to do indices or normals, because Cheng will be using the original indices and normals that are already installed on _tag_.

SetVerticesData() is like a diet version of applyToMesh(). 🙂

Good links/refs, though. It shows Cheng that there is another way besides polyonMeshBuilder… to create _tag_.

---

<div class="post-metadata">

**Author:** ![cheng](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@cheng](https://forum.babylonjs.com/u/cheng)\
**Post date:** [November 9, 2019, 9:55am UTC](https://forum.babylonjs.com/t/how-to-update-the-vertices-of-a-mesh/6867/8 "2019-11-09T09:55:36Z")

</div>

Exactly like what you have explain! Maybe I should give a try on BABYLON.VertexData.
