Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Change ZTest value for objects rendered in a custom pass.

Discussion in 'High Definition Render Pipeline' started by cubrman, Jul 27, 2020.

  1. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    409
    In the outline custom pass example objects are rendered into a custom buffer with their original material (say "Lit"). The only problem is, they can't be rendered with the ZTest as it is set to "Equal" as is default for the Lit shader and Forward rendering pass. I want to create an effect, where an entire mesh slowly fades away when it starts blocking the camera, but I don't want to use Transparent materials for it, instead I want to stop rendering the mesh at once when it crosses the camera and render it instead with the custom pass. I will then blend the custom render Texture into my scene with a variable alpha and be a happy camper. All I need right now is the ability to set the ZTest value for the Lit material to LEqual when the mesh is being rendered into my custom render texture with the custom pass.

    I know that the Forward rendering path renders every mesh twice (first into the depth buffer and then into color buffer) and I would ideally want to prevent this (I would wish my models were rendered with one pass with ZTest set to LEqual).
     
  2. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    409
    Ok I figured out that you can influence what pass the pipeline will use for rendering with ShaderTagId array that is passed to the RendererListDesc(). However, I cannot make the pipeline render my model in two passes properly: it seems to render the depth properly with the DepthOnly pass, but then it either renders the same depth values into the color buffer with the Forward pass or simply renders nothing, resulting in both the depth buffer of my RT as well as the color buffer containing the same values in the red channel:

    upload_2020-7-27_11-6-53.png

    I have no clue how to fix this.
     
  3. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    409
    I opened the code for DrawRenderersCustomPass and found out about stateBlock that goes into the RendererListDesc. I set the state block properly and changed the ZTest value, but still the result is a red object. This is due to the fact that once I set depth bits on my allocated RenderTarget:

    Code (CSharp):
    1.  
    2.         OutlineBuffer = RTHandles.Alloc(
    3.             Vector2.one, TextureXR.slices, depthBufferBits: DepthBits.Depth32, dimension: TextureXR.dimension,
    4.             colorFormat: GraphicsFormat.B10G11R11_UFloatPack32,
    5.             useDynamicScale: true, name: "Outline Buffer"
    6.         );
    7.  
    the whole RenderTarget turns into a depth render target:

    upload_2020-7-27_11-48-41.png

    How do I attach a depth buffer properly? If I set no depth bits, the model is rendered without the ZTest as if there is no depth buffer attached.
     
  4. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    409
    Ok I got it: the depth buffer must be a separate render target, allocated alongside the color buffer. The color buffer must have depthBits set to none. Then these two render targets are set via the CoreUtils.SetRenderTarget() function and then everything works.
     
    Egad_McDad likes this.
  5. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    111
    Can you share a simple working example? I am struggling to handle depth correctly using the new RTHandle system
     
  6. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    409
    @JSmithIR dude it was way too long ago, I was a different man back then :D