Is it possible to fill a SKShapeNode with a texture that would be 1 to 1 in scale and repeated until the entire SKShapeNode is filled? I tried to use fillTexture , but it just stretches the texture, and it’s necessary that it be repeated, creating a seamless texture without scaling.

  • one
  • @MaxMikheyenko thanks! It's a pity that Swift will have to tinker with what would be rewritten in Objective-C, because I don't know Swift at all) - xXxxX
  • one
    I would have such problems :) see the answer - Max Mikheyenko

1 answer 1

for obs (remember this is a category on SKShapeNode )

 - (void)setTiledFillTextureWithImageName:(NSString*)imageName tileSize:(CGSize)tileSize { CGFloat targetDimension = fmax(self.frame.size.width, self.frame.size.height); CGSize targetSize = CGSizeMake(targetDimension, targetDimension); UIImage *targetRef = [UIImage imageNamed:imageName]; UIGraphicsBeginImageContext(targetSize); CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextDrawTiledImage(contextRef, CGRectMake(0, 0, tileSize.width, tileSize.height), targetRef.CGImage); UIImage *tiledTexture = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.fillTexture = [SKTexture textureWithImage:tiledTexture]; } 

and swift

 extension SKShapeNode { func setTiledFillTexture(imageName: String, tileSize: CGSize) { let targetDimension = max(self.frame.size.width, self.frame.size.height) let targetSize = CGSizeMake(targetDimension, targetDimension) let targetRef = UIImage(named: imageName).CGImage UIGraphicsBeginImageContext(targetSize) let contextRef = UIGraphicsGetCurrentContext() CGContextDrawTiledImage(contextRef, CGRect(origin: CGPointZero, size: tileSize), targetRef) let tiledTexture = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.fillTexture = SKTexture(image: tiledTexture) } } 
  • Only I replaced the first 2 lines with CGSize targetSize = CGSizeMake(self.frame.size.width, self.frame.size.height); so that the texture does not resize under the proportions of the shape. - xXxxX