Last active
October 23, 2024 10:46
-
-
Save flatpickles/413642637ebd0b00fe2b to your computer and use it in GitHub Desktop.
SKShapeNode scales a fill texture by default, instead of repeating the pattern. This snippet extends SKShapeNode to support setting a tiled fill-texture.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can i cover ball with my texture image using this code?