从texturepacker在LibGDX纹理 [英] Texture from texturepacker in LibGDX

查看:190
本文介绍了从texturepacker在LibGDX纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让我的头周围的质感包装中(该真棒)LibGDX框架,我需要帮助。

Trying get my head around the texture wrapper in (the awesome) LibGDX framework and I need help.

我想绑定的纹理(根据网,颜色和放大器;纹理)是从挤满了TexturePacker一个TextureAtlas提取。纹理结合到矩形网

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.

它是可行使用的createsprite或findregion方法,并以某种方式跳过文件句柄一步?

Is it doable to use a the createsprite or findregion methods and somehow skip the filehandle step?

此外: 当结合上述方法与AssetManager什么特别的东西应该牢记?

Additionally: Anything special one should keep in mind when combining the above method with the AssetManager?

谢谢整理我出去!

推荐答案

首先,您指着描述你的图谱(即创建地图集将创建两个文件的工具,文本文件中创建一个 TextureAtlas 目标:图像和文本文件描述其内容):

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):

TextureAtlas myTextures = new TextureAtlas("images/packed.txt");

然后,你可以查找 TextureRegion 在阿特拉斯(即一个特定的子纹理地图集)。该地区应该有使用(还有更多的细节和选择,如果你遵循一些特殊的命名约定来创建纹理元素的数组,但是留给关现在)原始文件的基本名称:

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):

TextureRegion region = myTextures.findRegion(fname);

配置纹理网

要得出这样的纹理区域上的网格,你需要初始化网​​与纹理支持坐标:

Configure a textured Mesh

To draw this texture region on a mesh, you'll need to initialize the Mesh with support for texture coordinates:

Mesh myMesh = new Mesh(...,
                       new VertexAttribute(Usage.TextureCoordinates, 2, "y"));

新VertexAttribute(Usage.TextureCoordinates,2,...)告诉libGDX,这个网格将有两个纹理每个顶点(传统上,这两个纹理坐标坐标名为 U v )。你可以有一大堆的每个顶点不同的属性,但我要承担唯一的其他属性是一个三值 Usage.Position 为X,Y,Z空间坐标。

The new VertexAttribute(Usage.TextureCoordinates, 2, ...) tells libGDX that this mesh will have two texture coordinates per vertex (traditionally, the two texture coordinates are called u and v). You can have a bunch of different attributes per vertex, but I'm going to assume the only other attribute is a 3-valued Usage.Position for the x,y,z spatial coordinates.

现在,花车的数组定义你的网格(你传递给 setVertices 阵列),你需要设置X,Y和Z空间坐标加U,和v纹理坐标为每个顶点:

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:

final int floatsPerVertex = 5; // 3 spatial +  2 texture
float[] meshData = new float[numVerticies * floatsPerVertex];
for (int i = 0; i < numVerticies; i++) {
   meshData[(i * floatsPerVertex) + 0] = ... ; // x coordinate of i'th vertex
   meshData[(i * floatsPerVertex) + 1] = ... ; // y coordinate of i'th vertex
   meshData[(i * floatsPerVertex) + 2] = ... ; // z coordinate of i'th vertex
   meshData[(i * floatsPerVertex) + 3] = ... ; // u texture coordinate of i'th vertex
   meshData[(i * floatsPerVertex) + 4] = ... ; // v texture coordinate of i'th vertex
}
myMesh.setVertices(meshData);

您可以计算出正确的 U v 特定的 TextureRegion 使用格凸 getv时 getU2 getV2 的方法。注意,纹理坐标具有原点(U1,V1)的左上方,而y轴指向下(在OpenGL屏幕和空间坐标通常在左下和y轴点原点向上)。它有点复杂,但非常灵活,因为您可以翻转或伸展或扭曲的纹理,它映射到您的网格。

You can compute the right u and v for a specific TextureRegion using the getU, getV, getU2, and getV2 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.

由于质地大(比如512×512)和特定地区的一个小部分(比如20×20 128×128时),你实际上最终给出了利用整个512×512仅仅是20×20集的网状纹理坐标图像。

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.

最后,当你渲染你需要绑定的形象,并呈现前启用纹理:

Finally, when you render you need to bind the image, and enable texturing before rendering:

region.getTexture().bind();
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
myMesh.render();
Gdx.graphics.getGL10().glDisable(GL10.GL_TEXTURE_2D);

请注意,这是效率低得多比它应该的。的纹理地图的好处之一是,它应该包含很多可以一起呈现的区域,所以你只需要绑定一个纹理,然后渲染了很多不同的纹理从一个绑定的纹理网格。

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 支持<一href="http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#Sprite%28com.badlogic.gdx.graphics.g2d.TextureRegion%29"相对=与定义nofollow的>精灵一个 TextureRegion AssetManager 支持加载和寻找 TextureAtlas 作为一流的元素。

SpriteBatch supports sprites defined with a TextureRegion and the AssetManager supports loading and finding a TextureAtlas as a first-class element.

这篇关于从texturepacker在LibGDX纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆