ios - How can you create a glow around a sprite via SKEffectNode -
i have skspritenode
i'd have blue glow around it's edges highlighting purposes. guessing need make sprite child of skeffectnode
, create/apply filter of sort.
update : have investigated quite chosen answer's approach, , discovered skeffectnode
has sizeable hit on performance if have set shouldrasterize
, 'no filter' defined. conclusion if game requires more 10 moving objects @ 1 time, can't involve skeffectnode
if rasterized.
my solution involve pre-rendered glow images/animations, skeffectnode not going cut requirements.
if has insight missing, i'd appreciate hearing whatever know!
i accepting answer because achieve asked for, wanted add these notes looking go route, can aware of of issues using skeffectnode
.
@rickster's answer great. since have low rep, i'm apparently not allowed add code comment his. hope doesn't break stackoverflow rules of propriety. i'm not trying userp rep in way.
here's code he's describing in answer:
header:
// enhglowfilter.h #import <coreimage/coreimage.h> @interface enhglowfilter : cifilter @property (strong, nonatomic) uicolor *glowcolor; @property (strong, nonatomic) ciimage *inputimage; @property (strong, nonatomic) nsnumber *inputradius; @property (strong, nonatomic) civector *inputcenter; @end //based on ascglowfilter apple
implementation:
#import "enhglowfilter.h" @implementation enhglowfilter -(id)init { self = [super init]; if (self) { _glowcolor = [uicolor whitecolor]; } return self; } - (nsarray *)attributekeys { return @[@"inputradius", @"inputcenter"]; } - (ciimage *)outputimage { ciimage *inputimage = [self valueforkey:@"inputimage"]; if (!inputimage) return nil; // monochrome cifilter *monochromefilter = [cifilter filterwithname:@"cicolormatrix"]; cgfloat red = 0.0; cgfloat green = 0.0; cgfloat blue = 0.0; cgfloat alpha = 0.0; [self.glowcolor getred:&red green:&green blue:&blue alpha:&alpha]; [monochromefilter setdefaults]; [monochromefilter setvalue:[civector vectorwithx:0 y:0 z:0 w:red] forkey:@"inputrvector"]; [monochromefilter setvalue:[civector vectorwithx:0 y:0 z:0 w:green] forkey:@"inputgvector"]; [monochromefilter setvalue:[civector vectorwithx:0 y:0 z:0 w:blue] forkey:@"inputbvector"]; [monochromefilter setvalue:[civector vectorwithx:0 y:0 z:0 w:alpha] forkey:@"inputavector"]; [monochromefilter setvalue:inputimage forkey:@"inputimage"]; ciimage *glowimage = [monochromefilter valueforkey:@"outputimage"]; // scale float centerx = [self.inputcenter x]; float centery = [self.inputcenter y]; if (centerx > 0) { cgaffinetransform transform = cgaffinetransformidentity; transform = cgaffinetransformtranslate(transform, centerx, centery); transform = cgaffinetransformscale(transform, 1.2, 1.2); transform = cgaffinetransformtranslate(transform, -centerx, -centery); cifilter *affinetransformfilter = [cifilter filterwithname:@"ciaffinetransform"]; [affinetransformfilter setdefaults]; [affinetransformfilter setvalue:[nsvalue valuewithcgaffinetransform:transform] forkey:@"inputtransform"]; [affinetransformfilter setvalue:glowimage forkey:@"inputimage"]; glowimage = [affinetransformfilter valueforkey:@"outputimage"]; } // blur cifilter *gaussianblurfilter = [cifilter filterwithname:@"cigaussianblur"]; [gaussianblurfilter setdefaults]; [gaussianblurfilter setvalue:glowimage forkey:@"inputimage"]; [gaussianblurfilter setvalue:self.inputradius ?: @10.0 forkey:@"inputradius"]; glowimage = [gaussianblurfilter valueforkey:@"outputimage"]; // blend cifilter *blendfilter = [cifilter filterwithname:@"cisourceovercompositing"]; [blendfilter setdefaults]; [blendfilter setvalue:glowimage forkey:@"inputbackgroundimage"]; [blendfilter setvalue:inputimage forkey:@"inputimage"]; glowimage = [blendfilter valueforkey:@"outputimage"]; return glowimage; } @end
in use:
@implementation enhmyscene //skscene subclass -(id)initwithsize:(cgsize)size { if (self = [super initwithsize:size]) { /* setup scene here */ [self setanchorpoint:(cgpoint){0.5, 0.5}]; self.backgroundcolor = [skcolor colorwithred:0.15 green:0.15 blue:0.3 alpha:1.0]; skeffectnode *effectnode = [[skeffectnode alloc] init]; enhglowfilter *glowfilter = [[enhglowfilter alloc] init]; [glowfilter setglowcolor:[[uicolor redcolor] colorwithalphacomponent:0.5]]; [effectnode setshouldrasterize:yes]; [effectnode setfilter:glowfilter]; [self addchild:effectnode]; _effectnode = effectnode; } return self; } -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { /* called when touch begins */ (uitouch *touch in touches) { cgpoint location = [touch locationinnode:self]; skspritenode *sprite = [skspritenode spritenodewithimagenamed:@"spaceship"]; sprite.position = location; [self.effectnode addchild:sprite]; } }
Comments
Post a Comment