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

Bug Why is color rendered to camera when the camera's depth buffer is used with a render texture?

Discussion in 'Universal Render Pipeline' started by Nallebeorn, Apr 18, 2023.

  1. Nallebeorn

    Nallebeorn

    Joined:
    Feb 13, 2017
    Posts:
    2
    I have a custom render pass in that renders some additional data to a render texture, for later use by a postprocessing effect. I figured that while this pass needs z-testing, there's no need for the render texture to have its own depth buffer -- so I tried reusing the main camera's depth buffer instead.

    The render pass uses the following code to set up its textures and targets.
    Code (CSharp):
    1.         public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
    2.         {
    3.             RenderTextureDescriptor cameraDescriptor = renderingData.cameraData.cameraTargetDescriptor;
    4.             RenderTextureDescriptor textureDescriptor = new(
    5.                 cameraDescriptor.width, cameraDescriptor.height,
    6.                 textureFormat, 0);
    7.  
    8.             cmd.GetTemporaryRT(destinationTextureId, textureDescriptor);
    9.  
    10.             ConfigureTarget(new RenderTargetIdentifier(destinationTextureId), renderingData.cameraData.renderer.cameraDepthTarget);
    11.             ConfigureClear(ClearFlag.Color, Color.clear);
    12.         }
    13.  
    This gives me some quite odd behaviour, and I'm unsure if it's a URP bug or just a funny little quirk that is Totally Intended. What happens is that by attaching
    cameraDepthTarget
    , the color output is now also rendered to the main camera target, and my precious render texture remains empty. This only happens in the game view and builds, while the scene view looks perfectly fine.

    upload_2023-4-18_21-10-36.png
    (The pure yellow colors is the intended color of all those objects, rendered to the camera color buffer. The red is the output of the custom pass, supposed to be rendered into a render texture).

    I initially, when using 2021.3.10, had some issues with inconsistencies with attaching the depth buffer in the first place, depending on whether or not a depth texture was being generated (see: https://forum.unity.com/threads/scr...consistently-access-the-depth-buffer.1263044/). These quirks seem to have been resolved after upgrading to the latest 2021 LTS release, though, and while replacing
    cameraDepthTarget
    with
    cameraColorTarget
    makes the pass render into its intended texture again, it also makes it not get a depth buffer at all :)

    I gave a quick try at importing the project into Unity 2022 and the same issue seemed to remain, but admittedly I didn't investigate that very thourougly.

    Does anyone have some insight into what might be going on here?