# Duplicating mesh using vertexData

**URL:** <https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002>\
**Category:** Questions\
**Tags:** mesh, vertex\
**Created:** [October 20, 2022, 11:42pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002 "2022-10-20T23:42:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjythebee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/benjythebee/32/13587_2.png) [@Benjythebee](https://forum.babylonjs.com/u/Benjythebee)\
**Post date:** [October 20, 2022, 11:42pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/1 "2022-10-20T23:42:06Z")

</div>

Hey,  
I’m trying to figure out if I can reconstruct a mesh by extracting its vertex data. The ultimate goal being for example a front end sending the data to a backend for the backend to reconstruct the mesh in a nullEngine or something like that.

I have this playground  
[Babylon.js Playground](https://playground.babylonjs.com/#AXHXAQ) which attempts at creating a copy of the original mesh by using vertex data.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/2/7/27bd81506ba5493dbbc116910e03e53f98ad0685.png)

However as you can see, I’m pretty far from the desired result.

How could this be achieved? Am I on the right track? Do we need to account for something else other than the vertex data?  
Cheers

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2022, 11:43am UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/2 "2022-10-21T11:43:40Z")

</div>

It’s possible to reconstruct a mesh by hand but it’s a bit complicated by the fact some meshes can be loaded from a gltf/glb file which stores data as right-handed…

To fix your PG:

- you should call `mesh.makeGeometryUnique()` after cloning the mesh. By default, cloned meshes share the same geometry (which is the purpose of cloning), so calling `bakeCurrentTransformIntoVertices()` on the clone (or the source mesh) will impact the other one if you don’t duplicate the geometry
- you should not parent your new meshes to anything as `bakeCurrentTransformIntoVertices()` will take care of all the parent hierarchy (this is why your mesh is 10x too big in your PG)
- `vertexData.colors` should be left `undefined` if there are no vertex colors for the mesh (this is why your mesh is black in your screenshot)
- for meshes loaded through a gltf/glb file, the `mesh.overrideMaterialSideOrientation` is set with a value which will override the `material.sideOrientation` property: you should copy this property when you create the new mesh
- you should also assign the material to the new mesh if you want the same rendering
- last complication is that gltf/glb meshes have right-handed geometry, meaning that computing normals with a `BABYLON.VertexData.ComputeNormals` call will need to negate the normal =\> you can use the `useRightHandedSystem` option for that.

Note that I determine that a mesh has a right-handed geometry by checking that `overrideMaterialSideOrientation` has a value (not `undefined` nor `null`) because there’s only the gltf/glb loader which is setting this property. It’s not a very robust way to recover this piece of information but I don’t know if there’s a better way to do it… Maybe @bghgary will know?

Here’s the corrected PG:

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

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 21, 2022, 3:57pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/3 "2022-10-21T15:57:43Z")

</div>

If the vertex data is from a glTF, the winding order of triangles are guaranteed to be counterclockwise if the geometry is not mirrored due to negative scales (i.e., negative determinant). So, in this case, you can just use `true` for `useRightHandedSystem` of `ComputeNormals`.

See [https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#instantiation](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#instantiation).

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2022, 7:03pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/4 "2022-10-21T19:03:10Z")

</div>

`ComputeNormals` is using the 3D space coordinates of the vertices as read from the file, so those are not impacted by transformations applied to the mesh.

So, in my understanding, for glTF meshes we should always pass `true` for `useRightHandedSystem` of `ComputeNormals`. Or did I miss something? The question is how to detect that a mesh has a right-handed geometry… We don’t have this information in Babylon.js (?). We have it indirectly for glTF meshes because it’s the only case where `overrideMaterialSideOrientation` is set with an actual value.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 21, 2022, 7:21pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/5 "2022-10-21T19:21:21Z")

</div>

I don’t understand what “right-handed geometry” means. AFAIK, geometries are not right-handed or left-handed. For ComputeNormal, this flag just changes which way the normals are computed.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2022, 8:58pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/6 "2022-10-21T20:58:07Z")

</div>

We discussed about this offline with @bghgary and the outcome is that we should do the same thing than:

> <https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Meshes/mesh.ts#L2268-L2275>

and set `useRightHandedSystem` depending on the value of `sideOrientation`.

---

<div class="post-metadata">

**Author:** ![Benjythebee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/benjythebee/32/13587_2.png) [@Benjythebee](https://forum.babylonjs.com/u/Benjythebee)\
**Post date:** [October 23, 2022, 9:01pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/7 "2022-10-23T21:01:49Z")

</div>

`you should also assign the material to the new mesh if you want the same rendering`

I’m aware the colors are given by the textures and the material, but ideally, since my question mentioned that a backend should be able to fully reconstruct the asset, is there a way we can merge the colors into the vertex data / vertices? maybe via some attribute?

Thanks!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 23, 2022, 9:19pm UTC](https://forum.babylonjs.com/t/duplicating-mesh-using-vertexdata/35002/8 "2022-10-23T21:19:49Z")

</div>

Not really, vertices can have a vertex color, but you won’t be able to get the same rendering than with a full material (albedo/bump/metallic/… textures).
