【发布时间】:2017-10-05 12:44:32
【问题描述】:
我的游戏背景渲染花费了我一直试图改进它的所有渲染时间的 1/4,但我仍然认为它需要这么长时间才能知道它有多简单是不正常的:
背景是一个方格(像棋盘),每个方格都有自己的颜色,每帧都会改变,方格的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));
这里是后台更新函数(每帧调用,在render()之前) (这不是我要优化的)
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() 之后每帧调用一次(这个方法代价太高了)
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 用于顶点缓冲区,但效果并不好)
【问题讨论】:
-
有多少行/列?
-
有17行10个方格!它填满了屏幕
-
你知道你是受填充率限制还是 CPU 限制,还是什么?这看起来真的很琐碎,所以如果您需要优化它,可能存在一些您未显示的代码中的严重问题。
标签: optimization opengl-es libgdx opengl-es-2.0