首页 文章

ARKit,检测何时可用跟踪

提问于
浏览
0

我正在寻找一种方法来检测何时空间跟踪在 ARKit 中“ working/not 工作”,何时 i.e 当 ARKit 具有足够的可视信息来启动 3d 空间跟踪时。

在我尝试过的其他应用中,如果 ARKit 无法从相机中获取足够的信息,则会提示用户使用 phone/camera 环顾四周以恢复空间跟踪。我什至看过带有进度条的应用程序,该进度条显示用户需要多得多的移动设备才能恢复跟踪。

一种检测跟踪是否可用以检查 ARSessions 当前帧有多少rawFeaturePoints的好方法? E.g 如果当前帧有超过 100 个 rawFeaturePoints,我们可以认为空间跟踪有效。

这是个好方法吗,还是 ARKit 中有内置的功能或更好的方法来检测空间跟踪是否在起作用,而我不知道呢?

1 回答

  • 3

    您可以使用特征点,但是我认为这可能是过大的,因为这样的事情可能是一个好的开始:

    使用ARSessioncurrentFrame,您可以像这样获取当前的跟踪状态:

    //------------------------------------------------
    //MARK: ARSession Extension To Log Tracking States
    //------------------------------------------------
    
    extension ARSession{
    
        /// Returns The Status Of The Current ARSession
        ///
        /// - Returns: String
        func sessionStatus() -> String? {
    
            //1. Get The Current Frame
            guard let frame = self.currentFrame else { return nil }
    
            var status = "Preparing Device.."
    
            //1. Return The Current Tracking State & Lighting Conditions
            switch frame.camera.trackingState {
    
            case .normal:                                                   status = ""
            case .notAvailable:                                             status = "Tracking Unavailable"
            case .limited(.excessiveMotion):                                status = "Please Slow Your Movement"
            case .limited(.insufficientFeatures):                           status = "Try To Point At A Flat Surface"
            case .limited(.initializing):                                   status = "Initializing"
            case .limited(.relocalizing):                                   status = "Relocalizing"
    
            }
    
            guard let lightEstimate = frame.lightEstimate?.ambientIntensity else { return nil }
    
            if lightEstimate < 100 { status = "Lighting Is Too Dark" }
    
            return status
    
        }
    
    }
    

    您将在ARSCNViewDelegate回调中调用类似的名称:

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    
          DispatchQueue.main.async {
    
                //1. Update The Tracking Status
                print(self.augmentedRealitySession.sessionStatus())
    
          }
     }
    

    e.g 也可以使用其他委托回调:

    func session(_ session: ARSession, didFailWithError error: Error) {
    
        print("The ARSession Failed")
    }
    
    func sessionWasInterrupted(_ session: ARSession) {
    
        print("ARSession Was Interupted")
    }
    

    这些 ARKit 准则还提供了一些有关如何处理这些状态的有用信息:苹果指南

    如果您确实想跟踪featurePoints的数量,则可以执行以下操作:

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    
        guard let currentFrame = self.augmentedRealitySession.currentFrame,
        let featurePointCount = currentFrame.rawFeaturePoints?.points.count else { return }
    
        print("Number Of Feature Points In Current Session = \(featurePointCount)")
    
    }
    

    如果您想查看示例,可以在这里查看:特征点示例

    希望能帮助到你...

相关问题