简体   繁体   English

SKCropNode屏蔽边缘抗锯齿

[英]SKCropNode masking edge anti-aliasing

I created a circular mask and animate a sprite inside the mask by using sprite kit SKCropNode class. 我创建了一个圆形蒙版,并使用精灵工具包SKCropNode类为蒙版内的精灵设置动画。 But the edge of the mask looks pixellated. 但是掩模的边缘看起来像素化。

Is there a way to use anti-aliasing to smooth the edges? 有没有办法使用抗锯齿来平滑边缘?

If you want to mask any SKNode/SKSpriteNode with an anti-aliasing effect - you can use SKEffectNode instead of SKCropNode. 如果要屏蔽任何具有抗锯齿效果的SKNode / SKSpriteNode,则可以使用SKEffectNode而不是SKCropNode。 It works with animated nodes as well. 它也适用于动画节点。 Here is an example: 这是一个例子:

// Set up your node
SKNode *nodeToMask = [SKNode node];
// ...

// Set up the mask node
SKEffectNode *maskNode = [SKEffectNode node];

// Create a filter
CIImage *maskImage = [[CIImage alloc] initWithCGImage:[UIImage imageNamed:@"your_mask_image"].CGImage];
CIFilter *maskFilter = [CIFilter filterWithName:@"CISourceInCompositing"
                                  keysAndValues:@"inputBackgroundImage", maskImage, nil];
// Set the filter
maskNode.filter = maskFilter;

// Add childs
[maskNode addChild:nodeToMask];
[scene addChild:maskNode];

From the official docs: 来自官方文档:

If the pixel in the mask has an alpha value of less than 0.05, the image pixel is masked out. 如果掩模中的像素具有小于0.05的α值,则图像像素被遮蔽。

So, SKCropNode doesn't support per pixel alpha values. 因此,SKCropNode不支持每像素alpha值。 It either draws the pixels or it doesn't. 它要么绘制像素,要么不绘制像素。 If alpha (as is used by anti-aliasing) is important to you, you should consider alternative routes. 如果alpha(由抗锯齿使用)对您很重要,您应该考虑替代路线。 For example: 例如:

How to Mask an UIImageView 如何掩盖UIImageView

As of now (June 2018) I can confirm that the SKCropNode will do per-pixel alpha blending. 截至目前(2018年6月),我可以确认SKCropNode将进行每像素alpha混合。 If the crop node has an SKSpriteNode as a mask, and the mask node uses a texture with an anti-aliased mask image, the crop node will do the proper blending. 如果裁剪节点具有SKSpriteNode作为蒙版,并且蒙版节点使用具有消除锯齿蒙版图像的纹理,裁剪节点将执行正确的混合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM