灰化图片
在res新建2个文件,1个是vsh,1个是fsh
shader_gray.fsh >>>
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main()
{
vec4 c = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);
float gray = dot(c.rgb, vec3(0.3, 0.2, 0.2));//灰化的三种颜色值
gl_FragColor = vec4(gray, gray, gray, c.a);
}
shader_gray.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;
}
调用方法:
local function getGLProgram()
return cc.GLProgram:createWithFilenames("shader_gray.vsh", "shader_gray_fsh")
end
local function graySprite(sprite )
local pProgram = getGLProgram()
sprite:setGLProgram(pProgram)
end
--清除shader
local function cleanSpriteShader( sprite )
sprite:setGLProgramState(cc.GLProgramState:getOrCreateWithGLProgram(cc.GLProgramCache:getInstance():getGLProgram("ShaderPositionTextureColor_noMVP")));
end
对于cocosStudio的节点,可以再封装下:
local function grayWidget( widget )
if string.find(tolua.type(widget), "ccui") then
local render = widget:getVirtualRenderer()
if widget:isScale9Enabled() then
widget:setCascadeOpacityEnabled(true)--忽略原来的透明度
end
local sprite = render:getSprite()
graySprite(sprite)
end
end
网友评论