0

在此处输入图像描述在此处输入图像描述

我想使用 Sprite Kit 创建一个具有 3D 效果的戒指。(见图片)

我尝试将 SKNode 子类化并添加两个节点作为子节点。(见代码)

一个节点是完整的 SKShapeNode 椭圆,另一个是使用具有更高 zPosition 的 SKCropNode 的半椭圆。

看起来不错,但 SKCropNode 将应用程序 CPU 使用率从 40% 提高到 99%。

关于如何降低 SKCropNode 性能成本的任何想法,或创建相同环 3D 效果的任何替代方法?

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
    }    
}
4

2 回答 2

2

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.

于 2015-12-14T23:33:03.367 回答
0

您可以尝试将您的转换SKShapeNodeSKSpriteNode. 您可以使用SKView textureFromNode:(但我们知道规模问题要求您仅在将节点添加到视图并且至少运行一个更新周期后使用它),或从头开始使用图像(使用CGBitmapContext,以编程方式创建课程)。

于 2015-12-14T23:36:06.063 回答