# How to get global coordinates of mesh vertices?

**URL:** <https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036>\
**Category:** Questions\
**Created:** [August 5, 2023, 7:35pm UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036 "2023-08-05T19:35:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [August 5, 2023, 7:35pm UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036/1 "2023-08-05T19:35:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![qq2315137135](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/qq2315137135/32/32219_2.png) [@qq2315137135](https://forum.babylonjs.com/u/qq2315137135)\
**Post date:** [August 6, 2023, 5:51am UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036/2 "2023-08-06T05:51:33Z")

</div>

![{Z%SS}CIQ~{4~@X$JYJ8B8K](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/2/e22a88042e2ab0379f994d3c7dedb69b04695e8c.gif)

```javascript
    let data = BABYLON.VertexData.ExtractFromMesh(mesh)
    mesh.computeWorldMatrix(true)
    data.transform(sphere.getWorldMatrix())

```

> **[Babylon.js Playground](https://playground.babylonjs.com/#ZNJKRT%2310)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![qq2315137135](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/qq2315137135/32/32219_2.png) [@qq2315137135](https://forum.babylonjs.com/u/qq2315137135)\
**Post date:** [August 6, 2023, 6:14am UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036/3 "2023-08-06T06:14:51Z")

</div>

About removing duplicate vertices.

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

```javascript
    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

> **[Babylon.js Playground](https://playground.babylonjs.com/#ZNJKRT%239)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

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

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [August 6, 2023, 11:12pm UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036/4 "2023-08-06T23:12:15Z")

</div>

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](https://playground.babylonjs.com/#PT1HMJ)

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [September 11, 2023, 4:44am UTC](https://forum.babylonjs.com/t/how-to-get-global-coordinates-of-mesh-vertices/43036/5 "2023-09-11T04:44:13Z")

</div>

I found out that approach

```auto
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:

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

```
