1

问题

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


试图

这是 的完整注释和工作代码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.

4

1 回答 1

-1

也许代码可以帮助你。

// 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/

于 2020-02-02T14:10:35.687 回答