Search code examples
iosxcodearkitsceneview

ARkit Scene Kit Embed Into View Controller


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


Solution

  • 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...