robo-monk
robo-monk

Reputation: 131

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 !

Upvotes: 0

Views: 350

Answers (1)

beyowulf
beyowulf

Reputation: 15331

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)

Upvotes: 3

Related Questions