Hi. For the past few days, I have been trying to create an SKShapeNode (a rectangle) that will be used as a menu screen moving down from the outside of the screen when a button is tapped. Everything behind the node should be blurred.
Something to this effect: https://stackoverflow.com/questions/49142867/spritekit-blurred-background-sknode
Alternatively, I may have everything in the background blurred with the exception of the SKShapeNode and its children.
Using this:
let blur = CIFilter(name:"CIGaussianBlur",withInputParameters: ["inputRadius": 10.0])
self.filter = blur
self.shouldRasterize = true
self.shouldEnableEffects = true
menu.run(moveMenuDown)
where self is the scene, blurs the background and the SKShapeNode. Besides, it is an awful hit on FPS.
I tried the following:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view!.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view!.addSubview(blurEffectView)
This is very fast, but again this blurs everything including the SKShapeNode, which is not intended.
Is there anyone who did something like that?
Thanks a lot!
moontiger is on the right track, however, it is absolutely possible to blur only a portion of your SpriteKit scene, here is how:
1. Set up a node (contentNode) within your scene that will hold all of your scene's contents, except for the blur node.
2. Add an SKEffectNode to the root of your scene, add an SKShapeNode (blurNode) as its child. Make sure that your blurNode is in front of all of the content in your contentNode by setting its zPosition.
3. Set the filter of your SKEffectNode to a CIGaussianBlur:
let blurFilter = CIFilter(name: "CIGaussianBlur", parameters: ["inputRadius": 75])
4. Set the initial fillColor of your blurNode to .white
5. Generate an SKTexture of your content node, crop this texture using the frame of your blurNode, set this cropped texture as the fillTexture of your blurNode (self is the SKScene here):
let fillTexture = self.view?.texture(from: contentNode, crop: blurNode.frame)