在遇到英雄死亡需要让它变黑白的过程中参考ps中图片渐变成灰色的方式
解决思路是使用两张图片,一张彩色一张黑白,让彩色透明度随时间增长变0
网上有彩图变灰的代码如下
do { 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 col = texture2D(u_texture, v_texCoord); \n float grey = dot(col.rgb, vec3(%f, %f, %f)); \n gl_FragColor = vec4(grey, grey, grey, col.a); \n }"; auto strPath = StringUtils::format(pszFragSource, r, g, b); GLProgram* pProgram = new GLProgram(); pProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, strPath.c_str());//pszFragSource); sp->setGLProgram(pProgram); CHECK_GL_ERROR_DEBUG(); sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD); CHECK_GL_ERROR_DEBUG(); sp->getGLProgram()->link(); CHECK_GL_ERROR_DEBUG(); sp->getGLProgram()->updateUniforms(); CHECK_GL_ERROR_DEBUG(); } while (0);
图片渐变成黑白代码如下
auto strPath = getFilePath("caocao", FileSuffix::Png); auto sp = Sprite::create(strPath); addChild(sp, 0); setSpriteGray(sp, 0.299, 0.587, 0.114); sp->setPosition(300, 400); cc = Sprite::create(strPath); addChild(cc, 1); cc->setPosition(sp->getPosition()); auto fadeTo = FadeOut::create(3.f); cc->runAction(/*Sequence::create*/(fadeTo));
时间: 2024-10-13 21:21:47