Trouble understanding TextureAtlas usage

I’m no stranger to texture atlases in general, but I think I’m struggling with how to use the TextureAtlas class for my specific application. Suppose I add two textures to textureAtlas like so:

atlas.addTexture(
                assetManager.loadTexture("Textures/Tubes/hexagonal.png"),
                "ColorMap");
        
atlas.addTexture(
                assetManager.loadTexture("Textures/Tubes/squares.png"),
                "ColorMap");

(Does this work? Am I adding two textures to the “ColorMap” master map? Or am I overwriting the first? Do I need to put it in a secondary map?)

And then I want to use the atlas to set the texture of a material of some geometry:

Material realm1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            realm1.setTexture("ColorMap", atlas.getAtlasTexture("ColorMap"));  //But which texture is it?
            realm1.getTextureParam("ColorMap").getTextureValue()
                    .setMinFilter(Texture.MinFilter.NearestNoMipMaps);
            Geometry trackGeom = (Geometry) ((Node)((Node)newTrackPiece).getChild("temp")).getChild("tube_straight");          
            atlas.applyCoords(trackGeom, 1024, trackGeom.getMesh());  //Don't understand the offset here. 
            trackGeom.setMaterial(realm1);

How do I access the second texture for this geom? Is it through the offset? And is this optimizing anything? Can I create a second trackGeom and utilize the textureAtlas for optimization? Right now all I can get is the first “hexagonal” texture to show up. I’ve looked at a couple of other threads, but I’m still not sure.

bump?

Did you read the javadoc, especially the main description?

http://javadoc.jmonkeyengine.org/jme3tools/optimize/TextureAtlas.html

As to your questions:

  1. Yes, you’re adding two textures
  2. The texture is defined through its (Asset)Name, the texture coordinates are set according to the material thats already on the geometry (and contains the name).
  3. Just apply it like the first one, yeah.

Obviously the texture coordinates on your model have to fit to the texture in the first place, this class only shifts the existing coordinates to the new position inside the atlas. Its mainly made to combine multiple textures that already exist on models to one atlas - for pre-processing e.g. scenes you put together in the SDK. If you make your models from scratch or procedurally then just make your atlas in your image editor or whatever and set the texture coordinates correctly…

Yeah I read all that, I just wasn’t sure if it could be used properly in the case that I just add a bunch of textures to the ColorMap, and then everytime I set the material of a geom, I could just transform the coordinates to be the next texture in the ColorMap set. How do I tell change the geom coordinates to start at a different spot in the colorMap atlas? I thought I might need to use transformTextureCoords on the atlasTile of interest, but I might not be doing that right (something about the offset).

Something like this:

 atlas.getAtlasTile(assetManager.loadTexture("Textures/Tubes/squares.png")).transformTextureCoords(trackGeom.getMesh().getFloatBuffer(Type.TexCoord), ???, trackGeom.getMesh().getFloatBuffer(Type.TexCoord));

Does that make sense? I’m pretty sure I’m misunderstanding a concept regarding texture coordinates of the geometry in relation to the atlas.

The geometry has a mesh and a material. The material references a texture. The texture coordinates in the mesh are correctly set for that texture. Now you add that texture to the atlas. Its at some offset in the atlas. You supply the geometry to the TextureAtlas.applyCoords() method (just the one with one Geometry parameter). The method reads the texture name from the geometry, finds the corresponding texture in its data and offsets the texture coords of the mesh.

Its important for the textures to have an AssetPath (which it always does if you load it from the assetManager), otherwise the textures can’t be found (e.g. if you create a texture procedurally). But if you create it procedurally then as said using the TextureAtlas class doesn’t make sense.