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

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

这是被挖掘的垂直平面的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更准确,只是想提一下。

这是你在找什么? 这里我使用屏幕中心的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, 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) }