【问题标题】:Texture from Texture Atlas Find Region or Create Sprite来自 Texture Atlas 的纹理 查找区域或创建 Sprite
【发布时间】:2014-06-02 02:41:22
【问题描述】:

我正在尝试从 Texture Atlas 创建的 Sprite 或 Atlas Region 获取纹理。

    atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    region=atlas.findRegion("green");

 Sprite s = atlas.createSprite("red");
 texture = s.getTexture();      

我在 findRegion 和 createSprite 方法中给出了图像的名称,它总是选择另一个图像对象。我想获取图像的纹理,以便可以渲染该纹理的一部分。

这是打包机图片: http://i61.tinypic.com/neupo2.png

即使我尝试获得绿色区域或红色区域,它总是返回蓝色。任何帮助将不胜感激。

【问题讨论】:

  • 我尝试了所有可能的方法。剩下的唯一方法是使用 Sprite Sheet 自己编写代码并读取存储的图像坐标。

标签: libgdx


【解决方案1】:

我的用例略有不同,但我遇到了同样的问题。

我有一个对象,它扩展了Actor,我想使用地图集中的一个区域进行绘制。

public class MyObject extends Actor {
    private Texture texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture").getTexture();
        Gdx.app.log(TAG, "Texture width: " + texture.getWidth());
        Gdx.app.log(TAG, "Texture height: " + texture.getHeight());

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture,
                worldPosition.x * Constants.STANDARD_TILE_WIDTH, worldPosition.y * Constants.STANDARD_TILE_WIDTH,
                texture.getWidth(), texture.getHeight());
    }
}

我收到的不是我期望的区域,而是整个图集,所以我记录的纹理宽度和高度为 1024 x 128。

不幸的是,仍然不知道为什么getTexture() 回报太多,但切换到batchDraw(TextureRegion, ...) 至少让我在一个更好的地方。

public class MyObject extends Actor {
    private TextureRegion texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture");

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture, getX(), getY());
    }
}

根据我看到的精灵,提问者看到了一个蓝色方块,原因与我相同;整个图像由getTexture() 加载,因为它从左下角开始,所以您总是看到一个蓝色方块。

使用Sprite(TextureRegion) 构造函数可能也解决了他们的问题。

【讨论】:

    【解决方案2】:

    当您执行“textureAtlas.findRegion("objectTexture").getTexture();”时(来自 James Skemp 的回答)您从地图集中获得了整个纹理,而不仅仅是来自“objectTexture”区域。

    不用费心创建Texture来创建Sprite,你可以使用TextureRegion来创建Sprite。像这样的:

    atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    regionGreen=atlas.findRegion("green");
    Sprite s = new Sprite(regionGreen);
    

    【讨论】:

    • 如何在没有 Sprite 类的情况下做到这一点?我只需要区域纹理
    • @lacas 在我的示例中,您可以使用“regionGreen”作为 TextureRegion。 "atlas.findRegion()" 返回一个 TextureAtlas,它是 TextureRegion 的子类。
    【解决方案3】:

    在我看来,您的问题是 Test.pack 文件,而不是纹理。

    它适用于我用 GDX Texture Packer 打包的这个:

    Test.png format: RGBA8888 filter: Nearest,Nearest repeat: none blue rotate: false xy: 1, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 green rotate: false xy: 54, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 red rotate: false xy: 107, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 yellow rotate: false xy: 160, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1

    【讨论】:

      【解决方案4】:

      使用索引也可以获取它。从包文件或.atlas文件中获取索引。

      atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
          region=atlas.findRegion("green",index);
      

       Sprite s = atlas.createSprite("red",index);
       texture = s.getTexture();   
      

      【讨论】:

      • 所有图像的包文件 (.txt) 或 .pack 文件中的索引为 -1。我是否需要在文件中手动更改索引然后访问它?让我也试试那个方法。
      • 我试过了,但没有成功。还有其他解决方案吗?
      • @Jatin 您只需在 .pack 文件中使用索引提及。(无论是 -1 或任何其他数字)
      • @Jatin 不需要更改 .pack 文件中的索引号。
      • @Jatin 如果解决方案有效,请接受它作为正确答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多