Search Unity

Question Custom Effect with sorting options with PostProcessing V2

Discussion in 'Image Effects' started by tomekkie2, Feb 13, 2021.

  1. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I have also written a selective custom effect and I am wondering how to manipulate with its order.
    The custom effect template does not seem to provide an option to set an order variable, even with a limited options (BeforeStack, BeforeTransparent and AfterStack).
    Depending on the selected V2 effect - my custom effect should be ordered once before, or the other time after.
    How to add an order option to it?
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Rendering.PostProcessing;
    4.  
    5. [Serializable]
    6. [PostProcess(typeof(GrayscaleRenderer), PostProcessEvent.AfterStack, "Custom/Grayscale")]
    7. public sealed class Grayscale : PostProcessEffectSettings
    8. {
    9.   [Range(0f, 1f), Tooltip("Grayscale effect intensity.")]
    10.    public FloatParameter blend = new FloatParameter { value = 0.5f };
    11. }
    12. public sealed class GrayscaleRenderer : PostProcessEffectRenderer<Grayscale>
    13. {
    14.    public override void Render(PostProcessRenderContext context)
    15.   {
    16.        var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/Grayscale"));
    17.        sheet.properties.SetFloat("_Blend", settings.blend);
    18.        context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    19.   }
    20. }
    This is the template from https://docs.unity3d.com/Packages/com.unity.postprocessing@3.0/manual/Writing-Custom-Effects.html
     
  2. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,266
    You can set the sorting order for custom effects on the PostProcessLayer camera component. It's tucked away under the foldout menus.
     
    tomekkie2 likes this.
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Is it possible to set it here?
    The sorting order is here already imposed by the effect script.

    Zrzut ekranu 2021-02-13 124441.png

    When trying to use a variable instead of constant in the script template, I am getting this error:
    error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

    Looks like it would have to be compiled separately for each sorting order?
     
    Last edited: Feb 13, 2021
  4. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,266
    Ah, you mean tying the PostProcessEvent enum to a variable? That's unfortunately not possible, class attributes always need to use constant values. Definitely a limitation imposed by how PPSv2 is designed.

    It may be possible to hack in with C# reflection and move an effect from the m_BeforeStackBundles list to m_AfterStackBundles (and vice versa). Then call InitBundles().
     
    tomekkie2 likes this.