1

I am trying to tile an SKTexture on a SKSpriteNode. From what I see this is not directly possible as answered here : How do you set a texture to tile in Sprite Kit

Upon checking the reference I noticed that SKTexture has a textureByApplyingCIFilter method.

How could this be used to tile a certain SKTexture?

This is what I have tried:

SKTexture *tile = [SKTexture textureWithImageNamed:@"tile"];
CIFilter *tileTransform = [CIFilter filterWithName:@"CIAffineTile" keysAndValues:kCIInputImageKey, conveyorTexture, nil ];
CGAffineTransform xform = CGAffineTransformIdentity;
[tile setValue:[NSValue valueWithBytes:&xform
                                  objCType:@encode(CGAffineTransform)]
            forKey:@"inputTransform"];
tile = [tile textureByApplyingCIFilter:tileTransform];

Also what would be the memory consequences of using this solution as opposed to using multiple SKSpriteNode objects with the same SKTextures ?

Community
  • 1
  • 1
Tibor Udvari
  • 2,932
  • 3
  • 23
  • 39
  • Well, more memory can be calculated: texture width * height * (color depth bits / 8) ... and time to process the textures. Gains: possibly smaller bundle size. – CodeSmile Jan 12 '14 at 19:13
  • So in that case the whole generated image would be in live memory? If for example we load a single SKTexture into memory (the tile) and then create a bunch of SKSpriteNode objects that reference that texture the live memory would be only the SKTexture(with it's single tile) and the other SKSpriteNode objects which reference the single tile ? As opposed to having a big SKSpriteNode object which contains a huge live memory eating image because the pixel data is just copied ? – Tibor Udvari Jan 12 '14 at 19:19
  • Whether you have one sprite or 100 using the same texture doesn't matter memory-wise, regardless of how the texture is created. The CIFiltered texture will probably create a copy of the original texture, duplicating its memory footprint. Easy to check with Instruments. – CodeSmile Jan 12 '14 at 19:28
  • That's what I thought as well. I just can't get the CIFilter code to work in order to duplicate the image to test it out. I think it could be useful in some situations but I can't seem to find a code example for this particular case. I think it can be used to make my code for this more robust http://stackoverflow.com/questions/21040217/spritekit-conveyor-belt – Tibor Udvari Jan 12 '14 at 19:37

1 Answers1

0

You can just create an SKSpriteNode, set its texture, and set its filter property to your CIFilter. The inputImage property of the Filter is automatically set to the SKSpriteNode's texture. But remember to set its property shouldEnableEffects to YES.

olidem
  • 1,961
  • 2
  • 20
  • 45