Search Unity

Custom Pass Depth Render Texture to eye units

Discussion in 'High Definition Render Pipeline' started by Tirus, Aug 15, 2020.

  1. Tirus

    Tirus

    Joined:
    Aug 9, 2013
    Posts:
    9
    Hey,

    I'm currently using the Scene Depth node in one of my shaders. However I want to exclude some layers from my depth mask for this shader, so I created a custom pass to copy the depth mask from certain layers into a render texture.

    Code (CSharp):
    1. public class WaterDepthBuffer : CustomPass
    2. {
    3.     private ShaderTagId[] _shaderTags;
    4.     public LayerMask maskLayer;
    5.  
    6.     public RenderTexture colorCopy = null;
    7.     public RenderTexture targetTexture = null;
    8.    
    9.     protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    10.     {
    11.         var hdrpAsset = (GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset);
    12.         var colorBufferFormat = hdrpAsset.currentPlatformRenderPipelineSettings.colorBufferFormat;
    13.  
    14.         colorCopy = RTHandles.Alloc(
    15.             Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
    16.             colorFormat: (GraphicsFormat)colorBufferFormat,
    17.             useDynamicScale: true, name: "Color Copy"
    18.         );
    19.        
    20.         targetTexture = RTHandles.Alloc(
    21.             Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
    22.             colorFormat: GraphicsFormat.R16_UInt,
    23.             name: "Filtered Depth Mask", depthBufferBits: DepthBits.Depth16
    24.         );
    25.        
    26.         _shaderTags = new ShaderTagId[6]
    27.         {
    28.             new ShaderTagId("Forward"),
    29.             new ShaderTagId("ForwardOnly"),
    30.             new ShaderTagId("SRPDefaultUnlit"),
    31.             new ShaderTagId("FirstPass"),
    32.             new ShaderTagId("DepthOnly"),
    33.             new ShaderTagId("DepthForwardOnly"),
    34.         };
    35.     }
    36.  
    37.     protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
    38.     {
    39.         var stateBlock = new RenderStateBlock(RenderStateMask.Depth)
    40.         {
    41.             depthState = new DepthState(true, CompareFunction.LessEqual),
    42.             stencilState = new StencilState(false),
    43.         };
    44.  
    45.         var result = new RendererListDesc(_shaderTags, cullingResult, hdCamera.camera)
    46.         {
    47.             rendererConfiguration = PerObjectData.None,
    48.             renderQueueRange = GetRenderQueueRange(CustomPass.RenderQueueType.AllOpaque),
    49.             sortingCriteria = SortingCriteria.CommonOpaque,
    50.             excludeObjectMotionVectors = false,
    51.             stateBlock = stateBlock,
    52.             layerMask = maskLayer,
    53.         };
    54.  
    55.         CoreUtils.SetRenderTarget(cmd, colorCopy, targetTexture, ClearFlag.Depth);
    56.         HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result));
    57.     }
    58.  
    59. }
    My plan is to use this renderTexture and feed it into my shader graph. Then use the screen position node to get the depth information from my rendertexture at that pixel. But I need the Depth mask to be in Eye Space Units, how do I convert my "targetTexture" to a depth mask in eye space units?