Cocos2D结合CoreGraphics实现RPG人物中空黑洞吸入效果

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)


之前的博文中我们实现了RPG人物的复古效果.

现在我们再完点high的,我们准备实现这么一种效果:

人物从中心点开始形成一个空洞,洞的边缘产生一种吸入变形效果.

有了上一篇的铺垫,我们可以很快搞定它.

首先看一下Apple对其CIHoleDistortion滤镜的说明:

Creates a circular area that pushes the image pixels outward, distorting those pixels closest to the circle the most.

Localized Display Name
Hole Distortion

inputImage  
A CIImage object whose display name is Image.
inputCenter 
A CIVector object whose attribute type is CIAttributeTypePosition and whose display name is Center.

Default value: [150 150]
inputRadius 
An NSNumber object whose attribute type is CIAttributeTypeDistance and whose display name is Radius.

Default value: 150.00

以上是滤镜对应的3个参数,很简单:

第一个是输入图片,第二个是黑洞的中心点,最后一个是黑洞的半径.

官方网站还给出了应该显示的效果:

这里写图片描述

好了,下面我们把它放到游戏中去:

//将CGImage转换为CIImage
    CIImage *ciImage = [CIImage imageWithCGImage:_image.CGImage];
    //用过滤器生成新的CIImage
    CIFilter *filter = [CIFilter filterWithName:@"CIHoleDistortion"];
    CIVector *vector = [CIVector vectorWithX:sz.width*_image.scale/2
                                           Y:sz.height*_image.scale/2];
    [filter setValue:ciImage forKey:@"inputImage"];
    [filter setValue:vector forKey:@"inputCenter"];
    [filter setValue:@(10.0) forKey:@"inputRadius"];
    CIImage *outputImage = [filter outputImage];

以上我们选择RPG人物的中心点为黑洞的中心,且黑洞的半径为10.

编译运行游戏,效果如下:

这里写图片描述

放大一点看一下效果:

这里写图片描述

可以看到上图中的狗狗的肚子里出现了背景草地上一朵小花.

一般来说要想实现该效果,需要自己写OpenGL ES的端点和片段着色器,但是我们通过Cocoa提供的滤镜,避免了较底层的方法,而且实现起来非常简单.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值