遊戲中人物死掉後要把人物頭像變成灰白圖,不少遊戲也是這樣處理,問題來了,怎麼將CCsprite生成的圖變成灰白呢?web
看了下實現,基本有了辦法。CCSprite是在initWithTexture的時候渲染的貼圖,若是在這裏面設置一個一個灰度Shader,也許能夠將圖片改爲灰白色。ide
GL Shader腳本代碼:函數
- #ifdef GL_ES
- precision mediump float;
- #endif
- uniform sampler2D u_texture;
- varying vec2 v_texCoord;
- varying vec4 v_fragmentColor;
- void main(void)
- {
-
- float alpha = texture2D(u_texture, v_texCoord).a;
- float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114));
- gl_FragColor = vec4(grey, grey, grey, alpha);
- }
這個主要目的是將RGB值轉換爲YUV值,Y就是灰度,因此咱們取出裏面的Y值來實現咱們的灰白圖。ui
Google了下,vec3(0.299, 0.587, 0.114) 是RGB轉YUV的參數值。this
計算完了以後根據原來的紋理alpha值,輸出咱們處理後的顏色到gl_FragColor,就能夠讓shader去渲染黑白圖。url
裏面主要是在光柵化後的片斷進行顏色處理,最後輸出的是像素,它不會產生額外紋理,因此不會卡。spa
注意:GLSL1.0的可能有些函數不支持,會crash。不過目前基本能夠不考慮。.net
下面就是我寫好的類,加入本身的工程就能夠了。orm
頭文件blog
-
-
-
-
-
-
-
-
-
- #ifndef Demo_BYGraySprite_h
- #define Demo_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();
-
-
- };
-
-
- #endif
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 \
-
- float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114)); \n \
- gl_FragColor = vec4(grey, grey, grey, 1.0); \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()->setUniformForModelViewProjectionMatrix();
-
-
- ccGLBindTexture2D( this->getTexture()->getName() );
-
-
- #define kQuadSize sizeof(m_sQuad.bl)
- long offset = (long)&m_sQuad;
-
-
-
- int diff = offsetof( ccV3F_C4B_T2F, vertices);
- glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
-
-
-
- diff = offsetof( ccV3F_C4B_T2F, texCoords);
- glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
-
-
-
- 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);
- }
使用方法:
- BYGraySprite* graySprite = BYGraySprite::create("Icon.png");
- graySprite->setPosition( ccp(size.width/2, size.height/2) );
- this->addChild(graySprite);
2、以前alpha值是直接用的1,因此透明圖會渲染成黑色。把原來紋理的alpha取出來並使用。就能夠避免該問題。
也就是將GL腳本代碼中的
float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114)); \n \
gl_FragColor = vec4(grey, grey, grey, 1.0); \n \
改爲:
vec4 col = texture2D(u_texture, v_texCoord); \n \
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
gl_FragColor = vec4(grey, grey, grey, col.a); \n \