How to create a Gradient in Spritekit?

前端 未结 4 785
长发绾君心
长发绾君心 2021-02-08 22:35

Is there any possible way to create a gradient filled box in SpriteKit? I\'ve tried filling a shape node with that but it notes that only solid colors work with skshapenode.

4条回答
  •  半阙折子戏
    2021-02-08 23:05

    Matt's answer is correct, but I have be unable to add a gradient as yet. This is my currrent attempt, if someone knows how to make it work, please update for thread.

    Here is the Core Image Ref

    CIFilter *gradientFilter = [CIFilter filterWithName:@"CILinearGradient"];
    //[gradientFilter setDefaults];
    CIColor *startColor = [CIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    CIColor *endColor = [CIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
    CIVector *startVector = [CIVector vectorWithX:0 Y:0];
    CIVector *endVector = [CIVector vectorWithX:0.21 Y:0.31];
    [gradientFilter setValue:startVector forKey:@"inputPoint0"];
    [gradientFilter setValue:endVector forKey:@"inputPoint1"];
    [gradientFilter setValue:startColor forKey:@"inputColor0"];
    [gradientFilter setValue:endColor forKey:@"inputColor1"];
    
    SKEffectNode *effectNode = [SKEffectNode node];
    effectNode.filter = gradientFilter;
    effectNode.shouldEnableEffects = YES;
    
    [self addChild:effectNode];
    //effectNode.position = CGPointMake(200, 200);
    

    Another good way to test your filters, is download the demo app CIFunHouse from WWDC 2013.

提交回复
热议问题