ARKit 1.5如何获得垂直平面的旋转

时间:2018-02-27 14:56:09

标签: ios swift arkit

我正在试验垂直平面,并且我试图将一个节点放在墙上,并根据垂直平面进行正确的旋转。

这里是被挖掘的垂直平面的ARHitTestResult:

let hitLocation = sceneView.hitTest(touchPoint, types: .existingPlaneUsingExtent)

我尝试过以下方法:

let hitRotation = hitLocation.first?.worldTransform.columns.2

let anchor = hitLocation.first?.anchor
let hitRotation = anchor?.transform.columns.2

它们中的任何一个似乎都不起作用。

这就是我希望做的事情:

boardNode.eulerAngles = SCNVector3((hitRotation?.x)!, (hitRotation?.y)!, 0)

如果有人可以帮助我,我会非常感激,因为我还无法找到很多关于ARKit的教程。

修改

所以这里有效(感谢Josh):

let hit = sceneView.hitTest(touchPoint, types: .existingPlaneUsingExtent)
let planeAnchor = hit.first?.anchor as? ARPlaneAnchor

guard let anchoredNode =  sceneView.node(for: planeAnchor!) else { return }

let anchorNodeOrientation = anchoredNode.worldOrientation

boardNode.eulerAngles.y = .pi * anchorNodeOrientation.y

注意:在这种情况下,.worldOrientation比.rotation更准确,只是想提一下。

1 个答案:

答案 0 :(得分:6)

这是你想要的吗?这里我使用屏幕中心作为CGPoint值,但你可以使用触摸等:

func addObjectToScreen() {

        if let hit = self.sceneView?.hitTest(self.viewCenter, types: [.existingPlaneUsingExtent]).last {

            let hitTestTransform = SCNMatrix4(hit.worldTransform)

            let vector = SCNVector3Make(hitTestTransform.m41, hitTestTransform.m42, hitTestTransform.m43)

            node.position = vector

            return
        }
}

更新:如果你想要旋转与Plane关联的节点,你可能不会这样:

 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

      print(node.rotation)

}

然后根据需要使用它?

进一步更新:

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

 /*
 1. Get The Current Touch Location
 2. Check That We Have Touched A Valid Node
 3. Check That Our Touched Object Is An ARPlane Anchor
 */

 guard let touchLocation = touches.first?.location(in: augmentedRealityView),
 let hitTest = augmentedRealityView.hitTest(touchLocation, types: .existingPlaneUsingExtent).first,
 let planeAnchor = hitTest.anchor as? ARPlaneAnchor
 else {

 // No Valid Plane Has Been Detected So Hide The Plane Information Label

 return

 }

    //We Have A Valid Plane So Display It's Current Info
    guard let anchoredNode =  augmentedRealityView.node(for: planeAnchor) else { return }
    print(anchoredNode.rotation)

 }