Search Unity

Converting depth to HDRP

Discussion in 'High Definition Render Pipeline' started by Nir_Tevel, Aug 9, 2021.

  1. Nir_Tevel

    Nir_Tevel

    Joined:
    Apr 23, 2019
    Posts:
    8
    Hi,

    I need a camera to export the depth buffer it sees as a render texture, in HDRP.

    With the default render engine, this is how I accomplish it:

    1. I create a render texture on startup in RenderTextureFormat.ARGBFloat format, and assign it to the camera.

    2. The camera Blits what it sees to the render texture using a shader:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class CameraScript : MonoBehaviour {
    5.    
    6.     public Material mat;
    7.    
    8.     void Start()
    9.     {
    10.         GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
    11.     }
    12.    
    13.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    14.     {
    15.         Graphics.Blit(source, destination, mat);
    16.        
    17.     }
    18. }
    3. This is the shader used:

    Code (CSharp):
    1. Shader "Custom/ZBuffer TCP"
    2. {
    3.  
    4.     Properties
    5.     {
    6.         _NearClip ("nearClip", Float) = 0.5
    7.         _FarClip ("farClip", Float) = 3.1
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.        
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             float _NearClip;
    22.             float _FarClip;
    23.  
    24.             struct v2f
    25.             {
    26.                 float4 pos : SV_POSITION;
    27.                 float4 screenuv : TEXCOORD1;
    28.             };
    29.            
    30.             v2f vert (appdata_base v)
    31.             {
    32.                 v2f o;
    33.                 o.pos = UnityObjectToClipPos(v.vertex);
    34.                 o.screenuv = ComputeScreenPos(o.pos);
    35.                 return o;
    36.             }
    37.            
    38.             sampler2D _CameraDepthTexture;
    39.  
    40.             float4 frag (v2f i) : SV_Target
    41.             {
    42.                 // Flip the texture vertically
    43.                 // i.screenuv.y = 1 - i.screenuv.y;
    44.  
    45.                 // Flip the texture horizontally
    46.                 i.screenuv.x = 1 - i.screenuv.x;
    47.                 float2 uv = i.screenuv.xy / i.screenuv.w;
    48.  
    49.                 float depth = 1 - Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv));
    50.                 float frustrumRatio =  1 - (_NearClip / _FarClip);
    51.                 float frustrumDistance = (_FarClip - _NearClip);
    52.                 float normalizedRatio = -1 * (depth - frustrumRatio) / frustrumRatio;
    53.                 float distanceFromNearClip = normalizedRatio * frustrumDistance;
    54.                 float finalDistance = _NearClip + distanceFromNearClip;
    55.                 return float4(finalDistance, finalDistance, finalDistance, 1);
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }

    I'd like to convert this behavior to HDRP - have a render texture that shows the depth buffer one camera sees, with adjustable near/far clipping.

    Thanks for any help :)