2017-06-18 38 views
1

在相机内放置对象我会想放置2条线路中的当前摄像机视图的边界作为流量:ARkit - 日提交的视图

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

回答

0

可以使用SCNSceneRenderer来unproject屏幕的界限在摄像机空间:

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