的OpenGL/Libgdx:方格网渲染优化

问题描述:

我的游戏背景渲染需要所有的渲染时间我一直在试图改善它的1/4,但我仍然不认为这是正常的,它需要这么长知道如何简单它是:的OpenGL/Libgdx:方格网渲染优化

的背景是一个正方形网格(如棋盘),其中每平方都有自己的颜色,从而改变每帧,正方形y位置变化,每帧过,但不x位置

这里是我的主要渲染功能

public void main.render() { 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    final float deltaS = Gdx.graphics.getDeltaTime(); 

    background.update(deltaS); 
    updateTheGame(deltaS); 

    background.render(); 

    spriteBatch.setProjectionMatrix(uiCamera.combined); 
    spriteBatch.begin(); 
    renderTheGame(spriteBatch); 
    spriteBatch.end(); 

} 

这里是背景的网声明

mesh = new Mesh(true, nbVertices, nbIndices, 
       new VertexAttribute(VertexAttributes.Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), 
       new VertexAttribute(VertexAttributes.Usage.ColorUnpacked, 3, GL20.GL_FLOAT, false, ShaderProgram.COLOR_ATTRIBUTE)); 

这里是后台更新功能(称为每一帧渲染()之前) (这不是一个我想优化)

void background.update(float deltaS) { 
    for (BGSquareRow row : squareRows) 
     row.update(deltaS); 
    setupVerticesArray(); 
    mesh.updateVertices(0, verticesArray); 
} 


private final Color tmpColor = new Color(); 

private void setupVerticesArray() { 
    int index = 0; 
    for (BGSquareRow row : squareRows) { 
     final float y0 = row.y; 
     final float y1 = row.y + squareSize; 
     for (int i=0; i<nbSquareX; i++) { 
      row.getSquareColor(i, tmpColor); 
      final float x0 = squareXs[i]; 
      final float x1 = squareXs[i+1]; 
      verticesArray[index++] = x0; 
      verticesArray[index++] = y0; 
      verticesArray[index++] = tmpColor.r; 
      verticesArray[index++] = tmpColor.g; 
      verticesArray[index++] = tmpColor.b; 
      verticesArray[index++] = x1; 
      verticesArray[index++] = y0; 
      verticesArray[index++] = tmpColor.r; 
      verticesArray[index++] = tmpColor.g; 
      verticesArray[index++] = tmpColor.b; 
      verticesArray[index++] = x1; 
      verticesArray[index++] = y1; 
      verticesArray[index++] = tmpColor.r; 
      verticesArray[index++] = tmpColor.g; 
      verticesArray[index++] = tmpColor.b; 
      verticesArray[index++] = x0; 
      verticesArray[index++] = y1; 
      verticesArray[index++] = tmpColor.r; 
      verticesArray[index++] = tmpColor.g; 
      verticesArray[index++] = tmpColor.b; 
     } 
    } 
} 

这里是),后台生成功能,被称为background.update(后每帧(这是太costy之一)

void background.render() { 
     shaderProgram.begin(); 
     shaderProgram.setUniformMatrix("u_projTrans", parent.uiCamera.combined); 
     mesh.render(shaderProgram, GL20.GL_TRIANGLES); 
     shaderProgram.end(); 
} 

我试图把仅有最少明白我的问题需要的,所以如果我忘了一些重要的东西,请告诉我

我有优化的一些想法,但我不知道如何做到这一点:

  • 有没有可能来在主spriteBatch上绘制背景网格?这将导致在main.render()函数中1次绘制调用,而不是2次如果我是对的

  • 是否可以仅更新网格顶点缓冲区中的y,红色,蓝色和绿色值?我找到了更新顶点缓冲的一部分,但只有当它是一个连续部分

  • 是否有针对verticesArray设置任何可能的优化?

(我还试图用ColorPacked为顶点缓冲区,但它是不是更好)

+0

有多少行/列有哪些? – Tenfour04

+0

有10分格17行!它填补了屏幕 –

+0

而且你知道,如果你是填充速率限制或CPU有限的,还是什么?这看起来真是小巫见大巫,所以如果你需要对其进行优化,可能会出现一些严重的问题,那就是在代码中你没有显示。 – Tenfour04

您可以创建使用meshParts,其中每个meshPart将代表一个方格您的网格。然后,您可以使用GLSL着色器为每个不同颜色的方块着色。这应该最大限度地减少您的CPU工作时间,并将利用GPU的功率

Here's an example of colorizing shader with libGDX.MeshPart render方法支持将ShaderProgram作为参数传递,并且可以包含特定的材质。