1

I am trying to embed a ARKit SceneKit View into a view controller in Xcode 9.2. However, when I do this my scene shows correctly, but I lose the camera view from my camera. It goes to a black background. Any one know why and how I can fix?

import UIKit
import SceneKit
import ARKit

class ViewControllerAR: UIViewController, ARSCNViewDelegate {

   
    
    @IBOutlet var sceneViewAr: ARSCNView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneViewAr.delegate = self
        
        // Show statistics such as fps and timing information
        sceneViewAr.showsStatistics = true
        
        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/sodium.scn")!
        
        
        // Set the scene to the view
        sceneViewAr.scene = scene
        
        let mainnode = scene.rootNode.childNode(withName: "mainnode", recursively: true)
        mainnode?.runAction(SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 0))
        
        
        let orbit = scene.rootNode.childNode(withName: "orbit", recursively: true)
        orbit?.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
        
        if let orbittwo = scene.rootNode.childNode(withName: "orbit2", recursively: true) {
            orbittwo.runAction(SCNAction.rotateBy(x: 0, y: -2 * 50, z: 0, duration: 100))
        }
        
        if let orbitthree = scene.rootNode.childNode(withName: "orbit3", recursively: true) {
            orbitthree.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

screen shot of Xcode storyboard etc

aheze
  • 24,434
  • 8
  • 68
  • 125

1 Answers1

0

The issue here is that you haven't configured the ARSession or ARWorldTrackingConfiguration.

Of course I don't have your models etc, but this shows the 'ARView' fine.

Here is what the code should look like:

class ViewController: UIViewController {

@IBOutlet weak var sceneViewAr: ARSCNView?
let configuration = ARWorldTrackingConfiguration()
let augmentedRealitySession = ARSession()


//--------------------
//MARK: View LifeCycle
//--------------------

override func viewDidLoad() {

    super.viewDidLoad()

    //1. Run The ARSession
    augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors])

    sceneViewAr?.session = augmentedRealitySession

    loadModels()

}

override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }


func loadModels(){

    // Create a new scene
    let scene = SCNScene(named: "art.scnassets/sodium.scn")!

    // Set the scene to the view
    sceneViewAr?.scene = scene

    let mainnode = scene.rootNode.childNode(withName: "mainnode", recursively: true)
    mainnode?.runAction(SCNAction.rotateBy(x: 10, y: 0, z: 0, duration: 0))


    let orbit = scene.rootNode.childNode(withName: "orbit", recursively: true)
    orbit?.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))

    if let orbittwo = scene.rootNode.childNode(withName: "orbit2", recursively: true) {
        orbittwo.runAction(SCNAction.rotateBy(x: 0, y: -2 * 50, z: 0, duration: 100))
    }

    if let orbitthree = scene.rootNode.childNode(withName: "orbit3", recursively: true) {
        orbitthree.runAction(SCNAction.rotateBy(x: 0, y: 2 * 50, z: 0, duration: 100))
    }

   }

}

Hope it helps...

BlackMirrorz
  • 7,217
  • 2
  • 20
  • 31
  • Thanks, but this line has an error: sceneViewAr.run(configuration, options: [.resetTracking, .removeExistingAnchors]). Value of type 'ARSCNView' has no member 'run – Neil Bergenroth Mar 06 '18 at 00:26
  • Update answer: augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors]) – BlackMirrorz Mar 06 '18 at 00:31
  • Thank you, I really appreciate it. Are there any resources you would recommend that I start to read? I wouldn't have been able to figure that out with you. Thank you again. – Neil Bergenroth Mar 06 '18 at 00:34
  • Just google ARKit Tutorials :) There are plenty there. Also try Ray Wenderlich, Udacity, and Udemy as well :) Also can you do me a favour and mark this as correct please :) – BlackMirrorz Mar 06 '18 at 00:37