├── .gitignore ├── Example ├── Sky.mat ├── Grey.mat ├── Example.unity ├── Example.unity.meta ├── Grey.mat.meta └── Sky.mat.meta ├── README.md.meta ├── Editor.meta ├── Example.meta ├── DayNightCycle.cs.meta ├── Editor ├── DayNightCycleEditor.cs.meta └── DayNightCycleEditor.cs ├── .github └── FUNDING.yml ├── LICENSE ├── README.md └── DayNightCycle.cs /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /Example/Sky.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EnricoMonese/DayNightCycle/HEAD/Example/Sky.mat -------------------------------------------------------------------------------- /Example/Grey.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EnricoMonese/DayNightCycle/HEAD/Example/Grey.mat -------------------------------------------------------------------------------- /Example/Example.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EnricoMonese/DayNightCycle/HEAD/Example/Example.unity -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2553aa37734174c81a78bc7173069945 3 | timeCreated: 1473107222 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Example/Example.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f04cccec2162b4fe2a3a22554ebe67fc 3 | timeCreated: 1473097256 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Example/Grey.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f68bca5df4a2a4b8d995268cdd871788 3 | timeCreated: 1473098744 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Example/Sky.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1fa745a5b0f3d4159aae04ed154c6189 3 | timeCreated: 1473098653 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4a7c4074fb8e04cc6bfcd6df0d59ee50 3 | folderAsset: yes 4 | timeCreated: 1473095969 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Example.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dac42f2af9d2344a4b878acf3d60d592 3 | folderAsset: yes 4 | timeCreated: 1473099248 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /DayNightCycle.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 417d931b7d7494546b2c26d63472b156 3 | timeCreated: 1473095961 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Editor/DayNightCycleEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 69e8b0af73f224195be0b8c0567075de 3 | timeCreated: 1473095961 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: enricomonese 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Enrico Monese 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Editor/DayNightCycleEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using UnityEditor; 4 | [CustomEditor (typeof(DayNightCycle))] 5 | public class DayNightCycleEditor : Editor { 6 | 7 | DayNightCycle script; 8 | 9 | void OnEnable () { 10 | script = target as DayNightCycle; 11 | script.EditorSetup (); 12 | } 13 | 14 | 15 | public override void OnInspectorGUI () { 16 | base.OnInspectorGUI (); 17 | SecondsDurationGUI ("Complete day duration: ", script.secondsInFullDay); 18 | SecondsDurationGUI ("Enviroment updates every: ", script.updateRateInSeconds); 19 | float currentSeconds = script.secondsInFullDay * script.currentTimeOfDay; 20 | SecondsDurationGUI ("Elapsed time from : ", currentSeconds); 21 | float hour = 86400f * script.currentTimeOfDay; 22 | SecondsDurationGUI ("Current in game time: ", hour); 23 | 24 | if (GUI.changed) { 25 | EditorUtility.SetDirty (script); 26 | script.UpdateEditor (); 27 | } 28 | } 29 | 30 | void SecondsDurationGUI (string message, float seconds) { 31 | System.TimeSpan time = System.TimeSpan.FromSeconds(seconds); 32 | string str = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D2}ms", time.Hours, time.Minutes, time.Seconds, time.Milliseconds); 33 | EditorGUILayout.HelpBox (message + str, MessageType.None, true); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![GitHub stars](https://img.shields.io/github/stars/enricomonese/DayNightCycle?style=social) 2 | ![GitHub forks](https://img.shields.io/github/forks/enricomonese/DayNightCycle?style=social) 3 | ![GitHub watchers](https://img.shields.io/github/watchers/enricomonese/DayNightCycle?style=social) 4 | ![GitHub followers](https://img.shields.io/github/followers/enricomonese?style=social) 5 | 6 | 7 | ![GitHub repo size](https://img.shields.io/github/repo-size/enricomonese/DayNightCycle) 8 | ![GitHub language count](https://img.shields.io/github/languages/count/enricomonese/DayNightCycle) 9 | ![GitHub top language](https://img.shields.io/github/languages/top/enricomonese/DayNightCycle) 10 | ![GitHub repo license](https://img.shields.io/github/license/enricomonese/DayNightCycle) 11 | ![GitHub last commit](https://img.shields.io/github/last-commit/enricomonese/DayNightCycle) 12 | 13 | 14 | Patreon donate button 15 | 16 | 17 | # Day-Night Cycle 18 | 19 | 20 | An Easy and Simple solution for day-night cycle in Unity game engine. 21 | 22 | ![Screen][Image3] 23 | 24 | ## How to use 25 | 26 | To use simply import *DayNightCycle.cs* and *Editor/DayNightCycleEditor.cs* into your project. 27 | Assign *DayNightCycle.cs* to a directional light in your scene and play around with the settings. 28 | Then assign your Directional light and a procedural skybox material in the *Lighting Settings*. 29 | You may also lower the *Reflection Intensity* to get get darker scenes during night. 30 | 31 | ![Screen][Image1] 32 | ![Screen][Image2] 33 | 34 | 35 | ## Requirements 36 | 37 | Tested in the 5.5 beta but should work in previous versions too. 38 | Procedural Skybox material is needed. 39 | 40 | 41 | 42 | ## TODO 43 | 44 | * [ ] Presets **(maybe)** 45 | * [ ] Range sliders for min-max variables 46 | * [ ] Quick method that returns in game time(hours:minutes:seconds) 47 | 48 | 49 | Patreon donate button 50 | 51 | 52 | [Image1]: http://www.edraflame.com/daynightcycle-github-images/inspector.png 53 | [Image2]: http://www.edraflame.com/daynightcycle-github-images/lighting-settings.png 54 | [Image3]: http://www.edraflame.com/daynightcycle-github-images/screen.png 55 | [Kino]: https://github.com/search?q=kino+user%3Akeijiro&type=Repositories 56 | -------------------------------------------------------------------------------- /DayNightCycle.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | //[ExecuteInEditMode] 4 | public class DayNightCycle : MonoBehaviour { 5 | 6 | public Light sun; 7 | public float secondsInFullDay = 120f; 8 | public float updateRateInSeconds = 5f; 9 | [Range(0,1)] 10 | public float currentTimeOfDay = 0.45f; 11 | public float timeMultiplier = 1f; 12 | [Space(10)] 13 | public Gradient nightDayColor; 14 | public float maxIntensity = 2f; 15 | public float minIntensity = 0f; 16 | public float minPoint = -0.2f; 17 | public float maxBounceIntensity = 1.0f; 18 | public float minBounceIntensity = 0.5f; 19 | public float maxAmbient = 1f; 20 | public float minAmbient = 0f; 21 | public float minAmbientPoint = -0.2f; 22 | public Gradient nightDayFogColor; 23 | public AnimationCurve fogDensityCurve; 24 | public float fogScale = 1f; 25 | public float exposureMultiplier = 1f; 26 | public float dayAtmosphereThickness = 0.4f; 27 | public float nightAtmosphereThickness = 0.87f; 28 | Skybox sky; 29 | Material skyMat; 30 | 31 | #if UNITY_EDITOR 32 | [HideInInspector] 33 | public bool showSettings = false; 34 | 35 | public void UpdateEditor () { 36 | UpdatePosition (); 37 | UpdateFX (); 38 | } 39 | public void EditorSetup () { 40 | sun = GetComponent(); 41 | skyMat = RenderSettings.skybox; 42 | } 43 | #endif 44 | void Start() { 45 | sun = GetComponent(); 46 | skyMat = RenderSettings.skybox; 47 | InvokeRepeating ("UpdateCycle", updateRateInSeconds, updateRateInSeconds); 48 | } 49 | void UpdateCycle () { 50 | UpdatePosition(); 51 | UpdateFX (); 52 | 53 | currentTimeOfDay += ((Time.deltaTime + updateRateInSeconds) / secondsInFullDay) * timeMultiplier; 54 | 55 | if (Input.GetKeyDown (KeyCode.O)) timeMultiplier *= 0.5f; 56 | if (Input.GetKeyDown (KeyCode.P)) timeMultiplier *= 2f; 57 | 58 | if (currentTimeOfDay >= 1) { 59 | currentTimeOfDay = 0; 60 | } 61 | } 62 | 63 | void UpdatePosition() { 64 | sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0); 65 | } 66 | 67 | void UpdateFX () { 68 | float tRange = 1 - minPoint; 69 | float dot = Mathf.Clamp01 ((Vector3.Dot (sun.transform.forward, Vector3.down) - minPoint) / tRange); 70 | float i = ((maxIntensity - minIntensity) * dot) + minIntensity; 71 | sun.intensity = i; 72 | 73 | i = ((maxBounceIntensity - minBounceIntensity) * dot) + minBounceIntensity; 74 | sun.bounceIntensity = i; 75 | 76 | tRange = 1 - minAmbientPoint; 77 | dot = Mathf.Clamp01 ((Vector3.Dot (sun.transform.forward, Vector3.down) - minAmbientPoint) / tRange); 78 | i = ((maxAmbient - minAmbient) * dot) + minAmbient; 79 | RenderSettings.ambientIntensity = i; 80 | 81 | sun.color = nightDayColor.Evaluate(dot); 82 | RenderSettings.ambientLight = sun.color; 83 | 84 | RenderSettings.fogColor = nightDayFogColor.Evaluate(dot); 85 | RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale; 86 | 87 | i = ((dayAtmosphereThickness - nightAtmosphereThickness) * dot) + nightAtmosphereThickness; 88 | skyMat.SetFloat ("_AtmosphereThickness", i); 89 | skyMat.SetFloat ("_Exposure", i * exposureMultiplier); 90 | } 91 | } --------------------------------------------------------------------------------