CameraMath

Screen-space/world-space conversions shared by anything that needs to reason about what a camera sees: the transform gizmo's hit-testing and viewport object picking both need a world ray from a screen point, and both need it to work for orthographic cameras, whose rays are parallel rather than emanating from the camera position.

Public Methods

Unproject

public static Vector3 Unproject(ICamera camera, float ndcX, float ndcY, float ndcZ)

Unprojects a normalized-device-coordinate point at a given depth into world space.

Parameters:

  • camera (ICamera): The camera whose view/projection matrices define the mapping.
  • ndcX (float): X in [-1, 1].
  • ndcY (float): Y in [-1, 1].
  • ndcZ (float): Depth in [0, 1]: 0 is the near plane, 1 is the far plane.

Returns: Vector3

ScreenPointToRay

public static Ray? ScreenPointToRay(ICamera camera, Vector2 screenPos, Vector2 viewportSize)

Builds the world-space ray a screen point casts from camera. Unprojects the point at two depths and derives origin and direction from those, which stays correct for both perspective (rays emanate from the camera) and orthographic cameras (rays are parallel to Front) — unprojecting the camera position alone cannot express the orthographic case.

Parameters:

  • camera (ICamera): The camera the screen point is viewed through.
  • screenPos (Vector2): Pointer position in viewport pixels.
  • viewportSize (Vector2): Viewport size in pixels.

Returns: Ray?

  • null when the viewport has no area or the near/far points coincide.