public render(){
...
while (y < Height){
while(x < Width){
batch.draw(bg,x,y,bg.getWidth()/ratio,bg.getHeight()/ratio);
x += bg.getWidth()/ratio;
}
y += bg.getHeight()/ratio;
x = 0;
}
...
}
我有一个图案纹理(bg)会重复制作一个重复的背景,所以我在渲染功能(上面的代码)中重复它。起初效果非常好,但是当比例变大时(精灵变小以适应背景),它变得非常迟钝。你能告诉我一个正确的方法来重复上面的代码而不影响性能。
答案 0 :(得分:1)
我猜测它会变得迟钝,因为你正在向屏幕绘制数百个四边形,如果有太多,它将成为CPU瓶颈并降低帧速率。但另一个答案可能就是原因:如果你在不使用mip映射的情况下显着缩小纹理,那么它将耗尽GPU时间。
由于这只是一个重复纹理,您只需要为整个屏幕绘制一个四边形。首先,当您加载纹理时,请确保它重复:
bg.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
//call this only one time, after you instantiate the Texture
然后你可以在屏幕上绘制一个四边形,并将顶点的UV设置得足够大,以达到你想要它平铺的次数。
// bottom left corner...wherever you want to put it.
float bottomY = 0;
float leftX = 0;
//width and height of background. This is just an example. By the way, Java
//convention is for all variable names to start with a lower-case letter,
//to avoid confusion.
float width = screenWidth;
float height= screenHeight;
//I'm using RATIO like you are, which I think is the reciprocal of how much
//you want to scale the texture up. By the way, it is convention to make
//constant variable (static final primitives in Java) names be in all caps.
float uRight = width * RATIO / bg.getWidth();
float vTop= height * RATIO / bg.getHeight();
batch.draw(bg, leftX, bottomY, width, height, 0, 0, uRight, vTop);
答案 1 :(得分:1)
有一些事情可能导致滞后。最大的一个原因是你的背景图像可能是一个很大的分辨率,你只需要在较小的宽度和高度上绘制多次。我想说看一下mipmap并看看这个教程http://www.badlogicgames.com/wordpress/?p=1403