ARkit 场景套件嵌入到视图控制器中

ARkit Scene Kit Embed Into View Controller

我正在尝试将 ARKit SceneKit View 嵌入到 Xcode 9.2 中的视图控制器中。但是,当我这样做时,我的场景正确显示,但我的相机失去了相机视图。它进入黑色背景。有人知道为什么以及如何解决吗?

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.
    }
    */

}

这里的问题是您没有配置 ARSession 或 ARWorldTrackingConfiguration。

当然我没有你的模型等,但这表明 'ARView' 很好。

代码如下所示:

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))
    }

   }

}

希望对您有所帮助...