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

Resolved Updating film grain during runtime

Discussion in 'High Definition Render Pipeline' started by BumpNumb, Oct 5, 2020.

  1. BumpNumb

    BumpNumb

    Joined:
    Aug 19, 2017
    Posts:
    3
    Hello,
    I have a project where I'd like to change the grain intensity and type during runtime.
    I have successfully changed other parts of the Volume such as HDRISky, Fog and Exposure, however I cannot seem to change the FilmGrain.

    At start I run:
    Code (CSharp):
    1. volume = GameObject.Find("StageRandomizer").GetComponent<Volume>();
    2. volume.profile.TryGet(out grain);

    My try to change the grain:
    Code (CSharp):
    1. grain.active = true;
    2. ClampedFloatParameter intensity = new ClampedFloatParameter(Random.Range(minGrain, maxGrain), 0.0f, 1.0f, true);
    3. grain.intensity = intensity;
    4.  
    5. int index = Random.Range(1, 10);
    6.  
    7. FilmGrainLookupParameter fglp = new FilmGrainLookupParameter((FilmGrainLookup)index, true);
    8. grain.type = fglp;
    I have yet to figure out how this works, have anyone of you guys had any success with this?


    Thanks,
    Daniel Fennhagen
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,136
    For most of these post processing settings, you don't assign a new value to the property. Instead, you call Override() on the property. For example:

    Code (CSharp):
    1. FilmGrain _postProcessingFilmGrain;
    2.  
    3. void Update() {
    4.     _postProcessingFilmGrain.intensity.Override(_someFloatValue);
    5. }
     
  3. BumpNumb

    BumpNumb

    Joined:
    Aug 19, 2017
    Posts:
    3
    Thank you @dgoyette, this did solve my problem!
     
  4. amazingjaba

    amazingjaba

    Joined:
    Aug 18, 2019
    Posts:
    2

    For those who can't find the Film Grain while working with URP (Universal Render Pipeline), you will need to import URP first :

    Code (CSharp):
    1. using UnityEngine.Rendering.Universal;
     
  5. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    What´s the difference between

    _postProcessingFilmGrain.intensity.Override(_someFloatValue);
    and
    _postProcessingFilmGrain.intensity =_someFloatValue;
    ?
     
  6. ElevenGame

    ElevenGame

    Joined:
    Jun 13, 2016
    Posts:
    143
    @Cascho01 I looked it up in the code. As you can see in com.unity.render-pipelines.core\Runtime\Volume\VolumeParameter.cs, the Override() sets an extra bool, which signifies that the volume parameter has changed. (And it probably gets an extra Update because of that) -> Whenever regular value setting does not work, use the Override().
     
  7. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Hmmm, ok.

    Besides this, when I change values at runtime, what´s the best way to set the values back to the original states after stopping playmode?
     
  8. ElevenGame

    ElevenGame

    Joined:
    Jun 13, 2016
    Posts:
    143
    I am assuming you are talking about ScriptableObject based Assets such as Volume Profiles. The best way to "reset" those changes is to make a duplicate of that asset before you enter Playmode. You could also script that to automate, otherwise there's no way. All changes within the scene will be reset after stopping playmode anyways, but those assets outside the scene serialize their changes by design. So just be aware, which of these you are dealing with when changing something in playmode.