Matthew Anguelo
Matthew Anguelo

Reputation: 293

How to Improve SKEffectNode Performance (Swift)?

so in my project I have an SKEffectNode that I use to provide a glow effect around some of my spriteNodes. I use a spriteNode (blurNode) to get the color of my obstacle and then give it to the effectNode. This works fine.

 let blurNode = SKSpriteNode(imageNamed: "neonLine.png")
    blurNode.color = obstacle.color
    blurNode.colorBlendFactor = 1.0
    blurNode.size = CGSize(width: obstacle.size.width + 30, height: obstacle.size.height + 30)


    let effectNode = SKEffectNode()
    effectNode.shouldRasterize = true
    obstacle.addChild(effectNode)
    effectNode.addChild(blurNode)
    effectNode.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius":30])
    effectNode.alpha = 1.0

My issue occurs here.

let colorFadegreen = SKAction.sequence([SKAction.colorize(with: UIColor(red: 0, green: 0.6471, blue: 0.3569, alpha: 1.0), colorBlendFactor: 1.0, duration: 3)])

obstacle.removeAllActions()
obstacle.run(colorFadegreen)

blurNode.removeAllActions()
blurNode.run(colorFadegreen)

What I want to do is have the "glow" that's around the obstacle change colors with the obstacle. That is exactly what happens; however, when I do so my frame rate drops down to 30fps.

So, my question is does anyone know how to improve the performance of this task? Or is there maybe another way I could go about doing this.

One of the ideas I thought of would be to manually blur the "neonLine.png" in photoshop and then add it to the blur node like so let blurNode = SKSpriteNode(imageNamed: "bluredNeonLine.png"). The only thing is I can never get the blur right it always looks off.

Any help would be very much appreciated. Thanks!

EDIT:

Here are some photos of the glows in my project:

Glowing lines on iPhone game

Here is the glow and lines changing color:

Glowing lines on iPhone game

Upvotes: 2

Views: 957

Answers (1)

Confused
Confused

Reputation: 6278

Three answers to the performance question with regards glows:

    1. Use a pre-rendered glow, as per your mentioning in the question, done in Photoshop or similar bitmap editor, exported as bitmap with opacity and used as an SKSpriteNode texture, probably with additive blending for best results, and colour caste to taste.
    1. Bake a texture of the SKEffectNode that's creating a desirable glow within SpriteKit by making it into a texture, and then loading it into an SKSpriteNode, as per this example: https://stackoverflow.com/a/40137270/2109038
    1. Rasterise the results from your SKEffectNode and then hope your changes to colour casts don't cause re-rendering. This is shown in a wonderful extension, here: https://stackoverflow.com/a/40362874/2109038

In all cases, you're best off rendering a white glow that fades out as you like, and then applying colour blend changes to it, since SpriteKit has this built in, and it's reasonably performant in the few tests I've done. This is known as colorizing:

You can change and animate both the blend amount: https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor

and the color being blended with the texture: https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

Upvotes: 2

Related Questions