0

I want to be able to create a custom camera node in SceneKit and view my scene from it (instead of the default camera).

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

  • If I use SCNCamera, nothing shows up in my scene.
  • If I don't use SCNCamera, the objects in my scene render correctly.

This is the code I am using (very simple code; from a tutorial):

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.

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

Any help is appreciated!

RodYt
  • 157
  • 2
  • 4
  • There's a recent post on the Apple dev forums that may be related: https://forums.developer.apple.com/thread/129049 – RodYt Feb 13 '20 at 13:12

1 Answers1

1

The sphere is a child node of the camera, without any offset (its position is (0, 0, 0)) and so the camera is inside the sphere. And if the sphere's material isn't doubleSided then you won't see anything.

mnuages
  • 13,049
  • 2
  • 23
  • 40
  • Ah yes, I can at least see the sphere now. Thank you. But I'm still wondering why pointOfView is nil in this example? Shouldn't SceneKit automatically create a camera node for you? – RodYt Feb 13 '20 at 19:22
  • It does but it's not user accessible. – mnuages Feb 13 '20 at 19:22
  • 1
    If I understand you correctly, what you mean is that the default cameraNode is not accessible by default. In my case however, I have set allowsCameraControl = true, which according to: https://stackoverflow.com/questions/24768031/can-i-get-the-scnview-camera-position-when-using-allowscameracontrol/30278102 will create a default camera node accessible through the .pointOfView property. Yet, when I try to access it, the property returns nil. Could you please elaborate on exactly what you mean by it's not user accessible? – RodYt Feb 14 '20 at 03:21