CameraComponent
- Namespace: Turian.Engine.Core
- Source File: CameraComponent.cs
Represents a camera component that defines view and projection transformations for rendering. Supports both perspective and orthographic projection modes, physical camera parameters, temporal anti-aliasing jitter, and frustum culling operations.
Properties
UsePerspective
(bool) { get; set } = true: Gets or sets whether the camera uses perspective projection. When false, orthographic projection is used.
NearPlane
(float) { get; set } = 0.01f: Gets or sets the distance to the near clipping plane. Must be greater than 0.0001.
FarPlane
(float) { get; set } = 100f: Gets or sets the distance to the far clipping plane. Must be greater than NearPlane + 0.0001.
FieldOfView
(float) { get; set } = 60f * Mathf.DegreesToRadians
FieldOfViewDegrees
(float) { get; set }: Gets or sets the vertical field of view in degrees.
FocalLength
(float) { get; set }: Gets or sets the focal length in millimeters. Affects field of view when using physical camera model.
SensorHeight
(float) { get; set }: Gets or sets the sensor height in millimeters. Affects field of view when using physical camera model.
Frustum
(float) { get; set } = 40f: Gets or sets the orthographic frustum size (half-height of the view volume).
AspectMode
(AspectMode) { get; set } = AspectMode.Viewport: Gets or sets how the camera determines its aspect ratio.
FixedAspectRatio
(float) { get; set } = 16f / 9f: Gets or sets the fixed aspect ratio used when AspectMode is Fixed.
AspectRatio
(float): The aspect ratio last set by Resize. Read-only, so a viewer that only wants to preview this camera — such as the Scene View's camera preview — can size its own render target from it without ever calling Resize itself. Doing so would overwrite the aspect whichever other consumer (the Game panel, the standalone runtime) actually owns this camera's viewport currently relies on.
Priority
(int) { get; set }: Gets or sets which camera the runtime renders through when a scene holds several. The highest value wins; ties go to the first found in hierarchy order. Every camera drawing to the same target would otherwise overdraw the others, which reads as flicker.
Pitch
(float) { get; set }: Gets or sets the camera's pitch rotation (X-axis) in radians. Clamped to ±89.9° to prevent gimbal lock. Mirrors Node's Transform.Rotation.X once attached — not independently serialized, like Position, so a scene file's authoritative Transform.Orientation is never second-guessed by a redundant scalar copy.
Yaw
(float) { get; set }: Gets or sets the camera's yaw rotation (Y-axis) in radians. Mirrors Node's Transform.Rotation.Y once attached — see Pitch for why it isn't independently serialized.
Front
(Vector3) { get; set } = Vector3.UnitZ
Right
(Vector3) { get; set } = Vector3.UnitX
Up
(Vector3) { get; set } = GlobalUp
Position
(Vector3) { get; set }
Public Methods
FindPrimary
public static CameraComponent? FindPrimary(Node? root)
Returns the camera the runtime should render through: the active camera with the highest Priority, or null when the hierarchy holds none.
Parameters:
root(Node?): Root of the hierarchy to search.
Returns: CameraComponent?
OnAttached
public override void OnAttached()
Remarks:
Deliberately does not push Pitch/Yaw's pre-attach staging fields onto Node's Transform.Rotation: the Transform is already the authoritative source (set directly by scene deserialization, the Inspector, or a script), and overwriting it here previously clobbered a correctly-authored orientation whenever those staging fields held anything else — as little as a units mismatch between a hand-authored scene file's redundant Pitch/Yaw scalars and this property's radians contract was enough to point a camera at the sky. This only recomputes the derived Front/Right/Up vectors from whatever orientation the Transform already carries.
GetViewMatrix
public Matrix4x4 GetViewMatrix()
Returns: Matrix4x4
GetProjectionMatrix
public Matrix4x4 GetProjectionMatrix()
Returns: Matrix4x4
GetInverseViewMatrix
public Matrix4x4 GetInverseViewMatrix()
Returns: Matrix4x4
GetFrustumPlanes
public Plane[] GetFrustumPlanes()
Returns the six frustum planes (left, right, bottom, top, near, far) in world space for culling.
Returns: Plane[]
SetJitterNdc
public void SetJitterNdc(Vector2 jitterNdc)
Sets the jitter offset in normalized device coordinates (NDC).
Parameters:
jitterNdc(Vector2)
SetJitterPixels
public void SetJitterPixels(float x, float y, float width, float height)
Sets the jitter offset in pixels. Automatically converts to NDC space.
Parameters:
x(float)y(float)width(float)height(float)
ClearJitter
public void ClearJitter()
Clears any active jitter offset.
Resize
public void Resize(uint width, uint height)
Updates the camera's aspect ratio based on the specified viewport dimensions.
Parameters:
width(uint)height(uint)
Project
public Vector2 Project(Vector3 p)
Parameters:
p(Vector3)
Returns: Vector2
UnProject
public Vector3 UnProject(Vector2 p)
Parameters:
p(Vector2)
Returns: Vector3