Search Unity

Question CustomPassUtils.RenderFromCamera not rendering UI

Discussion in 'High Definition Render Pipeline' started by olavrv, Apr 29, 2022.

  1. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    I am using custom pass to render from a disabled camera. This works perfect for opaque and transparent geometry, but it doesn't render UI elements. Any help with this would be appreciated!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.HighDefinition;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Experimental.Rendering;
    5. using System.Reflection;
    6.  
    7. #if UNITY_EDITOR
    8.  
    9. using UnityEditor.Rendering.HighDefinition;
    10.  
    11. [CustomPassDrawer(typeof(RenderFromCamera))]
    12. class FPSForegroundEditor : CustomPassDrawer
    13. {
    14.     protected override PassUIFlag commonPassUIFlags => PassUIFlag.Name;
    15. }
    16.  
    17. #endif
    18.  
    19. class RenderFromCamera : CustomPass
    20. {
    21.  
    22.     public LayerMask foregroundMask;
    23.     public Camera UICamera;
    24.     public RenderTexture targetRenderTexture;
    25.     public Material overrideMaterial;
    26.     public ClearFlag clearFlag = ClearFlag.None;
    27.     public RenderQueueType renderQue = RenderQueueType.All;
    28.     public CompareFunction depthState = CompareFunction.NotEqual;
    29.     protected override bool executeInSceneView => false;
    30.     FieldInfo cullingResultField, hdCamField;
    31.  
    32.  
    33.     protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
    34.         => cullingParameters.cullingMask |= (uint)foregroundMask.value;
    35.  
    36.     protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    37.     {
    38.         cullingResultField = typeof(CustomPassContext).GetField(nameof(CustomPassContext.cullingResults));
    39.     }
    40.  
    41.     protected override void Execute(CustomPassContext ctx)
    42.     {
    43.         if (ctx.hdCamera.camera.cameraType == CameraType.SceneView)
    44.             return;
    45.  
    46.         UICamera.enabled = false;
    47.  
    48.         var depthTestOverride = new RenderStateBlock(RenderStateMask.Depth)
    49.         {
    50.             depthState = new DepthState(true, depthState),
    51.         };
    52.  
    53.         UICamera.TryGetCullingParameters(out var cullingParams);
    54.  
    55.         cullingResultField.SetValueDirect(__makeref(ctx), ctx.renderContext.Cull(ref cullingParams));
    56.  
    57.         CustomPassUtils.RenderFromCamera(ctx, UICamera, /*targetRenderTexture*/ctx.cameraColorBuffer, clearFlag, foregroundMask, renderQue, overrideRenderState: depthTestOverride);
    58.     }
    59.  
    60.     protected override void Cleanup()
    61.     {
    62.     }
    63. }
     
    Last edited: Apr 29, 2022
  2. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    Anyone?
     
  3. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    @antoinel_unity Can you please give some input on this? Would really appreciate it, thanks!
     
  4. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    Hello,

    So there are a couple of things to check to make sure your effect is working:
    - Only the canvas render mode "Screen Space UI - Camera" mode is supported, the overlay renders directly to the screen so you can't capture it in a render target (it's rendered after the camera)
    - Screen Space Camera UI can only be rendered if the "Render Camera" is enabled, in your case you disable it in the script so that's not good. As an alternative to cutting the cost of the HDRP camera, you can enable "fullscreen passthrough" in the camera settings.
    - Make sure you also send a depth buffer with a stencil to the DrawRenderers call otherwise all the UI masking features will not work
    - UI Toolkit UI is not supported because it writes directly to the screen like "Screen Space- Overlay" mode

    Alternatively, you can also use this package: https://github.com/alelievr/HDRP-UI-Camera-Stacking it's simpler to use for camera stacking purposes.
     
    olavrv likes this.