ios – Apply multiple Core Image filters to a SpriteKit node


I can apply a Core Image filter to a SpriteKit node by:

  • Making the node a child of an SKEffectNode
  • Applying the filter to the SKEffectNode
  • Or in the case of the scene itself, apply the filter directly, since SKScene inherits from SKEffectNode. Make sure to enable filters by setting shouldEnableEffects = true on the scene.

However, I can only apply one filter at a time. How do I chain filters? And how do I control the order of their application?

A similar question exists here, but I don’t understand the answer. It assumes too much implicit knowledge.

Here is what I have.

The base code that sets the scene and creates a sprite node and an effect node:

override func didMove(to view: SKView) {
    shouldEnableEffects = true // enables filtering of the scene itself

    let mySprite = SKSpriteNode(imageNamed: "myImage")
    let myEffectNode = SKEffectNode()
    myEffectNode.addChild(mySprite)
    addChild(myEffectNode)
}

I can apply a Core Image filter like this:

override func didMove(to view: SKView) {
    // previous code

    filter = CIFilter(name: "CIMotionBlur") // filters the whole scene

    myEffectNode.filter = CIFilter(name: "CIMotionBlur") // filters the node
}

But I can not chain or pile the filters like this:

myEffectNode.filter = CIFilter(name: "CIMotionBlur")
myEffectNode.filter = CIFilter(name: "CIPixellate", parameters: [
    "inputScale": 8
])
// Only the last called filter is applied

How do I apply multiple filters on the same node? What are the coding patterns and structures I need to understand to do so?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img