ARKit 1.5 如何获得垂直平面的旋转 [英] ARKit 1.5 how to get the rotation of a vertical plane

查看:27
本文介绍了ARKit 1.5 如何获得垂直平面的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm experimenting with the vertical plane, and I'm trying to place a node on a wall with the correct rotation based on that vertical plane.

这是被点击的垂直平面的 ARHitTestResult:

here's the ARHitTestResult of the vertical plane that gets tapped:

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

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

neither one of them seem to work.

这就是我希望做的:

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

如果有人能帮我解决这个问题,我将不胜感激,因为我还没有找到很多关于 ARKit 的教程.

I'd much appreciate it if someone could help me out with this as I can't find a lot of tutorials on ARKit yet.

编辑

所以这是有效的(感谢 Josh):

so here's what worked (thanks to 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 更准确,只是想提一下.

Note: .worldOrientation is way more accurate than .rotation in this case, just wanted to mention this.

推荐答案

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

Is this what your looking for? Here I am using the screen center for the CGPoint value, but you can use touch etc:

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

更新:如果你想旋转与平面关联的节点,你可以这样:

Update: If you want to rotation of the node associated with the Plane couldn't you could get it like so:

 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)

 }

这篇关于ARKit 1.5 如何获得垂直平面的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆