cocos2d-x—使用shader使图片背景透明
这里用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;
- }while(0);
- 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, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
- // texCoods
- diff = offsetof( ccV3F_C4B_T2F, texCoords);
- glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
- // color
- 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
- " \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使图片背景透明的更多相关文章
- 用shader使图片背景透明
转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...
- 【转】cocos2d-x游戏开发(十四)用shader使图片背景透明
转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...
- ps让图片背景透明
效果图: jpg=>png,背景透明 步骤: 1.选择橡皮工具的第三个 魔术橡皮 保存为png, 按住Ctrl+alt+shift+s 保存:
- Android 设置按钮背景透明与半透明_图片背景透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明 ...
- SVG图片背景透明
今天在调整网页的时候,将logo以原有直接贴代码形式,改为加载文件. 其实真正的目的是做SEO.上次SEO交流后得出 结论:核心在于内容的本身的优化.信噪比很重要.也就是有效信息需要占文章的主要内容, ...
- Swift开发教程--怎样使UITableViewController背景透明
self.tableView.backgroundView? .backgroundColor = UIColor.clearColor(); self.tableView.backgroundCol ...
- JS - 使 canvas 背景透明
canvas = document.getElementById('canvas1'); var context = canvas.getContext('2d');context.fillStyle ...
- C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】
http://blog.sina.com.cn/s/blog_402c071e0102x4rl.html 以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说, ...
- jquery 图片背景透明度(支持IE5/IE6/IE7)
设置背景图片,以突出透明度的效果及jquery png背景透明插件实例教程 <head> <title>toggle()</title> <style typ ...
随机推荐
- python学习1(小白记录)
python创建cocos2d-x项目注意点1. 2.7.5版本号的.配置好环境变量之后.要切换到tools文件夹下.直接运行 python create_project.py ..........这 ...
- 【转向Javascript系列】从setTimeout说事件循环模型
本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/10/turning-to-javascript-series-from-settimeout ...
- Knockout应用开发指南 第八章:简单应用举例(2)
原文:Knockout应用开发指南 第八章:简单应用举例(2) 5 Control types 这个例子,对view model没有什么特殊的展示,只是展示如何绑定到各种元素上(例如,select ...
- zTree实现地市县三级级联Service接口測试
zTree实现地市县三级级联Service接口測试 ProvinceServiceTest.java: /** * @Title:ProvinceServiceTest.java * @Package ...
- XSS学习笔记(一个)-点击劫持
所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...
- mysql sqlserver Oracle字符串连接
mysql 例mysql> select CONCAT('My', 'S', 'QL'); sqlserver select name+'aa' from student; oracle sel ...
- MySQL保留关键字
今天在使用hibernate关联映射导出表的时候因为映射了一个表名为option,是MYSQL的关键字,总是生成错误,一开始以为是映射文件和代码问题,检查不出问题才想到可能用到数据库的保留关键字了,查 ...
- [ios仿系列]仿支付宝手势解码
呀~.这么快就转到ios阵营了???.android还有那么多坑呢???为此我也仅仅能啃着馒头留下屈辱的眼泪了. . 本次因为开发公司产品的android版,继而ios版也负责一部分.当中一部分就是手 ...
- 领域驱动设计(DDD)部分核心概念的个人理解(转)
领域驱动设计(DDD)是一种基于模型驱动的软件设计方式.它以领域为核心,分析领域中的问题,通过建立一个领域模型来有效的解决领域中的核心的复杂问题.Eric Ivans为领域驱动设计提出了大量的最佳实践 ...
- as3文本框的动态拖拽和编辑
如今非常多软件都支持了编辑界面的文本拖拽和点击编辑来直接改动数值, 这样便于操作, 并且体验性也好, 抛砖引玉吧 于是就用好久没编写的as3来写了一下: 由于用的flash ide写的没有提示, 就临 ...