How to Display Image in SKShapeNode


I have been using SKShapeNode extensively to draw quick graphics by filling them with CGPaths for quick visual debugging and making prototypes. For example I can create procedural mountains quickly with SKShapeNode. In early times of spritekit SKShapeNode didn’t have way to render images on it. We have fillTexture available now that allows texture to fill the shape.

let shapeNode = SKShapeNode()
shapeNode.fillTexture = SKTexture(imageNamed: “testImage")

But, Our SKShapeNode is displaying nothing.

Why is that?

Let’s see what documentation says:

The default fill color of a SKShapeNode is SKColor.clear. Since the fill texture is blended with the fill color, fillColor needs to be set to a non-clear color for it to display. For example, to display the texture without any color blend effects, set fillColor to SKColor.white.

fillColor of SKShapeNode is .clear which blends with our provided image texture. To make our texture visible without any blending we use .white

let shapeNode = SKShapeNode()
shapeNode.fillColor = .white
shapeNode.fillTexture = SKTexture(imageNamed: “testImage")

You might also like: Blurry Pixel Font in SpriteKit bug

You might also like: AVAudioPlayer not playing any sound

You might also like: App crashing when saving photos from WKWebView

More Articles

Recommended posts