Rendering issues (objects blurred, frame dimensions = 1) when SKScene is inside SwiftUI context

I am developing an app that contains a SpriteKit scene embedded inside a SwiftUI context.


I am trying to render a simple SKShapeNode like this:


import Foundation
import SpriteKit


class UIConnection: SKShapeNode {

    override init() {
        super.init()
        self.fillColor = UIColor.clear
        self.strokeColor = UIColor.blue
        self.lineWidth = 0.01
    }
    
    func updatePositions(node1 : CGPoint, node2 : CGPoint) {
        let path = CGMutablePath()
        path.move(to: node1)
        path.addLine(to: CGPoint(x: node1.x, y: node2.y))
        path.addLine(to: CGPoint(x: node2.x, y: node2.y))
        path.addLine(to: CGPoint(x: node2.x, y: node1.y))
        path.addLine(to: node1)
        self.path = path
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

When I am adding that Node inside the standard SpriteKit game project template in XCode (without any SwiftUI things), everything is working fine, I get a non-filled rectangle with a blue outline. The frame.width and frame.height variables are valid pixel dimensions like 750 x 1334.


let node = UIConnection()
self.addChild(node)
     
node.updatePositions(node1: CGPoint(x: frame.midX-frame.width/4, y: frame.midY-frame.height/4), node2: CGPoint(x: frame.midX+frame.width/4, y: frame.midY+frame.height/4))


Now I want to embed the SKScene inside a SwiftUI context using this approach:

https://dev.to/sgchipman/using-spritekit-with-swiftui-ibc


First thing I encounter is the fact that frame.width and frame.height both result in a value of 1.

Adding the above node to the embedded SKScene results in an extremely blurred rectangle, so no solid line but some weird cloudy object.

Answered by s_zapo in 695139022

Answering my own question: The view/scene had a size of 1x1 pixels stretched out, that was the root cause.

Accepted Answer

Answering my own question: The view/scene had a size of 1x1 pixels stretched out, that was the root cause.

Rendering issues (objects blurred, frame dimensions = 1) when SKScene is inside SwiftUI context
 
 
Q