shader 图片边缘虚化
function UICommon.setImageSideSheer(spr,Radius,centerPoint)
local vertSource1 = [[
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;
}
]]
local fragSource1 = [[
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform float Radius;
uniform vec2 centerPoint;
vec4 addLightColor(vec4);
void main(void)
{
vec4 o = vec4(1.0, 1.0, 1.0, 1.0);
o *= texture2D(CC_Texture0, v_texCoord);
//o.a *= texture2D(CC_Texture0, v_texCoord + vec2(0.0, 0.5)).r;
o *= v_fragmentColor;
gl_FragColor = o;
vec4 col = addLightColor(gl_FragColor); //* v_fragmentColor.rgb;
gl_FragColor = col;
}
vec4 addLightColor(vec4 textureColor)
{
// 计算当前 uv 到圆心起点的距离
vec2 centerPoint1 = 1.0 /centerPoint.xy;
float dis = distance(v_texCoord, centerPoint);
float b = 1.0 ;
b = 1-(dis/Radius)*(dis/Radius)*(dis/Radius);
// 改变原始图像透明度
return textureColor*textureColor.a*b;
}
]]
local pProgram = cc.GLProgram:createWithByteArrays(vertSource1,fragSource1)
local glProgramState = cc.GLProgramState:getOrCreateWithGLProgram(pProgram)
local centerPoint =centerPoint or cc.p(0.5, 0.35)
local Radius = Radius or 0.3
glProgramState:setUniformFloat("Radius",Radius);
glProgramState:setUniformVec2("centerPoint", centerPoint);
if spr:getDescription() == "ImageView" or spr:getDescription() == "Button" then
spr = spr:getVirtualRenderer():getSprite()
end
spr:setGLProgram(pProgram)
spr:setGLProgramState(glProgramState)
end