簡體   English   中英

從texturepacker的紋理在LibGDX

[英]Texture from texturepacker in 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)並且特定區域是其中的一小部分(例如,在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支持使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM