参考学长的项目,记录一下刀光效果的实现。
bool KongJianLayer::init() { //调用父类的初始化 if ( !Layer::init() ) { return false; } Vec2 origin=Director::getInstance()->getVisibleOrigin(); Size visibleSize=Director::getInstance()->getVisibleSize(); Sprite *title = Sprite::create("gameTitle2.png"); //初始化游戏标题 ClippingNode *cliper=ClippingNode::create(title); Sprite* stencil = title; //设置剪裁模板为自 Sprite* spark = Sprite::create("titleBlend.png");//初始化亮片 spark->setPosition(-title->getContentSize().width,0); //设置亮片的位置 BlendFunc cbl = {GL_DST_COLOR,GL_ONE}; //定义混合类型 spark->setBlendFunc(cbl); //设置亮片混合 cliper->setAlphaThreshold(0.5f); //设置剪裁的透明度为0.5 cliper->setStencil(stencil); //设置剪裁模板 cliper->addChild(spark); //添加亮片 this->addChild(cliper,10); //添加剪裁到布景中 cliper->setPosition(Point(origin.x+visibleSize.width/2,origin.y+visibleSize.height-200)); MoveTo* moveTo = MoveTo::create(1.5f,Point(title->getContentSize().width,0));<span style="white-space:pre"> </span>//移动动画 Place* moveBack = Place::create(Point(-title->getContentSize().width,0)); <span style="white-space:pre"> </span>//瞬时回到某点 Sequence* seq = Sequence::create(moveTo,moveBack,DelayTime::create(1.0f),nullptr);<span style="white-space:pre"> </span>//顺序动作 RepeatForever* action = RepeatForever::create(seq); //重复执行的动作 spark->runAction(action); //亮片执行动作a return true; }
此处涉及到剪裁模板概念,以及OpenGL里边的混合模式,下面列出OpenGL中常用的混合模式以及概念。
常量 |
RGB混合因子 |
Alpha混合因子 |
GL_ZERO |
(0,0) |
0 |
GL_ONE |
(1,1,1) |
1 |
GL_SRC_COLOR |
(Rs,Rs,Rs) |
As |
GL_ONE_MINUS_SRC_COLOR |
(1,1)- (Rs,Rs) |
1-As |
GL_DST_COLOR |
(Rd,Rd,Rd) |
Ad |
GL_ONE_MINUS_DST_COLOR |
(1,1)- (Rd,Rd) |
1-Ad |
GL_SRC_ALPHA |
(As,As,As) |
As |
GL_ONE_MINUS_SRC_ALPHA |
(1,1)- (As,As) |
1- As |
GL_DST_ALPHA |
(Ad,Ad,Ad) |
Ad |
GL_ONE_MINUS_DST_ALPHA |
(1,1)- (Ad,Ad) |
1-Ad |
GL_SRC_ALPHA_SATURATE |
(f,f,f):f=min(As,1-Ad) |
1 |
注:s:源颜色。d:目标颜色。
原文链接:https://www.f2er.com/cocos2dx/341126.html