Search Unity

[HDRP][2019.1] How Do You Change Effects Volume Overrides Through Script?

Discussion in 'High Definition Render Pipeline' started by Cinder, Apr 27, 2019.

  1. Cinder

    Cinder

    Joined:
    Mar 24, 2015
    Posts:
    20
    Found the basic documentation here:
    https://docs.unity3d.com/Packages/c...gh-definition@5.13/manual/Volume-Profile.html

    And found this thread for post processing stack v2 which seems to be a similar system here:
    https://answers.unity.com/questions/1355103/modifying-the-new-post-processing-stack-through-co.html

    I haven't been able to figure out how I can affect these override components of the post processing volume scriptable object in script. The depth of field affect is largely useless without some scripting control.
     
    AntonioModer and Jesus like this.
  2. Cinder

    Cinder

    Joined:
    Mar 24, 2015
    Posts:
    20
    Feel a bit dumb, now. I was a little too frustrated with the lack of info and documentation and managed it in a really hacky way. Maybe someone can tell me how I'm suppose to pull this one component out without this silly search loop.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Experimental.Rendering.HDPipeline;
    4.  
    5. public class AffectDepthOfField : MonoBehaviour
    6. {
    7.     public bool spherecast = true;
    8.  
    9.     public Transform mainCamera;
    10.     RaycastHit hit;
    11.  
    12.     void Update()
    13.     {
    14.         if (spherecast)
    15.         {
    16.             if(Physics.SphereCast(mainCamera.position, 0.1f, mainCamera.forward, out hit, 10f))
    17.             {
    18.                 Volume volume = gameObject.GetComponent<Volume>();
    19.                 for (int i = 0; i < volume.profile.components.Count; i++)
    20.                 {
    21.                     if(volume.profile.components[i].name == "DepthOfField(Clone)")
    22.                     {
    23.                         DepthOfField de = (DepthOfField)volume.profile.components[i];
    24.                         de.nearFocusStart = new MinFloatParameter(1f, 0f, true);
    25.                         de.nearFocusEnd = new MinFloatParameter(1f, 0f, true);
    26.                         de.farFocusStart = new MinFloatParameter(1f, 0f, true);
    27.                         de.farFocusEnd = new MinFloatParameter(1f,0f, true);
    28.  
    29.                     }
    30.                 }
    31.             }
    32.         }
    33.     }
    34. }
     
  3. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    704
    I recommend that you cache the DoF in the start function, and then modify it :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Experimental.Rendering.HDPipeline;
    4. public class AffectDepthOfField : MonoBehaviour
    5. {
    6.     public bool spherecast = true;
    7.     public Transform mainCamera;
    8.     RaycastHit hit;
    9.    
    10.     DepthOfField  dofComponent;
    11.    
    12.     void Start()
    13.     {
    14.         Volume volume = gameObject.GetComponent<Volume>();
    15.         DepthOfField tmp;
    16.         if( volume.profile.TryGet<DepthOfField>( out tmp ) )
    17.         {
    18.             dofComponent = tmp;
    19.         }
    20.     }
    21.     void Update()
    22.     {
    23.         if (spherecast)
    24.         {
    25.             if(Physics.SphereCast(mainCamera.position, 0.1f, mainCamera.forward, out hit, 10f))
    26.             {
    27.                 dofComponent.nearFocusStart = new MinFloatParameter(1f, 0f, true);
    28.                 dofComponent.nearFocusEnd = new MinFloatParameter(1f, 0f, true);
    29.                 dofComponent.farFocusStart = new MinFloatParameter(1f, 0f, true);
    30.                 dofComponent.farFocusEnd = new MinFloatParameter(1f,0f, true);
    31.             }
    32.         }
    33.     }
    34. }
    Also, it might be easier to simply create an other volume with just a Depth of Field override with the settings you want and a high priority, and enable/disable it when needed ?
     
    Sebik_, MrKsi, aidangig56 and 12 others like this.
  4. Cinder

    Cinder

    Joined:
    Mar 24, 2015
    Posts:
    20
    Thanks I'll change things to work this way now!

    I appreciate the pointers.
     
  5. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Not trying to necro, but this solution simply doesn't work anymore.
    At least for the Procedural Skybox it doesn't.

    I can turn off the skybox or even completely remove it. In fact I can update the values in it all I want, but the values are never reflected in the actual game, only the inspector. And even then - they only show the updated exposure values if I click off the object and click it again.

    Just filed a Bug Report.
    [Edit]
    I'm on 2019.2.1f1.
    [Edit 2]
    Just tried on 2019.2.3f1
     
    Last edited: Sep 4, 2019
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Just wanted to let everyone know, they were able to replicate the bug mentioned above, it was sent to the proper team to handle the fix.
     
  7. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Turns out it wasn't a bug - but by design. So the QA person said it was a bug as well - (perhaps because it isn't explain anywhere). But one of the actual devs got in touch me with and said you have to do

    Correct way: >>> skyComponent.exposure.value = 8888;
    You no longer have to do skyComponent.exposure = new FloatParameter(6, true);
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thank goodness since the old way was just a mindless chore that had very little real world development use at all for Unity's customers.
     
    N1warhead likes this.
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Yeah I always thought it was done quite weirdly.. Now perhaps the other things mentioned in here you still have to do it that way I'm not sure. But in the case of procedural skybox you do like I mentioned in my last post to make it work.

    Not sure why they went with that whole floatparameter stuff
     
  10. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    Hi, I'm having related problems here - I'm trying to script changes to an HDRISky, but it's complaining that Volume isn't a recognized namespace, though I'm using the same namespaces posted in here
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Experimental.Rendering.HDPipeline;
    4.  
    5. Volume skyVolume;
    (Unity 2019.1.12f1). Am I missing something? Our HDRISky settings in the scene work of course, I just want to procedurally change the intensity (I want to change the time of day). Oh, and it does see HDRISky as a valid type, just not Volume.
     
    Last edited: Sep 20, 2019
  11. lGillot

    lGillot

    Joined:
    Apr 11, 2013
    Posts:
    16
    Hi, thanks for this thread first.

    I'm trying to access the rotation of a HDRIsky and I can't manage to make it work. It's asking for a ClampedFloatParameter and when I try to set it at runtime nothing changes. And I can't find any documentation about all that volumesetting.
    Am I missing something ?
     
  12. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    For some reason, the volume.profile.TryGet<X>( out temp); line generate a defaulted (clone) reference of the type and doesn't actually return to the reference in the scene Volume's sub-component. I'm trying to access the Volumetric Fog this way and it's telling me that volume.profile.TryGet<VolumetricFog>() returns a (clone) of the Volumetric Fog and whenever I try to check what kind of value this clone has, the values are different than the ones in the actual scene.
    Clicking on the inspector reference also doesn't point toward the actual Volume component in the scene either. (But clicking on the actual Volume component from which I try to get the Volumetric Fog does point toward the right Volume component in the scene.)

    --- After a while ---
    In my code, I made a stupid mistake and forgot to adjust 1 boolean value and I also mixed 2 values that are poorly described in the docs so I wasn't updating the right value over time.
    The hint came when I try to change the Fog color and it worked instantly. That pointed me that I did the right thing... but not for the fog distance/density which isn't well explained as there's no "Base Fog Distance" parameters to change, but multiple inside-parameters instead.

    In case some wonder, to change the Base Fog Distance from the Volumetric Fog, the parameter is not density (even if the tip of the parameter point it as the density), but it's the meanFreePath Clamp parameter.
     
    Last edited: Nov 21, 2019
    Egad_McDad and Maarja like this.
  13. jexmatex

    jexmatex

    Joined:
    Jun 23, 2016
    Posts:
    47
    hi guy's i'm also trying to get to the farFocusStart parameter from the position of a game object
    basically i have a camera and a target and want the target distance to be my farFocusStart value.

    from
    dofComponent.farFocusStart i don't know how to include the distance between my gameObjectTarget and my camera in

    dofComponent.farFocusStart = new MinFloatParameter(1f,1f,true);

    if i can get any helop on this, considering you aare talking to a mid lvl noob ^^
     
  14. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @jexmatex you already asked this same in another thread. You could just calculate distance between your camera and the target object. Calculation of distance is not really a question about how the volume works but how to calculate a distance between two points in Unity. Also you can just assign a new value instead of every time giving a new parameter.
    To calculate distance, you can use Vector3.Distance function, which returns the distance between Vector3 point a and b as a float.

    So, for example, if you had your Depth Of Field Override in Use Physical Camera Focus Mode, you could do something like this:
    Code (CSharp):
    1. float distToFocusPoint = Vector3.Distance(Camera.main.transform.position, DOFFocus.position);
    2. depthOfField.focusDistance.value = distToFocusPoint;
    You can use same kind of approach to change the farFocusStart's value.
    That DOFFocus would be some transform you have exposed in your UI, and you dragged the object there. Or you could find it with code and don't expose in UI. It's up to you how you want to do that.
    Code (CSharp):
    1. // Assign via UI
    2. [SerializeField] Transform DOFFocus;
    3.  
    4. // Or in code
    5. void Start()
    6. {
    7.    DOFFocus = GameObject.Find("DOFFocusObjectInScene").transform;
    8. }
     
    Last edited: Nov 25, 2019
    wellingtonipf likes this.
  15. zottelix

    zottelix

    Joined:
    Jun 13, 2017
    Posts:
    1
    I am using Unity3d 2019.3.0f1. The Volume Class you find now in UnityEngine.Rendering !
     
  16. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @zottelix,

    Yes, and those Overrides are in that UnityEngine.Rendering.HighDefinition. You need both to access the Volume Override post-processing effects.

    EDIT: Here's up-to-date example for future visitors (2019.3.0f1):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4.  
    5. public class AccessVolumeOverride : MonoBehaviour
    6. {
    7.     DepthOfField depthOfField;
    8.  
    9.     void Start()
    10.     {
    11.         Volume volume = GetComponent<Volume>();
    12.         DepthOfField tempDof;    
    13.  
    14.         if (volume.profile.TryGet<DepthOfField>(out tempDof))
    15.         {
    16.             depthOfField = tempDof;
    17.         }            
    18.  
    19.         depthOfField.focusDistance.value = 42f;
    20.     }
    21. }
     
    Last edited: Dec 4, 2019
    Egad_McDad, Hounded, Jassucks and 4 others like this.
  17. Unity-Artcraft

    Unity-Artcraft

    Joined:
    Jul 28, 2018
    Posts:
    85
    mhhhhh Volume doesn't work, namespace cant be found...
    but I have the 2019.2.13 version and still have to use the: "using UnityEngine.Experimental.Rendering.HDPipeline;" namespace. ... Would like to upgrade but it would certainly brake my game....
     
  18. jexmatex

    jexmatex

    Joined:
    Jun 23, 2016
    Posts:
    47
    in 2019.3 i thought there was no more LDRP or HDrp but only the Universal RP ?
    thanks for the help Olmi i fainalyy used racasthit to make it automatically and it work perfectlly !

    thanks again
     
  19. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    No. LWRP just renamed to URP. HDRPis separate thing.
     
  20. Edward4

    Edward4

    Joined:
    Dec 13, 2016
    Posts:
    1
    Thankyou so much, this really helped me!
     
    N1warhead likes this.
  21. Itz-Joey

    Itz-Joey

    Joined:
    Feb 16, 2020
    Posts:
    2
    I need help with this subject! I tried using some of the code in the thread and it looks like it should all work but it shows "Object reference not set to instance" error for the Volume Components? I'm using the Beta 2020 version idk if it's that
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4. using TMPro;
    5.  
    6. public class ProcessingAmount : MonoBehaviour
    7. {
    8.     public TMP_Dropdown dd;
    9.  
    10.     Volume vol;
    11.     Exposure ex;
    12.     Bloom bl;
    13.     ChromaticAberration ca;
    14.  
    15.     void Start()
    16.     {
    17.         if (vol.profile.TryGet<Exposure>(out Exposure tempE))
    18.         {
    19.             ex = tempE;
    20.         }
    21.         if (vol.profile.TryGet<Bloom>(out Bloom tempB))
    22.         {
    23.             bl = tempB;
    24.         }
    25.         if (vol.profile.TryGet<ChromaticAberration>(out ChromaticAberration tempC))
    26.         {
    27.             ca = tempC;
    28.         }
    29.     }
    30.     void Update()
    31.     {
    32.         if (dd.value == 0)
    33.         {
    34.             vol.weight = 0.2f;
    35.             ex.fixedExposure.value = 44.98f;
    36.             bl.quality.value = 1;
    37.             ca.quality.value = 1;
    38.         }
    39.         else if (dd.value == 1)
    40.         {
    41.             vol.weight = 0.6f;
    42.             ex.fixedExposure.value = 14.71f;
    43.             bl.quality.value = 1;
    44.             ca.quality.value = 2;
    45.         }
    46.         else if (dd.value == 2)
    47.         {
    48.             vol.weight = 1f;
    49.             ex.fixedExposure.value = 8.94f;
    50.             bl.quality.value = 2;
    51.             ca.quality.value = 3;
    52.         }
    53.     }
    54. }
    55.  
     
    Ultroman and gecko123 like this.
  22. Itz-Joey

    Itz-Joey

    Joined:
    Feb 16, 2020
    Posts:
    2
    I need help with this subject! I tried using some of the code in the thread and it looks like it should all work but it shows "Object reference not set to instance" error for the Volume Components? I'm using the Beta 2020 version idk if it's that
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4. using TMPro;
    5.  
    6. public class ProcessingAmount : MonoBehaviour
    7. {
    8.     public TMP_Dropdown dd;
    9.  
    10.     Volume vol;
    11.     Exposure ex;
    12.     Bloom bl;
    13.     ChromaticAberration ca;
    14.  
    15.     void Start()
    16.     {
    17.         if (vol.profile.TryGet<Exposure>(out Exposure tempE))
    18.         {
    19.             ex = tempE;
    20.         }
    21.         if (vol.profile.TryGet<Bloom>(out Bloom tempB))
    22.         {
    23.             bl = tempB;
    24.         }
    25.         if (vol.profile.TryGet<ChromaticAberration>(out ChromaticAberration tempC))
    26.         {
    27.             ca = tempC;
    28.         }
    29.     }
    30.     void Update()
    31.     {
    32.         if (dd.value == 0)
    33.         {
    34.             vol.weight = 0.2f;
    35.             ex.fixedExposure.value = 44.98f;
    36.             bl.quality.value = 1;
    37.             ca.quality.value = 1;
    38.         }
    39.         else if (dd.value == 1)
    40.         {
    41.             vol.weight = 0.6f;
    42.             ex.fixedExposure.value = 14.71f;
    43.             bl.quality.value = 1;
    44.             ca.quality.value = 2;
    45.         }
    46.         else if (dd.value == 2)
    47.         {
    48.             vol.weight = 1f;
    49.             ex.fixedExposure.value = 8.94f;
    50.             bl.quality.value = 2;
    51.             ca.quality.value = 3;
    52.         }
    53.     }
    54. }
    55.  
     
    unity_4EBHLNG3Ae98yQ likes this.
  23. deab

    deab

    Joined:
    Aug 11, 2013
    Posts:
    93
    Old post, but I was struggling with clone version of the volume component, use volume.sharedProfile rather than .profile to use the existing instance.
     
    Frump likes this.
  24. wellingtonipf

    wellingtonipf

    Joined:
    Mar 15, 2018
    Posts:
    2
    [QUOTE = "zottelix, post: 5247083, membro: 1414426"] Estou usando o Unity3d 2019.3.0f1. A classe de volume que você encontra agora no UnityEngine.Rendering ! [/ QUOTE]
    thanks man!
     
  25. Maarja

    Maarja

    Joined:
    Jul 21, 2019
    Posts:
    12
    I'm trying to change Fog Attenuation Distance, Base Height and Volumetric Fog Anistotropy over time with a custom timeline track in Unity 2019.3 HDRP. I managed to reference Sky and Fog volume (public UnityEngine.Rendering.Volume volume;, but not to access Fog override yet. @Max_Bol could you give me some leads, please? I'm quite new in scripting.
     
    Last edited: Jul 31, 2020
  26. unity_4EBHLNG3Ae98yQ

    unity_4EBHLNG3Ae98yQ

    Joined:
    Apr 14, 2020
    Posts:
    2
    That's exactly the same error I'm getting too for using the same code. Anyone knows how to change volume components? Is there anything wrong in the code below?

     
  27. unity_4EBHLNG3Ae98yQ

    unity_4EBHLNG3Ae98yQ

    Joined:
    Apr 14, 2020
    Posts:
    2
    I actually found my own answer. Try using this instead:

    Code (CSharp):
    1. GameObject.Find("GameObject").GetComponent<Volume>().weight = 0.5f;
    Does anyone know how to change profile too?
     
  28. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    nasos_333 likes this.
  29. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,364
  30. Beervangeer

    Beervangeer

    Joined:
    Dec 4, 2012
    Posts:
    20
    Hi there, I am able to update settings in my volume effect through script.
    I see them changing in my inspector, but nothing happens in game.

    Anyone a idea how this is possible?

    This is the code im using.

    Code (CSharp):
    1. public class GradientConnect : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     OscIn _oscIn;
    5.     const string address1 = "/brt";
    6.     public float breath = 0;
    7.  
    8.     public Vector2 minMax;
    9.     public AnimationCurve curve;
    10.  
    11.     private Volume vol;
    12.     private Exposure sky;
    13.  
    14.     void Start()
    15.     {
    16.         vol = gameObject.GetComponent<Volume>();
    17.         Exposure temp;
    18.  
    19.         if (vol.sharedProfile.TryGet<Exposure>(out temp))
    20.         {
    21.             sky = temp;
    22.         }
    23.     }
    24.  
    25.  
    26.     void OnEnable()
    27.     {
    28.         // You can "map" messages to methods in two ways:
    29.  
    30.         // 1) For messages with a single argument, route the value using the type specific map methods.
    31.         _oscIn.MapFloat(address1, ReceiveOSC);
    32.  
    33.         // 2) For messages with multiple arguments, route the message using the Map method.
    34.         //_oscIn.Map( address2, OnTest2 );
    35.     }
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         sky.fixedExposure = new FloatParameter(Mathf.Lerp(minMax.x, minMax.y, breath), true);
    42.     }
    43.  
    44.     void ReceiveOSC(float value)
    45.     {
    46.         breath = curve.Evaluate(Mathf.Clamp(value, 0, 1));
    47.     }
    48. }
    49.  
     
    Last edited: Sep 8, 2020
  31. Beervangeer

    Beervangeer

    Joined:
    Dec 4, 2012
    Posts:
    20
    Ok made a mistake, only set parameters with .value ! new FloatParameter doesnt work well anymore!
     
    Lex4art likes this.
  32. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    To change values something like this should work. There might be some unnecessary junk here, I just cut'n'pasted my test snippet:
    (I will edit later if needed.)

    Code (CSharp):
    1. VisualEnvironment visualEnvironment;
    2. GradientSky gradientSky;
    3.  
    4. ...
    5. // Then somewhere in your code:
    6. Volume volume = gameObject.GetComponent<Volume>();
    7. VisualEnvironment tempVis;
    8.  
    9. if (volume.profile.TryGet<VisualEnvironment>(out tempVis) )
    10. {
    11.     visualEnvironment = tempVis;
    12. }
    13.  
    14. GradientSky tempGradSky;
    15. if (volume.profile.TryGet<GradientSky>(out tempGradSky) )
    16. {
    17.     gradientSky = tempGradSky;
    18. }
    19.  
    20. // Set the sky type
    21. visualEnvironment.skyType.value = (int)SkyType.Gradient;
    22.  
    23. // Set the sky parameters
    24. gradientSky.top.value = Color.red;
    25. gradientSky.middle.value = Color.green;
    26. gradientSky.bottom.value = Color.blue;
    27. gradientSky.gradientDiffusion.value = 2.0f;
     
  33. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    How to get and set depth of field far blur value in script ?
     
  34. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  35. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Unfortunately I am stuck. I cannot change it here:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.HighDefinition;
    6. public class LimitDepthOfField : MonoBehaviour {
    7.    
    8.      public Volume postProcessVolume;
    9.      DepthOfField dof;
    10.      public float min, max;
    11.      // Start is called before the first frame update
    12.      void Start () {
    13.      }
    14.      void ChangeValue() {
    15.              if (postProcessVolume.profile.TryGet<DepthOfField> (out DepthOfField tempB)) {
    16.                 dof = tempB;
    17.                
    18.            dof.farFocusStart = new MinFloatParameter (min, max, true);   //Does not work
    19.              }
    20.      }
    21. }
     
    Last edited: Oct 12, 2020
  36. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @saifshk17

    You probably have to set the value, not a new parameter, i.e. something like this:
    Code (CSharp):
    1. depthOfField.farFocusStart.value = 42;
     
    saifshk17 likes this.
  37. Jontiways

    Jontiways

    Joined:
    Sep 12, 2013
    Posts:
    8
    Hi,
    Is there anyway of using this script to turn an effect off? Like say I watched the option to turn my depth of field on and off at runtime, what is the code reference for that? I've looked around for a reference and this forum is the closest I have to what I am scripting.
     
  38. zheyuanzhou

    zheyuanzhou

    Joined:
    Oct 29, 2017
    Posts:
    22
    Hard to get the Vignette Type in 2020.1 URP.
    The "TryGet" always red line error and I watch the whole Post to end and try each method. But Still cannot find the best Solution.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.PostProcessing;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Experimental.Rendering.Universal;
    5.  
    6. public class PostProcessingController : MonoBehaviour
    7. {
    8.     public static PostProcessingController instance;
    9.  
    10.     public Volume volume;
    11.     private Vignette vignette;
    12.     [SerializeField] private float intensityAmount = 0.05f;
    13.     private bool isStopped;
    14.     [SerializeField] private float originVignetteAmount = 0.25f;
    15.  
    16.     private void Awake()
    17.     {
    18.         if(instance == null)
    19.         {
    20.             instance = this;
    21.         }
    22.         else
    23.         {
    24.             if(instance != this)
    25.             {
    26.                 Destroy(gameObject);
    27.             }
    28.         }
    29.  
    30.         DontDestroyOnLoad(gameObject);
    31.     }
    32.  
    33.     private void Start()
    34.     {
    35.         volume = GetComponent<Volume>();
    36.         Vignette tempVignette;
    37.         if(volume.profile.TryGet<Vignette>(out tempVignette))
    38.         {
    39.             vignette = tempVignette;
    40.         }
    41.     }
     
  39. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    I've never been able to change these HDRP volume values either, except for gamma and gain in (LiftGammaGain). I think unity doesn't let you. You can change the value and it shows in the volume in editor, but doesn't actually change the DoF. Would like to hear from unity on this :)
     
    Last edited: Nov 2, 2020
  40. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    This thread is about HDRP volume overrides.
     
  41. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @holyfot,

    Care to show a test snippet where you try that value change? And which Unity/HDRP version you are using?
     
  42. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. #if UNITY_2019_3 || UNITY_2019_4 || UNITY_2020
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.HighDefinition;
    6. #else
    7. using UnityEngine.Experimental.Rendering.HDPipeline;
    8. #endif
    9.  
    10. public class DoFTest : MonoBehaviour
    11. {
    12.     public float valueT = 0f;
    13.     private DepthOfField testDoF;
    14.  
    15.     void Awake()
    16.     {
    17.         Volume volume = gameObject.GetComponent<Volume>();
    18.         volume.profile.TryGet<DepthOfField>(out testDoF);
    19.         StartCoroutine(test());
    20.     }
    21.  
    22.     private IEnumerator test()
    23.     {
    24.         while (valueT < 1000f)
    25.         {
    26.             yield return 0; //Wait 3 frames
    27.             yield return 0;
    28.             yield return 0;
    29.             valueT += 1f;
    30.  
    31.             MinFloatParameter dofFloat = new MinFloatParameter(valueT, 0f, true);
    32.             testDoF.nearFocusEnd = dofFloat;
    33.             //testDoF.nearFocusStart = dofFloat;
    34.             //testDoF.farFocusEnd.value = valueT;
    35.             //testDoF.farFocusEnd.SetValue(dofFloat);
    36.         }
    37.     }
    38. }
     
  43. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @holyfot,
    I didn't have time to test today if this works in 2019.4.x or 2020.x, but I imagine it might. Maybe I'm wrong.
    But this is in 2019.3.0f1 because that's what one of my projects uses :)

    Anyway, here's a test scene with Depth of Field set to Manual focus mode. It's important to have the mode correct in Inspector.

    All zero values for start/end:
    all_zeroed1.PNG
    all_zeroed2.PNG

    Manually set to specific settings:
    manually_set1.PNG
    manually_set2.PNG

    Continues in the next post...



     
    Last edited: Nov 3, 2020
  44. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    And here you see the code set values, starting from zeroed parameters:

    code_set1.PNG code_set2.PNG

    So as you can see, at least on 2019.3.0f1 this works fine. Both the actual render values and Inspector values seem to be updating OK. I can test this tomorrow on newer Unity versions if I happen to have time.

    And I don't really know if that value is the correct way to change values, but I've used it for my purposes and it seems to work OK for majority of parameters in sky, color adjustments and so on.

    EDIT: here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4.  
    5. public class DoFTest : MonoBehaviour
    6. {
    7.     private DepthOfField testDoF;
    8.  
    9.     void Awake()
    10.     {
    11.         Volume volume = gameObject.GetComponent<Volume>();
    12.         volume.profile.TryGet<DepthOfField>(out testDoF);
    13.  
    14.         // Near blur
    15.         testDoF.nearFocusStart.value = 0f;
    16.         testDoF.nearFocusEnd.value  = 5f;
    17.  
    18.         // Far blur
    19.         testDoF.farFocusStart.value = 5f;
    20.         testDoF.farFocusEnd.value = 20f;
    21.     }
    22. }
     
    Last edited: Nov 3, 2020
  45. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,279
    Useful examples here, but please add example code in the Scripting APIs for the rendering packages so we do not have to search for how to set things in code at runtime. The docs give no examples of extracting components and setting variables.
    Also, while I can change AO settings for HDRP at runtime, how do I turn off a VolumeComponent? - there is an active bool but it appears to do nothing.
     
    azmi_unity, chap-unity and Olmi like this.
  46. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    It seems to let you change the values once, me and various other people have always wanted to do an Auto DoF that raycasts and changes the focus based on what it hits. Are you able to change it multiple times?
     
  47. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @holyfot Why don't you just do an update when you need it? Either in Update but probably more likely in some function which you call when needed.
     
  48. Chris_Payne_QS

    Chris_Payne_QS

    Joined:
    Jul 16, 2016
    Posts:
    84
    I had the same issue as @holyfot - I've set up a script to pull focus, and it worked on the first frame (I can see the change in the editor) but not on subsequent frames. I was able to fix it by manually setting the overrideState every frame as well as the value:

    Code (CSharp):
    1.     depthOfField.focusDistance.value = liveFocalDistance;
    2.     depthOfField.focusDistance.overrideState = true;
     
  49. 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;
    }
     
  50. azmi_unity

    azmi_unity

    Joined:
    Dec 13, 2020
    Posts:
    62
    Toggling the
    active
    bool does nothing on an instantiated profile returned by
    .profile
    . It only works on
    .sharedProfile
    .

    Unity 2021.2.13f1 with HDRP 12.1.4
     
    hhoffren likes this.