ARkit - 将对象放置在摄像机视野内

时间:2017-06-18 18:10:19

标签: swift augmented-reality scenekit projection-matrix arkit

我想在当前摄像机视图的边界中放置2行作为流程:

enter image description here

来源 - SceneKit docs

从ARKit文档中,我了解到我需要projectionMatrix,但我如何计算来自" zNear"的差异?到" zFar"和x \ y?

我从这段代码开始:

let cameraProjectionMatrix = session.currentFrame?.camera.projectionMatrix 
let cameraPosition = SCNVector3.positionFromTransform(cameraProjectionMatrix)
let rightBoxNode = SCNNode(geometry: SCNBox(...))
rightBoxNode.position = SCNVector3(???)
sceneView.scene.rootNode.addChildNode(rightBoxNode)

对于左边的我可能需要

var leftPos = rightboxNode.position
leftPos.x = rightboxNode.position.x * -1
leftBoxNode.position = leftPos

但是我在尝试计算rightboxNode.position时失败了:

rightBoxNode.position = SCNVector3(x: x1 ,y: y1 z: zNear)

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用SCNSceneRenderer在相机空间中取消投影屏幕边界:

func renderer(_ sender: SCNSceneRenderer,  updateAtTime time: TimeInterval) {
  let position = sender.unprojectPoint(SCNVector3(0, 0, 0))
  // x, y in screen coordinate space, z in [0, 1] corresponds to [zNear, zFar] in a way that I don't think is proportional
  let node = SCNNode()
  node.position = position
  ...
}