Cocos2d-x 3.1.1 Study Notes [15] Shader coloring er, cocos2d-x3.1.1
First, two files are required.
Gray. fsh
varying vec4 v_fragmentColor;varying vec2 v_texCoord;void main(){vec4 v_orColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);float gray = dot(v_orColor.rgb, vec3(0.299, 0.587, 0.114));gl_FragColor = vec4(gray, gray, gray, v_orColor.a);}
Gray. vsh
attribute vec4 a_position;attribute vec2 a_texCoord;attribute vec4 a_color;varying vec4 v_fragmentColor;varying vec2 v_texCoord;void main(){ gl_Position = CC_MVPMatrix * a_position;v_fragmentColor = a_color;v_texCoord = a_texCoord;}
Declare a function
void shader(Sprite* sprite);
Implementation Functions
void Nice::shader(Sprite* sprite){ auto glprogram = GLProgram::createWithFilenames("gray.vsh", "gray.fsh"); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); sprite->setGLProgramState(glprogramstate);}
In the future, we need to implement this shader effect for a certain genie, so we can directly implement shader (sp ).
sp_point = Sprite::create("newAlwaysShow.png"); sp_point->setAnchorPoint(Vec2::ZERO); sp_point->setPosition(Vec2(200, 200)); addChild(sp_point); shader(sp_point);