我正在尝试使用(很棒的) LibGDX框架中的纹理包装器,我需要帮助。
我想绑定一个纹理(根据Mesh, Color & Texture),该纹理是从与TexturePacker打包的TextureAtlas中提取的。纹理绑定到矩形网格。
我希望纹理(纹理的实例)基本上是从一个压缩文件中提取出来的。
是否可以使用createsprite或findregion方法并以某种方式跳过文件句柄步骤?
另外:在将上述方法与AssetManager结合时,有什么需要注意的特殊之处吗?
谢谢你把我整理出来!
发布于 2012-06-13 00:42:40
创建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,该网格的每个顶点将有两个纹理坐标(传统上,这两个纹理坐标称为u和v)。每个顶点可以有一组不同的属性,但我将假设唯一的另一个属性是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);您可以使用getU、getV、getU2和getV2方法计算特定TextureRegion的正确u和v。请注意,纹理坐标的原点(u1,v1)位于左上角,y轴点为“下”( OpenGL中的屏幕和空间坐标的原点通常位于左下角,y轴点为“上”)。它有点复杂,但非常灵活,因为你可以翻转或拉伸或扭曲纹理,因为它映射到你的网格。
由于纹理很大(比方说512x512),而特定区域只是其中的一个小子集(比方说128x128下的20x20 ),实际上你最终得到的网格纹理坐标只利用了整个512x512图像的20x20子集。
渲染带纹理的网格
最后,渲染时需要绑定图像,并在渲染前启用纹理:
region.getTexture().bind();
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
myMesh.render();
Gdx.graphics.getGL10().glDisable(GL10.GL_TEXTURE_2D);请注意,这比它应有的效率要低得多。纹理图集的部分好处是它应该包含许多可以一起渲染的区域,因此您只需要绑定一个纹理,然后从该绑定的纹理渲染许多不同的纹理网格。
SpriteBatch支持sprites defined with a TextureRegion,AssetManager支持将TextureAtlas作为第一类元素加载和查找。
发布于 2013-06-30 20:46:35
使用上面的解释让它工作。
我不敢相信这么少的人在问这个问题,因为这似乎是其他人想要做的事情。
根据接受的解决方案,我创建了一个函数,该函数也为新的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;
}https://stackoverflow.com/questions/10991341
复制相似问题