SceneKit camera causes objects to Not Show Up

I want to be able to create a custom camera node in SceneKit and use it in my scene.

However, I've been encountering a very strange issue with SceneKit:

  • If I create a SCNCamera, attach it to a node, and add the node to my scene, then nothing shows up in my scene.
  • If I don't use a SCNCamera, the objects in my scene render correctly.

Also, if I try to access the camera node via sceneView.pointOfView, I get nil, even though sceneView.allowsCameraControl is set to true

This is the code I am using:

import UIKit
import SceneKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let sceneView = SCNView()
        sceneView.frame = self.view.frame
        self.view.addSubview(sceneView)
        
        let scene = SCNScene()
        
        sceneView.autoenablesDefaultLighting = true
        sceneView.allowsCameraControl = true
        
        let cameraNode = SCNNode()
        // If the below line of code is commented out (so no SCNCamera is added), everything shows up
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)
        
        let sphere = SCNSphere(radius: 5)
        sphere.firstMaterial?.diffuse.contents = UIColor.red
        
        let sphereNode = SCNNode(geometry: sphere)
        cameraNode.addChildNode(sphereNode)

        sceneView.backgroundColor = UIColor.green
        sceneView.scene = scene

    }
}

This seems pretty straightforward, yet I can't find any reason why this is happening on SO, etc.

There's a recent post on the Apple dev forums that may be related: https://forums.developer.apple.com/thread/129049

Terms of Service

Privacy Policy

Cookie Policy