Filippos T Filippos T - 5 months ago 66
Swift Question

How to change a SKSpriteNode saturation? - Swift

I develop a game and I'd like to have the background change its saturation over a certain amount of time, but have no idea how to do it... So here's my code:

var bg = SKSpriteNode()
bg.size = frame.size
bg.position = CGPoint(x: frame.midX, y: frame.midY)
bg.zPosition = -1000
bg.texture = SKTexture(imageNamed: "background")
bg.alpha = 0.5


Thanks a lot !


You can add you background as a child node of a SKEffectNode then set create a color controls filter as the effect node filter. Setting the kCIInputSaturationKey to values less than one will cause the background image to be desaturated, greater than one will cause the background to become more saturated.

var effectNode = SKEffectNode()
effectNode.filter = CIFilter(name: "CIColorControls")
effectNode.filter?.setValue(0.1, forKey: kCIInputSaturationKey)