几天来我一直在试图解决这个问题。
给定一个基于 ARKit 的应用程序,我可以在其中跟踪用户的脸部,如何从其锚点获得脸部的绝对旋转?
我可以获得 ARAnchor 的变换,它是 simd_matrix4x4。
有很多关于如何从该矩阵中获取位置的信息(它是第三列),但没有关于旋转的信息!
我希望能够通过传递 YAW、PITCH 和 ROLL 来控制应用程序外部的 3D 对象。
我最近尝试的事情实际上有些效果:
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° 并继续看着手机,这些值会根据手机的位置发生巨大变化。
我尝试更改 ARConfiguration 中的世界对齐方式,但这并没有什么区别。
我读错参数了吗?这应该容易得多!
我已经想通了...
一旦有了面部锚点,就需要对其变换矩阵和相机的变换进行一些计算。
像这样:
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 将返回面的俯仰、偏航、滚动(分别)
我添加了一个小的乘数并将轴反转 2,所以最终结果如下:
yaw = -rotation.y*3
pitch = -rotation.x*3
roll = rotation.z*1.5
Phew!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)