-
-
Save zolrath/fd500e6b20cade3bedf7 to your computer and use it in GitHub Desktop.
Control the sun, fog, ambient light, and sun rotation through curves and gradients.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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