MergeMeshes is Sharing UV2 Channel Buffer Data?

So I’m taking my grass system to the next level and have it to the point were you can almost export Terrain from the Unity Toolkit one for one even with the detail maps.

Im running into an odd problem though where I am doing a section of code like this:

public static UpdateDetailBlockData(detail: BABYLON.AbstractMesh, buffer:number[], startIndex: number): {mergeMesh: BABYLON.Mesh, endIndex: number}{
            const mergeList = []
            const vertexCount = detail.getTotalVertices()
            let index = 0
            for(let i = 0; i < buffer.length; i+=2){
                const detailMesh = detail.clone('detailMesh', null)               
                const supportBufferData = []
                for(let j = 0; j < vertexCount; j++){
                        index + startIndex, 0
                detailMesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, supportBufferData)

            const mergeMesh = BABYLON.Mesh.MergeMeshes(mergeList, true, true, undefined, undefined, false)
            return {mergeMesh, endIndex: index + startIndex}

If I console log out the supportBufferData it shows the correct information for example on the first block
it does [0,0] * 80 times then [1, 0] * 80 times because there are only two detail meshes in this block.

So the array is 160 numbers long half being 0,0 the other half being 1,0.

Now once I merge the meshes and check the uv2 buffer it is all [0, 0].

Basically each block only duplicates the uv2 data from the first item in the merge and applies it to the rest of the items.

Kinda odd behavior, not sure if this is a bug or Im a goober.

Thinking about just pulling the buffer out of the loop and setting the full results on the mergeMesh to see if that does it.

If you call makeGeometryUnique on each clone then it should work I think, otherwise they’re all sharing the same vertices so each call to setVerticesData is overwriting the same vertices data that are used for all of the clones.


You are a gentleman and a scholar.


Looks like we have a match!