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