如何从过去的相机位置获取 WorldToScreenPoint?
How can I get the WorldToScreenPoint from a camera position in the past?
我在 Unity 中有一个点云,我正在做一些图像识别的事情。图像识别完成后,我需要点云的屏幕点在屏幕space,从相机框架开始图像识别时的屏幕点。由于图像识别需要一些时间,至少经过几帧并且相机有新的变换(位置,旋转)。
如何从旧相机位置获取屏幕 space 点? (来自保存的投影矩阵和相机到世界矩阵)
我自己找到了解决方案:
保存你相机的projectionMatrix和worldToCameraMatrix
然后 运行 这个:
Matrix4x4 matrix = projectionMatrix * worldToCameraMatrix;
Vector3 screenPos = matrix.MultiplyPoint(destination.transform.position);
// (-1, 1)'s clip => (0 ,1)'s viewport
screenPos = new Vector3(screenPos.x + 1f, screenPos.y + 1f, screenPos.z + 1f) / 2f;
// viewport => screen
screenPos = new Vector3(screenPos.x * Screen.width, screenPos.y * Screen.height, screenPos.z);
var unityScreenPos = new Vector2(screenPos.x, Screen.height - screenPos.y);
我在 Unity 中有一个点云,我正在做一些图像识别的事情。图像识别完成后,我需要点云的屏幕点在屏幕space,从相机框架开始图像识别时的屏幕点。由于图像识别需要一些时间,至少经过几帧并且相机有新的变换(位置,旋转)。
如何从旧相机位置获取屏幕 space 点? (来自保存的投影矩阵和相机到世界矩阵)
我自己找到了解决方案:
保存你相机的projectionMatrix和worldToCameraMatrix 然后 运行 这个:
Matrix4x4 matrix = projectionMatrix * worldToCameraMatrix;
Vector3 screenPos = matrix.MultiplyPoint(destination.transform.position);
// (-1, 1)'s clip => (0 ,1)'s viewport
screenPos = new Vector3(screenPos.x + 1f, screenPos.y + 1f, screenPos.z + 1f) / 2f;
// viewport => screen
screenPos = new Vector3(screenPos.x * Screen.width, screenPos.y * Screen.height, screenPos.z);
var unityScreenPos = new Vector2(screenPos.x, Screen.height - screenPos.y);