applyToMesh taking a long time.
Currently I’m generating a chunk mesh on each block placement/removal which creates an entirely new mesh. Here’s a sketch of the code which does this:
// meshData has been generated by iterating through the chunk... let mesh = new BABYLON.Mesh('chunk', scene); let vertexData = new BABYLON.VertexData(); vertexData.positions = meshData.positions; vertexData.indices = meshData.indices; vertexData.normals = meshData.normals; vertexData.applyToMesh(mesh);
I’ve profiled the code and a significant bottleneck is the
applyToMesh function. In fact, this takes roughly as long (~30ms for a 32x32x32 chunk worst case scenario) as it takes to generate the positions/indices/normals arrays in the first place.
It looks like this function is slow because it is creating a vertexbuffer object and copying a lot of data from the vertex data object. This seems like a lot of unnecessary overhead, so would my best approach here be to try and create a vertexbuffer directly, or is there another way which is better? And if so, is this supported by the engine or will I have to use unsupported apis/properties for this? No problem if I have to hack the engine, I just want to make sure I have the right approach.