Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to progressively blur a SKSpriteNode's image using Sprite Kit?

Can someone provide an example of how to progressively blur a SKSpriteNode's image using Apple's Sprite Kit? For instance, let's say the user touches a button on the screen which will then trigger the background to slowly (i.e. progressively) blur until it reaches a specific threshold. Ideally, I would like to reverse the process too (e.g. allow the user to unblur the image by touching the same button).

like image 224
blueicecream Avatar asked Nov 01 '13 15:11

blueicecream


People also ask

Is there a way to UN-Blur a sprite?

Google Kawase blur and Gaussian blur shaders for some reference. I've considered it, but discarded it, as there's times when I would want to un-blur it. A solution I've thought is to have every sprite imported blurred and unblurred, and fade between them. But this would mean to double the size of all the sprites.

What is SpriteKit used for?

SpriteKit. The SpriteKit framework makes it easy to create high-performance, battery-efficient 2D games. With support for custom OpenGL ES shaders and lighting, integration with SceneKit, and advanced new physics effects and animations, you can add force fields, detect collisions, and generate new lighting effects in your games.

Why do my sprite borders look so ugly?

The sprite is blurred in the center, but the borders keep the hard edges that they had when not blurred, so it looks ugly. So still not a good solution for the problem... but the borders keep the hard edges that they had when not blurred, so it looks ugly.

Does depth of field FX work with sprites?

They just blur the color which makes the output alpha the same as the input alpha, thus the hard edges. I've actually tried depth of field fx, but couldn't make it to work with sprites. Even tried some asset store depth of field shader and they don't work with sprites, not sure why.


1 Answers

There are two possible paths to take on this, both use SKEffectNodes

SKEffectNodes allow you to apply CI Filters to a node.

There is a CI Filter for Gaussian Blur. So Create a SKEffectNode, and assign it a blur filter, then add the button as a child.

How do you animate it?

Use SKAction to create a custom action, and change the parameters of filter, however, this can be slow and doesn't always give the 'progressive' blur effect you might expect, so what I do is this:

I create a filter and SKEffectNode like described above, then I render the result to a Texture, using SKView.textureForNode. I then add the resulting texture to an array, after that I loop this, continuinng to apply the blur effect on top of the previous image created, until I have a set number of frames. Then use the textures created to make an animation with SKAction.animateWithTextures. In my experience, this comes out very nicely.

like image 84
AwDogsGo2Heaven Avatar answered Nov 12 '22 08:11

AwDogsGo2Heaven