0
我需要在我的游戏中进行一些繁重的优化,我在想,因为我们可以将vec4颜色传递给只包含一个浮点的着色器,有没有办法通过vec2纹理坐标吗?例如,在下面的代码中,我可以将两个纹理坐标(它总是1和0)作为唯一一个元素传递,就像它在颜色元素中发生的一样。OpenGL传递打包的纹理坐标到着色器
public void point(float x,float y,float z,float size,float color){
float hsize=size/2;
if(index>vertices.length-7)return;
vertices[index++]=x-hsize;
vertices[index++]=y-hsize;
vertices[index++]=color;
vertices[index++]=0;
vertices[index++]=0;
vertices[index++]=x+hsize;
vertices[index++]=y-hsize;
vertices[index++]=color;
vertices[index++]=1;
vertices[index++]=0;
vertices[index++]=x+hsize;
vertices[index++]=y+hsize;
vertices[index++]=color;
vertices[index++]=1;
vertices[index++]=1;
vertices[index++]=x-hsize;
vertices[index++]=y+hsize;
vertices[index++]=color;
vertices[index++]=0;
vertices[index++]=1;
num++;
}
{
mesh=new Mesh(false,COUNT*20, indices.length,
new VertexAttribute(Usage.Position, 2,"a_position"),
new VertexAttribute(Usage.ColorPacked, 4,"a_color"),
new VertexAttribute(Usage.TextureCoordinates, 2,"a_texCoord0")
);
}
断枝着色器
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
vec4 color=v_color * texture2D(u_texture, v_texCoords);
gl_FragColor = color;
}
VERT着色器
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
我知道这不会使性能有很大的不同,但我只愿意花一些额外的为了让我的游戏运行得更快,在这里和这里进行小规模的优化。
作品!我不知道为什么libgdx默认情况下不具有这样至少因为精灵的纹理坐标的SpriteBatch一个调整不改很报价,可能是绩效收益并不显着,但它的东西。 – SteveL
SpriteBatch有很多代码取决于其特定的顶点大小和属性分配以进行优化。在任何显着大小的TextureAtlases上,U和V单个字节的精度不够。对于您而言,不要使用TextureAtlas中的TextureRegions,而您并不关心UV精度。 – Tenfour04
我明白了,我已经准备好要处理SpriteBatch和TextureRegion源代码,并且完全忘记了精度损失。我在其他所有应用程序中使用了TextureAtlase,以上仅仅是针对某些对象的发光效果(可能是400- 500个物体,如颗粒)。感谢您的维修人员。 – SteveL