我有一个颜色数组。当我使用 ShapeRenderer 时,需要花费很多时间。 我使用这段代码:
for (int i = 0; i < colors.length; i++) {
for (int j = 0; j < colors[0].length; j++) {
shapeRenderer.setColor(colors[i][j]);
shapeRenderer.rect(i,j,1,1)
}
}
必须有一种更快的方法,因为纹理是由像素制成的,并且它们的渲染速度非常快。我的问题是:如何像纹理一样快速渲染这个颜色数组?
最佳答案
纹理的绘制速度非常快,因为通常只需要对 GPU 进行几次调用。逐个像素地执行此操作需要更多的调用和状态更改。如果数组经常更改,则您无能为力进行更改。如果没有,您应该在更改它们时将它们绘制到纹理上,然后只使用该纹理。
在 LibGDX 中,您可以将每个像素绘制到 Pixmap 上,然后从中创建新的纹理。 来源:https://github.com/libgdx/libgdx/wiki/Pixmaps
示例代码:
Pixmap pixmap = new Pixmap( colors.length, colors[0].length, Format.RGBA8888 );
for (int i = 0; i < colors.length; i++)
{
for (int i = 0; i < colors.length; i++)
{
pixmap.setColor( 0, 1, 0, 0.75f ); //Set to color r,g,b,a
pixmap.drawPixel(i,j); //Draw the pixel
}
}
Texture pixmaptex = new Texture(pixmap); //Create new texture from the Pixmap
pixmap.dispose();
关于java - LibGDX 渲染颜色数组的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400719/