swift ARRaycast查询和前置摄像头(ARFaceTrackingConfiguration)

c9qzyr3d  于 2024-01-05  发布在  Swift
关注(0)|答案(1)|浏览(164)

我已经搜索了所有的文档,可能只是错过了一些非常重要的东西。光线投射是否可以在前置True Depth摄像头上使用,比如iPad Pro上?
我目前正在做一个应用程序,我在ARFaceTrackingConfiguration中,从屏幕中心进行简单的光线投射没有产生结果。
使用后置摄像头的世界配置中的相同代码正在产生结果。此代码直接来自文档。

  1. let raycastQuery: ARRaycastQuery? = sceneView.raycastQuery(
  2. from: sceneView.center,
  3. allowing: .estimatedPlane,
  4. alignment: .any)
  5. let rayresults: [ARRaycastResult] = sceneView.session.raycast(raycastQuery!)
  6. print(rayresults)

字符串
我的理解是,考虑到背景模糊的面部跟踪深度图的例子,前置摄像头将具有与后置摄像头基本相同的深度数据,只是可用的总深度距离较少。
由于这段代码返回了后置摄像头的结果集,但没有返回前置摄像头的结果,我想知道这是不是因为光线的方向?对于前置摄像头,如果支持rayCast,也许它需要光线在负z方向投射?但我还没有成功。
该项目的目标是能够在3D空间中获得2点之间的距离,但使用前置摄像头而不是后置摄像头。
谢谢你让我明白了我很感激

dced5bon

dced5bon1#

ARFaceTrackingConfig点击测试

很明显,基于World Tracking配置用于检测平面的ARKit光线投射方法不适用于Face Tracking场景。在您的情况下,您需要使用SceneKit的hit-testing。对于hit-testing,您将无法使用estimatedPlane,你需要一个canonical face mask。当使用前置摄像头时,你对XZ坐标的理解是完全正确的-XZ平面是镜像的。另外值得注意的是,相机的IR TrueDepth传感器在10到100 cm的距离下“工作”。ARFaceAnchor仅在此范围内生成。
在iPad Pro上的Swift Playgrounds应用中运行此代码。

  1. import ARKit
  2. import SceneKit
  3. import PlaygroundSupport
  4. class ViewController : UIViewController {
  5. var sceneView = ARSCNView(frame: .zero)
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. self.view = self.sceneView
  9. sceneView.delegate = self
  10. sceneView.scene = SCNScene()
  11. let config = ARFaceTrackingConfiguration()
  12. config.maximumNumberOfTrackedFaces = 1
  13. sceneView.session.run(config)
  14. }
  15. }
  1. extension ViewController {
  2. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  3. let location: CGPoint? = touches.first?.location(in: sceneView)
  4. var hitTestOptions = [SCNHitTestOption: Any]()
  5. hitTestOptions[.boundingBoxOnly] = true
  6. let hitTestResults: [SCNHitTestResult] = sceneView.hitTest(location!,
  7. options: hitTestOptions)
  8. guard let hit = hitTestResults.first else { return }
  9. let sphere = SCNNode(geometry: SCNSphere(radius: 0.005))
  10. sphere.simdPosition = hit.simdWorldCoordinates
  11. sceneView.scene.rootNode.addChildNode(sphere)
  12. }
  13. }
  1. extension ViewController : ARSCNViewDelegate {
  2. func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
  3. let faceGeo = ARSCNFaceGeometry(device: sceneView.device!)
  4. let node = SCNNode(geometry: faceGeo)
  5. node.geometry?.firstMaterial?.lightingModel = .physicallyBased
  6. node.geometry?.firstMaterial?.diffuse.contents = UIColor.green
  7. return node
  8. }
  9. func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode,
  10. for anchor: ARAnchor) {
  11. if let faceAnchor = anchor as? ARFaceAnchor,
  12. let faceGeo = node.geometry as? ARSCNFaceGeometry {
  13. faceGeo.update(from: faceAnchor.geometry)
  14. }
  15. }
  16. }
  17. PlaygroundPage.current.needsIndefiniteExecution = true
  18. PlaygroundPage.current.liveView = ViewController()
展开查看全部

相关问题