ios - ARKit 在 ARWorldTrackingConfiguration 和 ARFaceTrackingConfiguration 之间切换 - 后置和前置摄像头

标签 ios swift augmented-reality arkit arscnview

在我的项目中,我想在 ARWorldTrackingConfigurationARFaceTrackingConfiguration 之间切换。

我使用两种不同类型的 View :ARSCNView 使用后置摄像头,以及 ARView 进行面部跟踪。 首先,我启动 ARSCNView,之后,如果用户愿意,他可以切换到面部跟踪

我在此模式下启动 View Controller :

sceneView.delegate = self
sceneView.session.delegate = self

            // Set up scene content.
setupCamera()            
sceneView.scene.rootNode.addChildNode(focusSquare)

let configurationBack  = ARWorldTrackingConfiguration(
configurationBack.isAutoFocusEnabled = true
configurationBack.planeDetection = [.horizontal, .vertical]


sceneView.session.run(configurationBack, options: [.resetTracking, .removeExistingAnchors])

然后我加载我的对象 (.scn)

当我想切换到前置摄像头并传递到 ARView 时,我会这样做:

 let configurationFront  = ARFaceTrackingConfiguration()

  // here I stop my ARSCNView session
  self.sceneView.session.pause()

    self.myArView = ARView.init(frame: self.sceneView.frame)

    self.myArView!.session.run(configurationFront)
    self.myArView!.session.delegate = self

    self.view.insertSubview(self.myArView!, aboveSubview: self.sceneView)

然后我加载我的.rcproject

所以当我尝试返回后置摄像头并再次传递到 ARWorldTracking 时,我的问题从这里开始。

这是我的方法:

// remove my ARView with face tracking
        self.myArView?.session.pause()

        UIView.animate(withDuration: 0.2, animations: {
            self.myArView?.alpha = 0
        }) { (true) in
            self.myArView?.removeFromSuperview()
            self.myArView = nil

        }
// here I restart the initial ARSCNView
    let configurationBack  = ARWorldTrackingConfiguration(
        configurationBack.isAutoFocusEnabled = true
        configurationBack.planeDetection = [.horizontal, .vertical]


    session.run(configurationBack, options: [.resetTracking, .removeExistingAnchors])

当我切换到后置摄像头时,传感器无法正确跟踪飞机。

我该如何解决这个问题,那么如何在 ARWorldTrackingConfiguration 和 ARFaceTrackingConfiguration 之间正确切换?

提前致谢

最佳答案

在暂停 session 时,请确保同时删除场景中添加的所有节点。在使用 self.sceneView.session.pause() 暂停 session 后添加以下代码:

self.sceneView.scene.rootNode.enumerateChildNodes { (childNode, _) in
    childNode.removeFromParentNode()
}

关于ios - ARKit 在 ARWorldTrackingConfiguration 和 ARFaceTrackingConfiguration 之间切换 - 后置和前置摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663720/

相关文章:

ios - iOS 是否存储用户的性别?

iphone - UITextfield 的行为类似于 email.app 中的 "to:"UItextfield

swift - 改变 NSLayoutConstraint

ios - 在展开 UIButton 设置标题的可选值时意外发现 nil

arrays - 如何在 Swift 中遍历不同类的所有属性?

ios - ARKit中何时使用worldTransform()以及何时使用transform()

objective-c - 单例模式实现

android - 如何在 ARCore/Sceneform 中加速平面检测

java - ARCore – WorldScale 和 LocalScale 有什么区别?

objective-c - 带有 Objective-C 的 MVC。 View 的格式化数据发生在哪里?