ARKit 根据触摸位置设置 ARAnchor 变换 [英] ARKit set ARAnchor transform based on touch location

查看:35
本文介绍了ARKit 根据触摸位置设置 ARAnchor 变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XCode 9 上的 AR 入门应用程序,其中锚点是在点击场景中创建的:

override func touchesBegan(_ touches: Set, with event: UIEvent?) {守卫让sceneView = self.view as?ARSK查看其他{返回}//使用相机的当前位置创建锚点如果让 currentFrame = sceneView.session.currentFrame {//在前面创建一个平移 0.2 米的变换//相机的var 翻译 = matrix_identity_float4x4翻译.columns.3.z = -0.2让变换 = simd_mul(currentFrame.camera.transform, translation)//为会话添加一个新的锚点让锚= ARAnchor(变换:变换)SceneView.session.add(锚点:锚点)}}

无论我点击何处,这始终会导致在屏幕中间创建锚点,这是有道理的,因为我们正在获取当前相机变换并仅对其应用 z 轴上的平移.

我希望将锚点放置在我实际用手指轻敲的位置.我可以使用 touches.first?.location(in:sceneView) 获取水龙头的位置,即与屏幕左上角的距离(以点为单位),但我不确定如何将这些 2D pt 坐标应用于以米为单位的锚点变换,也不是它们应用于哪个轴.

解决方案

我假设您指的是 Apple 的 ARKitExample 项目在增强现实中放置虚拟对象".

查看方法 VirtualObject.translateBasedOnScreenPos(_:instantly:infinitePlane:) 当您在屏幕中移动(已放置)模型时调用的方法 - 基本上需要解决您描述的相同问题.

你会发现这反过来调用了ViewController.worldPositionFromScreenPosition(_:objectPos:infinitePlane:).

从这个方法中提取出来,他们的做法是:

<块引用>

  1. 总是先对现有的平面锚进行命中测试.(如果任何此类锚点存在且仅在其范围内.)

  2. 通过对特征点云的命中测试来收集有关环境的更多信息,但尚未返回结果.

  3. 如果需要或有必要(没有好的特征命中测试结果):针对无限的水平面进行命中测试(忽略现实世界).

  4. 如果可用,如果针对无限平面的命中测试是针对高质量特征的命中测试的结果,则返回跳过或未命中无限平面.

  5. 作为最后的手段,对特征进行第二次未过滤的命中测试.如果场景中没有特征,结果返回此处将为零.

如您所见,他们考虑了可能适用于或可能不适用于您的用例的各个方面.考虑研究和重复使用他们的方法(部分).

I'm playing around with the AR starter app on XCode 9 where anchors are created in a scene on tap:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  guard let sceneView = self.view as? ARSKView else {
    return
  }

  // Create anchor using the camera’s current position
  if let currentFrame = sceneView.session.currentFrame {

    // Create a transform with a translation of 0.2 meters in front     
    // of the camera
    var translation = matrix_identity_float4x4
    translation.columns.3.z = -0.2
    let transform = simd_mul(currentFrame.camera.transform, translation)

    // Add a new anchor to the session
    let anchor = ARAnchor(transform: transform)
    sceneView.session.add(anchor: anchor)
  }
}

This always results in the anchor being created in the middle of the screen regardless of where I tap, which makes sense, as we're getting the current camera transform and applying only a translation in the z axis to it.

I would like the anchor instead to be placed where I actually tapped with my finger. I can get the location of the tap using touches.first?.location(in: sceneView), i.e., just the distance from the top left corner of the screen in points, but I'm unsure how to apply these 2D pt coordinates to the anchor transform in meters, nor which axis they apply to.

解决方案

I assume you're referring to Apple's ARKitExample project "Placing Virtual Objects in Augmented Reality".

Have a look at the method VirtualObject.translateBasedOnScreenPos(_:instantly:infinitePlane:) that is being called when you're moving an (already placed) model in the screen—that basically needs to solve the same problems that you describe.

You'll find that this in turn calls ViewController.worldPositionFromScreenPosition(_:objectPos:infinitePlane:).

Extracted from this method, their approach is:

  1. Always do a hit test against exisiting plane anchors first. (If any such anchors exist & only within their extents.)

  2. Collect more information about the environment by hit testing against the feature point cloud, but do not return the result yet.

  3. If desired or necessary (no good feature hit test result): Hit test against an infinite, horizontal plane (ignoring the real world).

  4. If available, return the result of the hit test against high quality features if the hit tests against infinite planes were skipped or no infinite plane was hit.

  5. As a last resort, perform a second, unfiltered hit test against features. If there are no features in the scene, the result returned here will be nil.

As you can see, they consider various aspects that may-or-may-not apply to your use-case. Consider studying and re-using (parts of) their approach.

这篇关于ARKit 根据触摸位置设置 ARAnchor 变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆