Game.Rendering.UndergroundPass
Assembly:
Game (inferred from namespace and project structure)
Namespace:
Game.Rendering
Type:
public class UndergroundPass : CustomPass
Base:
UnityEngine.Rendering.HighDefinition.CustomPass
Summary:
UndergroundPass is a HDRenderPipeline CustomPass that renders underground features (tunnels, pipelines, markers and contour lines) into an offscreen color & depth buffers and then composites those results back into the camera color buffer using a compute shader. It queries the world for an UndergroundViewSystem to determine which underground elements are enabled, sets up layer masks and resources in Setup, augments culling using AggregateCullingParameters, and performs rendering + compute dispatches in Execute. The pass expects resources named "UndergroundPass" (ComputeShader) and "TerrainHeights" (Shader) to exist in Resources.
Fields
private enum ComputeFlags
-
Compute flags used to control compute shader behavior:
- FadeCameraColor = 1
- FadeNearSurface = 2
- EmphasizeCustomColor = 4
-
private UndergroundViewSystem m_UndergroundViewSystem
-
Reference to the game system that exposes which underground features are turned on (tunnelsOn, pipelinesOn, subPipelinesOn, markersOn, contourLinesOn, undergroundOn). Populated in Setup by searching World.All for the Simulation world.
-
private ShaderTagId[] m_ShaderTags
-
Shader pass tags used to build renderer lists. Initialized to HD forward/forward-only/unlit pass names.
-
private ComputeShader m_ComputeShader
-
ComputeShader loaded from Resources with name "UndergroundPass". Used to composite underground color/depth buffers into the camera color buffer.
-
private Material m_ContourMaterial
-
Material created from the "TerrainHeights" shader (Resources.Load
("TerrainHeights")) and used to render terrain contour lines into the custom render target. -
private int m_TunnelMask
-
Bit mask for Tunnel and Moving layers: (1 << LayerMask.NameToLayer("Tunnel")) | (1 << LayerMask.NameToLayer("Moving"))
-
private int m_MarkerMask
-
Bit mask for Marker layer: (1 << LayerMask.NameToLayer("Marker"))
-
private int m_PipelineMask
-
Bit mask for Pipeline layer: (1 << LayerMask.NameToLayer("Pipeline"))
-
private int m_SubPipelineMask
-
Bit mask for SubPipeline layer: (1 << LayerMask.NameToLayer("SubPipeline"))
-
private int m_CameraColorBuffer
-
Shader property ID for "_CameraColorBuffer"
-
private int m_UndergroundColorBuffer
-
Shader property ID for "_UndergroundColorBuffer"
-
private int m_UndergroundDepthBuffer
-
Shader property ID for "_UndergroundDepthBuffer"
-
private int m_UndergroundFlags
-
Shader property ID for "_UndergroundFlags"
-
private int m_UndergroundPassKernel
-
Kernel index for compute shader kernel named "UndergroundPass"
-
private int m_ContourPassKernel
- Kernel index for compute shader kernel named "ContourPass"
Properties
- None (this class exposes no public properties)
Constructors
public UndergroundPass()
- No explicit constructor is defined in the source — default parameterless constructor will be used. Initialization of resources happens in Setup, which is called by the HDRP CustomPass lifecycle.
Methods
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
- Searches World.All for the Simulation world and caches the UndergroundViewSystem instance.
- Initializes m_ShaderTags to HD forward/forward-only/unlit pass names.
- Loads resources:
- ComputeShader: Resources.Load
("UndergroundPass") - Shader: Resources.Load
("TerrainHeights") -> used to create m_ContourMaterial
- ComputeShader: Resources.Load
- Initializes layer masks for Tunnel, Moving, Marker, Pipeline, SubPipeline (by name).
- Caches shader property IDs for buffers and flags.
- Retrieves compute kernel indices for "UndergroundPass" and "ContourPass".
-
Notes:
- If the expected resources or layers are missing at runtime, the pass will fail to work correctly.
- m_ContourMaterial is allocated here; Cleanup currently does not dispose of it (see Cleanup notes).
-
protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
- If m_UndergroundViewSystem is available, modifies the cullingParameters.cullingMask to include the layers relevant to enabled underground features:
- tunnelsOn adds Tunnel (and optionally Marker) layers
- pipelinesOn adds Pipeline layer
- subPipelinesOn adds SubPipeline layer
-
This ensures that the renderer lists include objects from those layers when the pass runs.
-
protected override void Execute(CustomPassContext ctx)
- If m_UndergroundViewSystem is null or none of the underground features are active, returns early.
- Sets ComputeFlags based on m_UndergroundViewSystem.undergroundOn. If underground view is on, includes FadeCameraColor and EmphasizeCustomColor (value 5), otherwise 0.
- Contour lines:
- If contourLinesOn, obtains the valid TerrainSurface and its CBT (CBTSubdivisionTerrainEngine).
- If the terrain CBT and material are valid, renders the terrain to ctx.customColorBuffer / ctx.customDepthBuffer using HDRenderPipeline helper methods with m_ContourMaterial.
- Sets compute shader textures for the contour pass: camera color, underground color/depth buffers, and dispatches the ContourPass kernel.
- Tunnels rendering:
- Builds a RendererListDesc that targets layerMask m_TunnelMask (optionally includes markers and pipeline layer depending on toggles).
- Disables HDRP decal keyword DECALS_4RT and enables DECALS_OFF while drawing to avoid decal interference.
- Renders the renderer list into ctx.customColorBuffer / ctx.customDepthBuffer.
- Sets compute shader textures and sets underground flags to include FadeNearSurface and whatever computeFlags is currently set, then dispatches the UndergroundPass kernel.
- Clears the FadeCameraColor flag from the computeFlags variable after the first use.
- Pipelines / SubPipelines rendering:
- Similar to tunnels but uses layer masks for pipeline and subpipeline depending on which toggles are set.
- Renders into custom buffers and dispatches the UndergroundPass kernel with the remaining computeFlags.
- Compute dispatch group size:
- The dispatch width/height use (texture.width + 15) >> 4 and (texture.height + 15) >> 4 — this corresponds to a 16x16 thread group size in the compute shader.
-
Shader keywords toggled by this method:
- DECALS_OFF and DECALS_4RT are swapped around renderer list draws to control decal rendering.
-
protected override void Cleanup()
- Currently empty in the provided source. Note that m_ContourMaterial is allocated in Setup but not explicitly destroyed here — potential material leak if the pass is created/destroyed frequently. Consider destroying the material in Cleanup if lifecycle management requires it:
- Object.DestroyImmediate(m_ContourMaterial) (call from main thread) or ensure a shared material is reused.
Usage Example
Ensure the required resources and layers exist in your project: - Add a ComputeShader asset named "UndergroundPass" to a Resources folder (Resources/UndergroundPass.compute or .asset). - Add a Shader asset named "TerrainHeights" to Resources or ensure it is accessible via Resources.Load("TerrainHeights"). - Define layers with names: "Tunnel", "Moving", "Marker", "Pipeline", "SubPipeline".
Attach this CustomPass to an HD Custom Pass Volume (via inspector) or register it in your HDRP custom pass manager. The CustomPass lifecycle will call Setup, AggregateCullingParameters and Execute automatically. Example notes:
// Example notes (not a runnable full example):
// - Place ComputeShader "UndergroundPass" and Shader "TerrainHeights" in Resources.
// - Create a Custom Pass Volume in your scene and add the UndergroundPass as a CustomPass.
// - Ensure the UndergroundViewSystem (game system) is present in the Simulation world and toggles the following booleans appropriately:
// tunnelsOn, pipelinesOn, subPipelinesOn, markersOn, contourLinesOn, undergroundOn
//
// The pass will:
// - Render selected underground layers into ctx.customColorBuffer & ctx.customDepthBuffer
// - Dispatch the compute shader kernels "UndergroundPass" and "ContourPass" to composite results into the camera color buffer
Additional implementation notes: - The compute shader kernels expected: "UndergroundPass" and "ContourPass". The C# side binds: - _CameraColorBuffer -> camera color texture - _UndergroundColorBuffer -> custom color buffer used to render underground objects - _UndergroundDepthBuffer -> custom depth buffer used during underground render - _UndergroundFlags -> integer bitfield of ComputeFlags - The pass depends on HDRenderPipeline internals/utilities: HDRenderPipeline.PrepareTerrainRenderingParameters, HDRenderPipeline.RenderTerrainSurfaceCBT, and CoreUtils.SetRenderTarget / DrawRendererList helpers. - Be careful with material allocation lifetime: if you spawn/destroy the CustomPass at runtime, free m_ContourMaterial in Cleanup to avoid leaks.