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

Question Scriptable Renderer Feature with new RTHandle system - depth not working as expected

Discussion in 'Universal Render Pipeline' started by JSmithIR, May 24, 2023.

  1. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    111
    Hello,

    Changing to URP 15 I find myself having to rewrite all my scriptable renderer features because of the new RTHandle system. However, even on the most basic custom pass I am finding it very difficult to get depth pass working. My script below is intended to: render scene objects with an override material into a temporary texture.

    The temporary texture is visualized on a quad with a material displaying this temporary texture (assigned using "SetGlobalTexture")

    It works fine EXCEPT that my temporary texture only captures the cameratargets color (when depthbufferbits = 0) OR its depth (when depthbufferbits is assigned 1 or higher). It cannot capture both. I either get a copy of the camera's depth texture or I get the correct color with no depth (ie no object culling).

    From this thread I learned that RTHandles cannot combine depth and color: https://forum.unity.com/threads/can...il-buffer-using-rthandles-if-not-why.1404010/

    How are we able to achieve this simple task using the new RTHandle system? My current code is below:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.Universal;
    6.  
    7. [System.Serializable]
    8. public class BlurTextureFeatureSettings
    9. {
    10.  
    11. }
    12.  
    13. public class BlurTextureFeature : ScriptableRendererFeature
    14. {
    15.     class BlurTexturePass : ScriptableRenderPass
    16.     {
    17.         private RenderTargetIdentifier cameraColorTargetIdent2;
    18.         private FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.opaque);
    19.         private readonly List<ShaderTagId> shaderTagIdList = new List<ShaderTagId>();
    20.  
    21.         RTHandle blur;
    22.         RTHandle m_CameraDepthTarget;
    23.         RTHandle tempTex;
    24.  
    25.         private readonly Material blurMaterial;
    26.         private readonly Material blurPass;
    27.  
    28.         public BlurTexturePass()
    29.         {
    30.  
    31.             blurMaterial = new Material(Shader.Find("BlurControl"));
    32.             blurPass = new Material(Shader.Find("Hidden/BlurShader"));
    33.  
    34.  
    35.             shaderTagIdList.Add(new ShaderTagId("UniversalForward"));
    36.             shaderTagIdList.Add(new ShaderTagId("UniversalForwardOnly"));
    37.            shaderTagIdList.Add(new ShaderTagId("LightweightForward"));
    38.             shaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
    39.         }
    40.  
    41.         public void SetTarget(RTHandle cameraColorTargetHandle, RTHandle cameraDepthTargetHandle)
    42.         {
    43.             blur = cameraColorTargetHandle;
    44.             m_CameraDepthTarget = cameraDepthTargetHandle;
    45.         }
    46.  
    47.         public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
    48.         {
    49.              RenderTextureDescriptor cameraTextureDescriptor = renderingData.cameraData.cameraTargetDescriptor;
    50.             RenderTextureDescriptor cameraTextureDescriptor2 = new RenderTextureDescriptor(cameraTextureDescriptor.width, cameraTextureDescriptor.height,
    51.                 RenderTextureFormat.ARGB32);
    52.             cameraTextureDescriptor2.depthBufferBits = 0;
    53.             RenderTextureDescriptor cameraTextureDescriptor3 = new RenderTextureDescriptor(cameraTextureDescriptor.width, cameraTextureDescriptor.height,
    54.                 RenderTextureFormat.Depth);
    55.             cameraTextureDescriptor3.depthBufferBits = 16;
    56.  
    57.  
    58.             RenderingUtils.ReAllocateIfNeeded(ref tempTex, Vector2.one, cameraTextureDescriptor2, FilterMode.Bilinear, TextureWrapMode.Clamp,  name: "_TestMap");
    59.      
    60.              m_CameraDepthTarget = renderingData.cameraData.renderer.cameraDepthTargetHandle;
    61.              blur = renderingData.cameraData.renderer.cameraColorTargetHandle;
    62.  
    63.            // cmd.GetTemporaryRT(Shader.PropertyToID(tempTex.name), cameraTextureDescriptor2, FilterMode.Bilinear);
    64.  
    65.             ConfigureTarget(blur);
    66.            ConfigureTarget(m_CameraDepthTarget);
    67.             ConfigureTarget(tempTex);
    68.             ConfigureClear(ClearFlag.All, Color.black);
    69.         }
    70.  
    71.         public void ReleaseTargets()
    72.         {
    73.  
    74.             tempTex?.Release();
    75.  
    76.         }
    77.  
    78.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    79.         {
    80.             if (!blurMaterial)
    81.             {
    82.                 return;
    83.             }
    84.  
    85.             CommandBuffer cmd = CommandBufferPool.Get();
    86.  
    87.             using (new ProfilingScope(cmd, new ProfilingSampler("BlurTexture")))
    88.             {
    89.              
    90.                 context.ExecuteCommandBuffer(cmd);
    91.                 cmd.Clear();
    92.  
    93.                CoreUtils.SetRenderTarget(cmd, tempTex, m_CameraDepthTarget);
    94.  
    95.                 SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
    96.  
    97.                 Camera camera = renderingData.cameraData.camera;
    98.                 context.DrawSkybox(camera);
    99.  
    100.                 DrawingSettings drawSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortingCriteria);
    101.                 drawSettings.overrideMaterial = blurMaterial;
    102.  
    103.                 context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filteringSettings);
    104.  
    105.                 //  Blitter.BlitCameraTexture(cmd, m_CameraDepthTarget, tempTex);
    106.  
    107.                 cmd.SetGlobalTexture("_BlurControlTexture", tempTex);
    108.  
    109.             }
    110.  
    111.             context.ExecuteCommandBuffer(cmd);
    112.             CommandBufferPool.Release(cmd);
    113.         }
    114.  
    115.         public override void OnCameraCleanup(CommandBuffer cmd)
    116.         {
    117.             cmd.ReleaseTemporaryRT(Shader.PropertyToID(blur.name));
    118.             cmd.ReleaseTemporaryRT(Shader.PropertyToID(tempTex.name));
    119.             cmd.ReleaseTemporaryRT(Shader.PropertyToID(m_CameraDepthTarget.name));
    120.         }
    121.     }
    122.  
    123.     private BlurTexturePass m_ScriptablePass;
    124.  
    125.     /// <inheritdoc/>
    126.     public override void Create()
    127.     {
    128.         m_ScriptablePass = new BlurTexturePass();
    129.         m_ScriptablePass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
    130.  
    131.     }
    132.  
    133.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    134.     {
    135.         renderer.EnqueuePass(m_ScriptablePass);
    136.  
    137.     }
    138.  
    139.     public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
    140.     {
    141.         if (renderingData.cameraData.cameraType == CameraType.Game)
    142.         {
    143.             m_ScriptablePass.ConfigureInput(ScriptableRenderPassInput.Depth | ScriptableRenderPassInput.Color);
    144.             m_ScriptablePass.SetTarget(renderer.cameraColorTargetHandle, renderer.cameraDepthTargetHandle);
    145.         }
    146.     }
    147.  
    148.     protected override void Dispose(bool disposing)
    149.     {
    150.         m_ScriptablePass.ReleaseTargets();
    151.     }
    152.  
    153.  
    154. }