TextureAtlas on Mesh

Hello Everyone,

I’ve been working with Jme3 since just after release but haven’t posted until now because up until this point I’ve usually been able to find the answer to my questions either in the documentation or on the forums. I’ve been having an issue with the new TextureAtlas class which has regrettably led to me posting (even though I didn’t want to post until my game was complete, just to show it could be done).

I am using the new TextureAtlas class provided by Jme3. I have a single Mesh in which I need to apply multiple textures for. The Mesh is divided up into square ‘tiles’. Each tile has 4 vertices, 4 texture coordinates, 6 indices, and 12 normals. I was easily able to accomplish what I wanted by making each individual tile into a Geometry, Applying the texture I wanted to each tile, and then using the GeometryBatchFactory to merge them all into a single mesh. This approach worked great but it seems that I’m losing a significant amount of performance and possibly memory, due to the GeometryBatchFactory.optomize() calls and all the Geometry objects floating around (1 for each tile). Instead I would like to apply the Textures (or rather, Texture Coordinates) directly to each tile within the mesh manually and skip the GeometryBatchFactory.optomize() part.

Using the TextureAtlas API I can apply the coordinates to a geometry using: applyCoords(Geometry geom, int offset, Mesh outMesh) which is fine but I want to be able to set the correct texture coordinates without having to make each individual tile into its own Geometry object first (Because I want the end result to be a single Geometry, without having to use GeometryBatchFactory.optomize()).

TL;DR:
How can I use the TextureAtlas to set the correct texture on a mesh without making the mesh into a Geometry first? I would ideally like to set up the texture coordinates on the mesh before making it into a single Geometry object. I believe that GeometryBatchFactory actually uses a TextureAtlas internally to combine all the Geometries into one with separate textures (per material). I want to be able to do this without making 500 Geometry objects only to batch them all into a single Geometry in the end.

*Also note that each tile has its own Texture Coordinates set at 0.0 to 1.0. So the repeating / wrapping / seams-between-textures problem shouldn’t apply.

**It’s okay to assume I’m only using one material so separating materials is not an issue.

Thanks in advance for any help,
Kilabyte

inb4 don’t worry about optimization until the very end of production.

UPDATE:
After 5 days I’ve finally implemented a solution. I stored the vertices for each tile in an array and used BufferUtils to create a single mesh without the need to create a Geometry for each one. I was able to combine all the tiles manually instead of using GeometryBatchFactory simply by storing all the vertices in a single array and keeping track of indices for each tile.

I did have to create my own custom texture atlas. I prevented bleeding by supplying texture coordinates of the “center texel” instead of going by pixel (Take the size of a pixel (1 / TextureSize) and multiply it by 0.5 to get the “center texel”. This allows pixel perfect texture mapping as long as the texture is a power of two.) Maybe somebody else will find use for this information.