如何在 SpriteKit 中模糊场景? [英] How Do I Blur a Scene in SpriteKit?

查看:17
本文介绍了如何在 SpriteKit 中模糊场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SpriteKit 的 SKScene 中向所有节点(没有固定数量的节点)添加高斯模糊?稍后将在场景顶部添加一个标签,这将是我的暂停菜单.几乎任何事情都会有所帮助!

How would I add a gaussian blur to all nodes (there's no fixed number of nodes) in an SKScene in SpriteKit? A label will be added on top of the scene later, this will be my pause menu. Almost anything would help!

这样的事情是我想要的:

Something like this is what I'm going for:

推荐答案

您正在寻找的是一个 SKEffectNode.它将 CoreImage 过滤器应用于自身(以及所有子节点).只需将其设为场景的根视图,为其添加 CoreImage 的模糊滤镜之一,即可完成设置.

What you're looking for is an SKEffectNode. It applies a CoreImage filter to itself (and thus all subnodes). Just make it the root view of your scene, give it one of CoreImage's blur filters, and you're set.

例如,我设置了一个 SKScene,其中一个 SKEffectNode 作为它的第一个子节点和一个属性,root 包含一个弱引用给它:

For example, I set up an SKScene with an SKEffectNode as it's first child node and a property, root that holds a weak reference to it:

-(void)createLayers{
  SKEffectNode *node = [SKEffectNode node];
  [node setShouldEnableEffects:NO];
  CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:@"inputRadius", @1.0f, nil];
  [node setFilter:blur];
  [self setRoot:node];
}

这是我用来(动画!)我的场景模糊的方法:

And here's the method I use to (animate!) the blur of my scene:

-(void)blurWithCompletion:(void (^)())handler{
  CGFloat duration = 0.5f;
  [[self root] setShouldRasterize:YES];
  [[self root] setShouldEnableEffects:YES];
  [[self root] runAction:[SKAction customActionWithDuration:duration actionBlock:^(SKNode *node, CGFloat elapsedTime){
    NSNumber *radius = [NSNumber numberWithFloat:(elapsedTime/duration) * 10.0];
    [[(SKEffectNode *)node filter] setValue:radius forKey:@"inputRadius"];
  }] completion:handler];
}

请注意,和您一样,我将其用作暂停屏幕,因此我将场景栅格化.如果您希望您的场景在模糊时进行动画处理,您可能应该将 setShouldResterize: 设置为 NO.

Note that, like you, I'm using this as a pause screen, so I rasterize the scene. If you want your scene to animate while blurred, you should probably setShouldResterize: to NO.

如果你对动画过渡到模糊不感兴趣,你总是可以将滤镜设置为 10.0f 左右的初始半径,然后执行一个简单的 setShouldEnableEffects:YES 当你想打开它时.

And if you're not interested in animating the transition to the blur, you could always just set the filter to an initial radius of 10.0f or so and do a simple setShouldEnableEffects:YES when you want to switch it on.

另请参阅:SKEffectNode 类参考

更新:
请参阅下面的马库斯评论.他指出 SKScene 实际上是 SKEffectNode 的子类,所以你真的应该能够在场景本身上调用所有这些,而不是随意插入一个节点树中的效果节点.

UPDATE:
See Markus's comment below. He points out that SKScene is, in fact, a subclass of SKEffectNode, so you really ought to be able to call all of this on the scene itself rather than arbitrarily inserting an effect node in your node tree.

这篇关于如何在 SpriteKit 中模糊场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆