dobranoc
dobranoc

Reputation: 475

ARKit session's currentFrame is always nil

I try to get the camera transform from the currentFrame.camera property of the ARSession.

Why do I keep getting the sceneView.session.currentFrame? as nil?

Upvotes: 4

Views: 2030

Answers (1)

Hal Mueller
Hal Mueller

Reputation: 7655

I just ran a quick test and saw that the session.currentFrame is nil the first few passes through the render loop. That lag varies, it might be two or a dozen. But eventually currentFrame comes around.

class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate, SCNSceneRendererDelegate {

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Create a session configuration
    let configuration = ARWorldTrackingSessionConfiguration()
    configuration.worldAlignment = .gravityAndHeading
    configuration.planeDetection = .horizontal

    sceneView.session.delegate = self

    // Run the view's session
    sceneView.session.run(configuration)
    print(#function, sceneView.session.currentFrame)

}

// MARK: - SCNSceneRendererDelegate
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
    print(#function, sceneView.session.currentFrame)
}

// MARK: - ARSessionDelegate
func session(_ session: ARSession, didUpdate frame: ARFrame) {
    print(#function, sceneView.session.currentFrame)
}

Gives us

renderer(_:didRenderScene:atTime:) nil
...
session(_:didUpdate:) Optional(<ARFrame: 0x1c42cff10 timestamp=6334.963644 capturedImage=0x1c0124d80 camera=0x1c41262c0 lightEstimate=0x1c4238640 | no anchors, 0 features>)
...
renderer(_:didRenderScene:atTime:) Optional(<ARFrame: 0x1c02c9c30 timestamp=6334.996948 capturedImage=0x1c0124b00 camera=0x1c0124920 lightEstimate=0x1c02251a0 | no anchors, 0 features>)

Upvotes: 2

Related Questions