本文介绍了ARKit Stereo – 是否可以同时运行两个 ARSCNView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对我现有的 AR 应用做一些修改,我想拆分视图并添加到 2 个 ARSCNView
中,这样用户就可以使用 VR Card Box 并有不同的体验但 Xcode 总是让我回来:
Session (0x102617d10): 失败,错误:Error Domain=com.apple.arkit.error Code=102所需的传感器失败."
所以,我假设我不能同时运行 2 个 ARSCNView
会话,还是我错了?
解决方案
答案是:是的,这是可能的.
使用下面的代码来完成它:
导入 UIKit导入场景套件导入 ARKit类视图控制器:UIViewController,ARSCNViewDelegate {@IBOutlet 弱 var 场景视图:ARSCNView!@IBOutlet 弱变量sceneView2:ARSCNView!覆盖 func viewDidLoad() {super.viewDidLoad()场景视图.delegate = selfsceneView.showsStatistics = true让scene = SCNScene(命名为:art.scnassets/ship.scn")!场景视图.场景 = 场景场景视图.isPlaying = true//设置sceneView2sceneView2.scene = 场景sceneView2.showsStatistics = sceneView.showsStatistics//现在sceneView2 开始接收更新sceneView2.isPlaying = true}覆盖 func viewWillAppear(_ 动画:布尔){super.viewWillAppear(动画)让配置 = ARWorldTrackingConfiguration()场景视图.会话.运行(配置)}覆盖 func viewWillDisappear(_ 动画:布尔){super.viewWillDisappear(动画)场景视图.session.pause()}func 渲染器(_ 渲染器:SCNSceneRenderer,updateAtTime 时间:TimeInterval){DispatchQueue.main.async {self.updateFrame()}}功能更新框架(){//为第二个视图克隆 pointOfView让 pointOfView: SCNNode = (sceneView.pointOfView?.clone())!//确定右眼的调整位置让方向:SCNQuaternion = pointOfView.orientation让orientationQuaternion: GLKQuaternion = GLKQuaternionMake(orientation.x,方向.y,方向.z,方向.w)让 eyePos: GLKVector3 = GLKVector3Make(1.0, 0.0, 0.0)让rotatedEyePos: GLKVector3 = GLKQuaternionRotateVector3(orientationQuaternion,眼睛位置)让rotatedEyePosSCNV: SCNVector3 = SCNVector3Make(rotatedEyePos.x,旋转EyePos.y,旋转的EyePos.z)let mag: Float = 0.064//眼间距离(以米为单位)pointOfView.position.x +=rotatedEyePosSCNV.x * magpointOfView.position.y +=rotatedEyePosSCNV.y * magpointOfView.position.z +=rotatedEyePosSCNV.z * mag//为 SecondView 设置 PointOfViewsceneView2.pointOfView = pointOfView}}
有关详细信息,请查看 GitHub 上的
I was thinking to do some modification to my existing AR app, and I wanted to split the view and add inside 2 ARSCNView
in this way users can use the VR Card Box and have a different experience but Xcode is always returning me:
Session (0x102617d10): did fail with error: Error Domain=com.apple.arkit.error Code=102 "Required sensor failed."
So, I'm supposing that I can't run 2 ARSCNView
sessions at the same time, or am I wrong?
解决方案
The answer is: Yes, it's possible.
Use the following code to accomplish it:
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
}
}
这篇关于ARKit Stereo – 是否可以同时运行两个 ARSCNView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!