114 lines
3.0 KiB
C#
114 lines
3.0 KiB
C#
using System;
|
|
using Obvious.Soap;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Light))]
|
|
[ExecuteInEditMode]
|
|
public class TimeOfDay : MonoBehaviour
|
|
{
|
|
public FloatVariable timeOfDayGlobal;
|
|
|
|
[Tooltip("Time of day normalized between 0 and 24h. For example 6.5 amount to 6:30am.")]
|
|
public float timeOfDay = 12f;
|
|
|
|
[SerializeField]
|
|
[Tooltip("Sets the speed at which the time of day passes.")]
|
|
private float timeSpeed = 1f;
|
|
|
|
[SerializeField]
|
|
private float latitude = 48.83402f;
|
|
|
|
[SerializeField]
|
|
private float longitude = 2.367259f;
|
|
|
|
private DateTime date = new DateTime(2024, 4, 21).Date;
|
|
|
|
private DateTime time;
|
|
|
|
[SerializeField]
|
|
[HideInInspector]
|
|
private GUIStyle sliderStyle;
|
|
|
|
internal static TimeOfDay instance;
|
|
|
|
public bool isDebugGUIEnabled;
|
|
|
|
private void OnEnable()
|
|
{
|
|
instance = this;
|
|
TimeOfDayGlobalOnOnValueChanged(timeOfDayGlobal.Value);
|
|
timeOfDayGlobal.OnValueChanged += TimeOfDayGlobalOnOnValueChanged;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
timeOfDayGlobal.OnValueChanged -= TimeOfDayGlobalOnOnValueChanged;
|
|
}
|
|
|
|
private void TimeOfDayGlobalOnOnValueChanged(float obj)
|
|
{
|
|
timeOfDay = obj;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
GetHoursMinutesSecondsFromTimeOfDay(out var hours, out var minutes, out var _);
|
|
time = date + new TimeSpan(hours, minutes, 0);
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
GetHoursMinutesSecondsFromTimeOfDay(out var hours, out var minutes, out var seconds);
|
|
time = date + new TimeSpan(hours, minutes, seconds);
|
|
SetSunPosition();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
SetSunPosition();
|
|
}
|
|
|
|
private void SetSunPosition()
|
|
{
|
|
CalculateSunPosition(time, latitude, longitude, timeOfDay, out var outAzimuth, out var outAltitude);
|
|
if (double.IsNaN(outAzimuth))
|
|
{
|
|
outAzimuth = base.transform.localRotation.y;
|
|
}
|
|
Vector3 euler = new Vector3((float)outAltitude, (float)outAzimuth, 0f);
|
|
base.transform.localRotation = Quaternion.Euler(euler);
|
|
}
|
|
|
|
public void CalculateSunPosition(DateTime dateTime, double latitude, double longitude, float timeOfDay, out double outAzimuth, out double outAltitude)
|
|
{
|
|
float num = -23.45f * Mathf.Cos(MathF.PI * 2f * (float)(dateTime.DayOfYear + 10) / 365f);
|
|
float num2 = 15f * (timeOfDay - 12f);
|
|
num2 *= MathF.PI / 180f;
|
|
float f = num * (MathF.PI / 180f);
|
|
float f2 = (float)latitude * (MathF.PI / 180f);
|
|
float num3 = Mathf.Sin(f2);
|
|
float num4 = Mathf.Cos(f2);
|
|
float num5 = Mathf.Cos(num2);
|
|
float num6 = Mathf.Sin(f);
|
|
float num7 = Mathf.Cos(f);
|
|
float num8 = Mathf.Asin(num6 * num3 + num7 * num4 * num5);
|
|
float num9 = Mathf.Cos(num8);
|
|
float num10 = Mathf.Acos((num6 * num4 - num7 * num3 * num5) / num9);
|
|
num8 *= 57.29578f;
|
|
num10 *= 57.29578f;
|
|
if (num2 >= 0f)
|
|
{
|
|
num10 = 360f - num10;
|
|
}
|
|
outAltitude = num8;
|
|
outAzimuth = num10;
|
|
}
|
|
|
|
private void GetHoursMinutesSecondsFromTimeOfDay(out int hours, out int minutes, out int seconds)
|
|
{
|
|
hours = Mathf.FloorToInt(timeOfDay);
|
|
minutes = Mathf.FloorToInt((timeOfDay - (float)hours) * 60f);
|
|
seconds = Mathf.FloorToInt((timeOfDay - (float)hours - (float)minutes / 60f) * 60f * 60f);
|
|
}
|
|
}
|