Search code examples
libgdxclonemesh

LibGDX edit model mesh from a template


I am working on a project that I take a "base" model/mesh, and I form it to look like a more defined terrain. I am having an issue, though, when I try to edit the mesh. This is basically how I do it: (assets is an AssetManager)

Model terrain = assets.get("terrain.g3db", Model.class);
Mesh template = null;
for (float x = 0; x <= maxX; x += 1f) {
    for (float z = 0; z <= maxZ; z += 1f) {
        Mesh m = template.copy(false);

        // get proper vertices here

        mesh.setVertices(vertices);
        terrain.calculateTransforms();

        terrain.meshes.set(0, mesh);

        ModelInstance terrainInstance = new ModelInstance(terrain);
        terrainInstance.transform.setToTranslation(x, 0, z);
        instances.add(terrainInstance);
    }
}    

The only issue is when I do that I just get a flat terrain. When I remove this line:

terrain.meshes.set(0, mesh);

I get the terrain, but every model instance has the same thing. To fix this, I have to remove that line, and add this one:

terrain.meshes.get(0).setVertices(vertices);

What I think is happening with the line that causes the flat map is when I set it, it removes anything that might have said, "hey, this mesh relates to this part of the model".

Screenshots:
With the line that causes a flat map: Picture
Without that line: Picture

UPDATE: Thanks to Xoppa's comment suggesting that I load a new model for each spot, but in doing that, I have to use a G3dModelLoader. I would like to use an AssetManager if that is even possible, but I don't think it is...


Solution

  • To fix this, I made a custom loader that uses the G3dModelLoader. To make sure I do not have any memory leaks, I made two variables in the class, being the loader itself, and an array of the initialized models. One you run the #getNewModel() method it loads the new model, adds it to the list of initialized models, and returns the new model. In the #dispose() method, it just runs through the list of initialized models, and runs the dispose method on those models.