gpt4 book ai didi

swift - 我怎样才能绝对地获得 ARAnchor 的偏航、俯仰和滚动?

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:27 26 4
gpt4 key购买 nike

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

给定一个基于 ARKit 的应用程序,我可以在其中跟踪用户的面部,我如何才能从它的 anchor 获得面部的绝对旋转?

我可以获得 ARAnchor 的变换,它是一个 simd_matrix4x4。有很多关于如何从该矩阵(它是第 3 列)中获取位置的信息,但没有关于旋转的信息!

我希望能够通过传递 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 中更改世界对齐方式,但这并没有什么不同。

我是否读取了错误的参数?这应该容易得多!

最佳答案

我想通了...

有了面部 anchor 后,需要对其变换矩阵和相机变换进行一些计算。

像这样:

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

呸!

关于swift - 我怎样才能绝对地获得 ARAnchor 的偏航、俯仰和滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com