【问题标题】:What is a right way to render repeated-background in libgdx?在 libgdx 中呈现重复背景的正确方法是什么?
【发布时间】:2014-07-25 14:11:30
【问题描述】:
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),它会重复制作一个重复的背景,所以我让它在渲染函数中重复(上面的代码)。起初它工作得很好,但是当比例变大(所以精灵变小以适应背景)时,它变得非常滞后。你能告诉我一种在不影响性能的情况下制作重复背景的正确方法吗?

【问题讨论】:

    标签: java opengl graphics libgdx


    【解决方案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);
    

    【讨论】:

    • 你能解释一下什么是四边形吗?
    • 您绘制的每个精灵都有两个三角形,形成一个四边形(四边形)。基本上,每个精灵都有四个独特的顶点,当您调用batch.end() 时必须将它们传输到 GPU,这会占用 CPU 周期。您可以在典型的智能手机上每帧绘制数百个四边形并保持 60fps。但如果你达到数千个,它将成为大多数移动设备上的 CPU 瓶颈。
    【解决方案2】:

    有几件事可能会导致延迟。最大的一个事实是,您的背景图像可能是一个较大的分辨率,您在较小的宽度和高度上绘制了很多次。我想说看看 mipmap 并看看这个教程 http://www.badlogicgames.com/wordpress/?p=1403

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 2014-03-22
      • 2021-01-05
      • 1970-01-01
      • 2013-08-10
      • 2014-01-14
      • 2019-03-24
      • 2019-04-25
      • 2011-04-16
      相关资源
      最近更新 更多