【问题标题】:Texture from texturepacker in LibGDX来自 LibGDX 中的 texturepacker 的纹理
【发布时间】:2012-06-15 00:30:47
【问题描述】:

尝试了解(很棒的)LibGDX 框架中的纹理包装器,我需要帮助。

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

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

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

另外: 将上述方法与 AssetManager 结合使用时,有什么特别需要注意的吗?

谢谢你帮我整理!

【问题讨论】:

    标签: java android textures libgdx texturepacker


    【解决方案1】:

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

    我不敢相信很少有人跑来问这个问题,因为这似乎是其他人想做的事情。

    根据公认的解决方案,我创建了一个函数,该函数还计算新的 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;
    }
    

    【讨论】:

      【解决方案2】:

      创建纹理区域

      首先,您通过指向描述您的图集的文本文件创建一个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 支持sprites defined with a TextureRegionAssetManager 支持加载和查找TextureAtlas 作为一等元素。

      【讨论】:

      • 感谢您的帮助。谢谢!
      • @user1450458 很高兴您发现这个答案很有用。如果这令人满意地回答了您的问题,请“接受”答案(单击左侧复选标记的轮廓使其变为绿色)。见回复部分:stackoverflow.com/faq#howtoask
      • 我没有意识到accept-answer 标签,但谢谢。跟进:我找不到 Mesh 类的 setTexureCoordinates 方法或任何其他处理 TextureRegion 或 AtlasRegion 的方法。
      • @user1450458 糟糕,我搞砸了setTextureCoordinates 的东西……这是我自己的方法。我将更新答案以包含正确的详细信息。
      • 我终于有时间再看一遍,但我不能让一件事起作用:区域。 getImage() 方法不存在。另外,如果我使用 getTexture() 方法,那么我将获得整个打包的 texturepackeed 页面作为回报。有什么解决办法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2015-08-31
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多