swift - ARKit,检测何时跟踪可用
问题描述
我正在寻找一种方法来检测空间跟踪何时在 ARKit 中“工作/不工作”,即当 ARKit 有足够的视觉信息来启动 3d 空间跟踪时。
在我尝试过的其他应用程序中,如果 ARKit 没有从摄像头获得足够的信息,则会提示用户使用手机/摄像头环顾四周以恢复空间跟踪。我什至见过带有进度条的应用程序,显示用户需要移动设备才能恢复跟踪。
检测是否可以使用跟踪来检查ARSessions 当前帧有多少rawFeaturePoints的好方法?例如,如果当前帧有超过 100 个 rawFeaturePoints,我们可以假设空间跟踪正在工作。
这是一个好方法,还是 ARKit 中有内置功能或更好的方法来检测空间跟踪是否在我不知道的情况下工作?
解决方案
您可以使用特征点,但我认为这可能是矫枉过正,因为这样的事情可能是一个好的开始:
使用currentFrame
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
}
}
ARSCNViewDelegate
你会在回调中这样称呼它:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
//1. Update The Tracking Status
print(self.augmentedRealitySession.sessionStatus())
}
}
您还可以使用其他委托回调,例如:
func session(_ session: ARSession, didFailWithError error: Error) {
print("The ARSession Failed")
}
func sessionWasInterrupted(_ session: ARSession) {
print("ARSession Was Interupted")
}
这些 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)")
}
如果你想看一个例子,你可以看看这里:特征点示例
希望能帮助到你...
推荐阅读
- c++ - 矢量大小()被 NetBeans 标记为错误:无法解析标识符
- c - C 中的预期表达式
- javascript - MYSQL - PHP - 使用从提交按钮传递的变量更新数据库中的值
- javascript - 从 dataTable 行访问嵌套的对象数组
- date - 如果 golang 中的条件出错
- xaml - 图像未显示在 MVVM XAML Xamarin 表单上
- python - 通过python加载xgen预览
- python - Plotly Scatter3d:如何强制 3D 轴具有相同的比例/纵横比?
- facebook - 使用 v3.1 Graph API Explorer 访问 facebook 信息
- javascript - 基于父属性值的jQuery悬停效果