Search Unity

Question How to have both a Depth and Normal camera in the same HDRP Scene

Discussion in 'High Definition Render Pipeline' started by dblaauw, Nov 2, 2022.

  1. dblaauw

    dblaauw

    Joined:
    Oct 7, 2013
    Posts:
    9
    Hi There!

    I'm working on a project where I need to have both an RGB color camera as well as a Depth camera in the same scene. I'm using:
    • Unity 2021.3.10f1
    • HDRP 12.1.7

    I've created a standard HDRP scene, added a Camera (named Depth camera) and attached a volume to it so only one camera should render Depth.

    Screenshot 2022-11-02 at 20.05.02.png

    I have the RGB camera rendering to Display 1, and the Depth camera to Display 2 for testing.

    I've also made sure to add the Custom Post Process script to the project settings:

    Screenshot 2022-11-02 at 20.08.34.png


    In the volume attached the the Depth camera, I selected "Add override" -> "Post Processing" -> "Custom" -> DepthExample

    After doing that however, the entire scene changes to a Depth map, also on both cameras so the effect is not constrained to the volume.

    Screenshot 2022-11-02 at 20.12.41.png

    I also have test code that uses the Unity example for creating GrayScale using HDRP, and I get the same behaviour.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4. using System;
    5.  
    6. [Serializable, VolumeComponentMenu("Post-processing/Custom/DepthExample")]
    7. public sealed class DepthExample : CustomPostProcessVolumeComponent, IPostProcessComponent
    8. {
    9.     [Tooltip("Controls the intensity of the effect.")]
    10.     public ClampedFloatParameter depthDistance = new ClampedFloatParameter(1f, 0f, 32f);
    11.     public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
    12.  
    13.     Material m_Material;
    14.  
    15.     public bool IsActive() => m_Material != null;
    16.  
    17.     public override CustomPostProcessInjectionPoint injectionPoint => CustomPostProcessInjectionPoint.AfterPostProcess;
    18.  
    19.     public override void Setup()
    20.     {
    21.         if (Shader.Find("Hidden/Shader/DepthExample") != null)
    22.             m_Material = new Material(Shader.Find("Hidden/Shader/DepthExample"));
    23.     }
    24.  
    25.  
    26.     public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
    27.     {
    28.  
    29.         if (m_Material == null)
    30.             return;
    31.  
    32.  
    33.         // Packing multiple float paramters into one float4 uniform
    34.         Vector4 parameters = new Vector4(depthDistance.value, depthDistance.value, depthDistance.value, depthDistance.value);
    35.         m_Material.SetVector("_Params", parameters);
    36.         m_Material.SetTexture("_InputTexture", source);
    37.  
    38.         m_Material.SetFloat("_Intensity", intensity.value);  //< Test code for testing the grayscal effect
    39.         cmd.Blit(source, destination, m_Material, 0);
    40.  
    41.         //HDUtils.DrawFullScreen(cmd, m_Material, destination);
    42.     }
    43.  
    44.     public override void Cleanup() => CoreUtils.Destroy(m_Material);
    45. }

    Here is my shader code (with grayscale test code embedded, just uncomment the return you want:

    Code (CSharp):
    1. Shader "Hidden/Shader/DepthExample"
    2. {
    3.     Properties
    4.     {
    5.         // This property is necessary to make the CommandBuffer.Blit bind the source texture to _MainTex
    6.         _MainTex("Main Texture", 2DArray) = "grey" {}
    7.     }
    8.  
    9.     HLSLINCLUDE
    10.  
    11.     #pragma target 4.5
    12.     #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
    13.  
    14.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    15.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    16.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    17.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
    18.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
    19.  
    20.     struct Attributes
    21.     {
    22.         uint vertexID : SV_VertexID;
    23.         UNITY_VERTEX_INPUT_INSTANCE_ID
    24.     };
    25.  
    26.     struct Varyings
    27.     {
    28.         float4 positionCS : SV_POSITION;
    29.         float2 texcoord   : TEXCOORD0;
    30.         UNITY_VERTEX_OUTPUT_STEREO
    31.  
    32.     };
    33.  
    34.     Varyings Vert(Attributes input)
    35.     {
    36.         Varyings output;
    37.  
    38.         UNITY_SETUP_INSTANCE_ID(input);
    39.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    40.  
    41.         output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
    42.         output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
    43.  
    44.         return output;
    45.     }
    46.  
    47.  
    48.  
    49.     // List of properties to control your post process effect
    50.     float _Intensity;
    51.     TEXTURE2D_X(_MainTex);
    52.  
    53.     float4 _Params;
    54.     #define _Distance _Params.w
    55.     TEXTURE2D_X(_InputTexture);
    56.     TEXTURE2D(_DepthTexture);
    57.     int _RunOnce = 0;
    58.  
    59.     float4 CustomPostProcess(Varyings input) : SV_Target
    60.     {
    61.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    62.  
    63.  
    64.         /////////////////////////////////////////////////////////////////////
    65.         /*
    66.          * Code below generates grayscale output. Used for testing the shader
    67.          */
    68.         float3 sourceColor = SAMPLE_TEXTURE2D_X(_MainTex, s_linear_clamp_sampler, input.texcoord).xyz;
    69.  
    70.         // Apply greyscale effect
    71.         float3 color = lerp(sourceColor, Luminance(sourceColor), _Intensity);
    72.  
    73.         /////////////////////////////////////////////////////////////////////
    74.  
    75.         /*
    76.          * Code below generates Depth output.
    77.          */
    78.         uint2 positionSS = input.texcoord * _ScreenSize.xy;
    79.  
    80.         // Depth
    81.         float depth = LoadCameraDepth(positionSS);
    82.         float linearEyeDepth = LinearEyeDepth(depth, _ZBufferParams);
    83.         float coc = saturate(_Distance / linearEyeDepth);
    84.  
    85.    
    86.         return float4(color, 1); //< Test code for testing grayscale effect
    87.         //return coc;
    88.     }
    89.  
    90.     ENDHLSL
    91.  
    92.     SubShader
    93.     {
    94.         Pass
    95.         {
    96.             Name "DepthExample"
    97.  
    98.             ZWrite Off
    99.             ZTest Always
    100.             Blend Off
    101.             Cull Off
    102.  
    103.             HLSLPROGRAM
    104.                 #pragma fragment CustomPostProcess
    105.                 #pragma vertex Vert
    106.             ENDHLSL
    107.         }
    108.     }
    109.  
    110.     Fallback Off
    111. }

    I've also attached a unity package I exported of this project, really hope someone can help me with this! I don't need the grayscale effect, just the Depth effect, and for the life of me I can't figure this one out!

    I've seen tutorials online where this could also be done using Shader Graph with custom passes and reading / writing to custom buffers ( see this link ) , but this only works with 2022.2 (which is still in beta), and I can't use beta builds at this stage, really prefer to stick to LTS if possible.
     

    Attached Files:

    Last edited: Nov 3, 2022
  2. akent99

    akent99

    Joined:
    Jan 14, 2018
    Posts:
    588
    What is a "Depth Effect"? (Just wondering if there is a different approach, like distant fog or camera blur.)
     
  3. dblaauw

    dblaauw

    Joined:
    Oct 7, 2013
    Posts:
    9
    Screenshot 2022-11-03 at 18.11.08.png It's a grayscale image where pixels are scaled between 0 and 1 depending on how far the object is from the camera. I actually managed to get something working using Shader Graph, I'll share it here in case anyone needs something similar. Please feel free to share tips on how to imropove things in case I messed something up!
     

    Attached Files: