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

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:

  public GradientSky daySky;
    public Volume vol;

    void Start(){
        vol.profile.TryGet(out daySky);
    }

    void Update(){
   
         daySky.top.value = Color.Lerp(Color.white, Color.black, (Mathf.Sin(Time.time * 0.4f) + 1f) / 2f);

     }

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)

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.)

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

public class AccessVolumeOverrideSky : MonoBehaviour
{
    VisualEnvironment visualEnvironment;
    GradientSky gradientSky;

    void Awake()
    {
        Volume volume = gameObject.GetComponent<Volume>();
        VisualEnvironment tempVis;

        if (volume.profile.TryGet<VisualEnvironment>(out tempVis)) {
            visualEnvironment = tempVis;
        }

        GradientSky tempGradSky;
        if (volume.profile.TryGet<GradientSky>(out tempGradSky)) {
            gradientSky = tempGradSky;
        }

        // Set the sky type
        visualEnvironment.skyType.value = (int)SkyType.Gradient;

        // Set the sky parameters
        gradientSky.top.value = Color.red;
        gradientSky.middle.value = Color.green;
        gradientSky.bottom.value = Color.blue;
        gradientSky.gradientDiffusion.value = 2.0f;
    }
}

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:

void Update()
    {
        gradientSky.middle.value = new Color(
            Mathf.Sin(Time.time),
            Mathf.Sin(Time.time),
            Mathf.Sin(Time.time)
        );
    }

My sky had Update Mode set to On Demand.

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)

1 Like