2017-06-18 61 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 
    ... 
}