SceneKit - 获取相机的方向 [英] SceneKit – Get direction of camera
问题描述
我需要找出相机正在看哪个方向,例如如果它正在寻找 Z+
、Z-
、X+
或 X-
.
I need to find out which direction a camera is looking at, e.g. if it is looking towards Z+
, Z-
, X+
, or X-
.
我试过使用 eulerAngles
,但偏航的范围是 0
-> 90
-> 0
> -> -90
-> 0
这意味着我只能检测相机是否正在朝向 Z
或 X
>,而不是看那些轴的正方向或负方向.
I've tried using eulerAngles
, but the range for yaw goes 0
-> 90
-> 0
-> -90
-> 0
which means I can only detect if the camera is looking towards Z
or X
, not if it's looking towards the positive or negative directions of those axes.
推荐答案
您可以创建一个 SCNNode,将其放置在 worldFront 属性中以获取具有 x、y 和 z 方向的向量.
You can create an SCNNode that place it in worldFront property to get a vector with the x, y, and z direction.
另一种你可以做到的方式就像这个项目是如何做到的:
Another way you could do it is like how this project did it:
// 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))
}
这篇关于SceneKit - 获取相机的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!