Last active
July 6, 2024 04:13
-
-
Save andrew-raphael-lukasik/2f337c0e6c467f084cded9bf63dd8556 to your computer and use it in GitHub Desktop.
basic sun controller script
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
// src* https://gist.github.com/andrew-raphael-lukasik/2f337c0e6c467f084cded9bf63dd8556 | |
using UnityEngine; | |
public class SunController : MonoBehaviour | |
{ | |
[SerializeField][Range(0,1)] float _dayTime = 0.5f;// 0.5 means noon | |
[SerializeField] float _timeScale = 1; | |
[SerializeField] Light _sun = null;// make sure it is directional | |
[SerializeField] Vector2 _sunAtMidnight = new Vector2(45,0);// degrees | |
[SerializeField] Vector2 _earthRotationAxis = new Vector2(-45,0);// degrees, axis relative to your location (so pointing straight up if you are on a N or S pole) | |
const float degreesPerSecond = 360f / secondsPerDay; | |
const float secondsPerDay = 24*60*60;// seconds per day | |
[SerializeField] AnimationCurve _sunIntensity = new AnimationCurve( | |
new Keyframe(0,0) , | |
new Keyframe(0.25f,0) , | |
new Keyframe(0.5f,1) , | |
new Keyframe(0.75f,0) , | |
new Keyframe(1,0) | |
); | |
[SerializeField] Gradient _sunColor = new Gradient{ colorKeys=new GradientColorKey[]{ | |
new GradientColorKey(new Color(1,0.3f,0,1),0) , | |
new GradientColorKey(Color.white,0.5f) , | |
new GradientColorKey(new Color(1,0.3f,0,1),1) | |
} }; | |
void FixedUpdate () | |
{ | |
// step simulation time: | |
_dayTime = ( _dayTime + ( Time.fixedDeltaTime * _timeScale ) / secondsPerDay ) % 1f; | |
// update directional light: | |
Vector3 sunPositionNow = SunPositionAtTime(_dayTime); | |
_sun.transform.rotation = Quaternion.LookRotation( -sunPositionNow ); | |
_sun.intensity = _sunIntensity.Evaluate(_dayTime); | |
_sun.color = _sunColor.Evaluate(_dayTime); | |
_sun.enabled = _sun.intensity>0; | |
} | |
/// <param name="t">0-1 value range</param> | |
Vector3 SunPositionAtTime ( float t ) | |
{ | |
Vector3 midnightDir = Quaternion.Euler((Vector3)_sunAtMidnight) * Vector3.forward; | |
Vector3 earthAxisDir = Quaternion.Euler((Vector3)_earthRotationAxis) * Vector3.forward; | |
return Quaternion.AngleAxis( t*secondsPerDay*degreesPerSecond , earthAxisDir ) * midnightDir; | |
} | |
#if UNITY_EDITOR | |
void OnDrawGizmos () | |
{ | |
Vector3 position = transform.position; | |
Gizmos.color = Color.cyan * 0.3f; | |
Gizmos.DrawRay( position , Quaternion.Euler((Vector3)_earthRotationAxis) * Vector3.forward ); | |
Gizmos.color = Color.black; | |
Gizmos.DrawRay( position , Quaternion.Euler((Vector3)_sunAtMidnight) * Vector3.forward ); | |
if( Application.isPlaying ) | |
{ | |
Gizmos.color = Color.white; | |
Gizmos.DrawRay( position , SunPositionAtTime(_dayTime) ); | |
} | |
int numSteps = 100; | |
for( int i=0 ; i<=numSteps ; i++ ) | |
{ | |
float t = (float)i / (float)numSteps; | |
Gizmos.color = _sunColor.Evaluate(t) * new Color(1,1,1,Mathf.Max(_sunIntensity.Evaluate(t),0.05f)); | |
Gizmos.DrawRay( position , SunPositionAtTime(t) ); | |
} | |
Gizmos.color = Color.yellow * 0.3f; | |
Gizmos.DrawWireSphere( position , 1f ); | |
int dayTimeSeconds = (int)(_dayTime * secondsPerDay); | |
int h = dayTimeSeconds/(60*60); | |
int m = (dayTimeSeconds%(60*60))/60; | |
int s = dayTimeSeconds%60; | |
UnityEditor.Handles.color = Color.red; | |
UnityEditor.Handles.Label( position , $"time of day: {h:00.}:{m:00.}:{s:00.}" ); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment