I have a SKSpriteNode
that I'd like to have a blue glow around it's edges for highlighting purposes. I am guessing that I would need to make my sprite a child of a SKEffectNode
and then create/apply a filter of some sort.
UPDATE : I have investigated this quite a it with the chosen answer's approach, and discovered that SKEffectNode
has a sizeable hit on performance even if you have it set to shouldRasterize
and 'no filter' defined. My conclusion is that if your game requires more than 10 moving objects at one time, they can't involve a SKEffectNode
even if rasterized.
My solution will likely involve pre-rendered glow images/animations, as SKEffectNode is not going to cut it for my requirements.
If someone has insight as to anything I am missing, I'd appreciate hearing whatever you know!
I am accepting an answer because it does achieve what I asked for, but wanted to add these notes to anyone looking to go this route, so you can be aware of some of the issues with using SKEffectNode
.
You could use a
SKShapeNode
behind the sprite and define a glow using it'sglowWidth
andstrokeColor
properties. If you size and position it right, this should give you the appearance of a glow. This doesn't give you many options for customization, but I imagine it's much easier than using aCIFilter
with anSKEffectNode
which is likely the other logical option you have for this.You can create a glow effect in Core Image by creating a
CIFilter
subclass that composes multiple built-in filters. Such a filter would involve steps like these:CIColorMatrix
to create a monochrome version of the input image.CIAffineTransform
+CIGaussianBlur
).CISourceOverCompositing
).Once you have a
CIFilter
subclass that does all that, you can use it with aSKEffectNode
to get a realtime glow around the effect node's children. Here it is running in the "Sprite Kit Game" Xcode template on an iPad 4:I got this up and running in a few minutes by cribbing the custom filter class used for a similar effect in the Scene Kit presentation from WWDC 2013 -- grab it from the WWDC Sample Code package at developer.apple.com/downloads, and look for the
ASCGlowFilter
class. (If you want to use that code on iOS, you'll need to change theNSAffineTransform
part to useCGAffineTransform
instead. I also replaced thecenterX
andcenterY
properties with aninputCenter
parameter of typeCIVector
so Sprite Kit can automatically center the effect on the sprite.)Did I say "realtime" glow? Yup! That's short for "really eats CPU time". Notice in the screenshot it's no longer pegged at 60 fps, even with only one spaceship -- and with the software OpenGL ES renderer on the iOS Simulator, it runs at slideshow speed. If you're on the Mac, you probably have silicon to spare... but if you want to do this in your game, keep some things in mind:
shouldRasterize
toYES
on the effect node should help a lot. (Actually, in this case, you might get some improvement by rotating the effect node instead of the sprite within it.)@rickster's answer is great. Since I have low rep, I'm apparently not allowed to add this code as a comment to his. I hope this doesn't break stackoverflow rules of propriety. I'm not trying to userp his rep in any way.
Here's code that does what he's describing in his answer:
Header:
Implementation:
In use: