Mesh.convertToFlatShadedMesh() mutates vertex indices

Hi everyone.

I am not sure if this is intended behaviour. But it really takes me a while to identify the issue. So when I do vertexData.applyToMesh(mesh); mesh.convertToFlatShadedMesh() , the vertex indices that was originally assigned to it get mutated in place.
I fully understand the nature of this method is to add vertices and to have a [0,1,2,3,4…] kind of indices array. But it seems unexpected to me that this vertexData then cannot be used elsewhere, for example another mesh that uses exact same geometry/vertexData.

Here it gets a reference to the original indices array.

And later,
for (let i = 0; i < indices.length; i++) {indices[i] = i; // Mutates shared array}
I think the fix can be simple,
const indices = this.getIndices(false, true); //getIndices(copyWhenShared?: boolean, forceCopy?: boolean): Nullable<IndicesArray>
which makes more sense to me since we already create a new vertexBuffer anyway.

Here is a repro: Babylon.js Playground
I am glad to know your opinions!

I agree with your fix, This is clearly an oversight from us

Please send a PR :slight_smile:

Hi Deltakosh! I’ve submitted a PR to fix this issue:

The fix prevents both convertToFlatShadedMesh() and convertToUnIndexedMesh() from mutating the original indices array by using getIndices(false, true) to force a copy.

1 Like

Gentleman! You rock!

1 Like