Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to update Volumetric Fog value in runtime

Discussion in 'High Definition Render Pipeline' started by RoyLin86, Oct 22, 2019.

  1. RoyLin86

    RoyLin86

    Joined:
    Aug 30, 2016
    Posts:
    12
    Hi guys,
    I've a little problem about changing Volume Profile value via code.I'm going to change the Value of "Mean Height" and "Base Fog Distance" in runtime.I've tried this post #3 but it doesn't work for me.I got an NullReferenceException Error in code cs:15

    And When I tried to change "Base distance Height",I can't find "Base Fog Distance".
    Can anyone help me?Thanks.

    Unity Version : 2019.2.1.f1

    p1.png p2.png
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @RoyLin86 baseFogDistance / "Base Fog Distance" as written in UI is called meanFreePath (at least on 2019.2.x / HDRP 6.9.1 preview.)

    To set the value, you can use:
    Code (CSharp):
    1. volumetricFog.meanFreePath.value = 5f;
    Set the .value, not something else.

    At least I can here dial in values to the different parameters from code?

    Are you sure you've applied your script to the correct object, i.e. where your Volume script is (and make sure it's that one which has your VolumeProfile applied, and has the Volumetric Fog override.)
     
    Last edited: Oct 22, 2019
  3. RoyLin86

    RoyLin86

    Joined:
    Aug 30, 2016
    Posts:
    12
    Thanks for your reply!
    I have some question,I've tried to applied my script to scene Settings and set value as .value,but it didn't work for me.
    Am I setting wrong?
    p3.png p4.png
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Do you have that "Visual Environment" override somewhere? I don't see it in your image.
    You need to have that Fog/Type set correctly. It's possible that it's set to Exponential Fog or something else.

    This one:

    visual_environment.PNG

    But there might be something else wrong too.
     
  5. RoyLin86

    RoyLin86

    Joined:
    Aug 30, 2016
    Posts:
    12
    Yes,I do have add Visual Environment override.When I hit play it doesn't change.
    p6.png p7.png
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Do you get any errors now?
     
  7. RoyLin86

    RoyLin86

    Joined:
    Aug 30, 2016
    Posts:
    12
    Yes!After I change my code to update() function,then I got NullReferenceException Error in cs:21

    p8.png

    And I new an VolumetricFog object to it.The Error are gone, but it still doesn't change the value.
    p9.png
     
    Last edited: Oct 23, 2019
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Why are you creating a new fog?
    You should get the one that you have assigned the to Scene Settings file.

    i.e.:
    Code (CSharp):
    1. Volume volume = gameObject.GetComponent<Volume>();
    2. VolumetricFog tempFog;
    3. if (volume.profile.TryGet<VolumetricFog>(out tempFog) )
    4. {
    5.    volumetricFog = tempFog;
    6. }
    ...volumetricFog would be a class wide variable, in the beginning of your class. I just happened to do it that way. I'm sure it works as I just tried it out.

    You get that existing VolumetricFog object to that tempFog with that out. And your volume is acquired by having this specific script component assigned to the Scene Settings object where your existing fog and post effects are.

    As it is, where would you imagine that vFog is? As far as I understand you'd need to somehow apply that to the stack.

    And please use "insert code" to attach code as a text, no-one has time to re-write your non-functional code or any code to that matter.
     
    Last edited: Oct 23, 2019
    RoyLin86 likes this.
  9. RoyLin86

    RoyLin86

    Joined:
    Aug 30, 2016
    Posts:
    12
    @Olmi
    I am very appriceate for your apply.I'm a begginer in coding and Unity.
    I think I know where's the problem,I just do the stupid things...
    Finally I can change the value in runtime...Thank you.

    And I'm sorry for not using "insert code",next time I'll use it....

    Have a good day. :)