Filippos T Filippos T - 2 months ago 29
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

addChild(bg)


Thanks a lot !

Answer

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.addChild(bg)
effectNode.filter = CIFilter(name: "CIColorControls")
effectNode.filter?.setValue(0.1, forKey: kCIInputSaturationKey)