Trying get my head around the texture wrapper in (the awesome) LibGDX framework and I need help.
I would like to bind a texture (according to Mesh, Color & Texture) that is extracted from a TextureAtlas packed with the TexturePacker. The texture is binding to a rectangular mesh.
I want the texture (instance of Texture) to basically be extracted from a packed file.
Is it doable to use a the createsprite or findregion methods and somehow skip the filehandle step?
Additionally: Anything special one should keep in mind when combining the above method with the AssetManager?
Thanks for sorting me out!
Got it to work using the above explanation.
I can't believe so few are people are running asking this question as it seems like something that others would want to do.
From the accepted solution I created a function that also does the math for the new UV position.
Tested, and it works for me but please review as i'm not a java developer.
Create the TextureRegion
First, you create a
TextureAtlas
object by pointing at the text file that describes your atlas (the tool that creates the atlas will create two files: an image and a text file describing its contents):Then you can look up a
TextureRegion
in that atlas (that is, a specific sub-texture of the atlas). The region should have the basename of the original file that was used (there are more details and options if you follow some of the special naming conventions to create arrays of texture elements, but leave that off for now):Configure a textured Mesh
To draw this texture region on a mesh, you'll need to initialize the
Mesh
with support for texture coordinates:The
new VertexAttribute(Usage.TextureCoordinates, 2, ...)
tells libGDX that this mesh will have two texture coordinates per vertex (traditionally, the two texture coordinates are calledu
andv
). You can have a bunch of different attributes per vertex, but I'm going to assume the only other attribute is a 3-valuedUsage.Position
for the x,y,z spatial coordinates.Now, in the array of floats that defines your mesh (the array you pass to
setVertices
) you need to set x, y, and z spatial coordinates plus u, and v texture coordinates for each vertex:You can compute the right
u
andv
for a specificTextureRegion
using thegetU
,getV
,getU2
, andgetV2
methods. Note that texture coordinates have the origin (u1, v1) in the top-left, and the y-axis points "down" (screen and spatial coordinates in OpenGL usually have the origin in the bottom-left and the y-axis points "up"). Its a little complicated, but very flexible in that you can flip or stretch or distort the texture as it maps onto your mesh.Since the texture is large (say 512x512) and the specific region is a small subset of that (say 20x20 at 128x128), you'll actually end up giving your mesh texture coordinates that utilize just the 20x20 subset of the entire 512x512 image.
Render the textured Mesh
Finally, when you render you need to bind the image, and enable texturing before rendering:
Note that this is much less efficient than it should be. Part of the benefit of a texture atlas is that it should contain a lot of regions that can be rendered together, so you only need to bind one texture, and then render a lot of different textured meshes from that one bound texture.
SpriteBatch
supports sprites defined with aTextureRegion
and theAssetManager
supports loading and finding aTextureAtlas
as a first-class element.