Game.Rendering.BrushRenderSystem
Assembly: Assembly-CSharp
Namespace: Game.Rendering
Type: class
Base: GameSystemBase
Summary:
BrushRenderSystem is a managed ECS system responsible for rendering visual brush overlays used by tools (terraforming, object placement, etc.). It listens to the render pipeline (RenderPipelineManager.beginContextRendering) and draws a cached mesh for every active Brush entity using the brush texture and material supplied by the corresponding prefabs. It also forwards preview height information to the TerrainSystem for terraforming tools so the terrain preview can be shown in the editor/game view.
Fields
-
private EntityQuery m_BrushQuery
Used to query all active Brush components (ReadOnly) while excluding Hidden and Deleted entities. The system iterates over the resulting chunks to render each brush. -
private EntityQuery m_SettingsQuery
Query used to fetch overlay configuration prefab data (OverlayConfigurationData) — used to obtain materials for object brush rendering. -
private ToolSystem m_ToolSystem
Cached reference to the ToolSystem (fetched from the World) — the system that owns tool logic. -
private TerrainSystem m_TerrainSystem
Cached reference to the TerrainSystem used to submit preview brush data for terraforming preview. -
private PrefabSystem m_PrefabSystem
Cached reference to the PrefabSystem used to resolve Prefab/BrushPrefab/OverlayConfigurationPrefab data. -
private Mesh m_Mesh
Cached Mesh instance used to draw the brush overlay geometry. Created lazily by GetMesh(). -
private MaterialPropertyBlock m_Properties
Cached MaterialPropertyBlock used to pass per-brush texture and opacity values to the material when drawing. -
private int m_BrushTexture
Shader property ID for "_BrushTexture" (obtained via Shader.PropertyToID) used when setting the brush texture on the MaterialPropertyBlock. -
private int m_BrushOpacity
Shader property ID for "_BrushOpacity" used when setting brush opacity on the MaterialPropertyBlock. -
private TypeHandle __TypeHandle
Compiler-generated struct holding ComponentTypeHandleand ComponentTypeHandle (both ReadOnly). Used to get component arrays from archetype chunks during rendering.
Properties
- None (this system exposes no public properties)
Constructors
public BrushRenderSystem()
Default parameterless constructor. The system performs initialization in OnCreate rather than the constructor.
Methods
protected override void OnCreate()
Initializes the system:- Retrieves and caches ToolSystem, TerrainSystem and PrefabSystem from the World.
- Creates EntityQueries: m_BrushQuery (Brush, exclude Hidden, Deleted) and m_SettingsQuery (OverlayConfigurationData).
- Caches shader property IDs for the brush texture and opacity.
-
Subscribes the Render method to RenderPipelineManager.beginContextRendering so brush rendering happens as part of the render context.
-
protected override void OnDestroy()
Cleanup: - Unsubscribes the Render callback from RenderPipelineManager.
-
Destroys the cached mesh (CoreUtils.Destroy) and calls base.OnDestroy().
-
protected override void OnUpdate()
Empty/unused in this system. Rendering work is handled in the Render callback, so OnUpdate contains no logic. -
private void Render(ScriptableRenderContext context, List<Camera> cameras)
Main rendering routine invoked via RenderPipelineManager.beginContextRendering: - Attempts to fetch OverlayConfigurationPrefab via m_PrefabSystem.TryGetSingletonPrefab; returns early if not available.
- Computes vertical offsets from the TerrainSystem for placement of the brush mesh and preview heights.
- Obtains a lazily-initialized Mesh and MaterialPropertyBlock (GetMesh/GetProperties).
- Converts the m_BrushQuery into an ArchetypeChunk array and completes any pending dependencies (CompleteDependency) to safely read components.
- For each chunk, obtains native arrays of Brush and PrefabRef components via the component type handles from __TypeHandle.
- For each Brush instance:
- Builds transform (position, rotation by brush angle, scale from brush size).
- Resolves the tool PrefabBase and BrushPrefab via PrefabSystem.
- Sets texture and opacity into MaterialPropertyBlock.
- Selects the appropriate material depending on the tool prefab type (terraforming uses terraformingPrefab.m_BrushMaterial, objects use OverlayConfigurationPrefab.m_ObjectBrushMaterial).
- Draws the mesh via Graphics.DrawMesh for cameras of type Game or SceneView using the computed matrix and material (no shadows, no receive).
- If the tool entity has TerraformingData, calls PreviewHeight to forward preview info to TerrainSystem.
- Disposes the native chunk array after use.
- The method wraps operations in try/finally to ensure proper disposal and to avoid leaking resources if exceptions occur.
Notes: - Uses ECS chunk iteration with ComponentTypeHandle to efficiently read components. - Uses MaterialPropertyBlock to set per-instance texture and opacity, avoiding material instance allocations. - DrawMesh is invoked per brush and per relevant camera.
private void PreviewHeight(Brush brush, BrushPrefab prefab, TerraformingType terraformingType)
Builds bounds for the brush and adjusts brush strength for certain terraforming types:- Uses ToolUtils.GetBounds(brush) to compute Bounds2.
- If terraforming type is Level or Slope and brush.m_Strength is negative:
- For Level: converts strength to absolute value.
- For Slope: clamps strength to 0.
-
Calls m_TerrainSystem.PreviewBrush(terraformingType, bounds, brush, prefab.m_Texture) to instruct the TerrainSystem to show a height preview.
-
private Mesh GetMesh()
Lazily creates and returns the mesh used for brush rendering. The mesh: - Is named "Brush".
- Has 8 vertices forming a box-like unit geometry and a triangles array (36 indices) describing the faces.
-
Cached in m_Mesh to avoid repeated allocations.
-
private MaterialPropertyBlock GetProperties()
Lazily creates and returns the MaterialPropertyBlock stored in m_Properties. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler-generated method for codegen/IL postprocessing. Present but contains only a call to create and dispose an EntityQueryBuilder(Allocator.Temp). Used by generated OnCreateForCompiler logic. -
protected override void OnCreateForCompiler()
Compiler helper invoked for generated code paths: - Calls base.OnCreateForCompiler().
- Calls __AssignQueries and assigns component type handles via __TypeHandle.__AssignHandles(ref base.CheckedStateRef).
Usage Example
// Example: create a brush entity so BrushRenderSystem will render an overlay.
// This code runs on the main thread where you have access to the World/EntityManager.
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
// Create an entity with Brush and PrefabRef components (pseudo-fields shown; match actual component fields)
var archetype = em.CreateArchetype(typeof(Brush), typeof(PrefabRef));
var entity = em.CreateEntity(archetype);
// Fill Brush component (set position, size, angle, opacity, tool entity)
var brush = new Brush {
m_Position = new float3(100f, 0f, 100f),
m_Size = 50f,
m_Angle = 0f,
m_Opacity = 0.8f,
m_Strength = 1f,
m_Tool = /* Entity referencing the tool prefab / tool instance */,
};
em.SetComponentData(entity, brush);
// Set PrefabRef pointing to a BrushPrefab asset (the index/handle depends on game's prefab system)
var prefabRef = new PrefabRef { /* fill with valid prefab reference for a BrushPrefab */ };
em.SetComponentData(entity, prefabRef);
// Once the entity exists, BrushRenderSystem will render the brush overlay automatically
// during render pipeline callbacks (no further calls required).
Notes: - BrushRenderSystem is driven by the render pipeline callback and ECS; do not call Render manually unless you understand the rendering context. - Ensure prefabs (BrushPrefab, OverlayConfigurationPrefab, TerraformingPrefab, etc.) are present/loaded in the PrefabSystem; otherwise brush rendering may be skipped.