Search Unity

Question Feedthrough on CommandBuffer/RenderFeature. [SRP | URP | 2021.1.0b1]

Discussion in 'General Graphics' started by HarryPigskin, Jan 14, 2021.

  1. HarryPigskin


    Jan 14, 2021

    I've been recently trying to create an outline shader following this video:

    Which creates the outline in the following steps:
    1. Render outlined objects as a solid colour.
    2. Apply a blur to the render texture.
    3. Remove the original render from the newly blurred render texture.
    4. Overlay that over the main camera's render.


    With this being the net result, repo here:

    With this video being made in 2017 I was trying to update it to use the SRP and Unity's new rendering features to make simpler and more streamlined code.

    I used this repo as a starting off point:

    Then created two cameras and two renderers, with one camera being the base and one being an overlay on the other.

    The problem I'm having is while my replacement shader works fine:

    When I try to appy my blur it blurs the whole camera instead of just the current RenderTexture which should be active within the Renderer:


    I'm assuming my issue is that instead of renderers acting as independent camera views, all render features/renderers are simply applied to the main camera texture in order.

    Meaning while I expect this to be in the command buffer:

    It's actually this: Unity_GFcGae4EaW.png

    So my issue is, what is the correct way to do this?
    I've been struggling on it for a few days but I've had a hard time making any progress.

    I've tried a couple different solutions:
    - Having one renderfeature instead of two and doing the whole thing in one compute buffer. (You can't make the camera render in a command buffer, I think maybe I was going about it wrong.)
    - Making it all into one shader with multiple passes, but it never seemed to compile.
    - Applying all the materials in order to a secondary camera with a C# script, then applying that camera's texture to the main camera, again just issues with the whole thing.

    I think I just have some fundamental misunderstanding with the data flow and structure of how to go about doing something like this.

    If anyone could be of any help it would be much appreciated.

  2. HarryPigskin


    Jan 14, 2021
    I have resolved this issue, so I'll post my findings for the future forum historians.

    The way the Base/Overlay system seems to work is that BaseCameras and OverlayCameras share the same RenderTexture, they simply render to it in a specific order, this is what caused my blurring on my main texture.

    To workaround this I do the following steps:
    1. Set my Overlay Camera to a Base Camera.
    2. Change the Enivornment Background Type to be SolidColor and the Background to be Black with no Alpha.
    3. Send the output to a RenderTexture.

    4. Create a AdditiveBlending material with my RenderTarget
    Code Here:
    Code (csharp):
    1. Shader "Add" {
    2.     Properties{
    3.         _Combine("_Combine", 2D) = "black" {}
    4.         _MainTex("_MainTex", 2D) = "black" {}
    5.     }
    6.         SubShader{
    7.             Tags { "Queue" = "Transparent" }
    8.             Pass {
    9.                 Blend One One
    10.                 SetTexture[_Combine]
    11.                 SetTexture[_MainTex] { combine previous, texture }
    12.             }
    13.     }
    14. }
    5. Add a OverlayPass RenderFeature to my BaseCamera's RenderPipeline.
    Code here:
    Code (csharp):
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Rendering.Universal;
    6. public class OverlayPass : ScriptableRendererFeature
    7. {
    8.     [System.Serializable]
    9.     public class OverlaySettings
    10.     {
    11.         public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
    12.         public Material overlayMaterial;
    13.     }
    15.     public OverlaySettings settings = new OverlaySettings();
    17.     class CustomRenderPass : ScriptableRenderPass
    18.     {
    19.         public Material overlayMaterial;
    20.         private string profilerTag;
    22.         private RenderTargetIdentifier source { get; set; }
    24.         public void Setup(RenderTargetIdentifier source)
    25.         {
    26.             this.source = source;
    27.         }
    29.         public CustomRenderPass(string profilerTag)
    30.         {
    31.             this.profilerTag = profilerTag;
    32.         }
    34.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    35.         {
    36.             CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
    37.             cmd.Blit(source, source, overlayMaterial);
    38.             context.ExecuteCommandBuffer(cmd);
    39.             cmd.Clear();
    40.             CommandBufferPool.Release(cmd);
    41.         }
    42.     }
    44.     CustomRenderPass scriptablePass;
    46.     public override void Create()
    47.     {
    48.         scriptablePass = new CustomRenderPass("OverlayPass");
    49.         scriptablePass.overlayMaterial = settings.overlayMaterial;
    50.         scriptablePass.renderPassEvent = settings.renderPassEvent;
    51.     }
    53.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    54.     {
    55.         var src = renderer.cameraColorTarget;
    56.         scriptablePass.Setup(src);
    57.         renderer.EnqueuePass(scriptablePass);
    58.     }
    59. }
    (Based off KawaseBlur's code here:

    Which then achieves this lovely effect:

    Because I'm rendering to a RenderTexture, I don't seem to be able to change resolution at runtime, I've tried it with a script it doesn't seem to work.
    This is quite a heavy limitation, in theory you'd have to have set resolutions and swap out rander textures as your resolution changes at runtime to make this effect continue to work instead of getting stretched out and distorted.

    This occurs because the blending shader renders the pixel information of both textures based off UV co-ordinates, meaning the overlay image is warped to match the aspect ratio of the main image.

    There are two workarounds I see for this:
    1. Alter the shader to scale the RenderTexture's X or Y to scale properly with the main cameras Render.
    Limitations: Any difference in aspect Ratio between the two cameras will cause outlines to be culled.

    2. Instead of Rendering to a RenderTexture, simply make the Overlay camera a Base Camera with a lower rendering index, pass the camera to the OverlayPass RenderFeature and finally apply it's activeRenderTexture to the additive material.
    Limitations: In theory none, but it feels rather hacky.

    There's also a secret 3rd workaround which seems very obvious, "why not simply make the second base camera have a higher Priority than your first base camera? Surely that'll simply make it render on top and avoid this issue?", in theory yes, in practice it seems only the BaseCamera with the highest Priority gets rendered to the screen, whether this is a bug or intentional I'm not sure, only time will tell.

    Good luck to ya!

    Update: Solution 2 is not possible because it relies on references to objects within the scene heirarchy, which you cannot pass into a static render pass, it seems solution 1 is the only way to do this from a practical standpoint, a shame.

    All the best,
    Last edited: Jan 17, 2021