Java 来自LibGDX中texturepacker的纹理

Java 来自LibGDX中texturepacker的纹理,java,android,textures,libgdx,texturepacker,Java,Android,Textures,Libgdx,Texturepacker,尝试在(很棒的)LibGDX框架中使用纹理包装器,我需要帮助 我想绑定一个纹理(根据),该纹理是从使用TexturePacker打包的TextureAtlas中提取的。纹理将绑定到矩形网格 我希望纹理(纹理实例)基本上是从压缩文件中提取的 使用createsprite或findregion方法并以某种方式跳过filehandle步骤是否可行 此外: 在将上述方法与AssetManager相结合时,有什么特别的地方需要记住吗 谢谢你帮我整理 创建纹理区域 首先,通过指向描述地图集的文本文件(创建地

尝试在(很棒的)LibGDX框架中使用纹理包装器,我需要帮助

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

我希望纹理(纹理实例)基本上是从压缩文件中提取的

使用createsprite或findregion方法并以某种方式跳过filehandle步骤是否可行

此外: 在将上述方法与AssetManager相结合时,有什么特别的地方需要记住吗

谢谢你帮我整理

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

然后可以在该图集中查找
纹理区域
(即图集的特定子纹理)。该区域应具有使用的原始文件的基本名称(如果遵循某些特殊命名约定来创建纹理元素阵列,则会有更多详细信息和选项,但暂时不使用):

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

Mesh myMesh = new Mesh(...,
                       new VertexAttribute(Usage.TextureCoordinates, 2, "y"));
新的VertexAttribute(Usage.TextureCoordinates,2,…)
告诉libGDX该网格每个顶点将有两个纹理坐标(传统上,这两个纹理坐标称为
u
v
)。每个顶点可以有一组不同的属性,但我将假设唯一的其他属性是x、y、z空间坐标的3值
用法

现在,在定义网格的浮点数组(传递给
设置顶点的数组
)中,需要为每个顶点设置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);
请注意,这样做的效率远远低于应有的效率。纹理图集的部分好处是,它应该包含许多可以一起渲染的区域,因此您只需要绑定一个纹理,然后从该绑定纹理渲染许多不同的纹理网格


SpriteBatch
支持,并且
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;
}
公共网格重建MeshSuvToTextureRegion(网格对象网格,纹理区域UVMapPos)
{
int numFloats=ObjectMesh.getNumVertices()*ObjectMesh.getVertexSize()/4;
float[]顶点=新的float[numFloats];
获取顶点(顶点);
int numIndices=ObjectMesh.getNumIndices();
short SourceIndex[]=新的short[Numindies];
getindex(sourceindex);
最终int floatsServerTex=5;
int TimesToLoop=((顶点.length)/floatsServerTex);
漂浮在水面上;
浮动前v;
float FullMapHeight=UVMapPos.getTexture().getHeight();
float FullMapWidth=UVMapPos.getTexture().getWidth();
float NewMapWidth=UVMapPos.getRegionWidth();
float NewMapHeight=UVMapPos.getRegionHeight();
浮动充分;
浮动全图百分比;
for(int i=0;i
感谢您的帮助。谢谢@用户1450458很高兴您发现这个答案很有用。如果这令人满意地回答了您的问题,请“接受”答案(单击左侧复选标记的轮廓,使其呈绿色)。请参阅的回答部分:我不知道接受回答标签,但谢谢。后续操作:我找不到Mesh类的SettXureCoordinates方法,也找不到处理TextureRegion或AtlasRegion的任何其他方法。@user1450458哦,我把
setTextureCoordinates
的事情搞糟了。。这是我自己的方法。我会更新答案,以包含正确的细节。我终于有时间再看一遍了,但我不能让一件事起作用:地区。getImage()方法不存在。另外,如果我使用getTexture()methcod,那么作为回报,我将获得整个打包的TexturePacked页面。有什么解决办法吗?
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);
region.getTexture().bind();
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
myMesh.render();
Gdx.graphics.getGL10().glDisable(GL10.GL_TEXTURE_2D);
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;
}