GLSL着色器 - 具有透明度的阴影(glasstable效果)

时间:2012-04-13 14:18:27

标签: glsl opengl-es-2.0 fragment-shader vertex-shader

我创建了一个可以将图像旋转180°并使用黑色渐变覆盖它的着色器,但现在我想创建真正的透明度,而不是使用黑色作为我的背景颜色。

这是我到目前为止所得到的:

// Vertex Shader
uniform highp mat4 u_modelViewMatrix;
uniform highp mat4 u_projectionMatrix;
attribute highp vec4 a_position;
attribute lowp vec4 a_color;
attribute highp vec2 a_texcoord;
varying lowp vec4 v_color;
varying highp vec2 v_texCoord;
uniform int offset;
uniform int space;
uniform int vph;
void main()
{\
highp float h = float(offset)/float(vph);
highp float s = float(space)/1000.0;
highp vec4 pos = a_position;
pos.y = pos.y - (h + s);
gl_Position = (u_projectionMatrix * u_modelViewMatrix) * pos;
    v_color = a_color;
v_texCoord = vec2(a_texcoord.x, 1.0 - a_texcoord.y);
}

// Fragment Shader
varying highp vec2 v_texCoord;
uniform sampler2D u_texture0;
uniform int gradient;
void main()
{
  lowp vec3 w = vec3(1.0,1.0,1.0);
  lowp vec3 b = vec3(0.0,0.0,0.0);
  lowp vec3 mix = mix(b, w, (v_texCoord.y-(float(gradient)/10.0)));
  gl_FragColor = texture2D(u_texture0,v_texCoord) * vec4(mix, 1.0);
}

1 个答案:

答案 0 :(得分:1)

在片段着色器中用半透明颜色替换黑色:

void main()
{
  lowp vec4 w = vec3(1.0,1.0,1.0,1.0);
  lowp vec4 b = vec3(0.0,0.0,0.0,0.0);
  lowp vec4 mix = mix(b, w, (v_texCoord.y-(float(gradient)/10.0)));
  gl_FragColor = texture2D(u_texture0,v_texCoord) * mix;
}