简体   繁体   中英

Apply CIFilter to SKEffectNode on iOS7

Using SpriteKit in Swift, I have this code inside my GameScene (a SKScene):

//Init function
let self.itemsLayer = SKNode()

let grayNode = SKEffectNode()
let grayFilter = CIFilter(name: "CIPhotoEffectNoir")
grayFilter.setDefaults()
grayNode.filter = grayFilter
grayNode.addChild(self.itemsLayer)

self.addChild(grayNode)

It is supposed to render the items I display into the itemsLayer in gray color (later in the code I add some SKSpriteNode with textures/images into the itemsLayer).

On iOS8, it works perfectly: I can see all my items being in gray.

But on iOS7, the SKSpriteNodes (my items on itemsLayer) are not displayed (as if hidden).

CIPhotoEffectNoir does exist in iOS7 according to the documentation and to filterNamesInCategory("CICategoryColorEffect") .

Edit : When I comment the filter lines, my itemsLayer is still hidden. When I convert let grayNode to a SKNode , it works back (but no effect is applied of course). The problem seems to come from the grayNode being a SKEffectNode

After hours of debugging, I realized that iOS7 doesn't render SKEffectNode the same way as iOS8 does.

In iOS7, adding a SKEffectNode to a bunch of SKSpriteNode layers will not put it automatically to the most front layer (which is the case with SKSpriteNode).

So I only needed to add the line :

grayNode.zPosition = 10 //Or any number > 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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