Search Unity

Question Modified URP: Render to RenderTexture then to backbuffer

Discussion in 'Universal Render Pipeline' started by blablaalb, May 21, 2021.

  1. blablaalb

    blablaalb

    Joined:
    Oct 28, 2015
    Posts:
    53
    I was looking for a performant way to add a custom post-processing effect. Selective blur in particular. First I implemented it with the Render Feature by blitting the backbuffer to render texture and then blitting the render texture back to screen, but it had huge performance hit on a mobile device. I investigated it a bit further and found out that even the most basic shader has major performance impact with Render Features.

    Now I'm looking for a way to modify the URP. I want it to first render to render texture, then blit the render texture with material to backbuffer and then render everything that should not be blurred with an overlay camera. I added two fields to the UniversalRenderPipelineAsset

    public RenderTexture TargetTexture;
    public Material BlurMaterial;

    and in then the UniversalRenderPipeline in the RenderSingleCamera method I do this
    Code (CSharp):
    1.             if (camera.CompareTag("MainCamera"))
    2.             {
    3.                 camera.targetTexture = TargetTexture;
    4.                 var cb = CommandBufferPool.Get();
    5.                 cb.Blit(TargetTexture, null as RenderTexture, BlurMaterial);
    6.                 context.ExecuteCommandBuffer(cb);
    7.                 cb.Clear();
    8.                 CommandBufferPool.Release(cb);
    9.             }
    10.             using (new ProfilingScope(cmd, Profiling.Pipeline.Context.submit))
    11.             {
    12.                 context.Submit(); // Actually execute the commands that we previously sent to the ScriptableRenderContext context
    13.             }
    14.             if (camera.CompareTag("MainCamera"))
    15.             {
    16.                 camera.targetTexture = null;
    17.             }
    This works quite well, the performance is much better than what it was when I used Render Feature.

    The only problem I'm facing now is that if I add an overlay camera with a layer mask in order to render the car interior on top of the blurry image, the blur stops working and everything is rendered as usual. Nothing gets blurred.

    You can see on the image above the Overlay Camera has Culling Mask set to Player, but the whole blur effect is discarded if enable the Overlay Camera.
    I'd be glad if someone could shed some light on why this happens and how I can fix this. It's the second day I'm struggling with this and still have no clue.