Search Unity

HOWTO: In Renderpass change color attachment but keep depth attachment

Discussion in 'Universal Render Pipeline' started by Marco-Playable, May 4, 2021.

  1. Marco-Playable

    Marco-Playable

    Joined:
    Nov 12, 2020
    Posts:
    53
    I have a custom RenderPass that is supposed to use the default camera depth buffer but output to a separate color buffer. Figuring this out took longer than I'd like to admit, but sharing it here in case someone else stumbles over it.

    RenderPass looks like this:
    Code (CSharp):
    1.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    2.         {
    3.             var shaderPropertyId = Shader.PropertyToID("_MyShaderPropertyName");
    4.  
    5.             // Make new render target
    6.             RenderTextureDescriptor colorTargetDesc = cameraTextureDescriptor;
    7.             colorTargetDesc.colorFormat = RenderTextureFormat.RGFloat;
    8.             colorTargetDesc.depthBufferBits = 0;
    9.             cmd.GetTemporaryRT(shaderPropertyId, colorTargetDesc);
    10.  
    11.             ConfigureClear(ClearFlag.Color, new Color(1, 1, 0, 0));
    12.            // Override color attachment, keep Depth ?
    13.             ConfigureTarget(shaderPropertyId);
    14.         }
    But turns out that sets the depth buffer to null. I tried a couple of the texture enums (Depth, CameraTarget,..) but the only one that works is this:
    Code (CSharp):
    1. ConfigureTarget(shaderPropertyId, BuiltinRenderTextureType.CurrentActive);
     
    FrankLee_legou and ToreOsc like this.