How to get global coordinates of mesh vertices?

The first idea is to use getVerticesData. However, it returns more vertices than is practically needed (24 instead of 8 for a cube), also it returns them in local coordinates.

I think I still can handle it with the following steps:

  1. Get the array returned by getVerticesData and group entries by 3.
  2. After grouping remove duplicate groups.
  3. Get mesh absolute position and add to it vertex local position to find the absolute position of the vertex.

But this approach makes me feel like I am overcomplicating things and missing something that probably is already implemented somewhere in BabylonJS. Is there an easier way to find all vertices global coordinates?

1 Like

{Z%SS}CIQ~{4~@X$JYJ8B8K

    let data = BABYLON.VertexData.ExtractFromMesh(mesh)
    mesh.computeWorldMatrix(true)
    data.transform(sphere.getWorldMatrix())
4 Likes

About removing duplicate vertices.

If you don’t mind affecting the source mesh, you can add it before.

    mesh.forceSharedVertices()

If you don’t want to affect the source mesh, you can copy a new mesh, here is an example of copying a new mesh and doing it

Checking the console, you can see that the length of the array is 24
and the number of vertices is 24/3 = 8.

1 Like

Regarding your second post,
I came up with my own “clean up” algorithm, so I don’t need to deal with meshes.

It does:
a) group the coordinates by 3.
b) sort by z, then by x, then by y in ascending order (required for solving my next problem).
c) remove duplicates.

https://playground.babylonjs.com/#PT1HMJ

1 Like

I found out that approach

BABYLON.VertexData.ExtractFromMesh(box).transform(box.getWorldMatrix());

is very destructive.
Practically, if you had box.checkCollisions = true, then collisons will stop working and you will be able to walk through walls.

Instead, it should be done like following:

BABYLON.VertexData.ExtractFromMesh(box).clone().transform(box.getWorldMatrix());