我正在寻找一种方法来检测 ARKit 中空间跟踪何时“工作/不工作”,即 ARKit 何时有足够的视觉信息来启动 3d 空间跟踪。
在我尝试过的其他应用程序中,如果 ARKit 没有从相机获取足够的信息,系统会提示用户使用手机/相机环顾四周以恢复空间跟踪。我什至见过带有进度条的应用程序,显示用户需要移动设备多少才能恢复跟踪。
是否有一个好方法来检测跟踪是否可用于检查ARSessions 当前帧有多少rawFeaturePoints ?例如,如果当前帧有超过 100 个 rawFeaturePoints,我们可以假设空间跟踪正在工作。
这是否是一个好方法,或者 ARKit 中是否有内置功能或更好的方法来检测空间跟踪是否正在工作,而我不知道?
您可以使用特征点,但我认为这可能有点过头了,因为这样的事情可能是一个好的开始:
使用currentFrameof anARSession您可以获取当前的跟踪状态,如下所示:
//------------------------------------------------
//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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在回调中这样称呼它ARSCNViewDelegate:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
//1. Update The Tracking Status
print(self.augmentedRealitySession.sessionStatus())
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用其他委托回调,例如:
func session(_ session: ARSession, didFailWithError error: Error) {
print("The ARSession Failed")
}
func sessionWasInterrupted(_ session: ARSession) {
print("ARSession Was Interupted")
}
Run Code Online (Sandbox Code Playgroud)
这些 ARKit 指南还提供了一些关于如何处理这些状态的有用信息:Apple 指南
如果您确实想跟踪数量,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)")
}
Run Code Online (Sandbox Code Playgroud)
如果您想查看示例,可以查看此处:特征点示例
希望能帮助到你...
| 归档时间: |
|
| 查看次数: |
2049 次 |
| 最近记录: |