Search Unity

How do you add a Post Process Image Effect pass in URP(apply image effect shader)?

Discussion in 'Universal Render Pipeline' started by Yukken, Sep 25, 2020.

  1. Yukken

    Yukken

    Joined:
    Jul 12, 2015
    Posts:
    93
    Hello,
    I'm trying to implement a glitch iamge effect in URP. I'm using this shader:
    http://halisavakis.com/my-take-on-shaders-glitch-image-effect/

    But that was written assuming onrenderimage() which is not available on URP. I tried to use custom renderpass but I was unable to make it work.

    This is the code:
    Code (CSharp):
    1. public class GlitchPassFeature : ScriptableRendererFeature
    2. {
    3.  
    4.     class GlitchPass : ScriptableRenderPass
    5.     {
    6.         private RenderTargetIdentifier sourceCameraTargetIdentifier;
    7.         private Material effectMat;
    8.         string m_ProfilerTag = "GlitchPass";
    9.         RenderTargetHandle m_TemporaryColorTexture;
    10.  
    11.         public void setup(RenderTargetIdentifier _cameRenderTargetIdentifier, Material mat)
    12.         {
    13.             sourceCameraTargetIdentifier = _cameRenderTargetIdentifier;
    14.             effectMat = mat;
    15.         }
    16.  
    17.         // This method is called before executing the render pass.
    18.         // It can be used to configure render targets and their clear state. Also to create temporary render target textures.
    19.         // When empty this render pass will render to the active camera render target.
    20.         // You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
    21.         // The render pipeline will ensure target setup and clearing happens in an performance manner.
    22.         public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    23.         {
    24.         }
    25.  
    26.         // Here you can implement the rendering logic.
    27.         // Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
    28.         // https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
    29.         // You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
    30.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    31.         {
    32.             CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
    33.  
    34.  
    35.             cmd.Blit(sourceCameraTargetIdentifier, BuiltinRenderTextureType.CurrentActive,effectMat);
    36.             context.ExecuteCommandBuffer(cmd);
    37.  
    38.             cmd.GetTemporaryRT(m_TemporaryColorTexture.id, renderingData.cameraData.cameraTargetDescriptor);
    39.             Blit(cmd, sourceCameraTargetIdentifier, m_TemporaryColorTexture.Identifier(), effectMat);
    40.             Blit(cmd, m_TemporaryColorTexture.Identifier(), sourceCameraTargetIdentifier);
    41.  
    42.  
    43.             cmd.Release();
    44.         }
    45.  
    46.         /// Cleanup any allocated resources that were created during the execution of this render pass.
    47.         public override void FrameCleanup(CommandBuffer cmd)
    48.         {
    49.  
    50.         }
    51.     }
    52.  
    53.  
    54.  
    55.  
    56.     [System.Serializable]
    57.     public class GlitchSettings
    58.     {
    59.         public RenderPassEvent whenToRender = RenderPassEvent.AfterRenderingTransparents;
    60.         //RenderPassEvent.AfterRenderingPostProcessing;
    61.         public bool enabled = false;//#TODO USE THIS DAMMIT
    62.         public Material effectMat;
    63.     }
    64.  
    65.     GlitchPass glitchPass;
    66.     public GlitchSettings settings;
    67.  
    68.  
    69.     public override void Create()
    70.     {
    71.         glitchPass = new GlitchPass();
    72.  
    73.         // Configures where the render pass should be injected.
    74.         glitchPass.renderPassEvent = settings.whenToRender;
    75.     }
    76.  
    77.     // Here you can inject one or multiple render passes in the renderer.
    78.     // This method is called when setting Up the renderer once per-camera.
    79.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    80.     {
    81.         if(!settings.enabled )
    82.             return;
    83.         // we're getting the camera's color buffer target
    84.         var cameraColorTarget = renderer.cameraColorTarget;
    85.         glitchPass.setup(cameraColorTarget,settings.effectMat );
    86.         renderer.EnqueuePass(glitchPass);
    87.     }
    88. }
    89.  
    90.  
    91.  
    It seems to blitting something as it ovewrrides the camera background color if I run it before skyboxes are rendered. However, it doesn't change anything else.
    When I run it after post processing, It gets rid of the post process bloom but the effect works. However, I need post processing for my game.

    How do I integrate an image effect in URP in 2019.4?