Search code examples
iosswiftsprite-kittexturessprite

SpriteKit texture "scaling" different from one to another


Exact same texture, different results

Here an SKSpriteNode and SKShapeNode are given the same texture, from a texture atlas.

The SKSpriteNode is on the right, and it does the right thing.

enter image description here

The SKShapeNode is on the left, and doing the wrong thing.

Please, what am I doing wrong?

here's the code...

override func didMove(to view: SKView) {


    let myTextureAtlas = SKTextureAtlas(named: "demoArt")
    let arrayOfFileNames = myTextureAtlas.textureNames
    let myTexture = myTextureAtlas.textureNamed(arrayOfFileNames.last!)

    let circleShape = SKShapeNode(circleOfRadius: 180)
    circleShape.fillColor = SKColor.white
    circleShape.fillTexture = myTexture
    circleShape.position = CGPoint(x: 256, y: 384)
    addChild(circleShape)

    let circleSprite = SKSpriteNode(color: SKColor.white, size: CGSize(width: 320, height: 320))
    circleSprite.texture = myTexture
    circleSprite.position = (CGPoint(x: 768, y: 384))
    addChild(circleSprite)

    print(myTexture)

The result of print(myTexture) is correct:

<SKTexture> 'seven.png' (512 x 512)

If you want to test this out, here are the image files going into the "atlas"...

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here


Solution

  • This is not a bug (it was wanted as is) , SKShapeNode scales a fill texture by default, instead of repeating the pattern and you don't have a direct control on this scaling.

    As explained to the API reference:

    Shape nodes are useful for content that cannot be easily decomposed into simple textured sprites. Shape nodes are also very useful for building and displaying debugging information on top of your game content. However, the SKSpriteNode class offers higher performance than this class, so use shape nodes sparingly.

    Using SKShapeNode beyond from these dictates will needlessly cost performance and can produce visual defects.