ios - 在ARSCNView中获取CGPoint的世界坐标(Swift)

标签 ios swift arkit arscnview scnvector3

问题

我希望以下函数返回由点击手势处理的点的世界坐标。我已经正确地实现了它(使用一些在线资源)来处理框架的中心点,但我无法在屏幕上移动该点。

<小时/>

尝试

这是 getScreenCoordinates 的完整注释和工作代码,它返回一个元组,其中包含 (0) 屏幕坐标的方向向量和 (1) 屏幕坐标的位置向量。问题是让这个工作与中心以外的点一起工作,通过 screenCoord 传入。参数。

private func getScreenCoordinates(screenCoord:CGPoint) -> (SCNVector3, SCNVector3) { // (direction, position)
      if let frame = self.canvasView.session.currentFrame {
            let mat = SCNMatrix4(frame.camera.transform) // 4x4 transform matrix describing camera in world space
            let direction = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33) // orientation of camera in world space
            let position = SCNVector3(mat.m41, mat.m42, mat.m43) // location of camera in world space

            return (dir, position)
      }
      return (SCNVector3(0, 0, -1), SCNVector3(0, 0, -0.2))
}
<小时/>

所需的解决方案

提供修改后的getScreenCoordinates返回相同数据类型的函数,只不过不是获取相机框架中心点的世界坐标,而是获取 screenCoord 的世界坐标.

最佳答案

也许代码可以帮助你。

// Get transform using ARCamera
func getCameraTransform(for camera: ARCamer) -> MDLTransform {
  return MDLTransform(transform: camera.transform)
}

// Intercept touch and place object 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  guard let touch = touches.first else { return }
  guard touch.tapCount == 1 else { return }

  guard let camera = sceneView.session.currentFrame?.camera else { return }
  let transform = getCameraTranform(for: camera)
  let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
  let cube = SCNNode(geometry: boxGeometry)
  let position = SCNVector3(
   transform.translation.x,
   transform.translation.y,
   transform.translation.z,
  )
  cube.position = position
  sceneView.scene.rootNode.addChildNode(cube)
}

要查看更多详细信息,请参阅此帖子 https://arvindravi.com/arkit-a-noobs-guide-part-three/

关于ios - 在ARSCNView中获取CGPoint的世界坐标(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49355605/

相关文章:

swift - 从 url 异步加载 anchor 不起作用

ios - 表达式类型 'DataRequest' 在没有更多上下文 Swift 的情况下是不明确的

arrays - 无法分配给属性 : 'itemArray' is a get-only property

ios - 粘三个容器和 iPhone X 的一个问题

ios - SceneKit 物理在 ARKit 中不能正常工作

ios - ARKit + Core 位置 - 点不固定在同一个地方

iOS SwiftUI : ObservableObject from parent to child

ios - UICollectionView 从 NSAttributedString 图像滚动时滞后/断断续续

ios - 如何使用 Swift 在 iOS 应用程序上存储持久数据?

iOS:UIView 外的实心边框