Cocos2d combined with Coregraphics to realize the inhalation effect of RPG characters hollow black hole

Source: Internet
Author: User

Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)

In the previous blog post we realized the retro effect of RPG characters.

Now that we're done with the high, we're going to do something like this:

The character starts from the center point to form an empty hole, the edge of which produces an inhalation deformation effect.

With the foreshadowing of the last article, we can fix it quickly.

First look at Apple's description of its ciholedistortion filter:

Creates a circular area thatPushes theImage pixels outward, distorting those pixels closest to  theCircle theMost. Localized Display namehole distortioninputimage A ciimage ObjectwhoseDisplayname  isImage.inputcenter A Civector ObjectwhoseAttribute type isCiattributetypeposition and whoseDisplayname  isCenter.default value: [ Max  Max]inputradius an NSNumber objectwhoseAttribute type isCiattributetypedistance and whoseDisplayname  isRadius.default Value:150.00

These are the 3 parameters for the filter, which is simple:

The first one is the input image, the second is the center point of the black hole, and the last is the radius of the black hole.

The official website also gives the effect that should be shown:

Well, let's put it in the game:

//convert cgimage to ciimage ciimage *ciimage = [Ciimage imagewithcgimage:_image. Cgimage]; Generate a new ciimage cifilter *filter = [cifilter filterwithname:@] ; Civector *vector = [civector vectorwithx:sz.width*_image.scale/2  Y:sz.height*_image.scale/2 ]; [Filter set  value:ciimage for  key:@ "Inputimage" ]; [Filter set  value:vector for  key:@" Inputcenter "]; [Filter set  value:@ (10.0 ) for  Key:@ "Inputradius" ]; Ciimage *outputimage = [Filter outputimage]; 

Above we select the center point of the RPG character as the center of the black hole, and the radius of the black hole is 10.

Compile and run the game with the following effects:

Zoom in a bit to see the effect:

You can see a small flower on the background grass in the dog's belly.

In general, to achieve this effect, you need to write your own OpenGL ES endpoint and fragment shader, but we have the filter provided by cocoa to avoid the lower-level method, and implementation is very simple.

Cocos2d combined with Coregraphics to realize the inhalation effect of RPG characters hollow black hole

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.