Game.CameraUpdateSystem
Assembly: Assembly-CSharp
Namespace: Game.Rendering
Type: class
Base: GameSystemBase
Summary:
Manages the game's active camera/viewer and updates camera-related systems each frame. Responsibilities include caching the active viewer, updating depth of field and shadow settings based on viewer distances, driving Cinemachine brains, refreshing camera input activation/blocking, and coordinating multiple camera controllers (gameplay, cinematic, orbit). It creates a small HD Volume used to tweak DepthOfField and HDShadowSettings for the active camera.
Fields
-
private RaycastSystem m_RaycastSystem
References the RaycastSystem used to perform viewer raycasts and collision/visibility queries. -
private Volume m_Volume
A local HD Volume created to host depth-of-field and shadow setting overrides. -
private DepthOfField m_DepthOfField
Reference to the DepthOfField component inside m_Volume used to apply DoF settings. -
private HDShadowSettings m_ShadowSettings
Reference to the HD shadow settings component inside m_Volume used to override cascade splits and max distance. -
private float4 m_StoredShadowSplitsAndDistance
Cached original shadow cascade split distances and max shadow distance read from the camera/HD pipeline (packed into a float4). -
private float4 m_StoredShadowBorders
Cached original cascade shadow border values. -
private InputActivator[] m_CameraActionActivators
Array of InputActivators created from the "Camera" action map actions; used to enable/disable camera action handling while a camera controller is active. -
private InputBarrier[] m_CameraActionBarriers
Array of InputBarriers created from the "Camera" action map actions; used to block camera input when the mouse is over UI (with some conditions).
Properties
-
public Viewer activeViewer { get; private set; }
The currently active Viewer wrapper around the Unity Camera. Null when no camera is active. Many updates read/viewer metrics (position, forward, distances) from this instance. -
public CameraController gamePlayController { get; set; }
Reference to the gameplay camera controller. Can be toggled on/off via activeCameraController. -
public CinematicCameraController cinematicCameraController { get; set; }
Reference to the cinematic camera controller. -
public OrbitCameraController orbitCameraController { get; set; }
Reference to the orbit camera controller. -
public Camera activeCamera { get; set; }
Getter returns the Unity Camera of the activeViewer, or null. Setter wraps a Camera into a Viewer and assigns activeViewer (or clears it when value is null). Setting logs the change via COSystemBase.baseLog. -
public float nearClipPlane { get; private set; }
Cached near clip plane of the active viewer camera (updated on CheckOrCacheViewer). -
public float3 position { get; private set; }
Cached position of the active viewer (world space). -
public float3 direction { get; private set; }
Cached forward direction of the active viewer. -
public float zoom { get; private set; }
Current zoom value (source is the active camera controller when available, otherwise preserved value). -
public IGameCameraController activeCameraController { get; set; }
Gets the currently active IGameCameraController. The getter enforces that only one of the controllers (gameplay / cinematic / orbit) is enabled at a time and returns the enabled one. The setter disables any other controllers and enables the provided controller (if non-null).
Constructors
public CameraUpdateSystem()
Default constructor. Initialization of runtime state is done in OnCreate. The constructor itself does not perform special logic.
Methods
-
protected override void OnGameLoaded(Context serializationContext)
Called when the game world is loaded. Calls base implementation and sets activeCamera = Camera.main to initialize the active viewer to the main camera. -
public CameraBlend GetBlendWeight(out float weight)
Queries Cinemachine to determine if a brain is currently blending between cameras. If a blend is active and not complete, sets weight to the blend weight and returns a CameraBlend enum indicating whether the blend is to/from the cinematic camera (if that camera participates in the blend). Otherwise returns CameraBlend.None and weight = 1f. -
public bool TryGetViewer(out Viewer viewer)
Returns the cached activeViewer via out parameter and returns true when a viewer exists. -
public bool TryGetLODParameters(out LODParameters lodParameters)
If there is an activeViewer, attempts to fetch LODParameters via activeViewer.TryGetLODParameters and returns true on success. Otherwise returns false and out param is defaulted. -
private bool CheckOrCacheViewer()
Checks that activeViewer and its camera are valid. If valid, populates nearClipPlane, position, direction and zoom (from the active camera controller if present), invokes activeViewer.Raycast to update raycast state, and returns true. If invalid, clears cached values, sets activeCamera = null, and returns false. -
[Preserve] protected override void OnCreate()
Creates/initializes required systems and resources: - Obtains RaycastSystem from the world.
- Creates an HD Volume named "CameraControllerVolume" (priority 51).
- Ensures DepthOfField and HDShadowSettings components exist in the created volume and caches references.
-
Fetches the "Camera" action map from InputManager and builds InputActivator[] and InputBarrier[] arrays for its actions so camera input can be enabled/blocked as needed.
-
private void UpdateDepthOfField(float distance)
Updates the DepthOfField component (m_DepthOfField) according to shared GraphicsSettings: - If settings specify TiltShift mode, sets manual focus mode and configures near/far focus ranges relative to the provided distance and configured tilt shift factors.
- If settings specify Physical mode, sets physical camera focus distance to the provided distance.
-
Otherwise turns DoF off.
-
private void UpdateShadows(Viewer viewer)
Adjusts HD shadow settings (m_ShadowSettings) based on the viewer's distance information: - On first run, caches original shadow split/distances and borders from HDCamera volume stack.
- If viewer.shadowsAdjustFarDistance is false, clears any override states (restores defaults).
-
If true, computes a suitable max shadow distance and cascade splits/clamps based on viewerDistances (farthestSurface, maxDistanceToSeaLevel, ground, etc.) and overrides m_ShadowSettings fields accordingly.
-
[Preserve] protected override void OnDestroy()
Cleans up created resources: - Destroys the created Volume.
- Disables controllerEnabled on gameplay/cinematic/orbit controllers if set.
-
Calls base.OnDestroy.
-
[Preserve] protected override void OnUpdate()
Per-frame update: - If an active viewer exists, updates its raycast state and stores the focus distance, then calls UpdateShadows(viewer).
- Calls UpdateDepthOfField with the computed focus distance.
- Calls UpdateCamera() on the activeCameraController (if any).
- Forces ManualUpdate() on all Cinemachine brains present.
- Calls CheckOrCacheViewer() to refresh cached viewer values.
-
Calls RefreshInput() to enable/disable activators and block/unblock barriers based on whether a camera controller is active and mouse-over-UI state.
-
private void RefreshInput()
Enables or disables InputActivators based on whether there is an active camera controller. For InputBarriers: - If no active controller, barriers are not blocked.
- If the mouse is not over UI according to InputManager, barriers are not blocked.
- Otherwise, if the corresponding actions are not in progress, the barrier is blocked (preventing camera input while over UI unless an action is actively occurring).
Usage Example
[Preserve]
protected override void OnGameLoaded(Context serializationContext)
{
base.OnGameLoaded(serializationContext);
// Initialize the system's active viewer to the main camera on load
activeCamera = Camera.main;
}
Notes and tips: - CameraUpdateSystem relies on a Viewer abstraction (not defined here) which provides viewerDistances and raycast helpers. Inspect the Viewer class for details on viewerDistances fields (focus, ground, farthestSurface, maxDistanceToSeaLevel, etc.). - The system integrates with Unity HDRP (HDCamera / HDShadowSettings / DepthOfField) and Cinemachine; ensure those packages/features are present in your project when interacting with its internals. - Input activation/blocking logic depends on InputManager and the "Camera" action map — modifying or renaming that action map will affect this system.