如何提高SKEffectNode性能(Swift)?

时间:2023-01-22 23:27:29

so in my project I have an SKEffectNode that I use to provide a glow effect around some of my spriteNodes. I use a spriteNode (blurNode) to get the color of my obstacle and then give it to the effectNode. This works fine.

在我的项目中,我有一个SKEffectNode用来在我的spriteNodes周围提供一个辉光效果。我使用spriteNode(模糊节点)来获取障碍物的颜色,然后将它给effectNode。这是很好。

 let blurNode = SKSpriteNode(imageNamed: "neonLine.png")
    blurNode.color = obstacle.color
    blurNode.colorBlendFactor = 1.0
    blurNode.size = CGSize(width: obstacle.size.width + 30, height: obstacle.size.height + 30)


    let effectNode = SKEffectNode()
    effectNode.shouldRasterize = true
    obstacle.addChild(effectNode)
    effectNode.addChild(blurNode)
    effectNode.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius":30])
    effectNode.alpha = 1.0

My issue occurs here.

我的问题发生。

let colorFadegreen = SKAction.sequence([SKAction.colorize(with: UIColor(red: 0, green: 0.6471, blue: 0.3569, alpha: 1.0), colorBlendFactor: 1.0, duration: 3)])

obstacle.removeAllActions()
obstacle.run(colorFadegreen)

blurNode.removeAllActions()
blurNode.run(colorFadegreen)

What I want to do is have the "glow" that's around the obstacle change colors with the obstacle. That is exactly what happens; however, when I do so my frame rate drops down to 30fps.

我想要做的是让障碍物周围的“辉光”随着障碍物改变颜色。这就是所发生的;然而,当我这么做的时候,我的帧率下降到了30fps。

So, my question is does anyone know how to improve the performance of this task? Or is there maybe another way I could go about doing this.

我的问题是,有没有人知道如何提高这个任务的性能?或者我是否可以用另一种方法。

One of the ideas I thought of would be to manually blur the "neonLine.png" in photoshop and then add it to the blur node like so let blurNode = SKSpriteNode(imageNamed: "bluredNeonLine.png"). The only thing is I can never get the blur right it always looks off.

我想到的一个主意是手动模糊“neonLine”。在photoshop中添加png“,然后添加到模糊节点中,比如让blur node = SKSpriteNode(imageNamed:“bluredneonlin .png”)。唯一的问题是我永远都不能正确的模糊它总是看起来很模糊。

Any help would be very much appreciated. Thanks!

如有任何帮助,我们将不胜感激。谢谢!

EDIT:

编辑:

Here are some photos of the glows in my project:

以下是我的项目中发光的照片:

如何提高SKEffectNode性能(Swift)?

Here is the glow and lines changing color:

这里是辉光和线条变化的颜色:

如何提高SKEffectNode性能(Swift)?

1 个解决方案

#1


1  

Three answers to the performance question with regards glows:

关于表演问题,有三个答案:

    1. Use a pre-rendered glow, as per your mentioning in the question, done in Photoshop or similar bitmap editor, exported as bitmap with opacity and used as an SKSpriteNode texture, probably with additive blending for best results, and colour caste to taste.
    2. 使用预渲染的辉光,如你在问题中提到的,在Photoshop或类似的位图编辑器中完成,输出为不透明的位图,并用作SKSpriteNode纹理,可能与添加剂混合,以获得最佳效果,颜色等级。
  • 使用预渲染的辉光,如你在问题中提到的,在Photoshop或类似的位图编辑器中完成,输出为不透明的位图,并用作SKSpriteNode纹理,可能与添加剂混合,以获得最佳效果,颜色等级。
    1. Bake a texture of the SKEffectNode that's creating a desirable glow within SpriteKit by making it into a texture, and then loading it into an SKSpriteNode, as per this example: https://*.com/a/40137270/2109038
    2. 烤一个SKEffectNode的纹理,它通过将SpriteKit制作成一个纹理在SpriteKit中创建一个理想的辉光,然后将它加载到SKSpriteNode中,如下例所示:https://*.com/a/40137270/2109038
  • 烤一个SKEffectNode的纹理,它通过将SpriteKit制作成一个纹理在SpriteKit中创建一个理想的辉光,然后将它加载到SKSpriteNode中,如下例所示:https://*.com/a/40137270/2109038
    1. Rasterise the results from your SKEffectNode and then hope your changes to colour casts don't cause re-rendering. This is shown in a wonderful extension, here: https://*.com/a/40362874/2109038
    2. 从您的SKEffectNode中提取结果,然后希望您对颜色强制转换的更改不会导致重新呈现。这里显示了一个很棒的扩展:https://*.com/a/40362874/2109038
  • 从您的SKEffectNode中提取结果,然后希望您对颜色强制转换的更改不会导致重新呈现。这里显示了一个很棒的扩展:https://*.com/a/40362874/2109038

In all cases, you're best off rendering a white glow that fades out as you like, and then applying colour blend changes to it, since SpriteKit has this built in, and it's reasonably performant in the few tests I've done. This is known as colorizing:

在所有的情况下,最好是呈现一个白色的辉光,然后对它应用颜色混合更改,因为SpriteKit内置了这个功能,在我所做的几个测试中,它表现得相当出色。这被称为着色:

You can change and animate both the blend amount: https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor

您可以更改和激活混合数量:https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor。

and the color being blended with the texture: https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

与纹理混合的颜色:https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

#1


1  

Three answers to the performance question with regards glows:

关于表演问题,有三个答案:

    1. Use a pre-rendered glow, as per your mentioning in the question, done in Photoshop or similar bitmap editor, exported as bitmap with opacity and used as an SKSpriteNode texture, probably with additive blending for best results, and colour caste to taste.
    2. 使用预渲染的辉光,如你在问题中提到的,在Photoshop或类似的位图编辑器中完成,输出为不透明的位图,并用作SKSpriteNode纹理,可能与添加剂混合,以获得最佳效果,颜色等级。
  • 使用预渲染的辉光,如你在问题中提到的,在Photoshop或类似的位图编辑器中完成,输出为不透明的位图,并用作SKSpriteNode纹理,可能与添加剂混合,以获得最佳效果,颜色等级。
    1. Bake a texture of the SKEffectNode that's creating a desirable glow within SpriteKit by making it into a texture, and then loading it into an SKSpriteNode, as per this example: https://*.com/a/40137270/2109038
    2. 烤一个SKEffectNode的纹理,它通过将SpriteKit制作成一个纹理在SpriteKit中创建一个理想的辉光,然后将它加载到SKSpriteNode中,如下例所示:https://*.com/a/40137270/2109038
  • 烤一个SKEffectNode的纹理,它通过将SpriteKit制作成一个纹理在SpriteKit中创建一个理想的辉光,然后将它加载到SKSpriteNode中,如下例所示:https://*.com/a/40137270/2109038
    1. Rasterise the results from your SKEffectNode and then hope your changes to colour casts don't cause re-rendering. This is shown in a wonderful extension, here: https://*.com/a/40362874/2109038
    2. 从您的SKEffectNode中提取结果,然后希望您对颜色强制转换的更改不会导致重新呈现。这里显示了一个很棒的扩展:https://*.com/a/40362874/2109038
  • 从您的SKEffectNode中提取结果,然后希望您对颜色强制转换的更改不会导致重新呈现。这里显示了一个很棒的扩展:https://*.com/a/40362874/2109038

In all cases, you're best off rendering a white glow that fades out as you like, and then applying colour blend changes to it, since SpriteKit has this built in, and it's reasonably performant in the few tests I've done. This is known as colorizing:

在所有的情况下,最好是呈现一个白色的辉光,然后对它应用颜色混合更改,因为SpriteKit内置了这个功能,在我所做的几个测试中,它表现得相当出色。这被称为着色:

You can change and animate both the blend amount: https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor

您可以更改和激活混合数量:https://developer.apple.com/reference/spritekit/skspritenode/1519780-colorblendfactor。

and the color being blended with the texture: https://developer.apple.com/reference/spritekit/skspritenode/1519639-color

与纹理混合的颜色:https://developer.apple.com/reference/spritekit/skspritenode/1519639-color