2

我正在考虑对我现有的 AR 应用程序进行一些修改,我想拆分视图并添加到 2ARSCNView中,这样用户可以使用 VR Card Box 并获得不同的体验,但 Xcode 总是返回我:

Session (0x102617d10): did fail with error: Error Domain=com.apple.arkit.error Code=102 "Required sensor failed."

所以,我假设我不能同时运行 2 个ARSCNView会话,还是我错了?

4

2 回答 2

5

答案是:是的,有可能

使用以下代码来完成它:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet weak var sceneView: ARSCNView!
    @IBOutlet weak var sceneView2: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        sceneView.delegate = self
        sceneView.showsStatistics = true
        let scene = SCNScene(named: "art.scnassets/ship.scn")!
        sceneView.scene = scene
        sceneView.isPlaying = true

        // Setup for sceneView2
        sceneView2.scene = scene
        sceneView2.showsStatistics = sceneView.showsStatistics

        // Now sceneView2 starts receiving updates
        sceneView2.isPlaying = true     
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let configuration = ARWorldTrackingConfiguration()
        sceneView.session.run(configuration)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        DispatchQueue.main.async {
            self.updateFrame()
        }
    }
    func updateFrame() {   
        // Clone pointOfView for Second View
        let pointOfView: SCNNode = (sceneView.pointOfView?.clone())!

        // Determine Adjusted Position for Right Eye
        let orientation: SCNQuaternion = pointOfView.orientation
        let orientationQuaternion: GLKQuaternion = GLKQuaternionMake(orientation.x, 
                                                                     orientation.y, 
                                                                     orientation.z, 
                                                                     orientation.w)
        let eyePos: GLKVector3 = GLKVector3Make(1.0, 0.0, 0.0)
        let rotatedEyePos: GLKVector3 = GLKQuaternionRotateVector3(orientationQuaternion, 
                                                                   eyePos)
        let rotatedEyePosSCNV: SCNVector3 = SCNVector3Make(rotatedEyePos.x, 
                                                           rotatedEyePos.y, 
                                                           rotatedEyePos.z)  
        let mag: Float = 0.064 // Interocular distance (in metres)
        pointOfView.position.x += rotatedEyePosSCNV.x * mag
        pointOfView.position.y += rotatedEyePosSCNV.y * mag
        pointOfView.position.z += rotatedEyePosSCNV.z * mag

        // Set PointOfView for SecondView
        sceneView2.pointOfView = pointOfView    
    }
}

有关更多详细信息,请查看GitHub 上的此项目。

在此处输入图像描述

于 2018-12-09T13:11:38.223 回答
2

不,或者至少你可能会一直出错。似乎当您同时使用 ARSCN 和 ARSKVIEW 视图时,会出现传感器错误。可能是因为隐私?

于 2019-08-04T07:55:53.153 回答