我如何才能绝对获得ARAnchor的偏航,俯仰和滚动? [英] How can I get the yaw, pitch, roll of an ARAnchor in absolute terms?

查看:130
本文介绍了我如何才能绝对获得ARAnchor的偏航,俯仰和滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在努力解决这个问题.

I've been trying to figure this out for a few days now.

考虑到一个基于ARKit的应用程序,该应用程序可以在其中跟踪用户的脸部,如何从其锚点绝对地获取脸部的旋转度?

Given an ARKit-based app where I track a user's face, how can I get the face's rotation in absolute terms, from its anchor?

我可以得到ARAnchor的变换,它是simd_matrix4x4.有很多关于如何从该矩阵中获取位置的信息(这是第三列),但是旋转上没有任何信息!

I can get the transform of the ARAnchor, which is a simd_matrix4x4. There's a lot of info on how to get the position out of that matrix (it's the 3rd column), but nothing on the rotation!

我希望能够通过传递YAW,PITCH和ROLL来控制应用程序外部的3D对象.

I want to be able to control a 3D object outside of the app, by passing YAW, PITCH and ROLL.

我尝试过的最新的东西实际上可以起作用:

The latest I thing I tried actually works somewhat:

let arFrame = session.currentFrame!
guard let faceAnchor = arFrame.anchors[0] as? ARFaceAnchor else { return }

let faceMatrix = SCNMatrix4.init(faceAnchor.transform)
let node = SCNNode()

node.transform = faceMatrix
let rotation = node.worldOrientation

rotation.x .y和.z具有我可以使用的值,但是当我移动手机时,这些值会改变.例如,如果我转动180°并继续注视着手机,则其值会根据手机的位置而发生巨大变化.

rotation.x .y and .z have values I could use, but as I move my phone the values change. For instance, if I turn 180˚ and keep looking at the phone, the values change wildly based on the position of the phone.

我尝试在ARConfiguration中更改世界对齐方式,但这没什么不同.

I tried changing the world alignment in the ARConfiguration, but that didn't make a difference.

我读错了参数吗?这本来应该容易得多!

Am I reading the wrong parameters? This should have been a lot easier!

推荐答案

我知道了...

一旦有了脸部定位点,就需要对其变换矩阵和相机的变换进行一些计算.

Once you have the face anchor, some calculations need to happen with its transform matrix, and the camera's transform.

赞:

let arFrame = session.currentFrame!
guard let faceAnchor = arFrame.anchors[0] as? ARFaceAnchor else { return }

let projectionMatrix = arFrame.camera.projectionMatrix(for: .portrait, viewportSize: self.sceneView.bounds.size, zNear: 0.001, zFar: 1000)
let viewMatrix = arFrame.camera.viewMatrix(for: .portrait)

let projectionViewMatrix = simd_mul(projectionMatrix, viewMatrix)
let modelMatrix = faceAnchor.transform
let mvpMatrix = simd_mul(projectionViewMatrix, modelMatrix)

// This allows me to just get a .x .y .z rotation from the matrix, without having to do crazy calculations
let newFaceMatrix = SCNMatrix4.init(mvpMatrix)
let faceNode = SCNNode()
faceNode.transform = newFaceMatrix
let rotation = vector_float3(faceNode.worldOrientation.x, faceNode.worldOrientation.y, faceNode.worldOrientation.z)

rotation.x .y和.z将分别返回面部的俯仰,偏航,横滚

rotation.x .y and .z will return the face's pitch, yaw, roll (respectively)

我要添加一个小的乘数并将轴的2反转,因此最终结果如下:

I'm adding a small multiplier and inverting 2 of the axis, so it ends up like this:

yaw = -rotation.y*3
pitch = -rotation.x*3
roll = rotation.z*1.5

Ph!

这篇关于我如何才能绝对获得ARAnchor的偏航,俯仰和滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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