2015-07-19 150 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