转自:http://blog.csdn.net/dawn_moon/article/details/8631783

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

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

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

ShaderSprite.h

 #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

 #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{
 //        CCLog("override initWithTexture!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
         CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, rect));

         // 加载顶点着色器和片元着色器
         m_pShaderProgram = new  CCGLProgram();
         m_pShaderProgram ->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, transparentshader);

         CHECK_GL_ERROR_DEBUG();

         // 启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
         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;

     });
     return false;
 }

 void ShaderSprite::draw(void)
 {
 //    CCLog("override draw!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     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();

     //
     // 启用attributes变量输入,顶点坐标,纹理坐标,颜色
     //
     ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
     ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);

     m_pShaderProgram->use();
     m_pShaderProgram->setUniformsForBuiltins();

     // 绑定纹理到纹理槽0
     ccGLBindTexture2D(m_pobTexture->getName());

 #define kQuadSize sizeof(m_sQuad.bl)
     long offset = (long)&m_sQuad;

     // vertex
     int diff = offsetof( ccV3F_C4B_T2F, vertices);
     glVertexAttribPointer(kCCVertexAttrib_Position, , GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));

     // texCoods
     diff = offsetof( ccV3F_C4B_T2F, texCoords);
     glVertexAttribPointer(kCCVertexAttrib_TexCoords, , GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));

     // color
     diff = offsetof( ccV3F_C4B_T2F, colors);
     glVertexAttribPointer(kCCVertexAttrib_Color, , GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));

     glDrawArrays(GL_TRIANGLE_STRIP, , );

     CHECK_GL_ERROR_DEBUG();

     CC_INCREMENT_GL_DRAWS();
     CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");
 }

片段着色器代码

tansparentshader.h

 "                                                       \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[] > texColor[]?(texColor[] > texColor[] ? texColor[] : texColor[]) :(texColor[] > texColor[]? texColor[] : texColor[]);                                      \n\
 if (ratio != 0.0)                                          \n\
 {                                                          \n\
     texColor[] = texColor[] /  ratio;                    \n\
     texColor[] = texColor[] /  ratio;                    \n\
     texColor[] = texColor[] /  ratio;                    \n\
     texColor[] = ratio;                                   \n\
 }                                                          \n\
 else                                                       \n\
 {                                                          \n\
     texColor[] = 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使图片背景透明的更多相关文章

  1. iOS cocos2d 2游戏开发实战(第3版)书评

    2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...

  2. STC8H开发(十四): I2C驱动RX8025T高精度实时时钟芯片

    目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...

  3. python运维开发(十四)----HTML基本操作

    内容目录: HTML概述 head标签 body中常用标签 css选择器 css常用属性 HTML HTML概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言) ...

  4. Java微信公众平台开发(十四)【番外篇】--微信web开发者工具使用

    转自:http://www.cuiyongzhi.com/post/58.html 为帮助开发者更方便.更安全地开发和调试基于微信的网页,微信推出了 web 开发者工具.它是一个桌面应用,通过模拟微信 ...

  5. cocos2d-x游戏开发(十五)游戏加载动画loading界面

    个人原创,欢迎转载:http://blog.csdn.net/dawn_moon/article/details/11478885 这个资源加载的loading界面demo是在玩客网做逆转三国的时候随 ...

  6. cocos2d-x游戏开发(十五)游戏载入动画loading界面

    这个资源载入的loading界面demo是在玩客网做逆转三国的时候随手写的,尽管我在那仅仅待了2个礼拜.可是也算參与了一个商业游戏项目了,学到不少东西.当时使用的cocos2d-x还是1.0版的,我用 ...

  7. cocos2d-x游戏开发(十六)帧动画

    欢迎转载:http://blog.csdn.net/dawn_moon/article/details/11775745 本来想写一下帧动画的,搜了一下网上好像有一大把,就懒得写了,直接贴代码. // ...

  8. cocos2d-x游戏开发 跑酷(四) 关联与物理世界

    原创.转载注明出处http://blog.csdn.net/dawn_moon/article/details/21451077 前面一节尽管实现了一个跑动的人物,可是他只不过一个精灵在运行一个跑动的 ...

  9. unity3D游戏开发十八之NGUI动画

    我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...

随机推荐

  1. 一、mysql使用入门

    mysql -h localhost -u root -p123456 登录mysql服务器 show databases 列出所拥有的数据库 use www 选择一个www的数据库 show tab ...

  2. C# asp Aspose.Cells 教程,包含增加勾选框,单元格,字体设置

    1,引用Aspose.Cells  dll 2,using Aspose.Cells; 3, Workbook excel = new Workbook(); string strFilePath = ...

  3. GNU_makefile_template

    #g++ compiler: options # -std=c++0x enables ISO C++ 11 standard # -I.. pulls in the Version_test.h f ...

  4. Unity3d本地存储

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49386507 (一)简单数据存储PlayerPrefs 这种存储方法比较简单直接上代码 ...

  5. 类的本质、description方法、SEL、NSLog输出增强

    一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...

  6. Shiro 缓存失效以后的一个问题

    shiro 1.2.2和1.2.3 为shiro设置了缓存,但是当服务器运行几个小时后,页面判断 <shiro:hasPermission name="admin"> ...

  7. GPS导航仪常见术语解释

    摘自百度百科: 坐标(coordinate) 有2维.3维两种坐标表示,当GPS能够收到4颗及以上卫星的信号时,它能计算出本地的3维坐标:经度.纬度.高度,若只能收到3颗卫星的信号,它只能计算出2维坐 ...

  8. Comet、SSE、Web Socket

    来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十一) Comet Comet是一种更加高级的Ajax技术("服务器推送&qu ...

  9. [转载]c# 多线程一个带多个参数的方法

    比如我要线程一个private void subPing(int pre,int end) 我在Thread t=之后应该如何写 用匿名委托吧!那么简单为什么要这样写!t = new Thread(d ...

  10. PAT-乙级-1012. 数字分类 (20)

    1012. 数字分类 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一系列正整数,请按要求对数字进 ...