小米电视盒子上出现屏保时有一个类似水墨画的渐变效果,挺好看的,研究了一下发现是用opengl做的,在cocos2d项目下在Scene里面写个Filter配合vsh、fsh 加载底图进行处理就行了,Shader文件如下
vsh文件
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_PMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
fsh文件
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform float u_max;
uniform float u_min;
uniform sampler2D u_targetTexture;
uniform sampler2D u_maskTexture;
void main(void)
{
vec4 color = texture2D(u_targetTexture, v_texCoord);
vec4 colorMask = texture2D(u_maskTexture, v_texCoord);
float fAlpha = clamp((colorMask.r-u_min)/(u_max-u_min),0.0,1.0);//clamp((colorMask.r - u_max)/(u_min - u_max),0.0,1.0);//
color.a *= fAlpha;
color.b*colorMask.b, color.a);
gl_FragColor = color;
}
我要评论