How to merge Meshes


I have two (or more) meshes and for performance reasons I want to merge them before I create the Geometry. How can I do that. I have searched through the forum but I didn’t find out how to do it.
The Meshes are created like this (example) :

Mesh m1 = new Mesh();                 
m1.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices1));
m1.setBuffer(VertexBuffer.Type.Normal,3, BufferUtils.createFloatBuffer(nb1)); 

Mesh m2 = new Mesh();                 
m2.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices2));
m2.setBuffer(VertexBuffer.Type.Normal,3, BufferUtils.createFloatBuffer(nb2));

Create a mesh big enough for all of the vertexes. Copy the vertex data into that mesh.

Its not exactly what you asked (and if it literally is what you asked then go with what pspeed suggested) but if there is something more complex this is just a simplification of there is GeometryBatchFactory.optimize(someNode). It is given a node with geometries attached (that must all use the same material) and optimised them and one of its options for optimising is to merge meshes (while maintaining any applied rotations, translations etc)

Thanks for the answer! I know WHAT I have to do. The problem is that I don’t know HOW to do it. When I create the Mesh, I use 2 arrays with the vertices I need.

Vector3f vertices[];
Vector3f nb[]; 

But I don’t know which methods of the Mesh give me back these arrays so that I can merge them with the arrays of another mesh.I only find methods that give back buffers, but I don’t know how to get back the arrays from these buffers.

Why are you giving them to the mesh in the first place? Why not build a double sized mesh to start with? (Keeping your vertices in an array or similar till then)

There are getters for getting the VertexBuffers. There are methods on the vertex buffer for getting the java.nio buffers. There are numerous mesh subclasses that have examples of updating mesh buffers. You could even look at the code in GeometryBatchFactory that is already doing that.

I don’t have time to search and cut and paste for you… nor would it be as educational. But if you are still stuck or can’t find your way around let us know.

Sorry if I’m annoying you. There are just to much things that I don’t understand because I’m just an autodidact (and the not working Javadoc doesn’t make it easier). I think it will be the best for me to write my own Methods to generate primitives that put their data into a big array and then generate the Meshes.

If I’m not mistaken, this is the type of code you are looking for.

      FloatBuffer terrainTexCoords = mesh.getFloatBuffer(Type.TexCoord);
      FloatBuffer terrainVertsBuffer = mesh.getFloatBuffer(Type.Position);
      FloatBuffer terrainNormalBuffer = mesh.getFloatBuffer(Type.Normal);

      Vector3f[] terrainVertsArray = BufferUtils.getVector3Array(terrainVertsBuffer);
      Vector3f[] terrainNormalArray = BufferUtils.getVector3Array(terrainNormalBuffer);

I recall struggling to find these myself while first learning mesh generation, as I expected a getter method on the actual Buffers, but instead this functionality is built into static methods of the BufferUtils class. I’m sure there’s a good reason for it that I don’t know since I didn’t write it, but it can definitely makes things a bit more confusing while learning to work with mesh buffers.

Thank you very much!!! That’s exactly the part I was looking for!!!


For the future:

From there you can also see the getters on Mesh, click through to see the getters on VertexBuffer, etc… but the list of subclasses and my hint above would have been enough to use the source link to find mesh subclasses. (If you use the SDK then I think this is also just a button click away.) has an example of updating existing buffers that is pretty illustrative:

The source code to GeometryBatchFactory may also be interesting (as already mentioned above) because it does basically what you want already and so would be a good example. I had to look up the javadoc to find the right package first but here is a link to its source code:

I personally find GeometryBatchFactory a bit wordy for just learning how to manipulate meshes but it certainly does get the data from the vertex buffers and modify it in an efficient way. It just can be kind of hard to read. is very straight forward (as are several of the other mesh classes that update their buffers instead of recalculating them).

There are getter methods on the actual buffers. I’m guessing the static methods you are referring to are dealing with Vector3f arrays or something? If so then those are utility methods that create a lot of unnecessary garbage just for convenience. Unnecessary if you just want to poke at the floats themselves (which for me is 99% of the time).

I only ever use BufferUtils for creating buffers. When I’m updating a VertexBuffer then I call getData()… which returns the data in the buffer.

1 Like