cocos2d-x 使用Shader让精灵变灰

来源:互联网 发布:聚橙网络上市 编辑:程序博客网 时间:2024/04/28 16:34

之前的Shader文件,在精灵修改透明度的时候有问题,这才想着记一下变灰的方法!


void GameColorSprite::setGrayMode(){    const GLchar* pszFragSource =    "#ifdef GL_ES \n \    precision mediump float; \n \    #endif \n \    uniform sampler2D u_texture; \n \    varying vec2 v_texCoord; \n \    varying vec4 v_fragmentColor; \n \    void main(void) \n \    { \n \    // Convert to greyscale using NTSC weightings \n \    vec4 pixel = texture2D(u_texture, v_texCoord); \n \    float grey = dot(pixel.rgb, vec3(0.299, 0.587, 0.114)); \n \    float alpha = v_fragmentColor.a; \n\    gl_FragColor = vec4(grey * alpha, grey * alpha, grey * alpha, pixel.a * alpha); \n \    }";        CCGLProgram* pProgram = new CCGLProgram();    pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);    this->setShaderProgram(pProgram);    pProgram->release();        CHECK_GL_ERROR_DEBUG();        this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);    this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);    this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);    CHECK_GL_ERROR_DEBUG();        this->getShaderProgram()->link();    CHECK_GL_ERROR_DEBUG();        this->getShaderProgram()->updateUniforms();    CHECK_GL_ERROR_DEBUG();    }




0 0
原创粉丝点击