Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
basic sun controller script
// 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
}
@andrew-raphael-lukasik
Copy link
Author

andrew-raphael-lukasik commented Aug 23, 2022

GIF 22 08 2022 00-32-42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment