Game.Tools.ApplyBrushesSystem
Assembly:
Assembly-CSharp (game assembly)
Namespace:
Game.Tools
Type:
class
Base:
GameSystemBase
Summary:
ApplyBrushesSystem is a GameSystem that processes temporary brush entities (Temp + Brush) created by tools and applies their effects to different in-game maps: natural resources (ore, oil, fertility), groundwater, terrain height, and terrain material. The system queries Temp brush entities each update, schedules parallel jobs to modify cell maps where appropriate, invokes terrain system calls for height/material changes, and marks processed Temp entities with Applied and Deleted components. It uses ECS type handles, cell-map systems, and a ToolOutputBarrier to produce command buffers that are played back safely with the simulation.
Fields
-
private ToolOutputBarrier m_ToolOutputBarrier
Reference to the ToolOutputBarrier system. Used to create an EntityCommandBuffer for marking processed Temp entities (adding Applied and Deleted components) and to synchronize output with tool-related systems. -
private NaturalResourceSystem m_NaturalResourceSystem
Reference to the NaturalResource system which provides access to the natural resource cell map (ore, oil, fertility). Used to read/write NaturalResourceCell data when applying brushes targeting natural resources. -
private GroundWaterSystem m_GroundWaterSystem
Reference to the GroundWater system providing access to groundwater cell map. Used when brushes target groundwater. -
private TerrainSystem m_TerrainSystem
Reference to the Terrain system. Used to apply height-related brush operations (level, slope, soften, etc.) via TerrainSystem.ApplyBrush. -
private TerrainMaterialSystem m_TerrainMaterialSystem
Reference to the TerrainMaterial system. Used when brush targets material—ensures the tool/material index is registered (GetOrAddMaterialIndex). -
private PrefabSystem m_PrefabSystem
Reference to the Prefab system. Used to lookup BrushPrefab data (texture, resolution) for a given brush prefab entity. -
private EntityQuery m_TempQuery
EntityQuery that selects entities with Temp and Brush components. The system requires this query for update and iterates matching archetype chunks to process brush entities. -
private ComponentTypeSet m_AppliedDeletedTypes
ComponentTypeSet containing Applied and Deleted component types. Used to add these components to Temp entities once the brush has been processed (so the entity is marked for removal/cleanup by other systems). -
private TypeHandle __TypeHandle
Generated helper struct used to cache EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle , ComponentLookup , BufferLookup , etc., and to assign those handles from the SystemState. This is an internal optimization for burst/Jobs compatibility. -
private interface ICellModifier<TCell> where TCell : struct, ISerializable
Interface for cell modifiers that can apply a strength delta to a cell type TCell. Implementations map brush strength/area to concrete cell field modifications. -
private struct NaturalResourcesModifier : ICellModifier<NaturalResourceCell>
Modifier implementation for NaturalResourceCell. Maps MapFeature (Ore, Oil, FertileLand, Forest) to the appropriate field on NaturalResourceCell and adjusts the base value using strength. Note: Forest is present in enum but not implemented here. -
private struct GroundWaterModifier : ICellModifier<GroundWater>
Modifier implementation for GroundWater cells. Converts stored integer amount to float, applies strength, clamps and writes back amount and sets max equal to amount. -
private struct ApplyCellMapBrushJob<TCell, TModifier> : IJobParallelFor where TCell : struct, ISerializable where TModifier : ICellModifier<TCell>
Burst-compiled parallel job that iterates over a set of rows (y range) of a cell map texture and, per cell, computes the brush texture overlap area and accumulates effective strength to apply. Reads brush resolution/brush cells buffers via BufferLookup and BrushData via ComponentLookup, and writes to a NativeArraybuffer (m_Buffer) of the cell map. When the computed applied amount is non-negligible, it calls the modifier to change the cell and writes it back to the buffer.
Properties
- (none public)
This system does not expose public properties.
Constructors
public ApplyBrushesSystem()
Default constructor. The system is created by the ECS world; most initialization happens in OnCreate.
Methods
-
protected override void OnCreate()
Initializes system references: gets or creates managed systems ToolOutputBarrier, NaturalResourceSystem, GroundWaterSystem, TerrainSystem, TerrainMaterialSystem, PrefabSystem. Builds the m_TempQuery to require Temp + Brush entities and sets m_AppliedDeletedTypes to contain Applied and Deleted component types. Calls RequireForUpdate(m_TempQuery) so the system only runs when matching entities exist. -
protected override void OnUpdate()
Main update loop: - Acquires entity and component type handles from cached TypeHandle.
- Creates an EntityCommandBuffer from the ToolOutputBarrier for marking processed entities.
- Collects archetype chunks matched by m_TempQuery into an array.
- Completes current dependencies to ensure safe access.
- Iterates chunks and each Temp entity (Entity e) with a Brush component and PrefabRef:
- Reads TerraformingData (component on brush.m_Tool) to determine target and type.
- Depending on target:
- For Ore/Oil/FertileLand: schedules ApplyCellMapBrush against NaturalResourceSystem with NaturalResourcesModifier.
- For GroundWater: schedules ApplyCellMapBrush against GroundWaterSystem with GroundWaterModifier.
- For Height: calls ApplyHeight (immediate, non-job).
- For Material: calls ApplyMaterial (immediate).
- Combines job handles into a cumulative jobHandle and then adds Applied/Deleted components to entity using entityCommandBuffer.
-
Ensures chunk array disposed in finally, and sets base.Dependency to the combined jobHandle so subsequent systems are aware of scheduled jobs.
-
private void ApplyMaterial(Brush brush, Entity prefab)
Ensures the brush tool material is registered by calling m_TerrainMaterialSystem.GetOrAddMaterialIndex(brush.m_Tool). Material application itself is handled by TerrainMaterialSystem and related systems. -
private void ApplyHeight(Brush brush, Entity prefab, TerraformingType terraformingType)
Applies height changes via TerrainSystem.ApplyBrush. It: - Computes bounds via ToolUtils.GetBounds(brush).
- Looks up the BrushPrefab using m_PrefabSystem.GetPrefab
(prefab) to access the texture. - Handles special cases: Level and Slope targets with negative strength are skipped; if terraformingType is Soften and strength is negative, it doubles the absolute strength.
-
Calls m_TerrainSystem.ApplyBrush(terraformingType, bounds, brush, prefab2.m_Texture).
-
private JobHandle ApplyCellMapBrush<TCell, TModifier>(CellMapSystem<TCell> cellMapSystem, TModifier modifier, Brush brush, Entity prefab, TerraformingType terraformingType, ApplyCellMapBrushJob<TCell, TModifier> applyCellMapBrushJob) where TCell : struct, ISerializable where TModifier : ICellModifier<TCell>
Prepares and schedules an ApplyCellMapBrushJob: - Computes the brush bounds and converts to cell-space indices/clamped texture coordinates using cellSize and texture size from the passed CellMapSystem
. - Builds and populates an ApplyCellMapBrushJob instance with coords, brush, prefab, modifier, texture/cell metadata, and component/buffer lookups for brush data and brush cells.
- Schedules the job as an IJobParallelFor over the vertical range (number of rows) and registers the returned JobHandle with the cellMapSystem via AddWriter so the cell map knows about scheduled writers.
-
Returns the scheduled JobHandle to the caller so it can be combined with other dependencies.
-
private void __AssignQueries(ref SystemState state)
Generated stub for assigning queries. In this generated code it creates an EntityQueryBuilder and disposes it; used by the compiler-generated OnCreateForCompiler flow. -
protected override void OnCreateForCompiler()
Compiler-generated initialization where __AssignQueries is called and the __TypeHandle.__AssignHandles(ref base.CheckedStateRef) is invoked to cache type/component lookups for the system. -
private struct TypeHandle { ... public void __AssignHandles(ref SystemState state) }
Contains cached typed handles and a method to assign them from the SystemState: - EntityTypeHandle
- ComponentTypeHandle
(read-only) - ComponentTypeHandle
(read-only) - ComponentLookup
(read-only) - BufferLookup
(read-only)
Usage notes: - The system expects Temp entities (short-lived) containing a Brush component and a PrefabRef referencing a BrushPrefab entity containing texture and BrushCell data. Typical workflow: a tool creates a Temp entity with Brush & PrefabRef (pointing to a BrushPrefab) and the system applies the brush then marks the Temp entity Applied/Deleted. - For cell-map modifications the system uses burst-parallel jobs to compute per-cell overlap area between the world-space cell bounds and individual brush texture quads, scales that by brush strength and cell area, and applies changes through a modifier (NaturalResourcesModifier or GroundWaterModifier). - Height and material operations are applied synchronously through TerrainSystem / TerrainMaterialSystem.
Usage Example
// Example: create a temporary brush entity (pseudocode)
// The system will pick up this Temp + Brush entity and apply the brush effects automatically.
Entity brushEntity = entityManager.CreateEntity(typeof(Temp), typeof(Brush), typeof(PrefabRef));
entityManager.SetComponentData(brushEntity, new Brush {
m_Position = new float3(100f, 0f, 200f),
m_Size = new float2(30f, 30f),
m_Strength = 0.5f,
m_Angle = 0f,
m_Tool = toolEntity // an Entity that holds TerraformingData describing target/type
});
entityManager.SetComponentData(brushEntity, new PrefabRef { m_Prefab = brushPrefabEntity });
// The ApplyBrushesSystem (runs automatically) will:
// - read TerraformingData from the toolEntity,
// - schedule jobs to modify cell maps for ore/oil/fertility/groundwater (if applicable),
// - call TerrainSystem.ApplyBrush for height changes,
// - call TerrainMaterialSystem.GetOrAddMaterialIndex for material changes,
// - and finally add Applied + Deleted components to the Temp entity via the ToolOutputBarrier's command buffer.
Notes and tips: - BrushPrefab must contain BrushData and a dynamic buffer of BrushCell items; if resolution or buffer is empty the cell-map job early-outs for that prefab. - Strength handling: cell modifiers convert stored integer cell data to normalized floats, add the computed strength, then clamp and convert back to stored integer form. - The ApplyCellMapBrushJob performs geometric overlap testing (quad vs cell bounds) to compute the exact area of the brush texture that affects each cell, giving spatially accurate influence even for rotated brushes. - Since many operations are scheduled as jobs, ensure you respect job dependencies if interacting with cell maps or terrain systems elsewhere. The system sets base.Dependency to the combined JobHandle to propagate dependencies.