我正在重构我在 libgdx 中开发的小型 RPG。
我有一个关于如何正确使用纹理图集的一般性问题。我有大约 100 名 Actor 和大约 10 种 Sprite 。目前我不使用图集,但我想提高渲染性能,因为如果我的游戏循环,这是唯一需要很长时间的东西。(大约 100 个绑定(bind)和 15 个渲染调用..)
所以它看了一下TextureAtlas
类link to git并且想知道如何从中获得更多性能。如果我得到一个具有正确纹理的 Sprite ,我确实得到了一个副本,所以我仍然会切换 OpenGL 绑定(bind),对吗?因此,如果我有大约 100 个 Actor 都有自己的 sprite,那么这不是一个更好的解决方案,或者我错了,我只是没有做对?
Atlas 本身确实建议不要多次调用 getsprite
但我需要这样做,以便每个 actor 都有自己的 sprite 和自己的状态。
也许你可以给我一个小例子,我如何在不切换 OpenGL 绑定(bind)的情况下使用一个图集中的多个 Sprite 。这应该是使用 map 集的原因,但我现在不明白。
最佳答案
If I get a sprite of it with the right texture I do get a copy so I still would switch the OpenGL binding right?
不,你得到了 TextureRegion它的。 (Sprite 是一种更专业的 TextureRegion)。
因此,当您绘制所有 Sprite 时,如果它们来自同一个 TextureAtlas,则它们只需要绘制一次。
how do I achieve that To get multiple sprites with same texture region of the atlas without New binding when rendering?
只需使用 TextureAtlas#createSprite :
sprite = textureatlas.createSprite("example");
当你绘制它们时,例如像这样:
batch.begin();
sprite.draw(batch)
sprite2.draw(batch);
sprite3.draw(batch);
batch.end();
如果 3 个 Sprite 来自同一个 TextureAtlas,它们只会刷新一次批处理。
编辑
TextureRegion 只是引用 Texture(Atlas) 的坐标和大小(矩形)。别担心,纹理不会被复制。它只有一个,无论您从中创建了多少 Sprite :)
关于java - TextureAtlas 的优势以及如何正确使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21158321/