Class Camera
Namespace: ValveResourceFormat.Renderer
Assembly: Renderer.dll
Perspective camera with view and projection matrix management.
public class CameraInheritance
object ← Camera
Constructors
Camera(float)
Initializes a new camera with a default position, 16:9 viewport, and the given field of view.
public Camera(float fieldOfView = 90)Parameters
fieldOfView float
Initial field of view in degrees (horizontal at 4:3, see Camera.FieldOfView).
Properties
AspectRatio
Viewport width divided by height.
public float AspectRatio { get; }Property Value
float
BillboardMatrix
Turns a quad in the view plane to face the camera: the rotation half of Camera.CameraViewMatrix, inverted.
public Matrix4x4 BillboardMatrix { get; }Property Value
CameraViewMatrix
World-to-view transform matrix.
public Matrix4x4 CameraViewMatrix { get; }Property Value
FarPlane
Distance from the eye to the far plane, or float.PositiveInfinity for none, the default. Call Camera.CreateProjectionMatrix() after changing it.
public float FarPlane { get; set; }Property Value
float
FieldOfView
Horizontal field of view in degrees. Converted to the vertical FOV actually used to build Camera.ProjectionMatrix by Camera.GetFOV().
public float FieldOfView { get; set; }Property Value
float
Forward
Unit vector pointing in the camera's look direction.
public Vector3 Forward { get; }Property Value
Location
World-space position of the camera.
public Vector3 Location { get; set; }Property Value
NearPlane
Distance from the eye to the near plane. Also where the light cull pass clips light volumes and where its depth slices start. Call Camera.CreateProjectionMatrix() after changing it.
public float NearPlane { get; set; }Property Value
float
Pitch
Vertical rotation angle in radians, positive looking down, as in a Source QAngle.
public float Pitch { get; set; }Property Value
float
ProjectionMatrix
Perspective projection matrix (reverse-Z; the far plane is infinite unless Camera.FarPlane is set).
public Matrix4x4 ProjectionMatrix { get; }Property Value
Right
Unit vector pointing to the camera's right.
public Vector3 Right { get; }Property Value
Roll
Clockwise rotation angle around the camera's forward axis in radians.
public float Roll { get; set; }Property Value
float
Up
Unit vector pointing upward from the camera's perspective.
public Vector3 Up { get; }Property Value
ViewFrustum
Frustum derived from the current view-projection matrix, used for culling.
public Frustum ViewFrustum { get; }Property Value
ViewProjectionMatrix
Combined world-to-clip transform matrix.
public Matrix4x4 ViewProjectionMatrix { get; }Property Value
WindowSize
Current viewport dimensions in pixels.
public Vector2 WindowSize { get; }Property Value
Yaw
Horizontal rotation angle in radians.
public float Yaw { get; set; }Property Value
float
Methods
Calculate4By3Fov(float)
Converts a horizontal field of view at a 4:3 aspect ratio to the equivalent vertical field of view in radians. The result is used directly as the vertical FOV in Camera.CreatePerspectiveFieldOfView_ReverseZ regardless of the actual aspect ratio, so a wider screen sees more to the sides rather than less above and below (Hor+ scaling).
public static float Calculate4By3Fov(float horizontalDegreesAt4By3)Parameters
horizontalDegreesAt4By3 float
Horizontal field of view in degrees at a 4:3 aspect ratio.
Returns
float
ClampRotation()
Clamps Camera.Pitch to prevent the camera from flipping upside-down.
public void ClampRotation()CopyFrom(Camera)
Copies all transform state from another camera into this one.
public void CopyFrom(Camera fromOther)Parameters
fromOther Camera
The camera to copy from.
CreateProjectionMatrix()
Rebuilds Camera.ProjectionMatrix from the current field of view and aspect ratio.
public void CreateProjectionMatrix()FrameObject(Vector3, float, float, float)
Positions the camera so the specified bounding box fills the view.
public void FrameObject(Vector3 objectPosition, float width, float height, float depth)Parameters
objectPosition Vector3
Center of the object to frame.
width float
Width of the object's bounding box.
height float
Height of the object's bounding box.
depth float
Depth of the object's bounding box.
FrameObjectFromAngle(Vector3, float, float, float, float, float)
Positions the camera at the given yaw/pitch angle so the bounding box fills the view.
public void FrameObjectFromAngle(Vector3 objectPosition, float width, float height, float depth, float yaw, float pitch)Parameters
objectPosition Vector3
Center of the object to frame.
width float
Width of the object's bounding box.
height float
Height of the object's bounding box.
depth float
Depth of the object's bounding box.
yaw float
Horizontal angle in radians.
pitch float
Vertical angle in radians, positive looking down.
GetDirectionVectors(float, float, float)
Computes the forward, up, and right vectors a camera with the given orientation would have.
public static (Vector3 Forward, Vector3 Up, Vector3 Right) GetDirectionVectors(float pitch, float yaw, float roll)Parameters
pitch float
Pitch in radians.
yaw float
Yaw in radians.
roll float
Roll in radians.
Returns
(Vector3 Forward, Vector3 Up, Vector3 Right)
GetFOV()
Returns the vertical FOV in radians used to build Camera.ProjectionMatrix, converted from Camera.FieldOfView via Camera.Calculate4By3Fov(float).
public float GetFOV()Returns
float
GetFramingDistance(Vector3, Vector3, Vector3)
Distance from the center of a box to the camera at which the whole box fits on screen.
public float GetFramingDistance(Vector3 size, Vector3 right, Vector3 up)Parameters
size Vector3
The box's dimensions.
right Vector3
The camera's right vector at the angle it frames the box from.
up Vector3
The camera's up vector at that angle.
Returns
float
GetFramingDistance(Vector3, Vector3)
Distance from the center of a box to the camera at which the whole box fits on screen.
public float GetFramingDistance(Vector3 size, Vector3 forward)Parameters
size Vector3
The box's dimensions.
forward Vector3
Unit direction the camera looks along, in place of its own.
Returns
float
GetPixelRemapTo(Camera, Vector2)
Maps a pixel of this camera's view to the one the same world ray lands on in target's view, as an xy scale and a zw bias in pixels. This is what lets a pass drawn at a different field of view find its light cull tile from gl_FragCoord alone.
public Vector4 GetPixelRemapTo(Camera target, Vector2 viewportSize)Parameters
target Camera
The camera whose screen the result maps into.
viewportSize Vector2
Pixel dimensions both cameras draw into.
Returns
Remarks
Exact while the two cameras differ only in projection, which is what the viewmodel is. Both turn a view ray into ndc as ndc = scale * (x / -z), so eliminating the ray leaves an affine map with no dependence on distance along it.
GetQAngle()
The camera's orientation as a Source QAngle, the inverse of Camera.SetFromQAngle(Vector3).
public Vector3 GetQAngle()Returns
The QAngle, as (pitch, yaw, roll) in degrees.
LookAt(Vector3)
Orients the camera to face the given world-space target.
public void LookAt(Vector3 target)Parameters
target Vector3
RecalculateDirectionVectors()
Recomputes Camera.Forward, Camera.Up, and Camera.Right vectors from the current pitch, yaw, and roll.
public void RecalculateDirectionVectors()RecalculateMatrices()
Recomputes the view and view-projection matrices (and updates the view frustum) from the current location and orientation. The projection matrix is not rebuilt here (see Camera.CreateProjectionMatrix()).
public void RecalculateMatrices()ScreenPointToDirection(Vector2)
Gets the direction from the camera through a point of its viewport.
public Vector3 ScreenPointToDirection(Vector2 screenPoint)Parameters
screenPoint Vector2
The point, in pixels from the top left of the viewport.
Returns
SetFromQAngle(Vector3)
Adopts the orientation of a Source QAngle, as read from an entity's "angles" keyvalue.
public void SetFromQAngle(Vector3 anglesDegrees)Parameters
anglesDegrees Vector3
The QAngle, as (pitch, yaw, roll) in degrees.
Remarks
Only the degrees to radians conversion, keeping the QAngle's order and signs. All three are adopted, so angles that carry no roll clear whatever roll was there rather than leaving it to show through. Not clamped, so an entity may point the camera anywhere.
SetFromTransformMatrix(Matrix4x4)
Sets the camera position and orientation from a transform matrix.
public void SetFromTransformMatrix(Matrix4x4 matrix)Parameters
matrix Matrix4x4
Transform matrix whose translation and first row determine position and direction.
SetLocation(Vector3)
Sets Camera.Location without recalculating matrices.
public void SetLocation(Vector3 location)Parameters
location Vector3
SetLocationPitchYaw(Vector3, float, float)
Sets Camera.Location, Camera.Pitch, and Camera.Yaw without recalculating matrices.
public void SetLocationPitchYaw(Vector3 location, float pitch, float yaw)Parameters
location Vector3
pitch float
yaw float
SetOrthographicProjection(float, float, float, float)
Replaces Camera.ProjectionMatrix with a reverse-Z orthographic projection centred on the view axis, until the next Camera.CreateProjectionMatrix(). Call Camera.RecalculateMatrices() after it.
public void SetOrthographicProjection(float width, float height, float nearPlane, float farPlane)Parameters
width float
The width of the view volume.
height float
The height of the view volume.
nearPlane float
Distance along the view direction to the near plane, which may be behind the eye.
farPlane float
Distance along the view direction to the far plane.
SetViewConstants(ViewConstants)
Writes camera matrices and direction vectors into the provided view constants object.
public void SetViewConstants(ViewConstants viewConstants)Parameters
viewConstants ViewConstants
View constants object to populate.
SetViewProjection(Matrix4x4, Matrix4x4)
Sets the view and projection matrices, leaving Camera.Location and the orientation unchanged.
public void SetViewProjection(Matrix4x4 view, Matrix4x4 projection)Parameters
view Matrix4x4
projection Matrix4x4
SetViewportSize(int, int)
Updates the viewport dimensions and rebuilds the projection matrix.
public void SetViewportSize(int viewportWidth, int viewportHeight)Parameters
viewportWidth int
viewportHeight int
WorldToScreenPoint(Vector3)
Gets the viewport position of a point in the world.
public Vector2? WorldToScreenPoint(Vector3 position)Parameters
position Vector3
The point.
Returns
The point in pixels from the top left of the viewport, or null when it is behind the camera.

