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;
}
...
}
我有一个图案texture(bg),它将重复进行重复的背景处理,因此我在render函数(上面的代码)中将其重复进行。刚开始时效果很好,但是当比例变大(因此精灵变小以适合背景)时,它就会变得很滞后。您能告诉我一种正确的方法,使上面的代码重复出现背景而不影响性能。
我猜它会因为您要在屏幕上绘制数百个四边形而变得迟钝,并且如果有太多的四边形,它将成为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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句