//Time and Light private void Start() { _sunLight = sun.GetComponentInChildren <Light>(); if (InitialStateIndex >= 0 && InitialStateIndex < timeOfDayTransitions.Length) { _currentTransition = timeOfDayTransitions[InitialStateIndex]; } else { _currentTransition = timeOfDayTransitions[0]; } if (RenderSettings.skybox == null || RenderSettings.skybox != SkyBlend) { RenderSettings.skybox = SkyBlend; } //If playercamera no have skycamera script, them added this. if (playerCamera != null) { if (playerCamera.GetComponent <DNC_SkyCamera>() == null) { playerCamera.gameObject.AddComponent <DNC_SkyCamera>(); playerCamera.cullingMask = UDayMask; } } else { Debug.LogError("you need to add the playerCamera"); } _timeInSeconds = HOUR * timeInHours; _sourceEnvironmentState = GetEnvironmentStateFromTransition(_currentTransition); ApplyEnvironmentState(_sourceEnvironmentState); }
private void Update() { // Update time var _realSecondToIngameSecond = 24 * 60 / dayCycleInMinutes; _timeInSeconds += Time.deltaTime * _realSecondToIngameSecond; if (_timeInSeconds >= DAY) { _timeInSeconds -= DAY; daysPassed++; } if (_timeInSeconds < 0) { timeInHours += DAY; } timeInHours = _timeInSeconds / HOUR; if (timeInHours >= 6 && timeInHours <= 18) { isDay = true; } else { isDay = false; } // Update Sun and Moon position transform.rotation = Quaternion.Euler(new Vector3(360 / DAY * _timeInSeconds, 0, 0)); if (playerCamera != null) { transform.position = attachedTo.position; var ambient = _sunLight.color * _sunLight.intensity; playerCamera.backgroundColor = ambient; } // Update environment state if (_currentTransition == null) { _currentTransition = FindActiveTransition(_timeInSeconds); if (_currentTransition != null) { _sourceEnvironmentState = ReadEnvironmentState(); } } if (_currentTransition != null) { ApplyCurrentTransition(); } }
public float timeInHours; // In game time in hours private void Start() { _sunLight = sun.GetComponentInChildren<Light>(); if (InitialStateIndex >= 0 && InitialStateIndex < timeOfDayTransitions.Length) { _currentTransition = timeOfDayTransitions[InitialStateIndex]; } else { _currentTransition = timeOfDayTransitions[0]; } _timeInSeconds = HOUR * timeInHours; _sourceEnvironmentState = GetEnvironmentStateFromTransition(_currentTransition); ApplyEnvironmentState(_sourceEnvironmentState); }
private static EnvironmentState GetEnvironmentStateFromTransition(TimeOfDayTransition t) { var env = new EnvironmentState { ambientLight = t.ambientLight, moonTintColor = t.moonTintColor, sunColor = t.sunColor, sunIntensity = t.sunIntensity, sunTintColor = t.sunTintColor, fogColor = t.fogColor, fogDensity = t.fogDensity, skyboxBlendValue = t.skyboxBlendValue, skyboxTintColor = t.skyboxTintColor, auxColor1 = t.auxColor1, auxColor2 = t.auxColor2, }; return(env); }
private void ApplyCurrentTransition() { var s = _sourceEnvironmentState; var t = _currentTransition; var currentTime = timeInHours; if (currentTime - t.startHour < 0) { currentTime += 24; } var x = (currentTime - t.startHour) / t.durationInHours; if (x > 1) { x = 1; _currentTransition = null; } var env = new EnvironmentState { ambientLight = Color.Lerp(s.ambientLight, t.ambientLight, x), moonTintColor = Color.Lerp(s.moonTintColor, t.moonTintColor, x), sunColor = Color.Lerp(s.sunColor, t.sunColor, x), sunIntensity = Mathf.Lerp(s.sunIntensity, t.sunIntensity, x), sunTintColor = Color.Lerp(s.sunTintColor, t.sunTintColor, x), fogColor = Color.Lerp(s.fogColor, t.fogColor, x), fogDensity = Mathf.Lerp(s.fogDensity, t.fogDensity, x), skyboxBlendValue = Mathf.Lerp(s.skyboxBlendValue, t.skyboxBlendValue, x), skyboxTintColor = Color.Lerp(s.skyboxTintColor, t.skyboxTintColor, x), auxColor1 = Color.Lerp(s.auxColor1, t.auxColor1, x), auxColor2 = Color.Lerp(s.auxColor2, t.auxColor2, x), }; ApplyEnvironmentState(env); }
private void ApplyCurrentTransition() { var s = _sourceEnvironmentState; var t = _currentTransition; var currentTime = timeInHours; if (currentTime - t.startHour < 0) { currentTime += 24; } var x = (currentTime - t.startHour) / t.durationInHours; if (x > 1) { x = 1; _currentTransition = null; } Debug.Log(x); var env = new EnvironmentState { ambientLight = Color.Lerp(s.ambientLight, t.ambientLight, x), moonTintColor = Color.Lerp(s.moonTintColor, t.moonTintColor, x), sunColor = Color.Lerp(s.sunColor, t.sunColor, x), sunIntensity = Mathf.Lerp(s.sunIntensity, t.sunIntensity, x), sunTintColor = Color.Lerp(s.sunTintColor, t.sunTintColor, x), fogColor = Color.Lerp(s.fogColor, t.fogColor, x), fogDensity = Mathf.Lerp(s.fogDensity, t.fogDensity, x), skyboxBlendValue = Mathf.Lerp(s.skyboxBlendValue, t.skyboxBlendValue, x), skyboxTintColor = Color.Lerp(s.skyboxTintColor, t.skyboxTintColor, x), auxColor1 = Color.Lerp(s.auxColor1, t.auxColor1, x), auxColor2 = Color.Lerp(s.auxColor2, t.auxColor2, x), }; ApplyEnvironmentState(env); }
private static EnvironmentState GetEnvironmentStateFromTransition(TimeOfDayTransition t) { var env = new EnvironmentState { ambientLight = t.ambientLight, moonTintColor = t.moonTintColor, sunColor = t.sunColor, sunIntensity = t.sunIntensity, sunTintColor = t.sunTintColor, fogColor = t.fogColor, fogDensity = t.fogDensity, skyboxBlendValue = t.skyboxBlendValue, skyboxTintColor = t.skyboxTintColor, auxColor1 = t.auxColor1, auxColor2 = t.auxColor2, }; return env; }
private void Update() { // Update time var _realSecondToIngameSecond = 24 * 60 / dayCycleInMinutes; _timeInSeconds += Time.deltaTime * _realSecondToIngameSecond; if (_timeInSeconds >= DAY) { _timeInSeconds -= DAY; daysPassed++; } if (_timeInSeconds < 0) { timeInHours += DAY; } timeInHours = _timeInSeconds / HOUR; // Update Sun and Moon position transform.rotation = Quaternion.Euler(new Vector3(360 / DAY * _timeInSeconds, 0, 0)); if (playerCamera != null) { transform.position = attachedTo.position; var ambient = _sunLight.color * _sunLight.intensity; playerCamera.backgroundColor = ambient; } // Update environment state if (_currentTransition == null) { _currentTransition = FindActiveTransition(_timeInSeconds); if (_currentTransition != null) { _sourceEnvironmentState = ReadEnvironmentState(); } } if (_currentTransition != null) { ApplyCurrentTransition(); } }