Search Unity

Post Processing v2 - Add Effects in Code

Discussion in 'Scripting' started by clamum, Jul 12, 2020.

  1. clamum

    clamum

    Joined:
    May 14, 2017
    Posts:
    61
    I'm trying to add a new ColorGrading PP effect in code, when the user clicks a button, and I've read multiple questions and Unity's docs, and it just isn't working for me.

    My code uses Unity's "QuickVolume" call and while there's no error, nothing happens (and clicking my "PostFx" object in the hierarchy, while in game, shows no effect added).

    Code (CSharp):
    1. GameObject postFx = GameObject.Find("PostFx");
    2.         PostProcessVolume volume = postFx.GetComponent<PostProcessVolume>();
    3.  
    4.         ColorGrading colorGradingSettings = ScriptableObject.CreateInstance<ColorGrading>();
    5.        
    6.         var colorGradingModeParameter = new GradingModeParameter
    7.         {
    8.             value = GradingMode.LowDefinitionRange
    9.         };
    10.  
    11.         BoolParameter boolParam = new BoolParameter();
    12.         boolParam.value = true;
    13.  
    14.         FloatParameter floatParam = new FloatParameter();
    15.         floatParam.value = -100f;
    16.  
    17.         colorGradingSettings.enabled = boolParam;
    18.         colorGradingSettings.gradingMode = colorGradingModeParameter;
    19.         colorGradingSettings.saturation = floatParam;
    20.  
    21.         PostProcessManager.instance.QuickVolume(postFx.layer, 100f, colorGradingSettings);
    (I think I could skip the *Parameter() instantiations and just assign the *.value property of each directly but I'm not concerned with that right now)

    Instead of the "QuickVolume" call, I've also tried instead using the "volume.profile.AddSettings(colorGradingSettings)" call instead but that produces a bunch of errors, including "array index out of bounds" or similar, and some other one.

    Anyone know what's wrong with this? I wish the docs were more clear; this should be super easy to do and the docs should explicitly state how to do it. Ugh.
     
  2. clamum

    clamum

    Joined:
    May 14, 2017
    Posts:
    61
    Well I figured this out thanks to a YouTube video:


    While doing it I decided that it would just be simpler to add the setting while in the Editor and just leave the values default so it basically would be off. Then I could just change the value in code (and set it back to default when necessary) instead of adding and removing settings.

    So if it helps anyone coming upon this, I added the following instance variables to my GameManger class:

    Code (CSharp):
    1. [SerializeField]
    2. private PostProcessVolume postProcessVolumeRoundEnd;
    3.  
    4. private ColorGrading roundEndColorGrading;
    Then assigned the PostFx object in my game (which has the PostProcessingVolume component on it) to the new slot:



    I then added this method:

    Code (CSharp):
    1. private void ToggleRoundEndPostProcessing(bool isRoundEnd)
    2.     {
    3.         bool colorGradingSuccess = postProcessVolumeRoundEnd.profile.TryGetSettings(out roundEndColorGrading);
    4.  
    5.         if (colorGradingSuccess)
    6.         {
    7.             // round ended; de-saturate, etc
    8.             if (isRoundEnd)
    9.             {
    10.                 roundEndColorGrading.saturation.value = -70f;
    11.             }
    12.             // normal gameplay
    13.             else
    14.             {
    15.                 roundEndColorGrading.saturation.value = 0f;
    16.             }
    17.         }
    18.         // failed to get Color Grading setting for some reason
    19.         else
    20.         {
    21.             Debug.Log("Failed to get PostFx Color Grading setting; skipping PostFx end of round adjustment.");
    22.         }
    23.     }
    I also had code to set the setting to "enabled" (roundEndColorGrading.enabled.value = true) but saw that wasn't necessary.

    The one thing that I didn't get right away was the actual Intensity setting in the Editor had to be checked, otherwise it wasn't working (maybe the color grading "Override" method would work if it was unchecked?). So I just have it like this in the Editor and it's basically off, until I desaturate it: