I have a pointcloud in Unity and I am doing some image recognition stuff. After the image recognition is finished, I need the screen points of the pointcloud's points in screenspace, from the camera frame, when I started the image recognition. As the image recognition take some time, at least a few frames have passed and the camera has a new transform (position, rotation).
How can I get the screen space points from an old camera position? (From a saved projection matrix and camera-to-world matrix)
I have found the solution on my own:
Save the projectionMatrix and worldToCameraMatrix of you camera and then run this:
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);