# Transforming copied vertex Data mutates the original mesh

**URL:** <https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272>\
**Category:** Questions\
**Created:** [July 18, 2022, 1:37pm UTC](https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272 "2022-07-18T13:37:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcace](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@pcace](https://forum.babylonjs.com/u/pcace)\
**Post date:** [July 18, 2022, 1:37pm UTC](https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272/1 "2022-07-18T13:37:52Z")

</div>

Hi, i am trying to find out why transforming copied vertexdata like this mutates the original mesh Object, even if `[ExtractFromMesh]` and matrix is forced to do a copy of the Mesh.

```auto
const transformVerticesData = (array:FloatArray | null, matrix: Matrix) => {
if(array){
  for(let i = 0; i<array.length/3; i++){
    const v = new Vector3(array[i*3], array[i*3+1], array[i*3+2]).clone();
    const mv = Vector3.TransformCoordinates(v, matrix).clone();
    array[i*3] = mv.x;
    array[i*3+1] = mv.y;
    array[i*3+2] = mv.z;
  }
}
  return array;
}

```

```auto
const inputVertexdata = VertexData.ExtractFromMesh(input as Mesh, true);
const matrix = input.computeWorldMatrix().clone();
const positions = transformVerticesData(inputVertexdata.positions, matrix);

```

Here in the last line lies the problem - i somehow change the vertexdata from `input`.

thank you so much in advance!

---

<div class="post-metadata">

**Author:** ![rdw1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rdw1/32/9212_2.png) [@rdw1](https://forum.babylonjs.com/u/rdw1)\
**Post date:** [July 18, 2022, 3:05pm UTC](https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272/2 "2022-07-18T15:05:32Z")

</div>

I did a bit of digging and this is what I found:

- In [this old changeset](https://github.com/BabylonJS/Babylon.js/blame/aa19f0642e78ba8f910e406119435f1489543dee/src/Mesh/babylon.geometry.ts#L436-L437) we can find a comment indicating a `slice` is performed on the vertex buffer in order to copy it
- In [a later change](https://github.com/BabylonJS/Babylon.js/blame/857c0eb3eafba1b4067f501d84d8be44589fcce1/src/Mesh/babylon.geometry.ts#L443-L444) the call is replaced, but the comment left in place
- On the latest [head](https://github.com/BabylonJS/Babylon.js/blob/797ca272cecf0429a8a8b0bb83a8c105bca65653/packages/dev/core/src/Meshes/geometry.ts#L982-L983) this part of the code is still the same
- The [relevant code](https://github.com/BabylonJS/Babylon.js/blob/797ca272cecf0429a8a8b0bb83a8c105bca65653/packages/dev/core/src/Meshes/geometry.ts#L444-L451) does not appear to slice (or otherwise copy) the buffer by default
- This would mean that your call to `VertexData.ExtractFromMesh` doesn’t actually create a copy

Please not that I’ve never seen this code before, so I could be completely wrong. If my understanding is correct, this would be a bug and the comment in question simply an outdated leftover.

A possible fix might then be to pass the `forceCopy` flag to the internal call to `getVerticesData()`, unless that has unintended side effects elsewhere?

If the Babylon gurus agree with the above, I’ll be happy to submit a PR with the fix. I’ve been meaning to learn the workflow for contributing to the core engine anyway, so this would be as good a chance as any 😃

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 18, 2022, 3:43pm UTC](https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272/3 "2022-07-18T15:43:01Z")

</div>

You should try `VertexData.ExtractFromMesh(input as Mesh, true, true);` to force a copy of the source data.

OR do not erase the data in your transformVerticesData function, but create a new array of the right size and place the result in the new array instead of replacing in the existing one.

Finally most of your .clone() won t help as they are anyway on new objects so unshared 🙂

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [August 17, 2022, 12:04pm UTC](https://forum.babylonjs.com/t/transforming-copied-vertex-data-mutates-the-original-mesh/32272/4 "2022-08-17T12:04:25Z")

</div>

Hello @pcace just checking in if there’s any more help you’d like
