Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change Post Processing Values on runtime.

Discussion in 'Scripting' started by Khazmadu, Jul 2, 2017.

  1. Khazmadu

    Khazmadu

    Joined:
    Jun 24, 2017
    Posts:
    92
    I want to change the Color Grading values of the Post Processing Profile on Runtime.

    At first I tried setting the values of the actual profil:
    profile.colorGrading.settings.basic.contrast = 0;

    but that doesn't work.

    So I tried setting it on the behavior it self but that didn't work as well:
    behavior.GetComponent<ColorGradingComponent>().model.settings.basic.contrast = 10;

    how exactly can I change the Post Processing on runtime?
     
  2. Khazmadu

    Khazmadu

    Joined:
    Jun 24, 2017
    Posts:
    92
  3. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    78
    The solution linked above (replace the profile with an instanced copy OnEnable, then edit that on Update), results in Post Processing Behaviors with null Profiles in Unity 5.6.2p2. It seems to only happen if the camera is inside a prefab, and might have something to do with the way cameras are handled by SteamVR.

    My solution is to check every PostProcessingBehaviour on EditorApplication.playmodeStateChanged and set any null profiles I find. Code (must be in an Editor folder):

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEditor.SceneManagement;
    5. using UnityEngine.PostProcessing;
    6.  
    7. [InitializeOnLoad]
    8. public class EditorManager {
    9.     static EditorManager() {
    10.         EditorApplication.playmodeStateChanged += FixMissingPostProcessingProfiles;
    11.     }
    12.  
    13.     static void FixMissingPostProcessingProfiles() {
    14.         PostProcessingBehaviour[] behaviors = Resources.FindObjectsOfTypeAll<PostProcessingBehaviour>();
    15.         foreach (PostProcessingBehaviour behavior in behaviors) {
    16.             if (behavior.profile == null) {
    17.                 Debug.LogWarning("PostProcessingProfile disconnected, reconnecting " + behavior.GetPath());
    18.                 behavior.profile = Resources.Load("MyPostProcess") as PostProcessingProfile;
    19.             }
    20.         }
    21.     }
    22. }
     
    Last edited: Jul 30, 2017
  4. mudabbirali92

    mudabbirali92

    Joined:
    Jan 25, 2016
    Posts:
    4
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.PostProcessing;
    6. public class BlurryEffect : MonoBehaviour
    7. {
    8.     //remember to drag and drop your scriptable object into this field in the inspector...
    9.     public PostProcessingProfile ppProfile;
    10.     public float changeValue;
    11.     void Update()
    12.     {
    13.         ChangeDepthOfFieldAtRuntime(changeValue);
    14.         ppProfile.depthOfField.enabled = true;
    15.      
    16.     }
    17.     void ChangeDepthOfFieldAtRuntime( float val)
    18.     {
    19.         //copy current "depth of field" settings from the profile into a temporary variable
    20.          DepthOfFieldModel.Settings deaptoffieldSettings = ppProfile.depthOfField.settings;
    21.         deaptoffieldSettings.aperture += Time.deltaTime * val;
    22.         //set the "depth of field" settings in the actual profile to the temp settings with the changed value
    23.         ppProfile.depthOfField.settings = deaptoffieldSettings;
    24.     }
    25.  
    26. }
    27.  
    28. }
    Thank me later :)
     
    Pixierrr likes this.
  5. yassine14

    yassine14

    Joined:
    Nov 27, 2014
    Posts:
    3
    thank you so much i had to make some small changes but overrall it worked perfectly
     
  6. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    789
    Sadly this doesn't work for PPSV2 :/
     
  7. Llama_w_2Ls

    Llama_w_2Ls

    Joined:
    May 16, 2020
    Posts:
    7
    I'm possibly using the most recent version of the post processing package - July 2020:
    I've found a way to edit the settings of your post process volume, and any effects inside of it. None of the answers above were working for me, since unity removed BloomModel.settings from the newest stack and other issues, so I was able to come up with a script that is able to lower the intensity of your bloom by calling the function whenever i press a button.
    using UnityEngine;
    using UnityEngine.Rendering.PostProcessing;
    public class BloomSettings : MonoBehaviour
    {
    public PostProcessVolume volume;
    public void ChangeBloomSettings()
    {
    Bloom bloom;
    volume.profile.TryGetSettings(out bloom);
    bloom.intensity.value = 5f;
    Debug.Log("Working...");
    }
    }
    Make sure that you reference the post process volume in the inspector. Thanks to all the answers above for assisting!
     
  8. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    78
    I'm back with the new version (Nov 2020) and this time I needed post process effects that fade in over a few seconds.

    PostProcessVolume.weight works (most of the time) if you want to fade in ALL the effects, but if you only want to change one, here's how I do that, by using the original shared profile as the "max" value, 0 as the "min", then using that to change the instanced profile as percent goes from 0 to 1:

    Code (CSharp):
    1.  
    2. private void SetEffectAlpha<T>(float percent) where T : PostProcessEffectSettings {
    3.    volume.sharedProfile.TryGetSettings(out T effectShared);
    4.    volume.profile.TryGetSettings(out T effectInstance);
    5.    if (effectShared == null || effectInstance == null) {
    6.       Debug.LogError("BackgroundEffects failed to get effect, " + typeof(T) + ", " + volume.sharedProfile?.name);
    7.       return;
    8.    }
    9.  
    10.    switch (effectShared) {
    11.       case Bloom bloomShared when effectInstance is Bloom bloomInstance:
    12.          bloomInstance.intensity.value = bloomShared.intensity.value * percent;
    13.          break;
    14.       case Ripples ripplesShared when effectInstance is Ripples ripplesInstance:
    15.          ripplesInstance.strength.value = ripplesShared.strength.value * percent;
    16.          break;
    17.       case RadialBlur radialShared when effectInstance is RadialBlur radialInstance:
    18.          radialInstance.amount.value = radialShared.amount.value * percent;
    19.          break;
    20.       case ColorSplit splitShared when effectInstance is ColorSplit splitInstance:
    21.          splitInstance.offset.value = splitShared.offset.value * percent;
    22.          break;
    23.       default:
    24.          Debug.LogError("BackgroundEffects unknown effect, " + typeof(T) + ", " + volume.sharedProfile?.name);
    25.          break;
    26.    }
    27.  
    28.    Debug.Log("=== SetEffectAlpha " + typeof(T) + ", " + percent);
    29. }
    30.  
    This uses effects from a plugin (SC Post Effects) but just add a case for whatever effect you need to change the "intensity", "strength", "amount", "offset", "alpha" or whatever the heck variable can be dialed down to 0 to hide the effect.
     
    Last edited: Nov 10, 2020
  9. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    78
    Another fine rigamarole: if you need to change a PostProcessVolume's entire profile at runtime, you should apparently do:

    Code (CSharp):
    1.  
    2. RuntimeUtilities.DestroyProfile(volume.profile, true);
    3. volume.sharedProfile = newProfile;
    4. volume.profile = null;
    5. PostProcessProfile junk = volume.profile;
    First line prevents a memory leak.

    Last two lines replace volume.profile with an instanced copy of the sharedProfile, which you can edit at runtime without screwing up the original one in your file system.

    Would love to know any simpler way to switch profiles or fade a full stack of PostProcess effects in and out as my solutions feel convoluted but I could not find a better way.
     
    mrven likes this.
  10. sacb0y

    sacb0y

    Joined:
    May 9, 2016
    Posts:
    789
  11. YashaISR

    YashaISR

    Joined:
    Nov 8, 2016
    Posts:
    16
    Not sure if anyone needs this but i did somethign similar to vignette with URP



    private Vignette GetVignette()
    {
    for (int i = 0; i < volume.profile.components.Count; i++)
    {
    if(volume.profile.components is Vignette)
    {
    return (Vignette)volume.profile.components;
    }
    }
    return null;
    }