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.
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.
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.
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.
Hi darcey. If you look at line 89 (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.