生活随笔
收集整理的這篇文章主要介紹了
cocos2d-x游戏开发(十四)用shader使图片背景透明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
歡迎轉載,地址:http://blog.csdn.net/fylz1125/article/details/8631783
好吧,終于抽時間寫這篇文章了。
手頭上有很多人物行走圖,技能特效圖等,但這些圖都有個純黑色背景,怎么樣將內容顯示出來,讓背景透明呢?前段時間搞了一下,感謝群里的童鞋們,提供了思路和方法。
這里用shader處理了像素,使黑色背景透明,直接上代碼
ShaderSprite.h
[cpp]?view plaincopyprint?
#ifndef?__TestShader__ShaderSprite__?? #define?__TestShader__ShaderSprite__?? ?? #include?"cocos2d.h"?? USING_NS_CC;?? ?? class?ShaderSprite?:?public?CCSprite?{?? ?????? public:?? ????static?ShaderSprite*?create(const?char*?pszFileName);?? ????virtual?bool?initWithTexture(CCTexture2D?*pTexture,?const?CCRect&?rect);?? ????virtual?void?draw(void);?? };?? ?? #endif?/*?defined(__TestShader__ShaderSprite__)?*/??
ShaderSprite.cpp
[cpp]?view plaincopyprint?
#include?"ShaderSprite.h"?? ?? static?CC_DLL?const?GLchar?*transparentshader?=?? #include?"tansparentshader.h"?? ?? ShaderSprite*?ShaderSprite::create(const?char?*pszFileName)?? {?? ????ShaderSprite?*pRet?=?new?ShaderSprite();?? ????if?(pRet?&&?pRet->initWithFile(pszFileName))?{?? ????????pRet->autorelease();?? ????????return?pRet;?? ????}?? ????else?? ????{?? ????????delete?pRet;?? ????????pRet?=?NULL;?? ????????return?NULL;?? ????}?? }?? ?? bool?ShaderSprite::initWithTexture(CCTexture2D?*pTexture,?const?CCRect&?rect)?? {?? ????do{?? ?? ????????CC_BREAK_IF(!CCSprite::initWithTexture(pTexture,?rect));?? ?????????? ?????????? ????????m_pShaderProgram?=?new??CCGLProgram();?? ????????m_pShaderProgram?->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert,?transparentshader);?? ?????????? ????????CHECK_GL_ERROR_DEBUG();?? ?????????? ?????????? ????????m_pShaderProgram->addAttribute(kCCAttributeNamePosition,?kCCVertexAttrib_Position);?? ????????m_pShaderProgram->addAttribute(kCCAttributeNameColor,?kCCVertexAttrib_Color);?? ????????m_pShaderProgram->addAttribute(kCCAttributeNameTexCoord,?kCCVertexAttrib_TexCoords);?? ?????????? ????????CHECK_GL_ERROR_DEBUG();?? ?????????? ?????????? ????????m_pShaderProgram->link();?? ?????????? ????????CHECK_GL_ERROR_DEBUG();?? ?????????? ?????????? ????????m_pShaderProgram->updateUniforms();?? ?????????? ????????CHECK_GL_ERROR_DEBUG();?? ?????????? ????????return?true;?? ?????????? ????}while(0);?? ????return?false;?? }?? ?? void?ShaderSprite::draw(void)?? {?? ?? ????CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite,?"CCSprite?-?draw");?? ?????? ????CCAssert(!m_pobBatchNode,?"If?CCSprite?is?being?rendered?by?CCSpriteBatchNode,?CCSprite#draw?SHOULD?NOT?be?called");?? ?????? ????CC_NODE_DRAW_SETUP();?? ?????? ?????? ?????? ?????? ????ccGLEnableVertexAttribs(?kCCVertexAttribFlag_PosColorTex?);?? ????ccGLBlendFunc(m_sBlendFunc.src,?m_sBlendFunc.dst);?? ?????? ????m_pShaderProgram->use();?? ????m_pShaderProgram->setUniformsForBuiltins();?? ?????? ?????? ????ccGLBindTexture2D(m_pobTexture->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);?? ?????? ????CHECK_GL_ERROR_DEBUG();?? ?? ????CC_INCREMENT_GL_DRAWS(1);?? ????CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite,?"CCSprite?-?draw");?? }??
片段著色器代碼
tansparentshader.h
[cpp]?view plaincopyprint?
"???????????????????????????????????????????????????????\n\?? #ifdef?GL_ES????????????????????????????????????????????\n\?? precision?lowp?float;???????????????????????????????????\n\?? #endif??????????????????????????????????????????????????\n\?? varying?vec4?v_fragmentColor;???????????????????????????\n\?? varying?vec2?v_texCoord;????????????????????????????????\n\?? uniform?sampler2D?u_texture;????????????????????????????\n\?? void?main()?????????????????????????????????????????????\n\?? {???????????????????????????????????????????????????????\n\?? ????float?ratio=0.0;????????????????????????????????????\n\?? ????vec4?texColor?=?texture2D(u_texture,?v_texCoord);???\n\?? ????ratio?=?texColor[0]?>?texColor[1]?(texColor[0]?>?texColor[2]???texColor[0]?:?texColor[2])?:(texColor[1]?>?texColor[2]??texColor[1]?:?texColor[2]);??????????????????????????????????????\n\?? if?(ratio?!=?0.0)??????????????????????????????????????????\n\?? {??????????????????????????????????????????????????????????\n\?? ????texColor[0]?=?texColor[0]?/??ratio;????????????????????\n\?? ????texColor[1]?=?texColor[1]?/??ratio;????????????????????\n\?? ????texColor[2]?=?texColor[2]?/??ratio;????????????????????\n\?? ????texColor[3]?=?ratio;???????????????????????????????????\n\?? }??????????????????????????????????????????????????????????\n\?? else???????????????????????????????????????????????????????\n\?? {??????????????????????????????????????????????????????????\n\?? ????texColor[3]?=?0.0;?????????????????????????????????????\n\?? }??????????????????????????????????????????????????????????\n\?? gl_FragColor?=?v_fragmentColor*texColor;???????????????????\n\?? }";??
注意shader編程沒有隱士數據類型轉換,所以都顯示為float了。
然后ratio是指在rgb中找最大的,如果ratio為0直接將alpha設為0,否則alpha設為ratio,然后各rgb除以ratio,這里是為了做漸變,否則變化太生硬。
上圖:
好了,上面兩張圖是一樣的。只是屏幕背景一個是白色,一個是黑色。圖片背景透明了。
總結
以上是生活随笔為你收集整理的cocos2d-x游戏开发(十四)用shader使图片背景透明的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。