从texturepacker的纹理在LibGDX

尝试在(令人敬畏的)LibGDX框架中了解纹理包装器,我需要帮助。

我想绑定一个纹理(根据网格,颜色和纹理 ),该纹理是从TexturePacker打包的TextureAtlas中提取的。 纹理绑定到矩形网格。

我希望纹理(纹理的实例)基本上从打包文件中提取。

是否可以使用createsprite或findregion方法并以某种方式跳过文件句柄步骤?

另外:将上述方法与AssetManager结合使用时,应该记住哪些特殊内容?

谢谢你把我排除在外!

创建TextureRegion

首先,通过指向描述图集的文本文件来创建TextureAtlas对象(创建图集的工具将创建两个文件:图像和描述其内容的文本文件):

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

然后,您可以在该图集中查找TextureRegion (即图集的特定子纹理)。 该区域应该具有所使用的原始文件的基本名称(如果您遵循一些特殊的命名约定来创建纹理元素数组,则会有更多详细信息和选项,但现在请将其关闭):

 TextureRegion region = myTextures.findRegion(fname); 

配置纹理网格

要在网格上绘制此纹理区域,您需要初始化Mesh并支持纹理坐标:

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

new VertexAttribute(Usage.TextureCoordinates, 2, ...)告诉libGDX这个网格每个顶点有两个纹理坐标(传统上,两个纹理坐标称为uv )。 每个顶点可以有一堆不同的属性,但我将假设唯一的其他属性是x,y,z空间坐标的3值Usage.Position

现在,在定义网格的浮点数组(传递给setVertices的数组)中,您需要为每个顶点设置x,y和z空间坐标加上u和v纹理坐标:

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

您可以使用getUgetVgetU2getV2方法为特定的TextureRegion计算正确的uv 。 请注意,纹理坐标在左上角有原点(u1,v1),y轴在“向下”点(OpenGL中的屏幕和空间坐标通常在左下角和y轴点有原点)上“)。 它有点复杂,但非常灵活,因为它可以在纹理映射到网格上时翻转或拉伸或扭曲纹理。

由于纹理很大(比如512x512)而且特定区域是其中的一小部分(比如说20x20在128x128处),实际上你最终会给出网格纹理坐标,它只利用整个512x512图像的20x20子集。

渲染纹理网格

最后,渲染时需要绑定图像,并在渲染之前启用纹理:

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

请注意,这比它应该效率低得多。 纹理图集的部分好处是它应该包含许多可以一起渲染的区域,因此您只需要绑定一个纹理,然后从该绑定纹理渲染许多不同的纹理网格。

SpriteBatch支持使用TextureRegion定义的精灵,并且AssetManager支持加载和查找TextureAtlas作为第一类元素。

使用上面的解释使它工作。

我不敢相信人们正在运行这么多问题,因为这似乎是其他人想要做的事情。

从接受的解决方案中,我创建了一个函数,该函数也可以为新的UV位置进行数学运算。

测试,它适用于我,但请审查,因为我不是一个Java开发人员。

 public Mesh RebuildMeshUVtoTextureRegion(Mesh ObjectMesh, TextureRegion UVMapPos) { int numFloats = ObjectMesh.getNumVertices() * ObjectMesh.getVertexSize() / 4; float[] vertices = new float[numFloats]; ObjectMesh.getVertices(vertices); int numIndices = ObjectMesh.getNumIndices(); short SourceIndices[] = new short[numIndices]; ObjectMesh.getIndices(SourceIndices); final int floatsPerVertex = 5; int TimesToLoop = ((vertices.length) /floatsPerVertex); float previousU; float previousV; float FullMapHeight = UVMapPos.getTexture().getHeight(); float FullMapWidth = UVMapPos.getTexture().getWidth(); float NewMapWidth = UVMapPos.getRegionWidth(); float NewMapHeight = UVMapPos.getRegionHeight(); float FullMapUPercent; float FullMapVPercent; for (int i = 0; i < TimesToLoop; i++) { previousU = (vertices[(i * floatsPerVertex) + 3]); previousV = (vertices[(i * floatsPerVertex) + 4]); FullMapUPercent = previousU / FullMapWidth; FullMapVPercent = previousV / FullMapHeight; vertices[(i * floatsPerVertex) + 3] = (NewMapWidth * FullMapUPercent) + UVMapPos.getU(); //New U vertices[(i * floatsPerVertex) + 4] = (NewMapHeight * FullMapVPercent) + UVMapPos.getV();//New V } ObjectMesh.setVertices(vertices); ObjectMesh.setIndices(SourceIndices); return ObjectMesh; }