SpriteKit - Adding a blur to the entire Scene

1.7k Views Asked by At

I am trying to blur my entire GameScene when my pause button is pressed. I have a method called blurSceen() but it doesn't seem to be adding the effect to the scene. Is there a way I can accomplish this or am I doing something wrong? I have viewed other posts about this topic but haven't been able to accomplish the effect.

       func blurScreen() {     
            let effectsNode = SKEffectNode()

            let filter = CIFilter(name: "CIGaussianBlur")
            let blurAmount = 10.0
            filter!.setValue(blurAmount, forKey: kCIInputRadiusKey)

            effectsNode.filter = filter
            effectsNode.position = self.view!.center
            effectsNode.blendMode = .Alpha

            // Add the effects node to the scene
            self.addChild(effectsNode)
        }
3

There are 3 best solutions below

5
rickster On

From the SKEffectNode docs:

An SKEffectNode object renders its children into a buffer and optionally applies a Core Image filter to this rendered output.

The effect node applies a filter only to its child nodes. Your effect node has no children, so there's nothing to apply a filter to.

Probably what you want is to add an effect node to your scene early on--but don't set the filter on it yet--and put all the nodes that you'll later want to blur in as its children. When it comes time to apply a blur, set the filter on the (already existing, already with children) effect node.

0
Geoff H On

I had the same issue trying to blur the whole SKScene and it just wasn't working. The missing piece of the puzzle was this line:

shouldEnableEffects = true
2
Alex Bailey On

Swift 4:

from gameScene:

    let  blur = CIFilter(name:"CIGaussianBlur",withInputParameters: ["inputRadius": 10.0])
    self.filter = blur
    self.shouldRasterize = true
    self.shouldEnableEffects = true