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

ShaderSprite.h

  1. #ifndef __TestShader__ShaderSprite__
  2. #define __TestShader__ShaderSprite__
  3. #include "cocos2d.h"
  4. USING_NS_CC;
  5. class ShaderSprite : public CCSprite {
  6. public:
  7. static ShaderSprite* create(const char* pszFileName);
  8. virtual bool initWithTexture(CCTexture2D *pTexture, const CCRect& rect);
  9. virtual void draw(void);
  10. };
  11. #endif /* defined(__TestShader__ShaderSprite__) */

ShaderSprite.cpp

  1. #include "ShaderSprite.h"
  2. static CC_DLL const GLchar *transparentshader =
  3. #include "tansparentshader.h"
  4. ShaderSprite* ShaderSprite::create(const char *pszFileName)
  5. {
  6. ShaderSprite *pRet = new ShaderSprite();
  7. if (pRet && pRet->initWithFile(pszFileName)) {
  8. pRet->autorelease();
  9. return pRet;
  10. }
  11. else
  12. {
  13. delete pRet;
  14. pRet = NULL;
  15. return NULL;
  16. }
  17. }
  18. bool ShaderSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect)
  19. {
  20. do{
  21. //        CCLog("override initWithTexture!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  22. CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, rect));
  23. // 载入顶点着色器和片元着色器
  24. m_pShaderProgram = new  CCGLProgram();
  25. m_pShaderProgram ->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, transparentshader);
  26. CHECK_GL_ERROR_DEBUG();
  27. // 启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
  28. m_pShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
  29. m_pShaderProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
  30. m_pShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
  31. CHECK_GL_ERROR_DEBUG();
  32. // 自己定义着色器链接
  33. m_pShaderProgram->link();
  34. CHECK_GL_ERROR_DEBUG();
  35. // 设置移动、缩放、旋转矩阵
  36. m_pShaderProgram->updateUniforms();
  37. CHECK_GL_ERROR_DEBUG();
  38. return true;
  39. }while(0);
  40. return false;
  41. }
  42. void ShaderSprite::draw(void)
  43. {
  44. //    CCLog("override draw!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  45. CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
  46. CCAssert(!m_pobBatchNode, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");
  47. CC_NODE_DRAW_SETUP();
  48. //
  49. // 启用attributes变量输入,顶点坐标,纹理坐标,颜色
  50. //
  51. ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
  52. ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
  53. m_pShaderProgram->use();
  54. m_pShaderProgram->setUniformsForBuiltins();
  55. // 绑定纹理到纹理槽0
  56. ccGLBindTexture2D(m_pobTexture->getName());
  57. #define kQuadSize sizeof(m_sQuad.bl)
  58. long offset = (long)&m_sQuad;
  59. // vertex
  60. int diff = offsetof( ccV3F_C4B_T2F, vertices);
  61. glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
  62. // texCoods
  63. diff = offsetof( ccV3F_C4B_T2F, texCoords);
  64. glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
  65. // color
  66. diff = offsetof( ccV3F_C4B_T2F, colors);
  67. glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
  68. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  69. CHECK_GL_ERROR_DEBUG();
  70. CC_INCREMENT_GL_DRAWS(1);
  71. CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
  72. }

片段着色器代码

tansparentshader.h

  1. "                                                       \n\
  2. #ifdef GL_ES                                            \n\
  3. precision lowp float;                                   \n\
  4. #endif                                                  \n\
  5. varying vec4 v_fragmentColor;                           \n\
  6. varying vec2 v_texCoord;                                \n\
  7. uniform sampler2D u_texture;                            \n\
  8. void main()                                             \n\
  9. {                                                       \n\
  10. float ratio=0.0;                                    \n\
  11. vec4 texColor = texture2D(u_texture, v_texCoord);   \n\
  12. ratio = texColor[0] > texColor[1]?(texColor[0] > texColor[2] ? texColor[0] : texColor[2]) :(texColor[1] > texColor[2]? texColor[1] : texColor[2]);                                      \n\
  13. if (ratio != 0.0)                                          \n\
  14. {                                                          \n\
  15. texColor[0] = texColor[0] /  ratio;                    \n\
  16. texColor[1] = texColor[1] /  ratio;                    \n\
  17. texColor[2] = texColor[2] /  ratio;                    \n\
  18. texColor[3] = ratio;                                   \n\
  19. }                                                          \n\
  20. else                                                       \n\
  21. {                                                          \n\
  22. texColor[3] = 0.0;                                     \n\
  23. }                                                          \n\
  24. gl_FragColor = v_fragmentColor*texColor;                   \n\
  25. }";

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

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

上图:

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

cocos2d-x—使用shader使图片背景透明的更多相关文章

  1. 用shader使图片背景透明

    转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...

  2. 【转】cocos2d-x游戏开发(十四)用shader使图片背景透明

    转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...

  3. ps让图片背景透明

    效果图:  jpg=>png,背景透明 步骤: 1.选择橡皮工具的第三个  魔术橡皮 保存为png,    按住Ctrl+alt+shift+s    保存:

  4. Android 设置按钮背景透明与半透明_图片背景透明

    Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... />  透明 ...

  5. SVG图片背景透明

    今天在调整网页的时候,将logo以原有直接贴代码形式,改为加载文件. 其实真正的目的是做SEO.上次SEO交流后得出 结论:核心在于内容的本身的优化.信噪比很重要.也就是有效信息需要占文章的主要内容, ...

  6. Swift开发教程--怎样使UITableViewController背景透明

    self.tableView.backgroundView? .backgroundColor = UIColor.clearColor(); self.tableView.backgroundCol ...

  7. JS - 使 canvas 背景透明

    canvas = document.getElementById('canvas1'); var context = canvas.getContext('2d');context.fillStyle ...

  8. C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】

    http://blog.sina.com.cn/s/blog_402c071e0102x4rl.html    以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说, ...

  9. jquery 图片背景透明度(支持IE5/IE6/IE7)

    设置背景图片,以突出透明度的效果及jquery png背景透明插件实例教程 <head> <title>toggle()</title> <style typ ...

随机推荐

  1. python学习1(小白记录)

    python创建cocos2d-x项目注意点1. 2.7.5版本号的.配置好环境变量之后.要切换到tools文件夹下.直接运行 python create_project.py ..........这 ...

  2. 【转向Javascript系列】从setTimeout说事件循环模型

    本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/10/turning-to-javascript-series-from-settimeout ...

  3. Knockout应用开发指南 第八章:简单应用举例(2)

    原文:Knockout应用开发指南 第八章:简单应用举例(2) 5   Control types 这个例子,对view model没有什么特殊的展示,只是展示如何绑定到各种元素上(例如,select ...

  4. zTree实现地市县三级级联Service接口測试

    zTree实现地市县三级级联Service接口測试 ProvinceServiceTest.java: /** * @Title:ProvinceServiceTest.java * @Package ...

  5. XSS学习笔记(一个)-点击劫持

    所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...

  6. mysql sqlserver Oracle字符串连接

    mysql 例mysql> select CONCAT('My', 'S', 'QL'); sqlserver select name+'aa' from student; oracle sel ...

  7. MySQL保留关键字

    今天在使用hibernate关联映射导出表的时候因为映射了一个表名为option,是MYSQL的关键字,总是生成错误,一开始以为是映射文件和代码问题,检查不出问题才想到可能用到数据库的保留关键字了,查 ...

  8. [ios仿系列]仿支付宝手势解码

    呀~.这么快就转到ios阵营了???.android还有那么多坑呢???为此我也仅仅能啃着馒头留下屈辱的眼泪了. . 本次因为开发公司产品的android版,继而ios版也负责一部分.当中一部分就是手 ...

  9. 领域驱动设计(DDD)部分核心概念的个人理解(转)

    领域驱动设计(DDD)是一种基于模型驱动的软件设计方式.它以领域为核心,分析领域中的问题,通过建立一个领域模型来有效的解决领域中的核心的复杂问题.Eric Ivans为领域驱动设计提出了大量的最佳实践 ...

  10. as3文本框的动态拖拽和编辑

    如今非常多软件都支持了编辑界面的文本拖拽和点击编辑来直接改动数值, 这样便于操作, 并且体验性也好, 抛砖引玉吧 于是就用好久没编写的as3来写了一下: 由于用的flash ide写的没有提示, 就临 ...