Game.Tools.GenerateBrushesSystem
Assembly: Game (Assembly-CSharp)
Namespace: Game.Tools
Type: public class GenerateBrushesSystem
Base: GameSystemBase
Summary:
GenerateBrushesSystem is an ECS system responsible for turning CreationDefinition (+ optional BrushDefinition) marker entities into runtime Brush entities that drive terraforming/painting tools. It schedules a Burst-compiled IJobChunk (GenerateBrushesJob) to iterate matching archetype chunks, read CreationDefinition and BrushDefinition components, look up prefab & brush metadata, and emit one or more Brush entities per creation point using an EntityCommandBuffer (parallel writer) produced by a ModificationBarrier1. The job computes interpolated brush positions along a line, adjusts strength/opacity over time, and marks newly created entities with Temp flags (Create/Select/Delete/Cancel) depending on CreationDefinition flags and prefab terraforming target. The system requires Updated + CreationDefinition (and optionally BrushDefinition) to run.
Fields
-
private ModificationBarrier1 m_ModificationBarrier
Holds the ModificationBarrier1 system instance used to produce EntityCommandBuffer. Used to create entities and components in a thread-safe manner (AsParallelWriter) from within the job. -
private EntityQuery m_DefinitionQuery
EntityQuery selecting entities that contain CreationDefinition and Updated, and optionally BrushDefinition. This query is required for the system to run (RequireForUpdate is called with it in OnCreate). -
private TypeHandle __TypeHandle
Internal struct instance that caches ComponentTypeHandle and ComponentLookup instances (CreationDefinition, BrushDefinition, PrefabRef, BrushData, TerraformingData). Populated via __AssignHandles and used to obtain handles/lookups for the scheduled job.
Properties
- None (the system exposes no public properties)
Constructors
public GenerateBrushesSystem()
Default public constructor. System is created by the world; OnCreate and OnCreateForCompiler perform initialization.
Methods
[Preserve] protected override void OnCreate()
Initializes the system:- Retrieves/creates the ModificationBarrier1 system via World.GetOrCreateSystemManaged
(). - Builds m_DefinitionQuery to require entities with CreationDefinition and Updated (and optionally BrushDefinition).
-
Calls RequireForUpdate(m_DefinitionQuery) so the system only updates when matching entities exist.
-
[Preserve] protected override void OnUpdate()
Builds and schedules the Burst-compiled GenerateBrushesJob: - Fills job fields using InternalCompilerInterface to get ComponentTypeHandle/ComponentLookup from __TypeHandle and the system's CheckedStateRef.
- Creates an EntityCommandBuffer from m_ModificationBarrier and gets a ParallelWriter.
- Schedules the job as a parallel JobChunk via JobChunkExtensions.ScheduleParallel and assigns the returned JobHandle to base.Dependency.
-
Registers the job handle with the modification barrier via m_ModificationBarrier.AddJobHandleForProducer to ensure command buffer safety.
-
protected override void OnCreateForCompiler()
Compiler helper: calls __AssignQueries and assigns component handles via __TypeHandle.__AssignHandles. Present to support generated code path. -
private void __AssignQueries(ref SystemState state)
Internal method that currently creates and disposes an EntityQueryBuilder(Allocator.Temp) — part of generated boilerplate. Called from OnCreateForCompiler. -
(Nested)
[BurstCompile] private struct GenerateBrushesJob : IJobChunk
Job executed per chunk. Key behavior: - Read-only fields: ComponentTypeHandle
, ComponentTypeHandle , ComponentLookup , ComponentLookup , ComponentLookup . - Writable: EntityCommandBuffer.ParallelWriter m_CommandBuffer.
-
Execute iterates chunk NativeArrays of CreationDefinition and BrushDefinition:
- For each entry, builds a PrefabRef (uses creationDefinition.m_Prefab or the original entity's PrefabRef if m_Original is set).
- Looks up BrushData for the prefab to get the archetype to create Brush entities.
- Constructs a Temp component with flags (Essential plus Create/Select/Delete or Cancel depending on CreationFlags and TerraformingData for the tool).
- Computes the number of steps (num3) to sample along brushDefinition.m_Line based on line length and brush size, computes time/opacity/strength interpolation, and for each sample:
- Creates an entity with the brushData.m_Archetype via the command buffer.
- Sets PrefabRef and Brush components on the new entity.
- Adds the Temp component unless CreationFlags.Permanent is set.
-
Implements IJobChunk.Execute explicit call-through to Execute(in ArchetypeChunk,...).
-
(Nested) private struct TypeHandle
Contains cached handles/lookups: - ReadOnly ComponentTypeHandle
__Game_Tools_CreationDefinition_RO_ComponentTypeHandle - ReadOnly ComponentTypeHandle
__Game_Tools_BrushDefinition_RO_ComponentTypeHandle - ReadOnly ComponentLookup
__Game_Prefabs_PrefabRef_RO_ComponentLookup - ReadOnly ComponentLookup
__Game_Prefabs_BrushData_RO_ComponentLookup - ReadOnly ComponentLookup
__Game_Prefabs_TerraformingData_RO_ComponentLookup
Method: __AssignHandles(ref SystemState state) — fills the above handles/lookups via state.GetComponentTypeHandle / state.GetComponentLookup with isReadOnly: true.
Usage Example
// Example: create a CreationDefinition + BrushDefinition to trigger GenerateBrushesSystem.
// This would typically be invoked from some tool/controller that prepares a creation request.
Entity creation = entityManager.CreateEntity();
entityManager.AddComponentData(creation, new CreationDefinition {
m_Prefab = myPrefabEntity, // prefab entity reference (or Entity.Null if using m_Original)
m_Original = Entity.Null,
m_Flags = CreationFlags.Create // or Select / Delete / Permanent
});
entityManager.AddComponentData(creation, new Updated()); // marks that it should be processed
// Optionally add a BrushDefinition to control line/size/strength/time/angle/target.
entityManager.AddComponentData(creation, new BrushDefinition {
m_Start = myStartPos,
m_Line = myLineVector,
m_Size = 8.0f,
m_Strength = 0.5f,
m_Time = 0.2f,
m_Angle = 0f,
m_Tool = myToolPrefab,
m_Target = TerraformingTarget.Surface
});
// The system will run (when the world updates) and create one or more Brush entities
// based on the provided definitions. The created Brush entities will have PrefabRef,
// Brush data, and a Temp component unless the CreationFlags.Permanent flag is set.
Notes and implementation details: - The system is Burst-compiled for the job; component lookups and command buffer are used to be job-thread-safe. - The command buffer is obtained from ModificationBarrier1 and scheduled as a producer with AddJobHandleForProducer. - Brush strength and opacity interpolation logic: - Number of samples = 1 + floor(length(line) / (size * 0.25f)) - Strength blends between squared-strength and fourth-power-weighted value over time slices. - Opacity is set to 1 / numSamples so repeated samples combine predictably. - If the prefab's TerraformingData indicates TerraformingTarget.None for the tool, the Temp.Cancel flag is set to cancel the brush operation. - The system only runs when there are entities matching the query (CreationDefinition + Updated).