【问题标题】:libGDX SpriteCache: transparency on images not displayed properlylibGDX SpriteCache:图像的透明度未正确显示
【发布时间】:2013-10-22 17:27:22
【问题描述】:

我正在使用 libGDX 开发塔防游戏。我刚刚开始,我能够显示路径、“环境”以及沿路径行走的敌人。

我使用 SpriteBatch-Object 显示环境,如下所示:

public LevelController(Level level) {
    spriteBach = new SpriteBach();
    atlas =  new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
}

public void setSize() {
    spriteBatch.setProjectionMatrix(this.cam.combined);
}

public void render() {
    spriteBatch.begin();
    drawTowerBases();
    spriteBatch.end();
}

private void drawTowerBases() {
    // for each tower base (=environment square)
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");

    if(towerBaseTexture != null) {
        spriteBatch.draw(towerBaseTexture, x, y, 1f, 1f);
    }
}

这工作正常,纹理显示良好:Tower Defense using spriteBatch

现在,我想知道是否可以缓存背景。因为它保持不变,所以不需要每次都计算它。我通过谷歌搜索找到了 SpriteCache。于是我把代码改成如下:

public LevelController(Level Level) {
    spriteCache= new SpriteCache();

    atlas =  new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));

    spriteCache.beginCache();
    this.drawTowerBases();
    this.spriteCacheEnvironmentId = spriteCache.endCache();
}

public void setSize() {
    this.cam.update();
    spriteCache.setProjectionMatrix(this.cam.combined);
}

public void render() {
    spriteCache.begin();
    spriteCache.draw(this.spriteCacheEnvironmentId);
    spriteCache.end();
}

private void drawTowerBases() {
    // for each tower base (=environment square)
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase");

    if(towerBaseTexture != null) {
        spriteCache.add(towerBaseTexture, x, y, 1f, 1f);
    }
} 

现在游戏是这样的:Tower Defense using spriteCache

对我来说,似乎透明度没有正确呈现。如果我拍摄没有透明度的图像,一切正常。有谁知道为什么会发生这种情况以及我该如何解决这个问题?

提前谢谢你。

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    取自SpriteCache documentation

    请注意,SpriteCache 不管理混合。您将需要启用混合 (Gdx.gl.glEnable(GL10.GL_BLEND);) 并在调用 draw(int) 之前或之间根据需要设置混合函数。

    我想没什么好说的了。

    【讨论】:

    • 谢谢。我让它在 spriteCache.begin() 之前调用这两行代码: Gdx.gl.glEnable(GL10.GL_BLEND); Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    • 我对 SpriteCache 有点困惑。 SpriteCache 文档中没有明确说明,但 SpriteCache 似乎是存储在 GPU 内存中的“您刚刚渲染的内容的快照”。您可以获取此快照及其 ID 并将其放在屏幕上,而无需再次发送所有内容。那是对的吗?这也意味着您可以在屏幕上的任何位置渲染快照 - 移动它,对吗?该文档对“不变的几何形状”有点令人费解。
    • 我不确定你的问题到底是什么。是的,您应该能够通过操纵其投影矩阵来“在任何地方”渲染缓存。
    • 在使用 SpriteCache 进行渲染时,您会创建渲染内容的快照并将其存储在 GPU 中的 ID 下。您可以稍后使用此 ID 调用它。通过操纵 SpriteCache 投影矩阵,你可以在任何你想要的地方显示这个快照。我的推理都对吗? :)
    • 我想是的,你为什么不试试呢?
    猜你喜欢
    • 2013-04-02
    • 2016-12-02
    • 2020-10-01
    • 2023-03-05
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多