Search Unity

Bug urp 2D renderer Custom render feature with a post processing volume not working (2021 LTS).

Discussion in 'Image Effects' started by Metthatron, Apr 15, 2023.

  1. Metthatron

    Metthatron

    Joined:
    May 26, 2017
    Posts:
    6
    Hi,
    I want to create a distortion post process effect using custom render feature with a 2D renderer that affects local volumes. So far, while testing, only Unity's built-in effects get triggered by the player inside the volume and my own effect isn't even drawn on the frame debugger. The shader works as predicted in standalone using _CameraSortingLayerTexture since _CameraOpaqueTexture won't work with 2D sprites. I thought at first something wrong with the feature code so I kept testing but after a while, I copied an existing, working code from Febucci's blog: https://www.febucci.com/2022/05/custom-post-processing-in-urp/ for testing purposes and it still didn't work, even with minimal reproduction (new empty 2D urp project), this led me to think this might be a bug.
    I believe my code might have something missing but I couldn't pinpoint the issue.
    Anyways, here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.Universal;
    6.  
    7. public class DistortionFeatureV2 : ScriptableRendererFeature
    8. {
    9.  
    10.     [System.Serializable]
    11.     public class Settings
    12.     {
    13.         public Material material;
    14.         public RenderPassEvent renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
    15.     }
    16.  
    17.     private DisortionPass distortionPass;
    18.  
    19.     public Settings settings = new Settings();
    20.     public override void Create()
    21.     {
    22.         this.name = "Distortion Pass";
    23.         this.distortionPass = new DisortionPass(settings.renderPassEvent, settings.material);
    24.     }
    25.  
    26.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    27.     {
    28.       renderer.EnqueuePass(distortionPass);
    29.     }
    30.  
    31. }
    32. public class DisortionPass : ScriptableRenderPass
    33. {
    34.     private static readonly string renderTag = "Distortion Effect";
    35.     private Material _material;
    36.     private DistortionPostProcessing distortionVolume;
    37.     public DisortionPass(RenderPassEvent evt,Material mat)
    38.     {
    39.         renderPassEvent = evt;
    40.         var material = mat;
    41.         if (material==null)
    42.         {
    43.             Debug.LogError("No Material!!!");
    44.  
    45.             return;
    46.         }
    47.  
    48.     }
    49.     public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    50.     {
    51.         if (!renderingData.cameraData.postProcessEnabled)
    52.         {
    53.             Debug.Log("Please Enable Post processing in camera settings!");
    54.             return;
    55.         }
    56.  
    57.         var stack = VolumeManager.instance.stack;
    58.         distortionVolume = stack.GetComponent<DistortionPostProcessing>();
    59.         if (distortionVolume==null)
    60.         {
    61.             Debug.LogWarning("Volume not found! please create a volume and assign a distortion effect to it");
    62.             return;
    63.         }
    64.  
    65.         CommandBuffer cmd = CommandBufferPool.Get(renderTag);
    66.         Render(cmd, ref renderingData);
    67.         context.ExecuteCommandBuffer(cmd);
    68.         CommandBufferPool.Release(cmd);
    69.     }
    70.  
    71.     private void Render(CommandBuffer cmd, ref RenderingData renderingData)
    72.     {
    73.         RenderTargetIdentifier source = renderingData.cameraData.renderer.cameraColorTarget;
    74.         RenderTextureDescriptor RTDesc = renderingData.cameraData.cameraTargetDescriptor;
    75.         RTDesc.depthBufferBits = 0;
    76.      
    77.         _material.SetVector(Shader.PropertyToID("_Velocity"),distortionVolume.distortionVelocity.value);
    78.         _material.SetVector(Shader.PropertyToID("_Noise_Scale"),distortionVolume.noiseScale.value);
    79.         _material.SetVector(Shader.PropertyToID("_Strength"),distortionVolume.strength.value);
    80.         int destination = Shader.PropertyToID("Temp1");
    81.         cmd.GetTemporaryRT(destination,RTDesc.width,RTDesc.height,0,FilterMode.Bilinear,RenderTextureFormat.ARGB32);
    82.         cmd.Blit(source,destination);
    83.         cmd.Blit(destination,source,_material,0);
    84.     }
    85. }
    86.  
    and the volume Component code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Rendering;
    6. using UnityEngine.Rendering.Universal;
    7.  
    8. [Serializable,VolumeComponentMenuForRenderPipeline("Custom/DistortionEffect",typeof(UniversalRenderPipeline))]
    9. public class DistortionPostProcessing : VolumeComponent,IPostProcessComponent
    10. {
    11.     public Vector2Parameter distortionVelocity = new(new Vector2(0, 0), overrideState: true);
    12.  
    13.     public Vector2Parameter noiseScale = new(new Vector2(0, 0), overrideState: true);
    14.     public Vector2Parameter strength = new(new Vector2(0, 0), overrideState: true);
    15.  
    16.  
    17.     public bool IsActive()
    18.     {
    19.         return noiseScale.value.y > 0;
    20.  
    21.  
    22.     }
    23.  
    24.     public bool IsTileCompatible()
    25.     {
    26.         return true;
    27.     }
    28. }
    29.  
    Shader variables' references are correct and written as is in code.

    At this point I am not sure if this would qualify as a bug but I am posting regardless as I have tried all I could before.
    Thank you for your time!

    Edit : Solved!
    Code (CSharp):
    1. if (!renderingData.cameraData.postProcessEnabled)
    2.         {
    3.             Debug.Log("Please Enable Post processing in camera settings!");
    4.             return;
    5.         }
    this was the culprit. for some reason it reversed the camera post processing settings.
     
    Last edited: Apr 15, 2023