2015-07-19 58 views
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; 
} 

我知道这不会使性能有很大的不同,但我只愿意花一些额外的为了让我的游戏运行得更快,在这里和这里进行小规模的优化。

回答

1

我没有试过这个,但我认为它会工作。只需使用Usage.ColorPacked即可获得您的纹理坐标。我不认为你可以发送小于4字节的任何东西,所以你可以使用已经定义好的打包颜色。你只会保存每个顶点一个字节。您可以将坐标放入前两个元素,并忽略后两个元素。

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") 
); 

我不认为VertexAttribute的usage参数实际上被用于在Libgdx什么。如果你看看源,它只有在usage被ColorPacked或没有检查,并从该决定是使用每个组件的单个字节与4

而在你的顶点着色器:

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; 
} 

要翻译纹理坐标正确:

final Color texCoordColor = new Color(0, 0, 0, 0); 

//.... 

texCoordColor.r = texCoord.x; 
texCoordColor.g = texCoord.y; 
float texCoord = texCoordColor.toFloatBits(); 
+0

作品!我不知道为什么libgdx默认情况下不具有这样至少因为精灵的纹理坐标的SpriteBatch一个调整不改很报价,可能是绩效收益并不显着,但它的东西。 – SteveL

+0

SpriteBatch有很多代码取决于其特定的顶点大小和属性分配以进行优化。在任何显着大小的TextureAtlases上,U和V单个字节的精度不够。对于您而言,不要使用TextureAtlas中的TextureRegions,而您并不关心UV精度。 – Tenfour04

+0

我明白了,我已经准备好要处理SpriteBatch和TextureRegion源代码,并且完全忘记了精度损失。我在其他所有应用程序中使用了TextureAtlase,以上仅仅是针对某些对象的发光效果(可能是400- 500个物体,如颗粒)。感谢您的维修人员。 – SteveL