2

I'm experimenting a bit with Sprite Kit. I'm trying to mask an image with an alpha png. This is easy enough but I can't find information on how to mask an image with alpha values. Right now it renders the pixel or it doesn't.

From the docs on SKCropNode it says: If the pixel in the mask has an alpha value of less than 0.05, the image pixel is masked out.

So this is a logical outcome from the way I'm doing this right now:

CGPoint location = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

SKCropNode *cropNode = [SKCropNode node];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
SKSpriteNode *maskImage = [SKSpriteNode spriteNodeWithImageNamed:@"mask5.png"];
[maskImage setName:@"mask"];

[cropNode setMaskNode:maskImage];
[cropNode addChild:sprite];
[cropNode setPosition:location];

[self addChild:cropNode];

The result is that the edges are jagged. Is there a way to mask the image in such way the edges are smooth?

Thanks!

Dion Snoeijen
  • 94
  • 2
  • 11
  • there was the same question, and here is an answer: http://stackoverflow.com/questions/20619378/skcropnode-masking-edge-anti-aliasing/32349220#32349220 – Oleg Sep 02 '15 at 09:26

1 Answers1

0

No, the mask nodes are binary in the way they work, either they show or the hide. Apple provides an example in their documentation where they apply a blur filter to the masknode afterwards. A 1px box blur might do it for you.

Documentation -> Mask and blur effect

Theis Egeberg
  • 2,556
  • 21
  • 30
  • Ah, ok. That's a shame. But maybe it will be good enough. Thank you! – Dion Snoeijen Mar 09 '14 at 22:26
  • You can turn the jagged look into a feature by scaling it up and achieving a pixelated effect. It's what I ended up doing. There's almost nothing worse than that straight pixelated no-antialias look. – Theis Egeberg Mar 10 '14 at 08:28
  • Hmm, i'm not entirely sure what you mean, you mean scaling up and down again or did you change the entire style of the game to be pixelated because of this? – Dion Snoeijen Mar 10 '14 at 18:53
  • I changed liquid into being this blocky liquid stuff 5x5px, instead of a sharp rolling shape. Masks are really the only way to achieve a liquid effect with spritekit. – Theis Egeberg Mar 10 '14 at 18:58
  • Ah I see. I would like to see that. Is the game released? – Dion Snoeijen Mar 10 '14 at 19:06
  • Sorry no, it'll be in dev for hopefully just a year more. – Theis Egeberg Mar 10 '14 at 19:10
  • Sorry, this won't work. The effect in the documentation doesn't seem to do what I'm after. – Dion Snoeijen Mar 12 '14 at 20:40
  • I noticed that the image you're using is just called "Spaceship", just making sure, you are aware that you can use regular transparent PNG's right? Just looking at your code I can't completely see what you're trying to do, could you post the mask and the image you're masking maybe? – Theis Egeberg Mar 12 '14 at 21:39