Color legend plain property method
A position and an orientation in the world, with the view and projection matrices to draw what it sees, rays for picking and a floating origin for large worlds.
A class. The quick start guide shows it in context, with code.
Names are colored by what they are: a property or a method; values and fields keep the plain color.
Properties
AspectRatio →
float · The width of the view over its height. 4/3 by default; set it from the viewport. Throws ArgumentOutOfRangeException for a value that isn’t positive and finite.
Controller →
BaseCameraController? · The controller driving the camera, or null to drive it by hand. Assigning one detaches the previous controller, takes the new one from any other camera it drives, and lets it take over this camera’s state once. Assigning the one already attached does nothing.
FarPlane →
float · The distance of the far clip plane. 1000 by default. Throws ArgumentOutOfRangeException for a value that isn’t positive and finite; Update throws InvalidOperationException when it isn’t greater than NearPlane.
FieldOfViewDegrees →
float · The vertical field of view, in degrees. 45 by default. Throws ArgumentOutOfRangeException unless it is strictly between 0 and 180.
Forward →
Vector3 · Read-only. The direction the camera faces, its local −Z, as a unit vector in world space. Follows Orientation at once, before the next update.
NearPlane →
float · The distance of the near clip plane. 0.1 by default. Throws ArgumentOutOfRangeException for a value that isn’t positive and finite; Update throws InvalidOperationException when it isn’t less than FarPlane.
Offset →
Vector3 · Read-only. The floating origin: a whole number of 1000-unit cells on each axis, moved by Update once the camera is more than a cell from it. Subtract it from world positions before drawing.
Orientation →
Quaternion · The rotation from the camera’s own axes to the world’s, stored normalized. Identity, looking down −Z, for Camera(). Throws ArgumentException for a quaternion of length zero, or with a NaN or an infinite component.
Position →
Vector3 · Where the camera is, in world coordinates. The origin for Camera().
Projection →
Matrix · Read-only. The perspective projection of the field of view, the aspect ratio and the clip planes, as of the last Update.
RebasedPosition →
Vector3 · Read-only. Position minus Offset: where the camera is in the coordinates the view matrix is built in.
Right →
Vector3 · Read-only. The camera’s right, its local +X, as a unit vector in world space. Follows Orientation at once.
Up →
Vector3 · Read-only. The camera’s up, its local +Y, as a unit vector in world space. Follows Orientation at once.
View →
Matrix · Read-only. The view matrix, built from RebasedPosition and Orientation as of the last Update.
Methods
Camera() →
Camera() · A camera at the origin looking down −Z, with a 45° field of view, a 4:3 aspect ratio and clip planes at 0.1 and 1000. Its matrices are ready at once.
Camera(Vector3 position, Vector3 target, Vector3 up) · The same settings, at the position, turned to face the target.
| Parameter | Type | Description |
|---|---|---|
position |
Vector3 |
Where the camera is, in world coordinates. |
target |
Vector3 |
The point it faces. One at position leaves it looking down −Z. |
up |
Vector3 |
The up it levels its roll against, usually Vector3.Up. |
LookAt() →
LookAt(Vector3 worldTarget, Vector3 worldUp) · Turns the camera to face the target, with no roll against worldUp. A target at the camera’s position changes nothing. A target along worldUp takes world +Y as the roll reference instead, and one straight above or below gets the default view tipped up or down by 90°. An attached free camera replaces the orientation at its next update.
| Parameter | Type | Description |
|---|---|---|
worldTarget |
Vector3 |
The point to face, in world coordinates. |
worldUp |
Vector3 |
The up the view levels its roll against. |
ScreenPointToRay() →
ScreenPointToRay(Vector2 pixel, Viewport viewport) · A Ray in world coordinates through the pixel, starting on the near plane, with a unit direction. Worked out from the current pose and projection, so it needs no Update after they change, and accurate to about 10−7 radians however far the camera is from the origin.
| Parameter | Type | Description |
|---|---|---|
pixel |
Vector2 |
The point in window coordinates, from the top left corner, as the mouse gives it. |
viewport |
Viewport |
The viewport the camera draws into. Its position in the window is taken into account. |
Update() →
Update(float elapsedSeconds) · Runs the controller, if there is one, then moves the floating origin and rebuilds the view and the projection where they changed. Call it once per frame, before drawing. Throws InvalidOperationException when NearPlane isn’t less than FarPlane.
| Parameter | Type | Description |
|---|---|---|
elapsedSeconds |
float |
The seconds since the last update, passed on to the controller. |