场景工具包 - 获取相机方向

5
我需要确定相机朝向哪个方向,例如是否朝向Z+Z-X+X-
我尝试使用eulerAngles,但是偏航的范围为0 -> 90 -> 0 -> -90 -> 0,这意味着我只能检测出相机是否朝向ZX,而无法确定其是否朝向这些轴的正或负方向。

你可以使用点积来完成这个操作。 - Fattie
1个回答

14

您可以创建一个SCNNode并将其放置在worldFront属性中,以获取具有x、y和z方向的向量。

另一种方法是像这个项目所做的那样:

// Credit to https://github.com/farice/ARShooter

func getUserVector() -> (SCNVector3, SCNVector3) { // (direction, position)
        if let frame = self.sceneView.session.currentFrame {
            let mat = SCNMatrix4(frame.camera.transform) // 4x4 transform matrix describing camera in world space
            let dir = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33) // orientation of camera in world space
            let pos = SCNVector3(mat.m41, mat.m42, mat.m43) // location of camera in world space

            return (dir, pos)
        }
        return (SCNVector3(0, 0, -1), SCNVector3(0, 0, -0.2))
    }

1
同时,这也假定您正在使用ARKit,因为您的标签表明了这一点。 - wriuhasdfhvhasdv
我没有明白,在计算“dir”之后如何知道方向? - lady

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接