How Do I Blur a Scene in SpriteKit?

后端 未结 7 915
北恋
北恋 2020-11-28 06:00

How would I add a gaussian blur to all nodes (there\'s no fixed number of nodes) in an SKScene in SpriteKit? A label will be added on top of the scene later, this will be m

7条回答
  •  清酒与你
    2020-11-28 06:33

    To add to this by using @Bendegúz's answer and code from http://www.bytearray.org/?p=5360

    I was able to get this to work in my current game project that's being done in IOS 8 Swift. Done a bit differently by returning an SKSpriteNode instead of a UIImage. Also note that my unwrapped currentScene.view! call is to a weak GameScene reference but should work with self.view.frame based on where you are calling these methods. My pause screen is called in a separate HUD class hence why this is the case.

    I would imagine this could be done more elegantly, maybe more like @jemmons's answer. Just wanted to possibly help out anyone else trying to do this in SpriteKit projects written in all or some Swift code.

    func getBluredScreenshot() -> SKSpriteNode{
    
        create the graphics context
        UIGraphicsBeginImageContextWithOptions(CGSize(width: currentScene.view!.frame.size.width, height: currentScene.view!.frame.size.height), true, 1)
    
        currentScene.view!.drawViewHierarchyInRect(currentScene.view!.frame, afterScreenUpdates: true)
    
        // retrieve graphics context
        let context = UIGraphicsGetCurrentContext()
    
        // query image from it
        let image = UIGraphicsGetImageFromCurrentImageContext()
    
        // create Core Image context
        let ciContext = CIContext(options: nil)
        // create a CIImage, think of a CIImage as image data for processing, nothing is displayed or can be displayed at this point
        let coreImage = CIImage(image: image)
        // pick the filter we want
        let filter = CIFilter(name: "CIGaussianBlur")
        // pass our image as input
        filter.setValue(coreImage, forKey: kCIInputImageKey)
    
        //edit the amount of blur
        filter.setValue(3, forKey: kCIInputRadiusKey)
    
        //retrieve the processed image
        let filteredImageData = filter.valueForKey(kCIOutputImageKey) as CIImage
        // return a Quartz image from the Core Image context
        let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent())
        // final UIImage
        let filteredImage = UIImage(CGImage: filteredImageRef)
    
        // create a texture, pass the UIImage
        let texture = SKTexture(image: filteredImage!)
        // wrap it inside a sprite node
        let sprite = SKSpriteNode(texture:texture)
    
        // make image the position in the center
        sprite.position = CGPointMake(CGRectGetMidX(currentScene.frame), CGRectGetMidY(currentScene.frame))
    
        var scale:CGFloat = UIScreen.mainScreen().scale
    
        sprite.size.width  *= scale
    
        sprite.size.height *= scale
    
        return sprite
    
    
    }
    
    
    func loadPauseBGScreen(){
    
        let duration = 1.0
    
        let pauseBG:SKSpriteNode = self.getBluredScreenshot()
    
        //pauseBG.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        pauseBG.alpha = 0
        pauseBG.zPosition = self.zPosition + 1
        pauseBG.runAction(SKAction.fadeAlphaTo(1, duration: duration))
    
        self.addChild(pauseBG)
    
    }
    

提交回复
热议问题