之前精灵变灰都用的是别人写的一个GraySprite,实在是有很多局限
后来有个需求是要让CCControlButton在不传disable状态图片的时候,自动把normal的图片变灰
查阅多方资料后,终于把问题搞定
- 在cocos2dx/shaders目录下新建一个ccShader_PositionTextureGray_frag.h
123456789101112131415161718192021#ifndef cocos2dx_ccShader_PositionTextureGray_frag_h#define cocos2dx_ccShader_PositionTextureGray_frag_h" n#ifdef GL_ES nprecision mediump float; n#endif nnuniform sampler2D u_texture; nvarying vec2 v_texCoord; nvarying vec4 v_fragmentColor; nnvoid main(void) n{ n// Convert to greyscale using NTSC weightings nvec4 col = texture2D(u_texture, v_texCoord); nfloat grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); ngl_FragColor = vec4(grey, grey, grey, col.a); n} n";#endif - 找到ccShaders.h,在最后加上
1extern CC_DLL const GLchar * ccPositionTextureGray_frag; - 找到ccShaders.cpp,在最后加上
12const GLchar * ccPositionTextureGray_frag =#include "ccShader_PositionTextureGray_frag.h" - 找到CCGLProgram.h在enum后面的define后加上
1#define kCCShader_PositionTextureGray "ShaderPositionTextureGray" - 找到CCShaderCache.cpp,在开头enum的最后加上kCCShaderType_PositionTextureGray
12345678910111213enum {kCCShaderType_PositionTextureColor,kCCShaderType_PositionTextureColorAlphaTest,kCCShaderType_PositionColor,kCCShaderType_PositionTexture,kCCShaderType_PositionTexture_uColor,kCCShaderType_PositionTextureA8Color,kCCShaderType_Position_uColor,kCCShaderType_PositionLengthTexureColor,kCCShaderType_PositionTextureGray,//add thiskCCShaderType_MAX,}; - 继续在CCShaderCache.cpp的loadDefaultShaders()最后添加
12345678//// Position Texture Gray shader//p = new CCGLProgram();loadDefaultShader(p, kCCShaderType_PositionTextureGray);m_pPrograms->setObject(p, kCCShader_PositionTextureGray);p->release(); - 继续在CCShaderCache.cpp的reloadDefaultShaders()最后添加
1234// Position Texture Gray shaderp = programForKey(kCCShader_PositionTextureGray);p->reset();loadDefaultShader(p, kCCShaderType_PositionTextureGray); - 继续在CCShaderCache.cpp的loadDefaultShader(CCGLProgram *p, int type)最后添加一个case条件
12345678case kCCShaderType_PositionTextureGray:p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionTextureGray_frag);p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);break; - 新建一个ColorUtils.h
1234567891011121314151617181920212223#ifndef __COLOR_UTILS_H__#define __COLOR_UTILS_H__#include "cocos2d.h"USING_NS_CC;class ColorUtils{public:ColorUtils();~ColorUtils();static void addGray(CCSprite *sp);static void removeGray(CCSprite *sp);static void addGray(CCSpriteBatchNode *sp);static void removeGray(CCSpriteBatchNode *sp);private:};#endif - 新建一个ColorUtils.cpp
12345678910111213141516171819202122232425262728293031323334353637383940414243444546#include "ColorUtils.h"ColorUtils::ColorUtils(){}ColorUtils::~ColorUtils(){}void ColorUtils::addGray(CCSprite* sp){do{CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureGray);sp->setShaderProgram(pProgram);CHECK_GL_ERROR_DEBUG();} while (0);}void ColorUtils::removeGray(CCSprite* sp){do{CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);sp->setShaderProgram(pProgram);CHECK_GL_ERROR_DEBUG();} while (0);}void ColorUtils::addGray(CCSpriteBatchNode* sp){do{CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureGray);sp->setShaderProgram(pProgram);CHECK_GL_ERROR_DEBUG();} while (0);}void ColorUtils::removeGray(CCSpriteBatchNode* sp){do{CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);sp->setShaderProgram(pProgram);CHECK_GL_ERROR_DEBUG();} while (0);} - 使用
对于CCSprite
12CCSprite *sp = CCSprite::create("demo.png");ColorUtils::addGray(sp);
对于CCScale9Sprite
12CCSpriteBatchNode *sp = (CCSpriteBatchNode*)scale9sprite->getChildren()->objectAtIndex(0);ColorUtils::addGray(sp);
remove亦然