Search Unity

Custom Pass Script - How to Render Scene Excluding a Layer

Discussion in 'High Definition Render Pipeline' started by michaelfritz, Jun 13, 2020.

  1. michaelfritz

    michaelfritz

    Joined:
    May 11, 2020
    Posts:
    2
    I'm writing a custom pass script to create a see-through effect for the player. This means I'd like to render the scene to a texture without game objects on the "See Through" layer.
    The CustomPass script is injected at "Before Post Process."
    In the CustomPass Execute(...) I call:
    Code (CSharp):
    1.  
    2. var result = new RendererListDesc(shaderTags, cullingResult, hdcamera.camera)
    3. {
    4.             rendererConfiguration = PerObjectData.LightProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.Lightmaps,
    5.             renderQueueRange = RenderQueueRange.all,
    6.             sortingCriteria = SortingCriteria.BackToFront,
    7.             excludeObjectMotionVectors = false,
    8.             layerMask = ~seeThroughLayer,
    9.  
    10.             stateBlock = new RenderStateBlock(RenderStateMask.Depth) { depthState = new DepthState(true, CompareFunction.LessEqual) },
    11. };
    12.  
    13. CoreUtils.SetRenderTarget(cmd, seeThroughBuffer, seeThroughDepthBuffer, ClearFlag.All);
    14. HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result));
    15.  
    Where seeThroughBuffer is the texture I want to render to.
    After this call, I immediately do a fullscreen pass only rendering that texture.
    Here are the results:
    upload_2020-6-12_15-55-11.png

    The trees are on the "See Through" layer but still show shadows on the trees and the plane.
    I don't expect the trees' geometry to be drawn at all because I specified ~seeThroughLayer in the RendererListDesc structure.

    Why is the tree geometry being drawn? How can I render the scene to a texture as if the camera were rendering without those layers?
     
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    265
    Hello,

    I'm not sure to understand exactly what you want to achieve for your effect but maybe you can try to turn the problem the other way: render only your blockers in the custom pass and then compose them with the camera color buffer.
    A bit like the technique used in this video:
    where the scene is rendered normally, then characters behind the walls (in greater equal ZTest) with a certain material and finally the character with standard material in less equal ZTest.

    Note that you can't mimic the rendering of a camera in your custom pass because HDRP uses multi-pass rendering for normals, roughness, depth etc. and most of our effects rely on this. Custom pass is only able to render a set of objects in Forward and doesn't support multi-pass rendering.