Game.WaterDebugSystem
Assembly:
Namespace: Game.Debug
Type: class
Base: BaseDebugSystem
Summary:
This debug system visualizes water simulation data in the editor/game using gizmos. It draws wireframe cylinders for water sources, optional culling boxes for water grid cells, and optional surface boxes representing surface water depth and pollution. The system queries WaterSourceData entities and uses WaterSystem for active/depth buffers, GizmosSystem for batched gizmo drawing, and TerrainSystem for position offsets. It schedules a job (WaterGizmoJob) to perform the heavy drawing work on a worker thread and manages job dependencies and native disposals.
Fields
-
private struct WaterGizmoJob
This nested IJob struct performs the actual gizmo drawing. It holds read-only component lookups and native arrays provided by WaterSystem, a GizmoBatcher for drawing, cell/grid sizes, options flags, and a position offset. Its Execute method iterates water sources to draw cylinders and iterates grid cells to draw culling boxes and surface boxes (sampling surface water depths and pollution). -
private struct TypeHandle
A small helper struct used by the generated/compiled system to cache ComponentLookup handles for WaterSourceData and Game.Objects.Transform. It exposes an __AssignHandles method that initializes the lookup handles from a SystemState. -
private WaterSystem m_WaterSystem
Reference to the WaterSystem (simulation). Used to obtain active flags, surface depth buffers, grid sizes and map size, and to register readers for the scheduled job's dependency tracking. -
private TerrainSystem m_TerrainSystem
Reference to the TerrainSystem to obtain position offset (terrain origin/offset) used when placing gizmos in world coordinates. -
private GizmosSystem m_GizmosSystem
Reference to the GizmosSystem used to acquire a GizmoBatcher for batched gizmo drawing and to register writers for dependency tracking. -
private EntityQuery m_WaterSourceGroup
An EntityQuery that matches entities with WaterSourceData (water sources). Used to produce a NativeListfor the job. -
private Option m_ShowCulling
Option toggle (exposed in debug UI) controlling whether to render the culling boxes for water grid cells. -
private Option m_showSurface
Option toggle controlling whether to render surface water boxes (depth/pollution) inside active water grid cells. -
private TypeHandle __TypeHandle
Instance of the TypeHandle struct used to cache and assign ComponentLookup handles for use during job construction.
Properties
- None (no public properties declared directly on this type).
This system relies on protected methods and fields inherited from BaseDebugSystem and exposes its functionality via the debug options added in OnCreate.
Constructors
public WaterDebugSystem()
Default constructor. The system uses OnCreate to initialize its dependencies and options; the constructor itself contains no custom initialization logic beyond any generated bookkeeping.
Methods
-
protected override void OnCreate()
Initializes the system: gets or creates GizmosSystem, WaterSystem and TerrainSystem instances from the World; registers two debug options ("Show Surface Boxes" and "Show Culling Boxes"); creates an EntityQuery for WaterSourceData; and disables the system by default (base.Enabled = false). This is where the system sets up references to other manager systems and UI toggles. -
protected override void OnUpdate()
Builds and schedules the WaterGizmoJob. Steps: - Calls m_WaterSourceGroup.ToEntityListAsync to get a NativeList
for water sources (produces an out JobHandle). - Uses cached ComponentLookup handles (via InternalCompilerInterface.GetComponentLookup) for WaterSourceData and Transform.
- Calls m_WaterSystem.GetActive() and m_WaterSystem.GetDepths(out deps) to acquire active flags and surface depth array (and its dependency).
- Calls m_GizmosSystem.GetGizmosBatcher(out dependencies) to acquire a GizmoBatcher and its dependency.
- Fills WaterGizmoJob with buffers, sizes (grid/cell/map), position offset, and UI option flags.
- Schedules the job and composes dependencies via JobUtils.CombineDependencies and IJobExtensions.Schedule.
- Disposes the NativeList
with the scheduled job's dependency. -
Registers readers/writers with WaterSystem and GizmosSystem to ensure proper dependency tracking (AddSurfaceReader, AddActiveReader, AddGizmosBatcherWriter).
-
private void __AssignQueries(ref SystemState state)
Generated helper method used by compiler-time initialization. In this class it creates/initializes any entity queries needed for the system; current implementation runs an EntityQueryBuilder and disposes it (placeholder generated code). -
protected override void OnCreateForCompiler()
Compiler-time initialization hook used to assign queries and type handles. It calls __AssignQueries and assigns component lookup handles via __TypeHandle.__AssignHandles. -
private void TypeHandle.__AssignHandles(ref SystemState state)
(Via the nested TypeHandle) Assigns ComponentLookup instances for WaterSourceData and Game.Objects.Transform with read-only access using the provided SystemState. Marked AggressiveInlining for performance. -
public void WaterGizmoJob.Execute()
The job's Execute method (IJob). Implementation details: - Iterates over m_WaterSources and draws wire cylinders at each source using source radius/amount, differentiating constant-depth sources with cyan/yellow colors and other sources with red/sized cylinders.
- Iterates grid cells using m_WaterActive flags to optionally draw culling wire cubes and, if enabled, samples surface water depths/pollution from m_WaterDepths and draws smaller wire cubes (stepped by blocks of 16 in the inner loops) colored by depth/pollution (lerp from blue to brown based on pollution).
- Uses m_PositionOffset to account for terrain origin and m_CellInMeters/m_GridCellInMeters to compute world positions and sizes.
Notes on threading and safety: WaterGizmoJob uses read-only ComponentLookup and NativeArray/s to safely run as a job. The job obtains a GizmoBatcher instance; the system requests writer access on scheduling to ensure no races.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization (similar to the system itself).
m_GizmosSystem = base.World.GetOrCreateSystemManaged<GizmosSystem>();
m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
// Add debug toggles (these call into BaseDebugSystem)
m_showSurface = AddOption("Show Surface Boxes", defaultEnabled: true);
m_ShowCulling = AddOption("Show Culling Boxes", defaultEnabled: false);
// Query water source entities
m_WaterSourceGroup = GetEntityQuery(ComponentType.ReadOnly<WaterSourceData>());
// Enable the system to make it start drawing (by default the system disables itself in OnCreate)
base.Enabled = true;
}
Additional notes: - The system is intended for debugging and visualization, not for gameplay logic. - The WaterGizmoJob intentionally batches gizmo drawing using GizmoBatcher to reduce draw overhead. - Make sure to respect dependency registration (AddSurfaceReader, AddActiveReader, AddGizmosBatcherWriter) if copying or modifying scheduling logic so Unity's job system and simulation systems remain in sync.