cocos2d-x 手电筒效果

常见的手电筒效果,可以通过CCRenderTexture来实现。主要是通过修改渲染表面的alpha值来达到手电筒光照范围内的透明效果。此方法纯原创,如有雷同,英雄所见略同。这里由于本人没有什么图片,这里直接用矩形区域来代替圆形区域。通过以下几个基本步骤来完成这个效果。

  1. 首先创建一个全黑的渲染表明覆盖在场景之上。
        //create render target
        CCRenderTexture* pRenderTexture = CCRenderTexture::create( BY_WIN_SIZE_WIDTH, BY_WIN_SIZE_HEIGHT );
        
        //render the target to black
        pRenderTexture->begin();
        glDisable( GL_BLEND );
        ccDrawSolidRect( ccp( 0, 0 ), ccp( BY_WIN_SIZE_WIDTH, BY_WIN_SIZE_HEIGHT ), ccc4f( 0, 0, 0, 1 ) );
        glEnable( GL_BLEND );
        pRenderTexture->end();
        
        //set the render target over the scene
        pRenderTexture->setPosition( ccp( BY_WIN_SIZE_WIDTH_HALF, BY_WIN_SIZE_HEIGHT_HALF ) );
        addChild( pRenderTexture, 2, 1000 );
        
        //render target is the sprite's texture.
        //set the sprite render parms.
        ccBlendFunc bf = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
        pRenderTexture->getSprite()->setBlendFunc( bf );
    
  2. 在ccTouchBegan()函数中透明处用户点击的区域
        //get transparent area
        CCPoint ptBottomLeftCorner = ccpSub( pTouch->getLocation(), ccp( 200, 200 ) );
        CCPoint ptTopRightCorner = ccpAdd( pTouch->getLocation(), ccp( 200, 200 ) );
        
        //set the area transparent
        BY_GET_CHILD( CCRenderTexture, 1000 )->begin();
        glDisable( GL_BLEND );
        ccDrawSolidRect( ptBottomLeftCorner, ptTopRightCorner, ccc4f( 0, 0, 0, 0 ) );
        glEnable( GL_BLEND );
        BY_GET_CHILD( CCRenderTexture, 1000 )->end();
  3. 在ccTouchMoved()函数中,首先抹黑原来的区域(这里为了保险,我们直接抹黑整个渲染表面),另外透明出新的区域
        //get transparent area
        CCPoint ptBottomLeftCorner = ccpSub( pTouch->getLocation(), ccp( 200, 200 ) );
        CCPoint ptTopRightCorner = ccpAdd( pTouch->getLocation(), ccp( 200, 200 ) );
        
        BY_GET_CHILD( CCRenderTexture, 1000 )->begin();
        glDisable( GL_BLEND );
        
        //set the render target black
        ccDrawSolidRect( CCPointZero, ccp( BY_WIN_SIZE_WIDTH, BY_WIN_SIZE_HEIGHT ), ccc4f( 0, 0, 0, 1 ) );
        //set the new area transparent
        ccDrawSolidRect( ptBottomLeftCorner, ptTopRightCorner, ccc4f( 0, 0, 0, 0 ) );
        
        glEnable( GL_BLEND );
        BY_GET_CHILD( CCRenderTexture, 1000 )->end();
    
  4. 在ccTouchEnded()和ccTouchCanceled()函数中,抹黑整块渲染表面。
        BY_GET_CHILD( CCRenderTexture, 1000 )->begin();
        glDisable( GL_BLEND );
        ccDrawSolidRect( CCPointZero, ccp( BY_WIN_SIZE_WIDTH, BY_WIN_SIZE_HEIGHT ), ccc4f( 0, 0, 0, 1 ) );
        glEnable( GL_BLEND );
        BY_GET_CHILD( CCRenderTexture, 1000 )->end();

好了,我们实现了这个“矩形"的手电筒效果。最好使用png图片来弄出手电筒光圈和透明区域来。

另外:其中BY开头的宏是我个人写的一些宏,以便本人加快编程速度。你可以通过字面意思猜到我的宏实现方法,呵呵。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值