由于网上流传版本很多,笔者也不清楚原创作者到底是哪位大神,所以上网随便找了一个版本,并将其API全部替换为了Cocos2d-x 2.2.2的版本。主要功能是将一个精灵变为灰色以及恢复正常颜色(注:不要使用纯色图片)。
首先看效果图:
下面是代码部分,将其复制粘贴即可使用。文件名
BYGraySprite.h:
#include "cocoa/CCGeometry.h"
#include "cocos2d.h"
USING_NS_CC;
class BYGraySprite : public CCSprite{
public:
BYGraySprite();
virtual ~BYGraySprite();
static BYGraySprite* create(const char* pszFileName);//创建一个变灰的精灵
bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);
virtual void draw();
void enableSprite(CCSprite* sp); //恢复正常颜色的方法
};
BYGraySprite.cpp:
#include "BYGraySprite.h"
BYGraySprite::BYGraySprite(){
}
BYGraySprite::~BYGraySprite(){
}
BYGraySprite* BYGraySprite::create( const char* pszFileName ){
BYGraySprite* graySprite = new BYGraySprite;
if (graySprite && graySprite->initWithFile(pszFileName)){
graySprite->autorelease();
return graySprite;
}else{
CC_SAFE_RELEASE(graySprite);
return NULL;
}
}
bool BYGraySprite::initWithTexture(CCTexture2D* pTexture, const CCRect& tRect ){
do{
CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));
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); float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); gl_FragColor = vec4(grey, grey, grey, col.a); \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();
return true;
} while (0);
return false;
}
void BYGraySprite::draw(){
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
this->getShaderProgram()->use();
this->getShaderProgram()->setUniformsForBuiltins();
ccGLBindTexture2D( this->getTexture()->getName() );
#define kQuadSize sizeof(m_sQuad.bl)
long offset = (long)&m_sQuad;
// vertex
int diff = offsetof( ccV3F_C4B_T2F, vertices);
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( ccV3F_C4B_T2F, texCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( ccV3F_C4B_T2F, colors);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWS(1);
}
void BYGraySprite::enableSprite(CCSprite* sp)
{
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 \
gl_FragColor = vec4(col.r, col.g, col.b, col.a); \n \
}";
CCGLProgram* pProgram = new CCGLProgram();
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
sp->setShaderProgram(pProgram);
pProgram->release();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
sp->getShaderProgram()->link();
sp->getShaderProgram()->updateUniforms();
}
在HelloWorld.cpp中测试代码:
BYGraySprite* graySprite = BYGraySprite::create("stone.png");
graySprite->setPosition(ccp(480,400));
//graySprite->enableSprite(graySprite); 恢复颜色的方法
this->addChild(graySprite);
CCSprite * sprite=CCSprite::create("stone.png");
sprite->setPosition(ccp(graySprite->getPositionX(), graySprite->getPositionY()-200));
this->addChild(sprite);