RenderObjects do not like SSAO

We have ended up using a setup with a lot of render objects unfortunately :smile:.
The render feature panel looks like

while we don’t have major problems with it, we realised that for some reason these do now write in the same depth buffer texture that is then used by Screen Space Ambient Occlusion, ending up with see-through AO like this:

(on)

(off)

in order to fix this I used this code (from URP Render Feature that manually renders DepthOnly pass to _CameraDepthTexture, filtered by LayerMask. Tested in URP v10, Unity 2020.3 · GitHub):

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

/*
- Renders DepthOnly pass to _CameraDepthTexture, filtered by LayerMask
- If the depth texture is generated via a Depth Prepass, URP uses the Opaque Layer Mask at the top of the Forward/Universal Renderer asset
  to determine which objects should be rendered to the depth texture. This feature can be used to render objects on *other layers* into the depth texture as well.

- The Frame Debugger window can be used to check if a project is using a Depth Prepass vs Copy Depth pass, though unsure if that is the same for final build.
- Also see :
  https://github.com/Unity-Technologies/Graphics/blob/d48795e93b2e62d936ca6fca61364922f8c91285/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs#L436
  https://github.com/Unity-Technologies/Graphics/blob/d48795e93b2e62d936ca6fca61364922f8c91285/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs#L1174

Example :
- This RenderObjects example from the URP docs allows you to see the character through walls :
  https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@11.0/manual/renderer-features/how-to-custom-effect-render-objects.html
- But the character will no longer appear in the depth texture if using a Depth Prepass (but still appears if using Copy Depth, just to be confusing! woo~)
- Any shaders that rely on the depth texture (aka Scene Depth node) now won't include the character. e.g. Water Foam
- Using this feature can fix that, by allowing us to add the Character layer to the depth texture

- Note : Probably shouldn't be used with SSAO Depth Normals mode since that makes depth texture generation use DepthNormals instead of DepthOnly passes
  and might be weird to have objects appear in the depth texture but not the normals texture.
*/
public class RenderToDepthTexture : ScriptableRendererFeature {

    public LayerMask layerMask;

    public RenderPassEvent _event = RenderPassEvent.BeforeRenderingSkybox;
    /*
    - Set to at least AfterRenderingPrePasses if depth texture is generated via a Depth Prepass,
        or it will just be overriden
    - Set to at least BeforeRenderingSkybox if depth texture is generated via a Copy Depth pass,
          Anything before this is already included in the texture! (Though not for Scene View as that always uses a prepass)
    */

    class RenderToDepthTexturePass : ScriptableRenderPass {

        private ProfilingSampler m_ProfilingSampler;
        private FilteringSettings m_FilteringSettings;
        private List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();

        private RenderTargetHandle depthTex;

        public RenderToDepthTexturePass(LayerMask layerMask) {
            m_ProfilingSampler = new ProfilingSampler("RenderToDepthTexture");
            m_FilteringSettings = new FilteringSettings(RenderQueueRange.opaque, layerMask);
            m_ShaderTagIdList.Add(new ShaderTagId("DepthOnly")); // Only render DepthOnly pass
            depthTex.Init("_CameraDepthTexture");
        }

        public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {
            ConfigureTarget(depthTex.Identifier());
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
            SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
            DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);

            CommandBuffer cmd = CommandBufferPool.Get();
            using (new ProfilingScope(cmd, m_ProfilingSampler)) {
                context.ExecuteCommandBuffer(cmd);
                cmd.Clear();
                context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings);
            }

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }

        public override void OnCameraCleanup(CommandBuffer cmd) { }
    }

    RenderToDepthTexturePass m_ScriptablePass;

    public override void Create() {
        m_ScriptablePass = new RenderToDepthTexturePass(layerMask);
        m_ScriptablePass.renderPassEvent = _event;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
    //if (!renderingData.cameraData.requiresDepthTexture) return;
        renderer.EnqueuePass(m_ScriptablePass);
    }
}

This code doesn’t work with URP 14 because I cannot get the texture used by SSAO anymore which is something like:

9536272--1346245--upload_2023-12-18_17-34-19.png

any help?

From the code looks like you should get it by access _CameraDepthTexture

that code used to work with URP 13 but doesn’t work with URP 14.

now I would get this:

Is the depth texture enabled in the URP pipeline settings ?

yes

To access the scene depth RTHandle, you need to use reflection. ( check this thread )

For future reference: In OP’s first screenshot you can see the SSAO is at the end of the stack. This will cause SSAO to draw over everything before, and for some reason the depth texture used for SSAO will NOT take those other things into account, resulting in the bug.
Solution: Move SSAO BEFORE any objects you want to render over SSAO, then activate the checkbox “After Opaque” in the SSAO effect. I tried many variations and this was the only way I could get it to work in Unity 2021.3.

2 Likes