Class CameraComponentExtensions
Provides a set of static methods for working with CameraComponent instances.
public static class CameraComponentExtensions
- Inheritance
-
CameraComponentExtensions
Remarks
This class includes extension methods for performing various operations with CameraComponent instances, such as raycasting, converting screen positions to world positions, and more. These methods are useful for implementing features like object picking, camera control, and coordinate transformations in a 3D environment.
Methods
CalculateRayFromScreenPosition(CameraComponent, Vector2)
The two ends of the ray through a screen position, as a tuple: the same points as the Start and End of ScreenToWorldRaySegment(CameraComponent, Vector2).
[Obsolete("Use ScreenToWorldRaySegment(screenPosition): its Start and End are these two points. This method will be removed in a later release.")]
public static (Vector3 nearPoint, Vector3 farPoint) CalculateRayFromScreenPosition(this CameraComponent camera, Vector2 screenPosition)
Parameters
cameraCameraComponentThe camera the ray starts from.
screenPositionVector2The normalized screen position, from (0,0) at the top-left to (1,1) at the bottom-right.
Returns
GetOrthographicVisibleBounds(CameraComponent)
The rectangle of the XY plane an orthographic camera looking along Z shows, as its lower-left and upper-right corners in world units: GetOrthographicVisibleSize(CameraComponent) centred on the camera's position. The edges of a 2D scene's view, for placing things at them or keeping things inside them.
public static (Vector2 Min, Vector2 Max) GetOrthographicVisibleBounds(this CameraComponent camera)
Parameters
cameraCameraComponentThe orthographic camera.
Returns
- (Vector2 Min, Vector2 Max)
The lower-left and the upper-right corner of the visible rectangle, in world units.
Exceptions
- ArgumentNullException
If
camerais null.- InvalidOperationException
If the camera is not orthographic.
GetOrthographicVisibleSize(CameraComponent)
The width and height of the world an orthographic camera shows, in world units: the orthographic size is the height, and the width follows the aspect ratio the projection was built with.
public static Vector2 GetOrthographicVisibleSize(this CameraComponent camera)
Parameters
cameraCameraComponentThe orthographic camera.
Returns
- Vector2
The visible width and height, in world units.
Remarks
Read from ProjectionMatrix, so it reflects a custom aspect ratio when one is set, and is right once the camera has been updated for a frame.
Exceptions
- ArgumentNullException
If
camerais null.- InvalidOperationException
If the camera is not orthographic.
IsInScreenRectangle(CameraComponent, Vector3, Vector2, Vector2)
Whether a world position lands inside a rectangle of the screen: the test a marquee selection makes for every candidate once the mouse is released. The corners are normalized screen points, top-left (0,0) to bottom-right (1,1), in either order; a position behind the camera or off the sides of the view is never inside.
public static bool IsInScreenRectangle(this CameraComponent cameraComponent, Vector3 position, Vector2 cornerA, Vector2 cornerB)
Parameters
cameraComponentCameraComponentThe camera the screen belongs to.
positionVector3The world space position.
cornerAVector2One corner of the rectangle, normalized.
cornerBVector2The opposite corner, normalized.
Returns
Exceptions
- ArgumentNullException
If
cameraComponentis null.
LogicDirectionToWorldDirection(CameraComponent, Vector2)
Converts a 2D logical direction into a 3D world direction relative to the camera's orientation.
public static Vector3 LogicDirectionToWorldDirection(this CameraComponent camera, Vector2 logicDirection)
Parameters
cameraCameraComponentThe camera component used for the calculation.
logicDirectionVector2The 2D logical direction (e.g., input from a joystick or keyboard), typically normalized between -1 and 1.
Returns
- Vector3
The corresponding 3D world direction vector.
Remarks
This method is useful for converting 2D input commands into 3D movements in the world, taking into account the camera's current orientation.
LogicDirectionToWorldDirection(CameraComponent, Vector2, Vector3)
Converts a 2D logical direction into a 3D world direction relative to the camera's orientation, using a specified up vector.
public static Vector3 LogicDirectionToWorldDirection(this CameraComponent camera, Vector2 logicDirection, Vector3 upVector)
Parameters
cameraCameraComponentThe camera component used for the calculation.
logicDirectionVector2The 2D logical direction (e.g., input from a joystick or keyboard), typically normalized between -1 and 1.
upVectorVector3The up vector to be used for the calculation, defining the vertical direction in world space.
Returns
- Vector3
The corresponding 3D world direction vector.
Remarks
This method is useful for converting 2D input commands into 3D movements in the world, taking into account the camera's current orientation and a custom vertical orientation.
ScreenToWorldPoint(CameraComponent, Vector3)
Converts the screen position to a point in world coordinates relative to cameraComponent.
public static Vector3 ScreenToWorldPoint(this CameraComponent cameraComponent, Vector3 position)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from the near camera plane.
Returns
- Vector3
The world position calculated from the screen position.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.
ScreenToWorldPoint(CameraComponent, ref Vector3)
Converts the screen position to a point in world coordinates relative to cameraComponent.
public static Vector3 ScreenToWorldPoint(this CameraComponent cameraComponent, ref Vector3 position)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from the near camera plane.
Returns
- Vector3
The world position calculated from the screen position.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.
ScreenToWorldPoint(CameraComponent, ref Vector3, out Vector3)
Converts the screen position to a point in world coordinates relative to cameraComponent.
public static void ScreenToWorldPoint(this CameraComponent cameraComponent, ref Vector3 position, out Vector3 result)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from the near camera plane.
resultVector3Position in world coordinates.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.
ScreenToWorldPointOnXYPlane(CameraComponent, Vector2)
The world point where the ray through a screen position meets the XY plane, Z = 0: a mouse position as a 2D world position, which is what a 2D physics world such as Box2D works in.
public static Vector2? ScreenToWorldPointOnXYPlane(this CameraComponent camera, Vector2 screenPosition)
Parameters
cameraCameraComponentThe camera the ray starts from.
screenPositionVector2The normalized screen position, typically the mouse position, from (0,0) at the top-left to (1,1) at the bottom-right.
Returns
- Vector2?
The point on the plane, or null when the ray is parallel to the plane or the plane lies outside the camera's near and far planes.
ScreenToWorldRay(CameraComponent, Vector2)
A Ray from the camera through a screen position, into the world: the starting point of picking and of a physics ray cast.
public static Ray ScreenToWorldRay(this CameraComponent camera, Vector2 screenPosition)
Parameters
cameraCameraComponentThe camera the ray starts from.
screenPositionVector2The normalized screen position, typically the mouse position, from (0,0) at the top-left to (1,1) at the bottom-right.
Returns
- Ray
The ray, starting on the camera's near plane with a unit direction towards the far plane.
ScreenToWorldRayEnds(CameraComponent, Vector2)
Calculates the near and far vectors for a ray that starts at the camera and passes through a given screen point. The ray is in world space, from the near plane to the far plane, through the specified normalized screen position.
public static (Vector4 VectorNear, Vector4 VectorFar) ScreenToWorldRayEnds(this CameraComponent camera, Vector2 screenPosition)
Parameters
cameraCameraComponentThe camera component used to calculate the ray.
screenPositionVector2The screen position (in normalized coordinates) through which the ray passes.
Returns
- (Vector4 VectorNear, Vector4 VectorFar)
A tuple containing the near vector (
VectorNear) and the far vector (VectorFar) of the ray in world space, each withWof 1.
ScreenToWorldRaySegment(CameraComponent, Vector2)
Converts the screen position to a RaySegment in world coordinates.
public static RaySegment ScreenToWorldRaySegment(this CameraComponent cameraComponent, Vector2 position)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector2The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1).
Returns
- RaySegment
RaySegment, starting at the near plane and ending at the far plane.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.
ScreenToWorldRaySegment(CameraComponent, ref Vector2, out RaySegment)
Converts the screen position to a RaySegment in world coordinates.
public static void ScreenToWorldRaySegment(this CameraComponent cameraComponent, ref Vector2 position, out RaySegment result)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector2The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1).
resultRaySegmentRaySegment, starting at the near plane and ending at the far plane.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.
TryWorldToScreenPoint(CameraComponent, Vector3, out Vector3)
Where a world position lands on screen when it is in view: in front of the camera, between its near and far planes and inside the sides of the view. A label hung on an object, or a marker on a target, drawn only while there is something on screen to point at.
public static bool TryWorldToScreenPoint(this CameraComponent cameraComponent, Vector3 position, out Vector3 screenPoint)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position.
screenPointVector3The screen position in normalized X, Y coordinates, top-left (0,0) to bottom-right (1,1), with Z in world units from the near plane, when the method returns true; otherwise default.
Returns
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation.
Exceptions
- ArgumentNullException
If
cameraComponentis null.
TryWorldToScreenPoint(CameraComponent, Vector3, GraphicsDevice, out Vector2)
Where a world position lands on screen, in pixels of the graphicsDevice's window, when it is
in view; see TryWorldToScreenPoint(CameraComponent, Vector3, out Vector3).
public static bool TryWorldToScreenPoint(this CameraComponent cameraComponent, Vector3 position, GraphicsDevice graphicsDevice, out Vector2 screenPoint)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position.
graphicsDeviceGraphicsDeviceThe graphics device providing the window size.
screenPointVector2The screen position in pixels when the method returns true; otherwise default.
Returns
WorldToClip(CameraComponent, ref Vector3)
Transforms a world-space position to clip space coordinates relative to camera, using the camera's view-projection matrix.
public static Vector3 WorldToClip(this CameraComponent cameraComponent, ref Vector3 position)
Parameters
cameraComponentCameraComponentThe camera component whose view-projection matrix will be used.
positionVector3The world-space position to be transformed.
Returns
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
Thrown if
cameraComponentis null.
WorldToClip(CameraComponent, ref Vector3, out Vector3)
Transforms a world-space position to clip space coordinates relative to camera, using the camera's view-projection matrix.
public static void WorldToClip(this CameraComponent cameraComponent, ref Vector3 position, out Vector3 result)
Parameters
cameraComponentCameraComponentThe camera component whose view-projection matrix will be used.
positionVector3The world-space position to be transformed.
resultVector3The resulting position in clip space.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
Thrown if
cameraComponentis null.
WorldToScreenPoint(CameraComponent, Vector3)
Converts the world position to screen space coordinates relative to camera.
public static Vector3 WorldToScreenPoint(this CameraComponent cameraComponent, Vector3 position)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position to be converted to screen space.
Returns
- Vector3
The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from near camera plane.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
Thrown if
cameraComponentis null.
WorldToScreenPoint(CameraComponent, ref Vector3)
Converts the world position to screen space coordinates relative to camera.
public static Vector3 WorldToScreenPoint(this CameraComponent cameraComponent, ref Vector3 position)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position to be converted to screen space.
Returns
- Vector3
The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from near camera plane.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
Thrown if
cameraComponentis null.
WorldToScreenPoint(CameraComponent, ref Vector3, out Vector3)
Converts the world position to screen space coordinates relative to camera.
public static void WorldToScreenPoint(this CameraComponent cameraComponent, ref Vector3 position, out Vector3 result)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position to be converted to screen space.
resultVector3The screen position in normalized X, Y coordinates. Top-left is (0,0), bottom-right is (1,1). Z is in world units from near camera plane.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
Thrown if
cameraComponentis null.
WorldToScreenPoint(CameraComponent, ref Vector3, GraphicsDevice)
Converts the world position to screen space coordinates relative to cameraComponent and the window size of the graphicsDevice.
public static Vector2 WorldToScreenPoint(this CameraComponent cameraComponent, ref Vector3 position, GraphicsDevice graphicsDevice)
Parameters
cameraComponentCameraComponentThe camera component used to perform the calculation.
positionVector3The world space position to be converted to screen space.
graphicsDeviceGraphicsDeviceThe graphics device providing information about the window size.
Returns
- Vector2
The screen position in pixels: normalized X times the window width, normalized Y times the window height.
Remarks
This method does not update the ViewMatrix or ProjectionMatrix before performing the transformation. If the CameraComponent or its containing Entity TransformComponent has been modified since the last frame, you may need to call the Update() method first.
Exceptions
- ArgumentNullException
If the cameraComponent argument is null.