SKSpriteNode with a glow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
extension SKSpriteNode { | |
/// Initializes a textured sprite with a glow using an existing texture object. | |
convenience init(texture: SKTexture, glowRadius: CGFloat) { | |
self.init(texture: texture, color: .clear, size: texture.size()) | |
let glow: SKEffectNode = { | |
let glow = SKEffectNode() | |
glow.addChild(SKSpriteNode(texture: texture)) | |
glow.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": glowRadius]) | |
glow.shouldRasterize = true | |
return glow | |
}() | |
let glowRoot: SKNode = { | |
let node = SKNode() | |
node.name = "Glow" | |
node.zPosition = -1 | |
return node | |
}() | |
glowRoot.addChild(glow) | |
addChild(glowRoot) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment