Search code examples
iosswiftsprite-kitskcropnode

How to create a ring with 3D effect using Sprite Kit?


enter image description hereenter image description here

I want to create a ring with a 3D effect using Sprite Kit. (SEE IMAGES)

I tried subclassing a SKNode and adding two nodes as children. (SEE CODE)

One node was a complete SKShapeNode ellipse, and the other was half ellipse using SKCropNode with a higher zPosition.

It looks good, but the SKCropNode increases the app CPU usage from 40% to 99%.

Any ideas on how to reduce the SKCropNode performance cost, or any alternative to create the same ring 3D effect?

class RingNode: SKNode {

    let size: CGSize

    init(size: CGSize, color: SKColor)
    {
        self.size = size
        self.color = color

        super.init()

        ringPartsSetup()
    }

    private func ringPartsSetup() {

        // LEFT PART (half ellipse)
        let ellipseNodeLeft = getEllipseNode()
        let leftMask = SKSpriteNode(texture: nil, color: SKColor.blackColor(), size: CGSize(
            width: ellipseNodeLeft.frame.size.width/2,
            height: ellipseNodeLeft.frame.size.height))
        leftMask.anchorPoint = CGPoint(x: 0, y: 0.5)
        leftMask.position = CGPoint(x: -ellipseNodeLeft.frame.size.width/2, y: 0)
        let leftNode = SKCropNode()
        leftNode.addChild(ellipseNodeLeft)
        leftNode.maskNode = leftMask
        leftNode.zPosition = 10 // Higher zPosition for 3D effect
        leftNode.position = CGPoint(x: -leftNode.frame.size.width/4, y: 0)
        addChild(leftNode)

        // RIGHT PART (complete ellipse)
        let rightNode = getEllipseNode()
        rightNode.position = CGPoint(x: 0, y: 0)
        rightNode.zPosition = 5
        addChild(rightNode)
    }

    private func getEllipseNode() -> SKShapeNode {
        let ellipseNode = SKShapeNode(ellipseOfSize: CGSize(
            width: size.width,
            height: size.height))
        ellipseNode.strokeColor = SKColor.blackColor()
        ellipseNode.lineWidth = 5
        return ellipseNode
    }    
}

Solution

  • You've got the right idea with your two-layer approach and the half-slips on top. But instead of using a shape node inside a crop node, why not just use a shape node whose path is a half-ellipse? Create one using either CGPath or UIBezierPath API — use a circular arc with a transform to make it elliptical — then create your SKShapeNode from that path.