--UICommon.setSaoguang --扫光效果 示例
--[[ local preTable = {
Radius = 0.2,--光束宽度系数
Speed = 0.3,--扫光速度
Arg = 30,--光束倾斜角度
Lightcolor = cc.vec4(1,0.25,0.25,1),--光束颜色系数
TouM = 1 --图片透明度限制(低于此值的不加扫光)
}
UICommon.setSaoguang(sp2,preTable)
--]]
function UICommon.setSaoguang(spr,properTable)
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 Arg;
uniform float Speed; //扫光速度
uniform float Radius;
uniform float TouM;
uniform vec4 Lightcolor;
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 到圆心起点的距离
float x = fract(CC_Time[2]*Speed);
//float x = 0.5;
x = x*(1.0+Radius*2.0)-Radius;
vec2 cPoint1 = vec2(x,x);
vec2 cPoint2 = vec2(x,1.0-x);
vec2 cPoint = cPoint1*step(0.0,tan(Arg)) + cPoint2*step(tan(Arg),0.0);
//vec2 cPoint =cPoint1;
float dis = abs(cPoint.x-v_texCoord.x);
if(abs(tan(Arg))!=0.0){
float y = (cPoint.x-v_texCoord.x)/tan(Arg)+cPoint.y;
dis = abs((v_texCoord.y-y)*sin(Arg));
}
float b = 1.0 ;
b *= step(TouM, textureColor.a);//透明度小于1的区域忽略不加特效
b *= step(dis, Radius);//超过指定宽度的不渲染
b *= 1.0-(dis/Radius);//渐变效果
vec4 lightColor = Lightcolor*b;
return textureColor+lightColor;
}
]]
local pProgram = cc.GLProgram:createWithByteArrays(vertSource1,fragSource1)
local glProgramState = cc.GLProgramState:getOrCreateWithGLProgram(pProgram)
local preperT = checktable(properTable)
local Radius = preperT.Radius or 0.4
local Lightcolor = preperT.Lightcolor or cc.vec4(1.0,1.0,1.0,0.5)
local Arg = preperT.Arg or 8.0
local Speed = preperT.Speed or 1.0
local TouM = preperT.TouM or 1.0
glProgramState:setUniformFloat("Radius",Radius);
glProgramState:setUniformVec4("Lightcolor",Lightcolor);
glProgramState:setUniformFloat("Arg", Arg);
glProgramState:setUniformFloat("Speed",Speed);
glProgramState:setUniformFloat("TouM", TouM);
if spr:getDescription() == "ImageView" or spr:getDescription() == "Button" then
spr = spr:getVirtualRenderer():getSprite()
end
spr:setGLProgram(pProgram)
spr:setGLProgramState(glProgramState)
end
网友评论