1

I'm trying to create a background for my game with a gradient, directly into SpriteKit. I've tried several things but nothing works and I haven't found any helping advice. The first thing: I'm creating a background SKSpriteNode with a color, then I'm doing the following, which does not appear.

CIFilter *gradientFilter = [CIFilter filterWithName:@"CILinearGradient"];

CIColor *startColor = [CIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
CIColor *endColor = [CIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
[gradientFilter setValue:startColor forKey:@"inputColor0"];
[gradientFilter setValue:endColor forKey:@"inputColor1"];

CIVector *startVector = [CIVector vectorWithX:0 Y:0];
CIVector *endVector = [CIVector vectorWithX:self.frame.size.width Y:self.frame.size.height];
[gradientFilter setValue:startVector forKey:@"inputPoint0"];
[gradientFilter setValue:endVector forKey:@"inputPoint1"];

SKEffectNode *effectNode = [SKEffectNode node];
effectNode.blendMode = SKBlendModeAdd;
effectNode.filter = gradientFilter;
effectNode.shouldEnableEffects = YES;
effectNode.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

[self addChild:effectNode];
McKinley
  • 1,123
  • 1
  • 8
  • 18
Macaret
  • 797
  • 9
  • 33
  • You should add the nodes as children of your SKEffectNode to apply the effect to those nodes, but apparently it doesn't work with CILinearGradient. I have used it with GaussianBlur and works perfectly. For gradients I'd suggest to take a look at this: http://stackoverflow.com/questions/19538624/gradient-texture-has-wrong-scale-on-retina-mac?lq=1 Is similar to what I've been using and works pretty well. – gzafra Oct 01 '14 at 20:52
  • The CILinearGradient is not a filter in the sense that it doesn't take an input image, apply an effect (e.g., blur), and output a processed image. – 0x141E Oct 01 '14 at 22:28

0 Answers0