位置: 编程技术 - 正文
推荐整理分享在cocos2dx 中添加自定义 shaders --- 改变 HSL 色调,饱和度, 亮度(cocos怎么用),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:cocos2dx怎么用,cocos2dx创建项目,cocos2d教程,cocos2dx创建项目,cocos怎么用,cocos2d怎么创建项目,cocos2dx怎么用,cocos2dx怎么用,内容如对您有帮助,希望把文章链接给更多的朋友!
订阅
步骤1
准备 shaders文件- ccShad_Hsl.h
将它放入E:HSluZiLongcocos2d-x-2.2.1cocos2d-x-2.2.1cocos2dxshaders 目录下步骤2.
在 ccShaders.h 中加入代码
extern CC_DLL const GLchar * ccPositionColorHSL_frag;extern CC_DLL const GLchar * ccPositionColorHSL_vert;
在 ccShaders.cpp 中加入代码
const GLchar * ccPositionColorHSL_frag =#include "ccShad_Hsl.h"
在 CCGLProgram.h 中定义
#define KCCShader_Position_hsl"KCCShader_Position_hsl"
在 CCShaderCache.cpp 中 追加枚举 kCCShaderType_Position_hsl,
在 CCShaderCache.cpp 的 reloadDefaultShaders中加入代码
p = programForKey(KCCShader_Position_hsl); p->reset(); loadDefaultShader(p, kCCShaderType_Position_hsl);
在 CCShaderCache.cpp 的 loadDefaultShader 中加入代码
case kCCShaderType_Position_hsl: p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionColorHSL_frag);p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); break;
在 CCShaderCache.cpp 的 loadDefaultShaders中加入代码
p = new CCGLProgram(); loadDefaultShader(p, kCCShaderType_Position_hsl); m_pPrograms->setObject(p, KCCShader_Position_hsl); p->release();
步骤3.
在 CCSprite 类中 追加 HSL 接口
[cpp] view plaincopyvoid initHSL(); void drawHSL(); void setHSL(float h, float s, float l); void setH(float h); void setS(float s); void setL(float l); float getH(void){ return m_color_h;} float getS(void){ return m_color_s;} float getL(void){ return m_color_l;} [cpp] view plaincopyvoid CCSprite::initHSL() { if(m_use_hsl == false) { m_use_hsl = true; setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(KCCShader_Position_hsl)); hLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddHue"); sLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddSat"); lLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddLig"); rLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddRed"); gLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddGreen"); bLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddBlue"); aLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddAlpha"); } } void CCSprite::drawHSL() { ccBlendFunc blend = getBlendFunc(); ccGLBlendFunc(blend.src, blend.dst); getShaderProgram()->use(); getShaderProgram()->setUniformsForBuiltins(); getShaderProgram()->setUniformLocationWith1f(hLocation, m_color_h); getShaderProgram()->setUniformLocationWith1f(sLocation, m_color_s); getShaderProgram()->setUniformLocationWith1f(lLocation, m_color_l); getShaderProgram()->setUniformLocationWith1f(rLocation, _displayedColor.r/.f); getShaderProgram()->setUniformLocationWith1f(gLocation, _displayedColor.g/.f); getShaderProgram()->setUniformLocationWith1f(bLocation, _displayedColor.b/.f); m_color_a = this->getOpacity() / .f; getShaderProgram()->setUniformLocationWith1f(aLocation, m_color_a); ccGLBindTexture2D( getTexture()->getName()); ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex ); #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); CC_INCREMENT_GL_DRAWS(1); } void CCSprite::setHSL(float h, float s, float l) { initHSL(); setH(h); setS(s); setL(l); } void CCSprite::setH(float h) { initHSL(); m_color_h = h; } void CCSprite::setS(float s) { initHSL(); m_color_s = s; } void CCSprite::setL(float l) { initHSL(); m_color_l = l; ccBlendFunc blend = getBlendFunc(); if(m_color_l > 0) blend.src = GL_SRC_ALPHA; else blend.src = GL_ONE; blend.dst = GL_ONE_MINUS_SRC_ALPHA; setBlendFunc(blend); }在 CCSprite.cpp 的 updateBlendFunc中追加代码
if(m_use_hsl){ccBlendFunc blend = getBlendFunc();if(m_color_l > 0)blend.src = GL_SRC_ALPHA;elseblend.src = GL_ONE;blend.dst = GL_ONE_MINUS_SRC_ALPHA;setBlendFunc(blend);}
在 CCSprite.cpp 的 setTexture中追加代码
if(this->m_use_hsl){m_use_hsl = false;initHSL();}
在 CCSprite.cpp 的 draw 开头插入代码
if(m_use_hsl){drawHSL();return;}
步骤5.调用接口
[cpp] view plaincopy{ CCSprite* pSprite = CCSprite::create("Icon.png"); // -1 ~ 1 0无变化 pSprite->setS(-1); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 origin.x, visibleSize.height/2 origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); }-- ::| 分类: cocos2dx|举报|字号 订阅
步骤1.
准备 shaders文件- ccShad_Hsl.h
将它放入E:HSluZiLongcocos2d-x-2.2.1cocos2d-x-2.2.1cocos2dxshaders 目录下步骤2.
在 ccShaders.h 中加入代码
extern CC_DLL const GLchar * ccPositionColorHSL_frag;extern CC_DLL const GLchar * ccPositionColorHSL_vert;
在 ccShaders.cpp 中加入代码
const GLchar * ccPositionColorHSL_frag =#include "ccShad_Hsl.h"
在 CCGLProgram.h 中定义
#define KCCShader_Position_hsl"KCCShader_Position_hsl"
在 CCShaderCache.cpp 中 追加枚举 kCCShaderType_Position_hsl,
在 CCShaderCache.cpp 的 reloadDefaultShaders中加入代码
p = programForKey(KCCShader_Position_hsl); p->reset(); loadDefaultShader(p, kCCShaderType_Position_hsl);
在 CCShaderCache.cpp 的 loadDefaultShader 中加入代码
case kCCShaderType_Position_hsl: p->initWithVertexShaderByteArray(ccPositionTextureColor_vert, ccPositionColorHSL_frag);p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);p->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); break;
在 CCShaderCache.cpp 的 loadDefaultShaders中加入代码
p = new CCGLProgram(); loadDefaultShader(p, kCCShaderType_Position_hsl); m_pPrograms->setObject(p, KCCShader_Position_hsl); p->release();
步骤3.
在 CCSprite 类中 追加 HSL 接口
[cpp] view plaincopyvoid initHSL(); void drawHSL(); void setHSL(float h, float s, float l); void setH(float h); void setS(float s); void setL(float l); float getH(void){ return m_color_h;} float getS(void){ return m_color_s;} float getL(void){ return m_color_l;} [cpp] view plaincopyvoid CCSprite::initHSL() { if(m_use_hsl == false) { m_use_hsl = true; setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(KCCShader_Position_hsl)); hLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddHue"); sLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddSat"); lLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddLig"); rLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddRed"); gLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddGreen"); bLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddBlue"); aLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "AddAlpha"); } } void CCSprite::drawHSL() { ccBlendFunc blend = getBlendFunc(); ccGLBlendFunc(blend.src, blend.dst); getShaderProgram()->use(); getShaderProgram()->setUniformsForBuiltins(); getShaderProgram()->setUniformLocationWith1f(hLocation, m_color_h); getShaderProgram()->setUniformLocationWith1f(sLocation, m_color_s); getShaderProgram()->setUniformLocationWith1f(lLocation, m_color_l); getShaderProgram()->setUniformLocationWith1f(rLocation, _displayedColor.r/.f); getShaderProgram()->setUniformLocationWith1f(gLocation, _displayedColor.g/.f); getShaderProgram()->setUniformLocationWith1f(bLocation, _displayedColor.b/.f); m_color_a = this->getOpacity() / .f; getShaderProgram()->setUniformLocationWith1f(aLocation, m_color_a); ccGLBindTexture2D( getTexture()->getName()); ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex ); #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); CC_INCREMENT_GL_DRAWS(1); } void CCSprite::setHSL(float h, float s, float l) { initHSL(); setH(h); setS(s); setL(l); } void CCSprite::setH(float h) { initHSL(); m_color_h = h; } void CCSprite::setS(float s) { initHSL(); m_color_s = s; } void CCSprite::setL(float l) { initHSL(); m_color_l = l; ccBlendFunc blend = getBlendFunc(); if(m_color_l > 0) blend.src = GL_SRC_ALPHA; else blend.src = GL_ONE; blend.dst = GL_ONE_MINUS_SRC_ALPHA; setBlendFunc(blend); }在 CCSprite.cpp 的 updateBlendFunc中追加代码
if(m_use_hsl){ccBlendFunc blend = getBlendFunc();if(m_color_l > 0)blend.src = GL_SRC_ALPHA;elseblend.src = GL_ONE;blend.dst = GL_ONE_MINUS_SRC_ALPHA;setBlendFunc(blend);}
在 CCSprite.cpp 的 setTexture中追加代码
if(this->m_use_hsl){m_use_hsl = false;initHSL();}
在 CCSprite.cpp 的 draw 开头插入代码
if(m_use_hsl){drawHSL();return;}
步骤5.调用接口
[cpp] view plaincopy{ CCSprite* pSprite = CCSprite::create("Icon.png"); // -1 ~ 1 0无变化 pSprite->setS(-1); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 origin.x, visibleSize.height/2 origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); }Cocos2dx的截屏与遮罩 Cocos2dx3.2截取精灵部分;boolHelloWorld::init(){//////////////////////////////if(!Layer::init()){returnfalse;}SizevisibleSize=Director::getInstance()-getVisibleSize();Vec2origin=Director::getInst
cocos2dx菜单重叠按钮实现按层优先选择及透明过滤 众所周知,在cocos2d-x中,通过CCMenu(抱歉,笔者至今任然在用2.2.2的版本)创建的菜单,如果遇到数个按钮有重叠,当用户点击重叠区域(包括PNG图标中
cocos2dx中滚动界面和小滚动条同步的实现 在游戏中,经常会遇到滚动界面,或者滚动列表,在滚动时,为了给用户一个更好的体验,我们会想到在滚动界面的旁边加上一个小小的滚动条,让这
标签: cocos怎么用
本文链接地址:https://www.jiuchutong.com/biancheng/368960.html 转载请保留说明!上一篇:Cocos2dx CCSprite CCSpriteFrame CCTexture2D CCAnimation学习总结
友情链接: 武汉网站建设