# How do I extract mesh vertex data properly?

**URL:** <https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381>\
**Category:** Questions\
**Created:** [February 5, 2022, 6:15am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381 "2022-02-05T06:15:53Z")\
**Posts on this page:** 13\
**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 5, 2022, 6:15am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/1 "2022-02-05T06:15:53Z")

</div>

Hey ya’ll

Just wondering what is the right method to extract vertex data onto the CPU (esp vertex data bounded to a skeleton)?  
I’ve tried all sorts of ways and they don’t seem to work well + look hacky.

For example, I’ve tried

```auto
mesh.computeBonesUsingShaders = false; // set to CPU
await new Promise<void>((resolve) => {  
        // wait for vertex data to be loaded to the CPU post render loop?
	scene.onAfterRenderObservable.addOnce(() => {
		resolve();
	});
});
mesh.refreshBoundingInfo(true);
mesh.computeWorldMatrix(true);
mesh.updateFacetData();
const vertices = mesh.getVerticesData(VertexBuffer.PositionKind);

```

But I don’t think it always works. The vertices data still looks different from the shader/GPU stuff

@Pryme8 @sebavan @Evgeni_Popov @syntheticmagus

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [February 5, 2022, 4:11pm UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/2 "2022-02-05T16:11:33Z")

</div>

I think your only option will be to make sure all your displacements are happening on the CPU.

Otherwise if like animations and other things are happening through the GPU can be considered fairy dust and not really exist in any tangible way that you can reference.

I might be wrong though, but I assume you are trying to get some info from a mouse click on a GPU deformed mesh right?

You could do a GPU picking method but that would require you to pack all your data on the shader and when you preform the pick have the meshes shaders display that data and then collect the pixel information from the screen point the decode that data. It’s hard to get more than just IDs or position something nested in that though.

For the skeleton render it was way easier to just toggle it to CPU animation mode. But if you can’t for performance reasons then the GPU pick might be the only option and just do like IDs and have an associated map with the IDs and what data you want ready to reference once you pull the GPU ID.

What data is coming back wrong in your setup?

---

<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 6, 2022, 3:23am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/3 "2022-02-06T03:23:18Z")

</div>

So actually I’m looking to get the actual vertices to do stuff like skinning/deformations. I don’t care about performance since it’s just a one time thing.  
For example, a user places a bone on the arm of the mesh. I would need the mesh vertices at that point in time to do skinning on the bone. And ofcourse the skinning algorithm needs to run on CPU data.  
The problem I’ve found is that the CPU vertex data is not the same as what’s on screen. The arm is one place but the CPU data suggests otherwise

A much more advanced usecase than skeleton rendering but I think BJS could make vertex updating etc easier for devs to do more interesting stuff 🙂

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 6, 2022, 4:05am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/4 "2022-02-06T04:05:29Z")

</div>

There’s an internal function \_getPositionData that can return the vertex positions after applying the skeleton:

```auto
const vertices = mesh._getPositionData(true); // true to apply skeleton

```

Or if you don’t want to use it directly, it could still serve as reference for how to apply the skeleton. 🙂

> <https://github.com/BabylonJS/Babylon.js/blob/819bf7ac2d3b8aff6304003a7efc02a835ac642b/src/Meshes/abstractMesh.ts#L1410>

---

<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 6, 2022, 4:06am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/5 "2022-02-06T04:06:19Z")

</div>

Thanks!  
I’m guessing this function is an improvement over getVerticesData for CPU extraction?

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 6, 2022, 4:09am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/6 "2022-02-06T04:09:05Z")

</div>

Well it’s used to refresh the bounding info for example and other places where the vertex positions need to have the skeleton and/or morphing applied in JS, but it’s not part of the official API…

---

<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 6, 2022, 4:14am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/7 "2022-02-06T04:14:55Z")

</div>

This should be a public method! Thanks Blake : )

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 6, 2022, 4:41am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/8 "2022-02-06T04:41:07Z")

</div>

It looks like there’s some other internal updating that the function does too, so maybe better to move just the parts that apply the skeleton and morphing into a public function (that uses the same helper as the private version would)… or maybe better to just add a flag to the existing function to skip the internal updating? Curious what @sebavan will think about this one. 🙂

---

<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 6, 2022, 4:43am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/9 "2022-02-06T04:43:01Z")

</div>

Yea I really think this should be the default for getting vertices instead of getVerticesData(PositionKind) 🙂

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 7, 2022, 4:52pm UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/10 "2022-02-07T16:52:30Z")

</div>

Yeah I’m not super keen on exposing that specific function as it handles a lot of internals. I would prefer have an additional function with only the data retrieval part

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 7, 2022, 6:30pm UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/12 "2022-02-07T18:30:51Z")

</div>

Okay here’s a little PR to add a separate exposed function that just gets the data and applies the skeleton/morph, which is also called by the hidden version that handles the internals, to get the ball rolling. 🙂

> <https://github.com/BabylonJS/Babylon.js/pull/11937>
>
> PR to add a public version of the \_getPositionData function to the AbstractMesh …class to allow users to get positions data with skeleton and/or morph applied.
> 
> Forum: https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/9
> 
> NOTE: Undid previous commits, started with fresh, up-to-date repo and re-applied the changes. Hopefully will build and validate properly now, like it is locally...
> 
> Original closed PR for reference: https://github.com/BabylonJS/Babylon.js/pull/11928

EDIT: welp seems I went astray somewhere, after I get up and have breakfast I’ll try to find where… 🤔 Okay back on it, hopefully can get it validating… And hurray, looks like it’s good now. 🙂

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [February 10, 2022, 2:21am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/13 "2022-02-10T02:21:01Z")

</div>

Heya, just a quick update: the user/API version of getPositionData() is available in the latest release now. 🙂

Examples:

```auto
// Get unmodified vertex positions
positions = mesh.getPositionData();

// Apply skeleton
positions = mesh.getPositionData(true);

// Apply morph
positions = mesh.getPositionData(false, true);

```

---

<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 10, 2022, 3:51am UTC](https://forum.babylonjs.com/t/how-do-i-extract-mesh-vertex-data-properly/27381/14 "2022-02-10T03:51:32Z")

</div>

cheers, thanks Blake!
