Game.Rendering.AreaColorSystem
Assembly: Game
Namespace: Game.Rendering
Type: class
Base: GameSystemBase
Summary:
AreaColorSystem is an ECS system responsible for populating per-area color entries used by the rendering batch for area visualization. It reads Batch, Owner, Lot and Triangle data and writes Vector3 color entries into an AreaBatchSystem-managed NativeList
Fields
-
private AreaBatchSystem m_AreaBatchSystem
This is a reference to the AreaBatchSystem instance in the same world. AreaColorSystem uses m_AreaBatchSystem to obtain the NativeListbuffer to write into (via GetColorData) and to register the job as a color writer (AddColorWriter). It is assigned during OnCreate via World.GetOrCreateSystemManaged (). -
private ToolSystem m_ToolSystem
Reference to the ToolSystem instance (World.GetOrCreateSystemManaged()). The system checks m_ToolSystem.activeInfoview; the FillColorDataJob is scheduled only when an infoview is active. This gates the color population logic to only run when needed for the UI/tool overlays. -
private EntityQuery m_AreaQuery
An EntityQuery that matches chunks with the Batch component and excludes Deleted and Temp. This query is used to schedule the FillColorDataJob in parallel across matching chunks. -
private TypeHandle __TypeHandle
Container struct that caches component/buffer type handles and component lookups required by the job: Batch, Owner, Lot, Triangle buffer, Owner component lookup, and Game.Objects.Color component lookup. The TypeHandle.__AssignHandles method is called during system initialization (OnCreateForCompiler) to fill these handles from the current SystemState. -
(Nested)
private struct FillColorDataJob : IJobChunk
Burst-compiled job that iterates chunks matching the query and fills the AreaColorData list for the area triangles in each batch. Key fields: - ComponentTypeHandle
m_BatchType (read-only) - ComponentTypeHandle
m_OwnerType (read-only) - ComponentTypeHandle
m_LotType (read-only) - BufferTypeHandle
m_TriangleType (read-only) - ComponentLookup
m_OwnerData (read-only) - ComponentLookup
m_ColorData (read-only) - NativeList
m_AreaColorData (NativeDisableParallelForRestriction) The job: - Skips empty batches.
- Optionally traverses Owner chain (using m_OwnerData) to find a Game.Objects.Color component (m_ColorData).
- If a relevant color exists and either the chunk has a Lot component or the color is marked m_SubColor, computes a Vector3 containing encoded color info:
- x = color index + 0.5f
- y = color value * (1/255) (0.003921569f)
- z = 1f
- Writes that Vector3 into m_AreaColorData for each Triangle entry belonging to the batch, using batch.m_BatchAllocation.Begin as the write offset.
Notes on m_AreaColorData: It is obtained from m_AreaBatchSystem.GetColorData(out dependencies) and must be treated carefully for parallel writes. The job is allowed to write into it using NativeDisableParallelForRestriction because the system ensures correct synchronization via the returned dependencies and by registering the job with AreaBatchSystem.AddColorWriter.
- (Nested)
private struct TypeHandle
Holds precomputed component/buffer type handles and lookups. It contains __AssignHandles(ref SystemState state) to fetch handles and lookups from the SystemState (GetComponentTypeHandle, GetBufferTypeHandle, GetComponentLookup). This reduces per-frame overhead of fetching handles repeatedly.
Properties
This system declares no public or notable properties. (All relevant state is held in fields and the nested TypeHandle.)
Constructors
public AreaColorSystem()
Default constructor. System initialization is primarily done in OnCreate and OnCreateForCompiler where cached handles and system references are set up.
Methods
protected override void OnCreate()
Initializes system references and entity queries:- Calls base.OnCreate().
- Retrieves/creates AreaBatchSystem and ToolSystem via base.World.GetOrCreateSystemManaged
(). -
Builds m_AreaQuery to match entities with Batch and exclude Deleted and Temp. This prepares the system to schedule jobs in OnUpdate.
-
protected override void OnUpdate()
Main update logic: - Checks whether m_ToolSystem.activeInfoview != null; if null, the job is not scheduled and no work is done.
- If active, it prepares a FillColorDataJob instance, populating all component/buffer handles and lookups from __TypeHandle via InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle / GetComponentLookup (bound to base.CheckedStateRef).
- Calls m_AreaBatchSystem.GetColorData(out dependencies) to obtain the NativeList
and a job dependency representing any required synchronization from AreaBatchSystem. - Schedules the FillColorDataJob in parallel over m_AreaQuery using JobChunkExtensions.ScheduleParallel, combining base.Dependency and dependencies.
- Registers the returned job with m_AreaBatchSystem.AddColorWriter so AreaBatchSystem is aware of writers to its color list, and stores the job handle in base.Dependency.
The method ensures job dependencies are combined and tracked so the AreaBatchSystem and other systems can synchronize correctly with the scheduled job.
-
private void __AssignQueries(ref SystemState state)
Compiler helper invoked from OnCreateForCompiler. The implementation in this generated file only creates and immediately disposes a new EntityQueryBuilder; in other builds this method might assign queries required for safety checks or codegen. It's marked with MethodImplOptions.AggressiveInlining in surrounding helper methods. -
protected override void OnCreateForCompiler()
Called by compiled codegen paths: calls base.OnCreateForCompiler(), assigns queries via __AssignQueries, and populates __TypeHandle handles via __TypeHandle.__AssignHandles(ref base.CheckedStateRef). Ensures handles are available before the first update. -
(Nested)
FillColorDataJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Implements IJobChunk.Execute. Iterates through the chunk's Batch components, checks batch allocations, optionally determines a color vector via Owner + Color components, and writes AreaColorData entries for each Triangle in the dynamic buffer attached to the entity. Skips empty batches and respects presence of Lot component for special behavior.
Implementation details: - Uses chunk.GetNativeArray(ref m_BatchType), chunk.GetNativeArray(ref m_OwnerType), and chunk.GetBufferAccessor(ref m_TriangleType). - Detects presence of Lot in chunk using chunk.Has(ref m_LotType). - For owner color lookup, it will walk the owner chain using m_OwnerData until it either finds a Game.Objects.Color (via m_ColorData.HasComponent(owner.m_Owner)) or runs out of owner components. - Writes AreaColorData entries sequentially starting at (int)batch.m_BatchAllocation.Begin for each triangle in the triangle buffer.
Notes: The job is Burst-compiled which improves performance but restricts certain managed operations; everything in the job is written with Burst-safe constructs and Native containers.
Usage Example
// Typical system overrides are already present in AreaColorSystem.
// Example showing the core idea: scheduling the FillColorDataJob when an infoview is active.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_AreaBatchSystem = base.World.GetOrCreateSystemManaged<AreaBatchSystem>();
m_ToolSystem = base.World.GetOrCreateSystemManaged<ToolSystem>();
m_AreaQuery = GetEntityQuery(ComponentType.ReadOnly<Batch>(), ComponentType.Exclude<Deleted>(), ComponentType.Exclude<Temp>());
}
[Preserve]
protected override void OnUpdate()
{
if (m_ToolSystem.activeInfoview != null)
{
// Prepare and schedule FillColorDataJob as in the system implementation.
// AreaBatchSystem.GetColorData(out dependencies) provides the NativeList to write to,
// and AddColorWriter(jobHandle) registers the job with the batch system.
}
}
Additional notes and modding tips:
- The system is intended to run only while an infoview overlay is active (m_ToolSystem.activeInfoview), reducing unnecessary work when the overlay is not visible.
- If you are creating or modifying area rendering, use AreaBatchSystem.GetColorData / AddColorWriter to coordinate writers and ensure proper synchronization.
- The job writes into a NativeList