Add glowing effect to an SKSpriteNode Add glowing effect to an SKSpriteNode ios ios

Add glowing effect to an SKSpriteNode


I created this extension to add a glow effect to an SKSpriteNode

Just add this to your project

extension SKSpriteNode {    func addGlow(radius: Float = 30) {        let effectNode = SKEffectNode()        effectNode.shouldRasterize = true        addChild(effectNode)        effectNode.addChild(SKSpriteNode(texture: texture))        effectNode.filter = CIFilter(name: "CIGaussianBlur", parameters: ["inputRadius":radius])    }}

Now given an SKSpriteNode

let sun = SKSpriteNode(imageNamed: "sun")

all you have to do it

sun.addGlow()

enter image description here


Just to add to this, you can perform this on any type of SKNode by first rendering its contents using the texture(from:SKNode) method available on an SKView instance.

Example:

extension SKNode{    func addGlow(radius:CGFloat=30)    {        let view = SKView()        let effectNode = SKEffectNode()        let texture = view.texture(from: self)        effectNode.shouldRasterize = true        effectNode.filter = CIFilter(name: "CIGaussianBlur",withInputParameters: ["inputRadius":radius])        addChild(effectNode)        effectNode.addChild(SKSpriteNode(texture: texture))    }}