SKSpriteNode shadow rendering incorrectly?

Viewed 64

I'm using Xcode 11.4.1 and Swift 5.2.2, and I am trying cast a shadow using an SKLightNode onto a circular SKSpriteNode.

However, the shadow is cast over the rectangular frame of the SpriteNode rather than the circle image png that I am using.

This is the desired effect

This is what happens

In the first image, I am using a 611x611px circle png while in the second I am using a 612x612 png. I have found that this "corner shadow" happens only when using specific image dimensions to create the SpriteNode.

Specifically, through my testing, square images with size <688px and >601px display no corner shadow, except sizes exactly 612 and 608. I am testing this in a playground but the same problem occurs in a full xcodeproj.

What have I done wrong here? I doubt my code is the issue but here it is:

import PlaygroundSupport
import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        let background = SKSpriteNode(color: UIColor.blue, size: frame.size)
        background.zPosition = 0
        background.position = CGPoint(x: frame.midX, y: frame.midY)
        background.lightingBitMask = 1
        addChild(background)

        let light = SKLightNode()
        light.zPosition = 100
        light.categoryBitMask = 1
        light.falloff = 0.5
        light.lightColor = UIColor.white
        light.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(light)

        let sprite = SKSpriteNode(imageNamed: "612.png")
        sprite.color = UIColor.yellow
        sprite.colorBlendFactor = 1
        sprite.zPosition = 3
        sprite.position = CGPoint(x: frame.midX, y: frame.midY + 100)
        sprite.size = CGSize(width: 100, height: 100)
        sprite.physicsBody = SKPhysicsBody(circleOfRadius: 50)
        sprite.physicsBody?.categoryBitMask = 2
        sprite.shadowCastBitMask = 1
        sprite.lightingBitMask = 1
        sprite.physicsBody?.isDynamic = false
        addChild(sprite)
    }
}

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))

let scene = GameScene()
scene.scaleMode = .aspectFill
scene.size = sceneView.frame.size
sceneView.presentScene(scene)

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
0 Answers
Related