因此,我使用我创建的等距渲染器类来渲染一个名为 WorldGeneration 的图 block map (只是带有 MapTiles 的二维数组)。我使用的相机每米使用 64 像素,因此其高度为:(游戏窗口高度)/64。等距渲染器中使用的 SpriteBatch 将其投影矩阵设置为此相机。 (batch.setProjectionMatrix(cam.combined)
问题是,在将纹理渲染到此 SpriteBatch 上时,我的性能很糟糕。我不确定这是否是由投影矩阵本身引起的(需要将我的 64x64 纹理在批量尺寸中缩放到 1x1),还是其他原因。
我尝试运行程序而不在渲染器中执行batch.draw调用(参见下面的代码),一切正常。有人能找到问题或给我一些提示吗?
相关代码片段:
等距渲染器的渲染方法:
public void render(WorldGeneration gen) {
//TODO compensate for elevation
int x1 = (int) (cameraIso.x - 23); // TODO compensate for zoom
int x2 = (int) (cameraIso.x + 23);
int y1 = (int) (cameraIso.y - 23);
int y2 = (int) (cameraIso.y + 23);
if(x1 < 0){
x1 = 0;
}else if(x1 >= gen.getLengthX()){
x1 = gen.getLengthX() - 1;
}
if(y1 < 0){
y1 = 0;
}else if(y1 >= gen.getLengthY()){
y1 = gen.getLengthY() - 1;
}
if(x2 < 0){
x2 = 0;
}else if(x2 >= gen.getLengthX()){
x2 = gen.getLengthX() - 1;
}
if(y2 < 0){
y2 = 0;
}else if(y2 >= gen.getLengthY()){
y2 = gen.getLengthY() - 1;
}
batch.begin();
for (int x = x2; x >= x1; x--) {
for (int y = y2; y >= y1; y--) {
tiles = gen.getTiles(x, y);
if(tiles == null){
continue;
}else if(tiles.size <= 0){
continue;
}
for(MapTile tile: tiles){
if(tile.getOpacity() < 1){
batch.setColor(batchColor.r, batchColor.g, batchColor.b, tile.getOpacity());
}else{
batch.setColor(batchColor.r, batchColor.g, batchColor.b, 1);
}
//TODO only visible (not underneath several tiles)
pos.x = x;
pos.y = y;
//As you can see the texture is scaled to 1x1 because of batch:
batch.draw(textures[tile.getId()], translateCartToIso(pos).x,
translateCartToIso(pos).y + tile.getZ(), 1f, 1f);
}
}
}
batch.end();
}
正如您所看到的,我什至在渲染方法之外创建了 Vector2 pos 以提高性能,但我不确定这是否有必要。还值得注意的是,图 block 被锁定到 xy 网格,但它们的 z 值却没有,这就是需要数组的原因。
编辑:不知何故,如果相机缩小,性能会很棒,不知道为什么会这样。这肯定与相机尺寸有关。
最佳答案
找到了答案,它实际上与一切背后的图形有关。我注意到,当我运行压力测试时,我的计算机 (mac) 会因为图形问题而关闭。
它基本上可以归结为这一行: cfg.useHDPI = true;
在我让 LwjglApplicationConfiguration 使用高清像素的地方,我猜我的计算机在必须处理太多高清像素时无法处理它(这就是为什么当我缩小相机,减少像素时它可以工作)
我想我现在必须处理一些扭曲的像素,这是我稍后必须解决的问题!
关于java - 渲染图 block map 时 SpriteBatch 性能不佳 [LibGDX],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631878/