Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I grab the depth buffer of 6 different cameras?

Discussion in 'High Definition Render Pipeline' started by morphex, Nov 17, 2023.

  1. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    111
    I have 6 Cameras in my world I need to grab the depth buffer from them.
    I am literally only interested in the Depth buffer of the cameras, no effects, no lighting, just opaque objects that the camera can see.

    What process do I need to render the cameras, and then:
    a) Put it inside a Render Texture
    b) Use it in shader graph to render custom view of the scene based on the Depth?
     
  2. boyaregames

    boyaregames

    Joined:
    Jun 23, 2021
    Posts:
    63
    I did it this way.
    Make sure your cameras are disabled
    And i am not sure about right clear flags (color, depth) on custom pass
    Make custom pass script and add it to your custom pass volume

    Code (CSharp):
    1.  
    2. class DepthToRTCustomPass : CustomPass
    3.     {
    4.         [SerializeField] private LayerMask _layerMask = ~0;
    5.         [SerializeField] private RenderTexture[] _depthRenderTextures;
    6.         [SerializeField] private Camera[] _camera;
    7.  
    8.         protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    9.         {
    10.      
    11.         }
    12.  
    13.         protected override void Execute(CustomPassContext ctx)
    14.         {
    15.             if (ctx.hdCamera.camera.cameraType == CameraType.SceneView)
    16.                 return;
    17.      
    18.                 for (int i = 0; i < _camera.Length; i++)
    19.                 {
    20.                     if (_camera[i] == null) continue;
    21.              
    22.                     // Culling
    23.                     _camera[i].TryGetCullingParameters(out var cullingParameters);
    24.                     var cullingResults = ctx.renderContext.Cull(ref cullingParameters);
    25.                     ctx.cullingResults = cullingResults;
    26.  
    27.                     // Depth state
    28.                     var depthTestOverride = new RenderStateBlock(RenderStateMask.Depth)
    29.                     {
    30.                         depthState = new DepthState(true, CompareFunction.LessEqual),
    31.                     };
    32.  
    33.                     if (_depthRenderTextures[i] != null)
    34.                     {
    35.                         CustomPassUtils.RenderDepthFromCamera(ctx,
    36.                             _camera[i],
    37.                             _depthRenderTextures[i],
    38.                             clearFlags,
    39.                             _layerMask,
    40.                             RenderQueueType.All,
    41.                             depthTestOverride);
    42.                      }
    43.                 }
    44.         }
    45.     }
    46.  
     
    Last edited: Nov 22, 2023