Add glowing effect to an SKSpriteNode

后端 未结 2 1570
太阳男子
太阳男子 2020-12-02 13:30

I have a moving black image on a dark screen, to make it easier to see I would like to add in a white glow to the image. This is my code for the moving image:



        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 14:19

    I created this extension to add a glow effect to an SKSpriteNode

    Just add this to your project

    extension SKSpriteNode {
    
        func addGlow(radius: Float = 30) {
            let effectNode = SKEffectNode()
            effectNode.shouldRasterize = true
            addChild(effectNode)
            effectNode.addChild(SKSpriteNode(texture: texture))
            effectNode.filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius":radius])
        }
    }
    

    Now given an SKSpriteNode

    let sun = SKSpriteNode(imageNamed: "sun")
    

    all you have to do it

    sun.addGlow()
    

提交回复
热议问题