Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2016 19:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/9932fc456f3f715bd2e6 to your computer and use it in GitHub Desktop.
Save anonymous/9932fc456f3f715bd2e6 to your computer and use it in GitHub Desktop.
Control the sun, fog, ambient light, and sun rotation through curves and gradients.
using UnityEngine;
public class TimeOfDayManager : MonoBehaviour
{
public float secondsInFullDay = 120f;
[HideInInspector]
public float timeMultiplier = 1f;
[Range(0.0f, 1f)]
public float currentTimeOfDay;
private float sunInitialIntensity;
#region Curves and Gradients
[Header("Ambience")]
public float CurrentAmbientIntensity;
public Color CurrentAmbientColor;
public AnimationCurve AmbientCurve;
public Gradient AmbientColorCurve;
[Header("Fog")]
public float FogIntensity;
public float FogDistance;
public Color CurrentFogColor;
public Gradient FogColorGradient;
public AnimationCurve FogColorCurve;
public AnimationCurve FogIntensityCurve;
public AnimationCurve FogDistanceCurve;
[Header("Sun")]
public Light sun;
public AnimationCurve SunLightIntensityCurve;
public Gradient SunLightColorGradient;
#endregion
private void Start()
{
this.sunInitialIntensity = this.sun.intensity;
}
private void Update()
{
this.UpdateSun();
this.UpdateRenderSettings();
this.currentTimeOfDay += Time.deltaTime / this.secondsInFullDay * this.timeMultiplier;
if ((double)this.currentTimeOfDay >= 1.0)
this.currentTimeOfDay = 0.0f;
if ((double)this.currentTimeOfDay <= 0.0)
this.currentTimeOfDay = 0.0f;
if (Input.GetKey(KeyCode.T))
this.currentTimeOfDay -= 0.05f;
if (!Input.GetKey(KeyCode.Y))
return;
this.currentTimeOfDay += 0.05f;
}
private void UpdateSun()
{
this.sun.transform.localRotation = Quaternion.Euler((float)((double)this.currentTimeOfDay * 360.0 - 90.0), 170f, 0.0f);
}
private void UpdateRenderSettings()
{
this.sun.intensity = this.SunLightIntensityCurve.Evaluate(this.currentTimeOfDay);
this.sun.color = this.SunLightColorGradient.Evaluate(this.currentTimeOfDay);
RenderSettings.ambientIntensity = this.AmbientCurve.Evaluate(this.currentTimeOfDay);
RenderSettings.fogColor = this.FogColorGradient.Evaluate(this.currentTimeOfDay);
RenderSettings.fogDensity = this.FogIntensityCurve.Evaluate(this.currentTimeOfDay);
RenderSettings.fogEndDistance = this.FogDistanceCurve.Evaluate(this.currentTimeOfDay);
RenderSettings.ambientSkyColor = this.AmbientColorCurve.Evaluate(this.currentTimeOfDay);
this.CurrentAmbientColor = RenderSettings.ambientSkyColor;
this.CurrentFogColor = RenderSettings.fogColor;
this.CurrentAmbientIntensity = RenderSettings.ambientIntensity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment