gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:15:51 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com