问题描述
这几天我一直在想办法解决这个问题.
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.有很多关于如何从矩阵中获取位置的信息(它是第 3 列),但没有关于旋转的信息!
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
呸!
这篇关于如何以绝对值获得 ARAnchor 的偏航、俯仰、滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!