3
votes

I'm trying to move from individual textures to using a texture atlas for my boxes (they all show individual letters for a Scrabble-like game). I'm stuck with this, all the examples I find are for 2D sprites.

How do I create and assign the material to a 3D surface with the right mapping coordinates from an AtlasRegion?

public void createModelInstance(TextureAtlas letterAtlas) {
   AtlasRegion region = letterAtlas.findRegion(this.textureID);
   Material mat = new Material(???);
   modelInstance = new ModelInstance(modelTile); 
   modelInstance.nodes.get(0).parts.get(0).material.set(mat);
}

When used individual files for each texture this worked:

Material mat = new Material(TextureAttribute.createDiffuse(
    assetManager.get("textures/" + textureFile, Texture.class));

The surface is created:

MeshPartBuilder tileBuilder;
tileBuilder = modelBuilder.part("top", GL10.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, new Material(ColorAttribute.createDiffuse(Color.WHITE)));
tileBuilder.rect(-0.45f, 0.1f, 0.45f,   0.45f, 0.1f, 0.45f,    0.45f, 0.1f, -0.45f,  -0.45f, 0.1f, -0.45f,  0f, 1f, 0f);
...
1
If you need texture you may get texture from TextueAtlas.getTexture also you can pass texture to custom shader.daniel

1 Answers

1
votes

I think it is currently not possible to use a TextureAtlas in this way. The only possible way to create a TextureAttribute is by supplying a Texture. A texture is a standalone PixMap.

TextureRegion in contrast describes an area of a bigger Texture. Currently there seems to be no way to create a Texture from a TextureRegion in the API. TextureRegion.getTexture() will only retrieve the full texture of the atlas it came from.

And it actually makes sense that the API doesn't offer such a conversion, because it destroys the purpose of the atlas. By making all regions individual textures, you wouldn't gain any performance anymore.

TextureAttribute currently seems to lack the support for texture atlasses completely, otherwise it would have to offer methods with TextureRegions instead of Textures.