我需要在游戏中进行一些重度优化,我在思考,因为我们可以将vec4颜色传递给只有一个浮点数的着色器,是否有办法传递vec2纹理坐标?例如,在下面的代码中,我可以将2个纹理坐标(它始终为1和0)作为唯一的元素传递,就像它在颜色元素中发生的那样。
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")
);
}
frag shader
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;
}
垂直着色器
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;
}
我知道这不会对性能产生很大的影响,但我只是愿意在这里和那里多花一些时间进行小幅优化,以使我的游戏运行得更快。
答案 0 :(得分:1)
我没试过,但我认为它会奏效。只需使用www.isDone
作为纹理坐标即可。我认为你不能发送任何小于4个字节的内容,所以你也可以使用已定义的打包颜色。每个顶点只能保存一个字节。您可以将坐标放在前两个元素中,并忽略后两个元素。
Usage.ColorPacked
我不认为VertexAttribute的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.ColorPacked, 4,"a_texCoord0")
);
参数实际上是由Libgdx中的任何东西使用的。如果查看源代码,它只检查usage
是否为ColorPacked,并从中决定是否每个组件使用一个字节而不是4个。
在您的顶点着色器中:
usage
正确翻译纹理坐标:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec4 a_texCoord0; //note using vec4
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0.xy; //using the first two elements
gl_Position = u_projTrans * a_position;
}