I’ve been playing around with TextureAtlas, trying to understand what it can be used for, and what it can’t be used for. However, what I thought it could do doesn’t seem to be working, so, I need some help.
I’ve looked at the example (https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/tools/TestTextureAtlas.java), and I understand what that does. It takes many geometries, each with different textures, and replaces them with a single geometry and a single texture, which is an amalgamation of all those different textures. Creating the same scene without using TextureAtlas results in slightly lower FPS, so, it certainly seems to make a difference.
This seems to be like GeometryBatchFactory.optimize, except rather than create a separate geometry to replace all the geometries that have the same texture, TextureAtlas creates a single geometry to replace all the geometries.
However, am I right in thinking that you can also add new geometries to the game and then use the TextureAtlas to give them their texture? Something like this:
// Create a TextureAtlas
Spatial sinbad0 = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
TextureAtlas atlas = TextureAtlas.createAtlas(sinbad0, 2048);
// Use TextureAtlas on a new model
Spatial sinbad1 = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
List<Geometry> geometries1 = new ArrayList<>();
GeometryBatchFactory.gatherGeoms(sinbad1, geometries1);
for (Geometry geom : geometries1) {
atlas.applyCoords(geom);
}
Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
material.setTexture("DiffuseMap", atlas.getAtlasTexture("DiffuseMap"));
sinbad1.setMaterial(material);
rootNode.attachChild(sinbad1);
Or, is this not what TextureAtlas is supposed to be used for? I know in this example TextureAtlas would only have one texture in it, which would be a bit pointless, but, I’m just doing that to keep the code simple.
The above snippit works (adds a Sinbad that looks fine), until I try to add a second Sinbad:
Spatial sinbad2 = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
List<Geometry> geometries2 = new ArrayList<>();
GeometryBatchFactory.gatherGeoms(sinbad2, geometries2);
for (Geometry geom : geometries2) {
atlas.applyCoords(geom);
}
sinbad2.setMaterial(material);
rootNode.attachChild(sinbad2);
Then this happens (the texture is screwed up):
Why is that happening?