2

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)
        }
Brejuro
  • 3,421
  • 8
  • 34
  • 61
  • Perhaps you should consider blurring only some of the nodes in your scene. If you blur the entire scene, text (e.g., the score, game status) will also be blurred. Here's an example: http://stackoverflow.com/questions/26385156/how-to-blur-everything-except-2-nodes-spritekit-swift – Epsilon Aug 04 '16 at 16:49
  • @Epsilon This is the tutorial I followed, and nothing in my scene is getting blurred after I call the method above. – Brejuro Aug 06 '16 at 01:17
  • Did you add a node as a child of the `effectsNode`? Only nodes added to the effects node will be blurred. – Epsilon Aug 06 '16 at 20:30

3 Answers3

3

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.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • But everything I need to blur already has a parent which is the scene itself. – Brejuro Aug 04 '16 at 15:43
  • If you want to blur *everything*, you're in luck — `SKScene` is a subclass of `SKEffectNode`, so you can apply filters to the scene itself. You need to add your own effect node only if there are nodes you *don't* want blurred (in that case, you make the things to be blurred children of the effect node and the things to remain un-blurred children of some other node or of the scene). – rickster Aug 04 '16 at 16:48
  • How would I accomplish this? I thought the code I have above would have done just that. – Brejuro Aug 06 '16 at 01:17
  • To blur the whole scene, assign to the `filters` property of the scene itself. (The scene is `self` in your function.) – rickster Aug 06 '16 at 01:22
  • self.filter = filter? The scene still isn't bluring – Brejuro Aug 06 '16 at 01:26
1

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
Geoff H
  • 3,107
  • 1
  • 28
  • 53
1

Swift 4:

from gameScene:

    let  blur = CIFilter(name:"CIGaussianBlur",withInputParameters: ["inputRadius": 10.0])
    self.filter = blur
    self.shouldRasterize = true
    self.shouldEnableEffects = true
Alex Bailey
  • 793
  • 9
  • 20
  • Do you have any idea as to why when I apply this to my scene, it cuts the rendering area into quarters and only shows the scene in one of the quarters? It's really weird! – Brad Root May 20 '19 at 21:29
  • 2
    Follow up to myself: turned out for some reason my scene's camera wasn't playing nicely with the filter. Once I removed it, the whole scene rendered with the effect. – Brad Root May 21 '19 at 00:12