ios - ARKit 根据触摸位置设置 ARAnchor 变换

标签 ios sprite-kit transform arkit

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

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)
  }
}

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

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

最佳答案

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

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

您会发现这会依次调用 ViewController.worldPositionFromScreenPosition(_:objectPos:infinitePlane:)

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

  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.

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

关于ios - ARKit 根据触摸位置设置 ARAnchor 变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45488881/

相关文章:

silverlight - 用于在 Silverlight 中拖放的 TranslateTransform

html - chrome 14 中的 webkit-transform 问题

objective-c - 继续从 dequeueReusableCellWithIdentifier 获取 nil?

ios - 为什么我的 uiswitch 变得困惑

ios - Desire2Learn:我如何将文件附加到讨论中的新帖子?

swift - NSTimer 重复方式快

ios - 集成SWRevealViewController后,屏幕全黑

sprite-kit - 速度: Meters per second or pixels per second?

ios - SpriteKit : how to turn touches into vectors of uniform speed?

python-3.x - python : How to replace "datetime" in a Dataframe with only the Day represented as integer value?