Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x游戏开发(十四)用shader使图片背景透明大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

欢迎转载,地址:http://blog.csdn.net/fylz1125/article/details/8631783

好吧,终于抽时间写这篇文章了。

手头上有很多人物行走图,技能特效图等,但这些图都有个纯黑色背景,怎么样将内容显示出来,让背景透明呢?前段时间搞了一下,感谢群里的童鞋们,提供了思路和方法。


这里用shader处理了像素,使黑色背景透明,直接上代码

ShaderSprite.h

  1. #ifndef__TestShader__ShaderSprite__
  2. #define__TestShader__ShaderSprite__
  3. #include"cocos2d.h"
  4. USING_NS_Cc;
  5. classShaderSprite:publicCCSprite{
  6. public:
  7. staticShaderSprite*create(constchar*pszFileName);
  8. virtualboolinitWithTexture(CCTexture2D*pTexture,constCCRect&rect);
  9. virtualvoiddraw(void);
  10. };
  11. #endif/*defined(__TestShader__ShaderSprite__)*/

ShaderSprite.cpp

?
    #include"ShaderSprite.h"
  1. staticCC_DLLconstGLchar*transparentshader=
  2. #include"tansparentshader.h"
  3. ShaderSprite*ShaderSprite::create(constchar*pszFileName)
  4. {
  5. ShaderSprite*pRet=newShaderSprite();
  6. if(pRet&&pRet->initWithFile(pszFileName)){
  7. pRet->autorelease();
  8. returnpRet;
  9. }
  10. else
  11. {
  12. deletepRet;
  13. pRet=NULL;
  14. returnNULL;
  15. }
  16. boolShaderSprite::initWithTexture(CCTexture2D*pTexture,constCCRect&rect)
  17. do{
  18. //CCLog("overrideinitWithTexture!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  19. CC_BREAK_IF(!CCSprite::initWithTexture(pTexture,rect));
  20. //加载顶点着色器和片元着色器
  21. @H_193_42@m_pShaderProgram=newCCGLProgram();
  22. @H_193_42@m_pShaderProgram->initWithVertexShaderByteArray(ccPositiontextureA8Color_vert,transparentshader);
  23. checK_GL_ERROR_DEBUG();
  24. //启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
  25. @H_193_42@m_pShaderProgram->addAttribute(kCCAttributenamePosition,kCCVertexAttrib_Position);
  26. @H_193_42@m_pShaderProgram->addAttribute(kCCAttributenameColor,kCCVertexAttrib_Color);
  27. @H_193_42@m_pShaderProgram->addAttribute(kCCAttributenameTexCoord,kCCVertexAttrib_TexCoords);
  28. checK_GL_ERROR_DEBUG();
  29. //自定义着色器链接
  30. @H_193_42@m_pShaderProgram->link();
  31. //设置移动、缩放、旋转矩阵
  32. @H_193_42@m_pShaderProgram->updateUniforms();
  33. returntrue;
  34. }while(0);
  35. returnfalse;
  36. voidShaderSprite::draw(void)
  37. //CCLog("overridedraw!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  38. CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite,"CCSprite-draw");
  39. CCassert(!m_pobBatchNode,"IfCCSpriteisbeingrenderedbyCCSpriteBatchNode,CCSprite#drawSHOULDNOTbecalled");
  40. CC_NODE_DRAW_SETUP();
  41. //
  42. //启用attributes变量输入,顶点坐标,纹理坐标,颜色
  43. ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTeX);
  44. ccGLBlendFunc(m_sBlendFunc.src,m_sBlendFunc.dst);
  45. @H_193_42@m_pShaderProgram->use();
  46. @H_193_42@m_pShaderProgram->setUniformsForBuilTins();
  47. //绑定纹理到纹理槽0
  48. ccGLBindTexture2D(m_pobTexture->getName());
  49. #definekQuadSizesizeof(m_sQuad.bl)
  50. longoffset=(long)&m_sQuad;
  51. //vertex
  52. intdiff=offsetof(ccV3F_C4B_T2F,vertices);
  53. glVertexAttribPointer(kCCVertexAttrib_Position,3,GL_FLOAT,GL_falSE,kQuadSize,(void*)(offset+diff));
  54. //texCoods
  55. diff=offsetof(ccV3F_C4B_T2F,texCoords);
  56. glVertexAttribPointer(kCCVertexAttrib_TexCoords,2,0); BACkground-color:inherit">//color
  57. :rgb(153,colors);
  58. glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,GL_TRUE,153); list-style:decimal-leading-zero outside; color:inherit; line-height:20px; margin:0px!important; padding:0px 3px 0px 10px!important"> glDrawArrays(GL_TRIANGLE_StriP,4);
  59. CC_INCREMENT_GL_DRAWS(1);
  60. CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite,"CCSprite-draw");
  61. }

片段着色器代码

tansparentshader.h

?
    "\n\
  1. #ifdefGL_ES\n\
  2. precisionlowpfloat;\n\
  3. #endif\n\
  4. varyingvec4v_fragmentColor;\n\
  5. varyingvec2v_texCoord;\n\
  6. uniformsampler2Du_texture;\n\
  7. void@H_495_45@main()\n\
  8. {\n\
  9. floatratio=0.0;\n\
  10. vec4texColor=texture2D(u_texture,v_texCoord);\n\
  11. ratio=texColor[0]>texColor[1]?(texColor[0]>texColor[2]?texColor[0]:texColor[2]):(texColor[1]>texColor[2]?texColor[1]:texColor[2]);\n\
  12. if(ratio!=0.0)\n\
  13. {\n\
  14. texColor[0]=texColor[0]/ratio;\n\
  15. texColor[1]=texColor[1]/ratio;\n\
  16. texColor[2]=texColor[2]/ratio;\n\
  17. texColor[3]=ratio;\n\
  18. }\n\
  19. else\n\
  20. texColor[3]=0.0;\n\
  21. gl_FragColor=v_fragmentColor*texColor;\n\
  22. }";

注意shader编程没有隐士数据类型转换,所以都显示为float了。

然后ratio是指在rgb中找最大的,如果ratio为0直接将alpha设为0,否则alpha设为ratio,然后各rgb除以ratio,这里是为了做渐变,否则变化太生硬。

上图:

好了,上面两张图是一样的。只是屏幕背景一个是白色,一个是黑色。图片背景透明了。

大佬总结

以上是大佬教程为你收集整理的cocos2d-x游戏开发(十四)用shader使图片背景透明全部内容,希望文章能够帮你解决cocos2d-x游戏开发(十四)用shader使图片背景透明所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:14cocoscocos2ddshaderx十四图片开发游戏背景透明
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap