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

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

问题描述

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



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

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

我尝试过以下方法:

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

 让anchor = hitLocation.first?.anchor 
让hitRotation =锚?.transform.columns.2

他们中的任何一个似乎都没有这是我希望做的事情:

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

如果有人可以帮我解决这个问题,我会非常感激,因为我还没有找到很多关于ARKit的教程。



编辑



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

  let hit = sceneView.hitTest(touchPoint,types:.existingPlaneUsingExtent)
让planeAnchor = hit.first?.anchor为? 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(){

如果让hit = self.sceneView?.hitTest(self.viewCenter,types:[。expertPlaneUsingExtent])。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,用于锚:ARAnchor){

print(node.rotation)

}

然后根据需要使用它?



进一步更新:

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

/ *
1.获取当前触摸位置
2.检查我们是否触摸了有效节点
3.检查我们的触摸对象是否为ARPlane锚点
* /

guard let touchLocation = touches.first ?。location(in:augmentedRealityView),
let hitTest = augmentedRealityView.hitTest(touchLocation,types:.existingPlaneUsingExtent)。第一,
让planeAnchor = hitTest.anchor为? ARPlaneAnchor
else {

//未检测到有效平面因此隐藏平面信息标签

返回

}

//我们有一个有效的平面所以显示它的当前信息
guard let anchoredNode = augmentedRealityView.node(for:planeAnchor)else {return}
print(anchoredNode.rotation)

}


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.

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

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

I've tried the following:

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

and

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

neither one of them seem to work.

And this is what I'm hoping to do:

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

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.

EDIT

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

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

解决方案

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)

}

Then use it as you need?

Further Update:

   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天全站免登陆