Skip to content

Game.Rendering.AreaBufferSystem

Assembly: Assembly-CSharp
Namespace: Game.Rendering

Type: Class

Base: GameSystemBase

Summary:
AreaBufferSystem prepares GPU-friendly area visualization data for the game's overlay rendering. It collects area triangles (lots, districts, map tiles, space, surface), builds ComputeBuffers for GPU shaders, and assembles label/name meshes and materials for rendering area names. The system uses Unity's Entities/JOBS to gather geometry and label vertex data, supports live updates (selection, edit mode), and exposes methods to retrieve the generated ComputeBuffer, material, bounds, name mesh and name materials for each AreaType. It also handles resource lifecycle across deserialization and when area-type prefabs/settings change.


Fields

  • private EntityQuery m_SettingsQuery
    Query used to fetch area type prefabs / settings (Created + AreaTypeData) for initializing materials and name-materials.

  • private RenderingSystem m_RenderingSystem
    Reference to the rendering system used to check overlay visibility and obtain text mesh for labels.

  • private OverlayRenderSystem m_OverlayRenderSystem
    Overlay helper for copying font atlas parameters and obtaining a TextMeshPro instance for label layout.

  • private PrefabSystem m_PrefabSystem
    Reference to the game's PrefabSystem used to retrieve AreaTypePrefab and related data.

  • private ToolSystem m_ToolSystem
    Tool system reference used to determine editor mode and which area types are required by the active tool.

  • private NameSystem m_NameSystem
    Name/label system used to get rendered label text for areas.

  • private AreaTypeData[] m_AreaTypeData
    Array holding per-area-type runtime data (buffers, materials, computed bounds, name meshes/materials, native lists, job dependencies, dirty flags, etc.). There are five entries corresponding to supported AreaType values.

  • private AreaType m_LastSelectionAreaType
    Tracks the last known selection area type to mark the appropriate area-type buffer as dirty when selection changes.

  • private EntityQuery m_SelectionQuery
    Query to obtain the current SelectionInfo and selection elements used to highlight selected areas.

  • private bool m_Loaded
    Flag set during PreDeserialize to force a full rebuild on the next update.

  • private int m_AreaParameters
    Shader property ID used to pass area-related parameters (min node distance derived values) to materials.

  • private Dictionary<Entity, string> m_CachedLabels
    Cache mapping entities to the last rendered label string to avoid recomputing label meshes when the string hasn't changed.

  • private TypeHandle __TypeHandle
    Internal generated container of ComponentTypeHandle/BufferTypeHandle/ComponentLookup fields used when scheduling jobs and reading component data.

  • (Note) There are many nested types inside AreaBufferSystem such as AreaTriangleData, AreaTypeData, ChunkData, job structs (ResetChunkDataJob, FillMeshDataJob, CalculateBoundsJob, FillNameDataJob), LabelVertexData and internal TypeHandle. These represent the data layout and jobs used to build buffers/meshes.

Properties

  • This system exposes no public properties.

Constructors

  • public AreaBufferSystem()
    Default constructor (system created by world). Initialization of system-level references and arrays is performed in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes references to other systems (RenderingSystem, OverlayRenderSystem, PrefabSystem, ToolSystem, NameSystem), initializes m_AreaTypeData array and queries, registers localization dictionary change callback, and prepares shader property IDs. Also prepares per-area-type queries with InitializeAreaData().

  • protected override void OnDestroy()
    Cleans up allocated resources: destroys materials and meshes, releases compute buffers, disposes native lists and mesh data arrays, and unregisters the dictionary change callback.

  • private void OnDictionaryChanged()
    Callback fired when localization changes; marks district (AreaType.District) entities as Updated so labels get rebuilt.

  • private AreaTypeData InitializeAreaData<T>() where T : struct, IComponentData
    Creates and returns an AreaTypeData instance for a specific area type T by building EntityQueries for either updated entities (Updated/BatchesUpdated/Deleted) or all existing areas of that type. Used during OnCreate.

  • public void PreDeserialize(Context context)
    Called before deserialization to release and clear runtime resources (native lists, compute buffers, meshes) so they can be recreated after load. Sets m_Loaded so the next update will perform a full rebuild.

  • private bool GetLoaded()
    Internal helper that returns true once immediately after PreDeserialize is called (consumes m_Loaded flag).

  • protected override void OnUpdate()
    Main system update. Checks settings queries and updates materials when area-type prefabs are created/changed. Tracks selection changes to mark dirty types. Schedules jobs (ResetChunkDataJob, FillMeshDataJob, CalculateBoundsJob and optionally FillNameDataJob) to collect triangle and label data into native lists / mesh data. Marks area type data as dirty so buffers / name meshes will be built in GetAreaBuffer/GetNameMesh. Uses Unity Jobs / Burst / ECS deferred arrays for performance.

  • public unsafe bool GetAreaBuffer(AreaType type, out ComputeBuffer buffer, out Material material, out Bounds bounds)
    Completes any pending data jobs for the specified AreaType and ensures the ComputeBuffer is created and populated from the native AreaTriangleData list. Returns buffer, the area material used for rendering, and the world Bounds covering the area data. Returns true if a valid (non-empty) buffer is available. This is the primary consumer-facing method to obtain GPU data to draw area overlays.

  • private void UpdateLabelVertices(AreaTypeData data, bool isLoaded)
    Builds/updates per-entity LabelVertex and LabelExtents buffers by using TextMeshPro to layout the rendered name text, copying vertex, UV and color info into entity dynamic buffers and collecting/getting/creating name-materials that match the font atlas textures. Updates the in-system cached label dictionary to avoid redundant rebuilds. Called by OnUpdate when name-materials are required or entities have been updated.

  • public bool GetNameMesh(AreaType type, out Mesh mesh, out int subMeshCount)
    If name mesh data is available (scheduled by UpdateLabelVertices via FillNameDataJob), completes job dependencies, applies and disposes the WritableMeshData to produce a Mesh instance, updates mesh bounds, and determines which sub-meshes (materials) actually have geometry. Returns the mesh and subMeshCount and a bool indicating whether any submesh had geometry (HasNameMesh).

  • public bool GetNameMaterial(AreaType type, int subMeshIndex, out Material material)
    Returns the material for the specified area type and sub-mesh index (font atlas-based materials created from the original name-material). Also returns whether that material has geometry associated (m_HasMesh).

  • private void __AssignQueries(ref SystemState state)
    Generated helper used by compiler-time scaffolding; noop in this decompiled listing.

  • protected override void OnCreateForCompiler()
    Generated initialization entry used for compiler-time wiring of queries/handles. Calls __AssignQueries and assigns __TypeHandle handles.

  • private void UpdateLabelVertices(AreaTypeData data, bool isLoaded) (private)
    (See above) Builds label vertex buffers and extents per entity using TMP text layout.

  • Several nested job structs and helper methods (ResetChunkDataJob, FillMeshDataJob, CalculateBoundsJob, FillNameDataJob) are used internally. Key points:

  • ResetChunkDataJob: counts triangles across archetype chunks and prepares ChunkData and the main AreaTriangleData native list.
  • FillMeshDataJob: iterates chunks and entities to sample triangle geometry, colors (selection / disabled), builds AreaTriangleData entries, and accumulates chunk bounds.
  • CalculateBoundsJob: aggregates chunk bounds into a single Bounds3 stored in the AreaTypeData.
  • FillNameDataJob: constructs writable mesh data for area name vertex buffers (multi-submesh) and fills vertex & index arrays from LabelVertex buffers for subsequent Apply to Mesh.

Usage Example

// Example: drawing area overlay from another rendering system
AreaBufferSystem areaBufferSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Rendering.AreaBufferSystem>();

if (areaBufferSystem.GetAreaBuffer(AreaType.Lot, out ComputeBuffer areaBuffer, out Material areaMaterial, out Bounds areaBounds))
{
    // Bind buffer to material and issue draw (example: material expects structured buffer "areaTriangles")
    areaMaterial.SetBuffer("areaTriangles", areaBuffer);
    // Set other per-draw shader parameters (e.g. camera matrices) as needed...
    // Example using Graphics.DrawProcedural (setup with proper shader pass):
    Graphics.DrawProcedural(areaMaterial, areaBounds, MeshTopology.Triangles, areaBuffer.count * 3);
}

Notes and tips: - GetAreaBuffer completes jobs and disposes the native area triangle list; call it from a render/update context after OnUpdate has run and before rendering. - Name meshes/materials are driven by TextMeshPro layout; call GetNameMesh/GetNameMaterial to retrieve name geometry and corresponding materials for rendering area names. - AreaBufferSystem uses ECS/job patterns (Burst, Native containers); avoid calling its internal/private APIs from non-main threads.