Search Unity

Updating PhysicallyBasedSky parameters during play

Discussion in 'Scripting' started by ozaengle, Apr 17, 2021.

  1. ozaengle

    ozaengle

    Joined:
    Mar 3, 2021
    Posts:
    1
    I am trying to dynamically update a parameter within the PhysicallyBasedSky upon play.

    I have added the Physically Based Sky override.
    I have the visual environment on the Sky and Fog Volume configured as follows:
    - Type : Physically Based Sky
    - Ambient Mode: Dynamic

    I have the following script attached to an empty GameObject.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Rendering.HighDefinition;
    5.  
    6. public class UpdateAerosolParameters : MonoBehaviour
    7. {
    8.     public Volume SkyVolume;
    9.     public Color AerosolTint;
    10.  
    11.     private PhysicallyBasedSky Sky;
    12.  
    13.  
    14.     private void OnValidate()
    15.     {
    16.         VolumeProfile profile = SkyVolume.sharedProfile;
    17.  
    18.         profile.TryGet<PhysicallyBasedSky>(out Sky);
    19.     }
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         Sky.aerosolTint = new ColorParameter(AerosolTint, true);
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         float random = Random.Range(0, 1);
    32.         Sky.aerosolDensity.SetValue(new FloatParameter(random, true));
    33.     }
    34. }
    I attach a color to the AerosolTint parameter to the script within the editor on the empty GameObject. Likewise, I attach the Sky and Fog Volume to the SkyVolume parameter.

    When I enter "Play" mode I do not receive any errors and the parameters are not updated during the scene. However, when I exit "Play" mode the parameters are changed. The Aerosol Tint has been updated to color added to my script, and the Aerosol Density has been update to the most recent number. This is evident visually as well as in the inspector on the Sky and Fog Volume.

    I would like these to change dynamically during the scene. In this case, I would like the aerosol density to be updated each frame.

    Any suggestions?