Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blurring a SKShapeNode incrementally using SKEffectNode

I'm trying to blur a SKShapenode in my program. However, I want the shapenode to blur progressively, with a duration of about 1 second. How do I achieve that? The current code blurs it instantly.

func generateAnimation() {

var blurAction : SKAction = SKAction.runBlock{
   //the method below returns a shapeNode
    var circle = self.generateImage()
    var effect : SKEffectNode = SKEffectNode()
    var filter : CIFilter = CIFilter(name:"CIGaussianBlur")
    filter.setValue(10, forKey: "inputRadius")
    effect.filter = filter
    effect.addChild(circle)
    self.addChild(effect)           
}
like image 724
Raghu Avatar asked Jul 06 '14 03:07

Raghu


2 Answers

Implement the update method in your SKScene subclass (or delegate). Then, over the course of one second, run this line again each time your update method is called:

filter.setValue(10, forKey: "inputRadius")

Except instead of passing the value 10, interpolate between 0 and 10 based on the elapsed time.

You might find that re-rendering a blur every frame makes it hard to keep a smooth frame rate. So you might consider faking it instead. Make two nodes, one of which has the blur effect, and use fadeInWithDuration/fadeOutWithDuration actions to fade in the blurred node and fade out the unblurred node.

like image 111
rickster Avatar answered Oct 05 '22 23:10

rickster


I agree with @rickster. In the apple spritekit demo they used this to simulate blinking of the lights (in which the lights fade out from the screen --> "blur" the light).

- (SKSpriteNode *)newLight
{
    SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(8,8)];

    SKAction *blink = [SKAction sequence:@[
                                       [SKAction fadeOutWithDuration:0.25],
                                       [SKAction fadeInWithDuration:0.25]]];
    SKAction *blinkForever = [SKAction repeatActionForever:blink];
    [light runAction: blinkForever];

    return light;
}

You can modify the behaviour of "blinkForever" to suit your case.

like image 20
user1872384 Avatar answered Oct 05 '22 22:10

user1872384