我需要在游戏中为一个精灵创建一些ripling效果,这里是vertexShader:
attribute vec4 a_position; // just taking in necessary attributes
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans; // Combination of view and projection matrix
varying vec2 v_texCoords;
void main() {
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position; //as I said, it is sprite so no need for modelMatrix
}
以及片段:
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoords;
uniform sampler2D u_texture; //texture of sprite
uniform float time;
void main()
{
vec2 uv;
if (time > 0.0) { // time is > 0.0 when I want the ripling effect to be applied,
vec2 cPos = -1.0 + 2.0 * v_texCoords.xy; // converting tex.Coords to -1 - 1
float cLength = length(cPos); //taking length of it
uv = v_texCoords.xy +( (cPos/cLength)*cos(cLength*12.0-time*4.0)*0.03 ) // just some calculations for the ripling effect
}
else
uv = v_texCoords.xy; // if I don't want to use the ripling effect, I use normal texCoords
vec4 tex = texture2D(u_texture, uv); //sampling texture
gl_FragColor = tex;
}
这一切都运行良好,PC上的性能很好,但是在Android上运行它时性能要差很多......正如你所看到的,着色器是微不足道的但它们不知怎的昂贵..无论如何,我绘制的精灵的宽度约为2000 - 4000 px,高度为720.另外,当我用cPos calc:vec2 cPos = -1.0 + 2.0 * v_texCoords.xy;
中的不同向量替换v_texCoords(例如vec2(1,1))时性能提高重..
我真的不知道那里有那么贵的东西。如果有人有一些建议,我会很高兴。提前致谢