iosswiftsprite-kitscenekit

SK3DNode taking up the whole screen despite the frame


I have the following minimum reproducible code:

class GameViewController: UIViewController {
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    if let view = self.view as! SKView? {
      
      let scene = SKScene()
      let node3D = SK3DNode(viewportSize: CGSize(width: 200, height: 200))
      node3D.position = CGPoint(x: 100, y: 100)
      
      let scene3D = SCNScene()
      node3D.scnScene = scene3D
      scene.addChild(node3D)
      
      let camera = SCNCamera()
      let cameraNode = SCNNode()
      cameraNode.camera = camera
      cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
      scene3D.rootNode.addChildNode(cameraNode)
      
      
      let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.1)
      box.firstMaterial?.diffuse.contents = UIColor.green
      scene3D.rootNode.addChildNode(SCNNode(geometry: box))
      scene3D.background.contents = UIColor.red
      
      
      
      
        
      view.presentScene(scene)
      
      view.ignoresSiblingOrder = true
      
      view.showsFPS = true
      view.showsNodeCount = true
    }
  }
}

Here I simply put a SK3DNode at bottom left corner. When I set breakpoint, I also see the frame of the SK3DNode to be (0, 0, 200, 200), which is correct.

However, when I see the node under view hierarchy, I found it taking up the whole screen, which is quite odd. See the pic below:

enter image description here


Solution

  • It turns out that this behavior only happens when SKScene does not have a size. It works again after I specify the size of the scene:

    
          let scene = SKScene(size: view.frame.size)
    

    I don't know how this is related. Could be a bug or some undocumented behavior.