UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Animating pixelation affect on SKSpriteNode using SKEffectNode ?

Forums > Swift

Working in SpriteKit I moved away from Hudson's example of pixelating an image as it assumed that it would be a UIImage (https://www.hackingwithswift.com/example-code/media/how-to-pixellate-a-uiimage), instead I found that SKEffectNode grants access to CoreGraphic's filters, and so was able to achieve the same thing:

        let pixelateNode: SKEffectNode = SKEffectNode()

        let pixelFilter = CIFilter(name: "CIPixellate")!
        pixelFilter.setDefaults()
        pixelFilter.setValue(110.0, forKey: "inputScale")

        pixelateNode.filter = pixelFilter
        pixelateNode.shouldEnableEffects = true
        addChild(pixelateNode)

        let testSprite = SKSpriteNode(imageNamed: "testSprite")

        pixelateNode.addChild(testSprite)

The code will pixelate the sprite, but I was looking for a transition from one state to another i.e. the defaul (normal) state, to the 110 value pixelation.

Is there a way of doing this please ?

2      

I have managed to simulate Apple's Core Image "CIPixelate" filter working with a transition.

Prepare by trawling all SKNodes on the scene and moving them to an SKEffectNode, which allows the filter to be applied to all its children in one go. Then place each iteration from a loop e.g. 1-20 (sweetspot) in a DispatchQueue with enough time for the pixelation effect to seem real.

//ITERATE OVER EACH PIXELATION REQUEST TO CREATE AN ANIMATION
for num in 1...20 {
        let seconds = 0.3 + 0.1 * Double(num)
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {     
            effectNode.filter = CIFilter(name       : "CIPixellate",
                                         parameters : [kCIInputScaleKey: (num)])
        }
    }

Finish by calling the scene transition function after the filter has had enough time to complete (I probably should use a dependency operation) gives the desired impression.

ref : https://github.com/rHockWorks/PixelationTransition/

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.