How do I alpha-mask a SKSpriteNode?

1.2k Views Asked by At

I know there's SKCropNode, but it will only fully in- or exclude pixels based on an alpha threshold of the maskNode. What I already tried is using the SKEffectNode with a CIBlendWithAlphaMask filter, but the result I'm getting is invisible and I also wouldn't be sure on how to move the mask around. Here's the code:

SKSpriteNode* overlay = [SKSpriteNode spriteNodeWithImageNamed:@"Overlay.png"];
//...
SKEffectNode* blendNode = [[SKEffectNode alloc] init];
blendNode.filter = [CIFilter filterWithName:@"CIBlendWithAlphaMask"];

UIImage* maskImage = [UIImage imageNamed:@"MaskImage.png"];
[blendNode.filter setValue:[maskImage CIImage] forKey:@"inputMaskImage"];

[blendNode addChild:overlay];
[self addChild:blendNode];

I already had figured out the perfect solution, using a SKLightNode to light the texture and then blend it into the framebuffer using SKBlendModeAdd, but for some reason it won't work on iPhone 5 (here's a related topic). I'm aware of this topic too, which didn't help either.

Any solution to properly alpha-mask a SKSpriteNode would be greatly appreciated! Running iOS 8.

1

There are 1 best solutions below

0
On

Using a dynamically generated CIImage as mask does the trick. It can be created by a CIFilter, using whatever gradient or image you need as mask, and setting the inputCenter property to the position you want.