Using result of ProjectWorldToScreen for DeprojectScreenToWorld does not produce initial input data

I am using this logic to project a point in world space into screen space.



const FVector Location = FVector(1, 2, 3);

FIntRect Rect;
Rect.Max.X = ViewportSize.X;
Rect.Max.Y = ViewportSize.Y;
FVector2D CurrentScreenSpaceCoords;

const FMatrix ViewProjectionMatrix = SceneView->ViewMatrices.GetViewProjectionMatrix();
SceneView->ProjectWorldToScreen(Location, Rect, ViewProjectionMatrix, CurrentScreenSpaceCoords);

I am using this code to get the initial value back



FMatrix const InvViewProjMatrix = ViewProjectionMatrix.Inverse();
FVector WorldPosition;
FVector WorldDirection;
FSceneView::DeprojectScreenToWorld(CurrentScreenSpaceCoords, Rect, InvViewProjMatrix, WorldPosition, WorldDirection);


However the result of DeprojectScreenToWorld does not produce the initial input (Location).
Instead of giving me the expected vector of [1, 2, 3], it gives me X = [69.8139954,3840.00073, 250.49826]

Somebody knows:

  1. Why this happens?
  2. How to get the initial value back from it

Thanks in advance

Hmm just thinking about it.

  • ProjectWorldToScreen: an infinite number of world (xyz) locations project to a single screen (xy) location (along a ray from the camera eye, to the object, intersecting the pixel on the view plane)

  • ProjectScreenToWorld: only one world location can be returned, the exact point where a ray casts from the camera eye, to the object, and intersects the pixel on the view plane

I think that is what you are asking …