Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to blur everything except 2 nodes. Spritekit (Swift)

I'd like to blur the background of my game when

self.view?.scene?.paused = true

But the the button and the paused label (both SKSpriteNode's) should not be blur. they all have different Z-index values. The scene is paused when the button node is pressed and resumed when the button is pressed again.

I cant find a way to achieve this in Swift. i found some suggestions that use SKEffectNode?

like image 490
Egghead Avatar asked Oct 15 '14 14:10

Egghead


1 Answers

The basic steps...

  1. Create an SKEffectsNode
  2. Create a CIGaussianBlur CIFilter
  3. Assign the filter to the effects node
  4. Add nodes to the effects node (child nodes will be blurred)

and example code in Swift...

// Create an effects node with a gaussian blur filter
let effectsNode = SKEffectNode()
let filter = CIFilter(name: "CIGaussianBlur")
// Set the blur amount. Adjust this to achieve the desired effect
let blurAmount = 10.0
filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)

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

// Create a sprite
let texture = SKTexture(imageNamed: "Spaceship")
let sprite = SKSpriteNode(texture: texture)

// Add the sprite to the effects node. Nodes added to the effects node
// will be blurred
effectsNode.addChild(sprite)
// Add the effects node to the scene
self.addChild(effectsNode)

// Create another sprite
let sprite2 = SKSpriteNode(texture: texture)
sprite2.position = self.view!.center
sprite2.size = CGSize(width:64, height:64);
sprite2.zPosition = 100

// Add the sprite to the scene. Nodes added to the scene won't be blurred
self.addChild(sprite2)
like image 87
0x141E Avatar answered Nov 03 '22 08:11

0x141E