Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

[Solved]Can't update color in GradientSky on post process volume by script

Discussion in 'High Definition Render Pipeline' started by wenzy, Oct 19, 2020.

  1. wenzy

    wenzy

    Joined:
    Nov 17, 2015
    Posts:
    44
    Hello community,

    In HDRP's Volume component, I want to lerp the color in gradient sky by script.

    I have set the UpdateMode to "realtime" and the Update Period to "0",

    and here is my script:


    Code (CSharp):
    1.   public GradientSky daySky;
    2.     public Volume vol;
    3.  
    4.     void Start(){
    5.         vol.profile.TryGet(out daySky);
    6.     }
    7.  
    8.     void Update(){
    9.    
    10.          daySky.top.value = Color.Lerp(Color.white, Color.black, (Mathf.Sin(Time.time * 0.4f) + 1f) / 2f);
    11.  
    12.      }
    I can see the "top color" is update in the inspector, but in the game scene nothing happen

    (PS: Update. The issue has solved when I update the HDRP version to 7.51)
     
    Last edited: Oct 21, 2020
  2. Olmi

    Olmi

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

    This is from my snippets I've written when I was testing HDRP, should work:
    (Attach it to an object with your Gradient Sky, it should then change the colors of the Gradient Sky when your enter the play mode.)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4.  
    5. public class AccessVolumeOverrideSky : MonoBehaviour
    6. {
    7.     VisualEnvironment visualEnvironment;
    8.     GradientSky gradientSky;
    9.  
    10.     void Awake()
    11.     {
    12.         Volume volume = gameObject.GetComponent<Volume>();
    13.         VisualEnvironment tempVis;
    14.  
    15.         if (volume.profile.TryGet<VisualEnvironment>(out tempVis)) {
    16.             visualEnvironment = tempVis;
    17.         }
    18.  
    19.         GradientSky tempGradSky;
    20.         if (volume.profile.TryGet<GradientSky>(out tempGradSky)) {
    21.             gradientSky = tempGradSky;
    22.         }
    23.  
    24.         // Set the sky type
    25.         visualEnvironment.skyType.value = (int)SkyType.Gradient;
    26.  
    27.         // Set the sky parameters
    28.         gradientSky.top.value = Color.red;
    29.         gradientSky.middle.value = Color.green;
    30.         gradientSky.bottom.value = Color.blue;
    31.         gradientSky.gradientDiffusion.value = 2.0f;
    32.     }
    33. }
    EDIT: Misread your message a bit, but it should be possible to update the gradient values in Update too, just try this, should work too:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         gradientSky.middle.value = new Color(
    4.             Mathf.Sin(Time.time),
    5.             Mathf.Sin(Time.time),
    6.             Mathf.Sin(Time.time)
    7.         );
    8.     }
    My sky had Update Mode set to On Demand.
     
    Last edited: Oct 20, 2020
  3. wenzy

    wenzy

    Joined:
    Nov 17, 2015
    Posts:
    44
    Thanks for your reply ! I think it's a bug on the older hdrp version. The issue has solved when I update the HDRP version from 3.31 to 7.51)

     
    Olmi likes this.