有没有办法优化这个glsl?

时间:2013-11-01 13:01:54

标签: opengl-es glsl webgl

有纹理数组(uSamplerArr),纹理坐标偏差(texBiasArr),不透明度(alfaArr)和透明颜色数组(tcolorArr)。

结果 - 低帧率。

如何优化此着色器?

顶点着色器

attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;

uniform mat4 uPMVMatrix;

varying vec2 vTextureCoord;

void main(void) {

  gl_Position = uPMVMatrix * vec4(aVertexPosition, 1.0);
  vTextureCoord = aTextureCoord;
}

片段着色器

precision mediump float;
precision lowp int;

const int MAX_TEX = 8;
const float COLOR_DIFF_EPS = 0.4;

varying vec2 vTextureCoord;

uniform int numTex;
uniform sampler2D uSamplerArr[MAX_TEX];
uniform float alfaArr[MAX_TEX];
uniform vec3 tcolorArr[MAX_TEX];
uniform vec3 texBiasArr[MAX_TEX];

void main(void) 
{
  vec4 res = texture2D(uSamplerArr[0], texBiasArr[0].z * (vTextureCoord + vec2(texBiasArr[0].x,     texBiasArr[0].y)));

  if(numTex > 1)
  {
    vec4 ti;
    for( int i = 1; i < MAX_TEX; i++ ) 
    {
      if( i < numTex ) 
      {
        ti = texture2D(uSamplerArr[i], texBiasArr[i].z * (vTextureCoord + texBiasArr[i].xy));

        if( length( ti.rgb - tcolorArr[i] ) > COLOR_DIFF_EPS ) 
        {
          res = mix(res, ti, alfaArr[i]);
        }
      }
      else
      {
        break;
      }
    }
  }
  gl_FragColor = res;
}

0 个答案:

没有答案