简体   繁体   English

SpriteKit快速全屏小插图/具有Alpha混合的照明

[英]SpriteKit fast fullscreen vignette / lighting with alpha blending

I am making a platforming game with SpriteKit. 我正在用SpriteKit制作平台游戏。 I want to achieve the effect that only the area around the player is lit and everything else fades into darkness. 我想达到这样的效果:仅播放器周围的区域被照亮,其他所有东西都变暗。 Imagine something like a fake light source or a strong vignette. 想象一下,诸如假光源或强效装饰图案之类的东西。 For testing purpose I created the following code: 为了进行测试,我创建了以下代码:

let effect = SKEffectNode()
effect.zPosition = 100
effect.position = CGPoint(x: 0, y: 0)
effect.shouldRasterize = false
effect.blendMode = SKBlendMode.Multiply

let gray = SKSpriteNode()
gray.color = SKColor.grayColor()
gray.size = self.frame.size
gray.blendMode = SKBlendMode.Multiply
gray.position = CGPoint(x: 0, y: 0)

let shine = SKSpriteNode(imageNamed: "Shine")
shine.position = CGPoint(x: 0, y: 0)
shine.blendMode = SKBlendMode.Screen
shine.setScale(3.0)

effect.addChild(gray)
effect.addChild(shine)

self.world.addChild(effect)

This works as desired but has a somewhat significant impact on the frame rate. 这可以按需工作,但是对帧速率有一定的影响。 Considering that there might be additional effects like these from eg torches or other light sources on the map, I expect the frame rate to drop even more. 考虑到地图上的火炬或其他光源可能还会产生类似的效果,因此我希望帧速率会下降得更多。

Next, I wanted to make the light flicker, so I assigned a random alpha value to the shine sprite in the update function. 接下来,我想让灯光闪烁,所以我在更新功能中为发光精灵分配了一个随机的alpha值。 This really killed the frame rate, letting it drop instantly to about 4 frames. 这确实杀死了帧速率,使其立即下降到大约4帧。

I have read about SKEffectNodes and their heavy impact on the frame rate. 我已阅读有关SKEffectNodes及其对帧速率的严重影响。 Is there any other way to achieve this sort of fullscreen alpha blending, that is reasonably fast, even with multiple "lights"? 是否有其他方法可以实现这种全屏alpha混合,即使有多个“灯光”,也相当快?

Any advice is truly appreciated. 任何建议是真正的赞赏。

Could this be solved with a SKLightNode perhaps? 可以用SKLightNode解决吗?

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKLightNode_Ref/index.html https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKLightNode_Ref/index.html

Here's an article about how to use the SKLightNode: 这是有关如何使用SKLightNode的文章:

http://www.ymc.ch/en/playing-with-ios-8-sprite-kit-and-sklightnode http://www.ymc.ch/zh-CN/playing-with-ios-8-sprite-kit-and-sklightnode

Hope that's of any usage to you 希望对您有用

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

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