1

我正在尝试将 ARKit SceneKit 视图嵌入到 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.
    }
    */

}

Xcode故事板等的屏幕截图

4

1 回答 1

0

这里的问题是您尚未配置 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))
    }

   }

}

希望能帮助到你...

于 2018-03-06T00:07:40.003 回答