shader图片溶解消失
2021-05-20 本文已影响0人
园Flora
--图片溶解消失:
--spr:需要溶解的图片
--speed:溶解速度
--sp2:按某个纹理溶解(可为nil,默认spr的纹理)
--finishCall:消失后回调(可为nil)
function UICommon.rongJieXiaoshi(spr,Speed,spr2,finishCall)
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 sampler2D u_texture1;
uniform float Threshold;
void main(void)
{
vec4 col=texture2D(u_texture1,v_texCoord);
if(col.g>Threshold){
discard;
}
gl_FragColor=texture2D(CC_Texture0,v_texCoord);
}
]]
if spr2 and (spr2:getDescription() == "ImageView" or spr2:getDescription() == "Button") then
spr2 = spr2:getVirtualRenderer():getSprite()
end
if spr:getDescription() == "ImageView" or spr:getDescription() == "Button" then
spr = spr:getVirtualRenderer():getSprite()
end
local Texture = spr2 and spr2:getTexture() or spr:getTexture()
local cache = cc.GLProgramCache:getInstance()
local pProgram = cc.GLProgram:createWithByteArrays(vertSource1,fragSource1)
local Speed = Speed or 1
gl.deleteShader(pProgram)
pProgram:link()
pProgram:updateUniforms()
pProgram:use()
cache:addGLProgram(glProgram, "rongJieXiaoshi")
local glProgramState = cc.GLProgramState:getOrCreateWithGLProgram(pProgram)
glProgramState:setUniformTexture("u_texture1",Texture);
spr:setGLProgram(pProgram)
spr:setGLProgramState(glProgramState)
local Threshold = 1
local scheduleId
_callback = function ( ... )
glProgramState:setUniformFloat("Threshold",Threshold);
Threshold = Threshold-0.1
if Threshold<=0 then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(scheduleId)
scheduleId = nil
if finishCall and type(finishCall) == "function" then
finishCall()
end
return
end
end
scheduleId = cc.Director:getInstance():getScheduler():scheduleScriptFunc(_callback, 0.1*Speed, false)
_callback()
end