Game.OverlayRenderSystem
Assembly: Assembly-CSharp
Namespace: Game.Rendering
Type: class
Base: GameSystemBase
Summary:
OverlayRenderSystem collects vector overlay primitives (circles, lines, Bezier curves) submitted by game code or jobs, packs them into native buffers, converts them to GPU-friendly data (ComputeBuffers) and issues instanced indirect draws (DrawMeshInstancedIndirect) to render overlay curves and projected overlays. It also manages related resources such as meshes, materials, compute buffers, bounds aggregation and a TextMeshPro instance for overlay text rendering. The class contains nested types used to describe per-instance curve data (CurveData), aggregate bounds (BoundsData), draw style flags (StyleFlags) and a helper Buffer struct used by clients to submit geometry from main thread or jobs.
Fields
-
private RenderingSystem m_RenderingSystem
Used to query rendering-related state (e.g. hideOverlay). Acquired during OnCreate to check whether overlays should be rendered. -
private TerrainSystem m_TerrainSystem
Used to obtain terrain height scale/offset used when fitting projected overlay boxes (used to compute Y extents). -
private PrefabSystem m_PrefabSystem
Used to access the OverlayConfigurationPrefab to create materials, font infos and curve material templates. -
private EntityQuery m_SettingsQuery
EntityQuery used to retrieve the overlay configuration singleton prefab (OverlayConfigurationData). -
private Mesh m_BoxMesh
Cached Mesh used for projected (box-shaped) instanced draws. -
private Mesh m_QuadMesh
Cached Mesh used for absolute (flat quad) instanced draws. -
private Material m_ProjectedMaterial
Material instance used to render projected curve instances. Created from configuration prefab when first needed. -
private Material m_AbsoluteMaterial
Material instance used to render absolute-mode curve instances. -
private ComputeBuffer m_ArgsBuffer
ComputeBuffer used as an indirect args buffer for DrawMeshInstancedIndirect. -
private ComputeBuffer m_ProjectedBuffer
ComputeBuffer that stores per-instance CurveData for projected overlays. -
private ComputeBuffer m_AbsoluteBuffer
ComputeBuffer that stores per-instance CurveData for absolute overlays. -
private List<uint> m_ArgsArray
Temporary list used to prepare indirect draw args uploaded into m_ArgsBuffer. -
private int m_ProjectedInstanceCount
Number of projected instances prepared in the current frame. -
private int m_AbsoluteInstanceCount
Number of absolute instances prepared in the current frame. -
private int m_CurveBufferID
Shader property ID for the curve StructuredBuffer (colossal_OverlayCurveBuffer). -
private int m_GradientScaleID
Shader property ID for gradient scale used when copying font/material parameters. -
private int m_ScaleRatioAID
Shader property ID for scale ratio parameter used when copying font/material parameters. -
private int m_FaceDilateID
Shader property ID for face dilate used when creating TMP fonts (set on TMP_FontAsset material). -
private NativeList<CurveData> m_ProjectedData
NativeList holding CurveData for projected overlays. Allocated persistent and written by callers using Buffer. -
private NativeList<CurveData> m_AbsoluteData
NativeList holding CurveData for absolute overlays. Allocated persistent and written by callers using Buffer. -
private NativeValue<BoundsData> m_BoundsData
NativeValue used to accumulate bounds (BoundsData) for all submitted curves. Used to produce the rendering bounds for indirect draws. -
private JobHandle m_BufferWriters
Aggregate JobHandle of writers that write into the native lists; GetBuffer returns this as dependencies and AddBufferWriter allows combining additional handles. -
private TextMeshPro m_TextMesh
Cached TextMeshPro instance created on demand to provide TMP fonts for overlay text rendering; fonts are created from the OverlayConfigurationPrefab FontInfo entries.
Properties
- This class does not expose public properties.
Constructors
public OverlayRenderSystem()
Default (preserved) constructor. The system subscribes to RenderPipelineManager.beginContextRendering in OnCreate and performs resource allocation/lazy initialization as needed.
Methods
-
protected override void OnCreate()
Initializes references to other game systems (RenderingSystem, TerrainSystem, PrefabSystem), sets up shader property IDs and subscribes Render to RenderPipelineManager.beginContextRendering. Called by the ECS system when the system is created. -
protected override void OnDestroy()
Unsubscribes from RenderPipelineManager.beginContextRendering and releases/destroys all allocated resources: meshes, materials, compute buffers, native lists/values, and any created TMP assets. Ensures persistent native allocations are disposed to avoid leaks. -
public Buffer GetBuffer(out JobHandle dependencies)
Prepares and returns a Buffer struct that callers use to submit overlay primitives (DrawCircle, DrawLine, DrawCurve, etc.). Ensures the backing NativeListand NativeValue are allocated (Allocator.Persistent) and returns the current m_BufferWriters as dependencies (out parameter). The Buffer holds references to the native collections and terrain Y extents used for fitting. -
public void AddBufferWriter(JobHandle handle)
Combine the supplied JobHandle with the internal m_BufferWriters (JobHandle.CombineDependencies). Call this after scheduling jobs that write into the NativeList/NativeValue returned via GetBuffer so the system waits for them before reading/uploading. -
public TextMeshPro GetTextMesh()
Lazily creates a GameObject with a TextMeshPro component (kept DontDestroyOnLoad) and constructs TMP_FontAsset instances for each FontInfo in the OverlayConfigurationPrefab. Copies fallback font assets into the primary TMP font's fallback list. The created text mesh is disabled (renderer.enabled = false) and returned for clients to configure text rendering for overlays. -
private TMP_FontAsset CreateFont(FontInfo info)
Creates a TMP_FontAsset from a FontInfo (font, sampling size, padding, atlas size) using TMP_FontAsset.CreateFontAsset and sets the face dilate on the font material to the configured face dilate property (m_FaceDilateID). Returns the created TMP_FontAsset. -
public void CopyFontAtlasParameters(Material source, Material target)
Copies font atlas related shader parameters from one material to another: multiplies gradient scale by 2f when setting on target, copies scale ratio A and mainTexture. Useful to sync TMP font/material parameters to the overlay materials. -
protected override void OnUpdate()
Completes any pending buffer writer JobHandles, resets instance counts and uploads native CurveData arrays to the corresponding ComputeBuffers. For each non-empty native list (projected/absolute), it ensures a corresponding material and ComputeBuffer exist, sets buffer data and assigns the buffer to the material. Clears the native lists after copying. If the overlay configuration query is empty, it clears lists and returns early. -
private void Render(ScriptableRenderContext context, List<Camera> cameras)
Render callback subscribed to RenderPipelineManager.beginContextRendering. If overlays are not hidden, builds or resizes the indirect args buffer, collects bounds (from m_BoundsData), prepares draw args (index count, instance count, etc.) for box and quad meshes and issues Graphics.DrawMeshInstancedIndirect calls for each camera of type Game or SceneView. This is where the GPU actually draws the overlay instances using the per-instance CurveData ComputeBuffer bound to the overlay material(s). -
private void GetMesh(ref Mesh mesh, bool box)
Lazy-creates the Mesh used for instanced draws. If box == true creates a 8-vertex box mesh (used for projected overlays), otherwise creates a 4-vertex quad mesh (used for absolute overlays). Initializes vertices and triangle/index arrays. -
private void GetCurveMaterial(ref Material material, bool projected)
Lazily instantiates a material from the OverlayConfigurationPrefab's m_CurveMaterial. Names it "Overlay curves" and enables the PROJECTED_MODE shader keyword when requested for projected overlays. -
private unsafe void GetCurveBuffer(ref ComputeBuffer buffer, int count)
Ensures the supplied ComputeBuffer has capacity for at least count instances, doubling capacity when reallocation is needed. If buffer is null, creates a new ComputeBuffer sized at max(64, count) with stride sizeof(CurveData) and names it "Overlay curve buffer". Uses math from Unity.Mathematics for sizing. -
public OverlayRenderSystem()
Empty preserved constructor (kept in code with [Preserve] attribute in OnCreate region). No special parameters.
Usage Example
// Example usage from other game code or a mod script:
// - Get the overlay render system
// - Acquire a Buffer to submit overlays
// - Submit a circle and a dashed line and add dependencies (if any)
var overlaySystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Rendering.OverlayRenderSystem>();
if (overlaySystem != null)
{
// Acquire buffer and dependencies (jobs writing to buffer would provide a JobHandle)
JobHandle deps;
var buffer = overlaySystem.GetBuffer(out deps);
// Submit a simple circle at world position (10, 0, 20) with diameter 5
buffer.DrawCircle(Color.yellow, new float3(10f, 0f, 20f), 5f);
// Submit a dashed straight line between two points
var seg = new Line3.Segment(new float3(0f, 0f, 0f), new float3(50f, 0f, 0f));
buffer.DrawDashedLine(Color.cyan, seg, width: 1.5f, dashLength: 2f, gapLength: 1f);
// If any jobs wrote into the Buffer's native lists, pass their JobHandle:
// overlaySystem.AddBufferWriter(jobHandle);
// Here we just pass the dependencies returned by GetBuffer (usually JobHandle = default)
overlaySystem.AddBufferWriter(deps);
}
Notes and tips:
- Use GetBuffer(out JobHandle) to obtain a Buffer for submitting overlays. If you schedule jobs that write into the NativeList/NativeValue in Buffer, combine their JobHandles with overlaySystem.AddBufferWriter so OnUpdate will wait for completion before uploading GPU data.
- The Buffer struct exposes convenience methods: DrawCircle, DrawLine, DrawCurve, DrawDashedLine, DrawDashedCurve. These fill NativeList