首页 > 解决方案 > ARKit 在 ARWorldTrackingConfiguration 和 ARFaceTrackingConfiguration 之间切换 - 后置和前置摄像头

问题描述

ARWorldTrackingConfiguration在我的项目中,我想在和之间切换ARFaceTrackingConfiguration

我使用两种不同类型的视图 aARSCNView使用后置摄像头和 aARView进行面部跟踪。首先我启动ARSCNView之后,如果用户愿意,他可以切换到人脸跟踪

我以这种模式启动我的视图控制器:

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 之间正确切换?

提前致谢

标签: iosswiftaugmented-realityarkitarscnview

解决方案


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

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

推荐阅读