libgdx中的TextureAtlas和renderCalls

时间:2017-12-05 22:57:56

标签: android libgdx textures texture-atlas

我正在创建两个纹理并将它们放在相同的TextureAtlas中,如代码所示:

public void create () {
    pix = new Pixmap(100, 100, Pixmap.Format.RGBA8888);
    textureAtlas = new TextureAtlas();

    pix.setColor(Color.WHITE);
    pix.fill();
    textureAtlas.addRegion("white", new TextureRegion(new Texture(pix)));
    pix.setColor(Color.RED);
    pix.fill();
    textureAtlas.addRegion("red", new TextureRegion(new Texture(pix)));

    tr_list = textureAtlas.getRegions();

    pix.dispose()
}

然后渲染时:

public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    for (int i = 0; i < 200; ++i) {
        batch.draw(tr_list.get(i % tr_list.size), (int)(Math.random()* 500), (int)(Math.random()* 500));
    }
    font.draw(batch, "FPS: " + Integer.toString(Gdx.graphics.getFramesPerSecond()), 0, Gdx.graphics.getHeight() - 20);
    font.draw(batch, "Render Calls: " + Integer.toString(batch.renderCalls), 0, Gdx.graphics.getHeight() - 60);
    batch.end();
}

我期待batch.renderCalls等于1,因为纹理在同一个TextureAtlas中,但它等于200.我做错了什么?

2 个答案:

答案 0 :(得分:2)

为了在单个渲染调用中绘制TextureRegions,每个调用都必须属于同一个Texture,而不是TextureAtlas

TextureAtlas可以包含不同TextureRegions的{​​{1}},这在您的情况下实际发生。即使您使用相同的Textures,也可以从中创建两个不同的Pixmap

答案 1 :(得分:0)

你正在循环绘制它们200次。

for (int i = 0; i < 200; ++i) {
    // this will be called 200 times
    batch.draw(tr_list.get(i % tr_list.size), (int)(Math.random()* 500), (int)(Math.random()* 500)); 
}

此外,TextureAtlas类不会创建添加到其中的纹理图片。这只是TextureRegions @ Arctic23注意到的容器。

因此,主要问题是在一次渲染调用中绘制2个纹理。我不建议你这样做。这是可能的,但很难与他们合作。

每次绘制纹理时调用batch.draw(..)都不是一个很大的性能问题。所以我不知道你为什么分开这个。