EnviroManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Enviro
  6. {
  7. [ExecuteInEditMode]
  8. public class EnviroManager : EnviroManagerBase
  9. {
  10. private static EnviroManager _instance; // Creat a static instance for easy access!
  11. public static EnviroManager instance
  12. {
  13. get
  14. {
  15. if (_instance == null)
  16. _instance = GameObject.FindObjectOfType<EnviroManager>();
  17. return _instance;
  18. }
  19. }
  20. //General
  21. public GeneralObjects Objects = new GeneralObjects();
  22. //Setup
  23. public bool dontDestroyOnLoad;
  24. public Camera Camera;
  25. public string CameraTag = "MainCamera";
  26. public List<EnviroCameras> Cameras = new List<EnviroCameras>();
  27. //Inspector
  28. public bool showSetup;
  29. public bool showModules;
  30. public bool showThirdParty;
  31. // Publics
  32. public bool isNight;
  33. public float solarTime;
  34. public float lunarTime;
  35. public bool notFirstFrame = false;
  36. //Effect Removal Zones
  37. public List<EnviroEffectRemovalZone> removalZones = new List<EnviroEffectRemovalZone>();
  38. struct ZoneParams
  39. {
  40. public Vector3 pos;
  41. public float radius;
  42. public Vector3 axis;
  43. public float stretch;
  44. public float density;
  45. public float feather;
  46. public float padding1;
  47. public float padding2;
  48. }
  49. public ComputeBuffer clearZoneCB;
  50. public ComputeBuffer removeZoneParamsCB;
  51. public ComputeBuffer clearCB;
  52. ZoneParams[] removalZoneParams;
  53. //Non time module controls
  54. [Range(0f,360f)]
  55. public float sunRotationX;
  56. [Range(0f,360f)]
  57. public float sunRotationY;
  58. [Range(0f,360f)]
  59. public float moonRotationX;
  60. [Range(0f,360f)]
  61. public float moonRotationY;
  62. public bool showNonTimeControls;
  63. ///////
  64. //Events
  65. public delegate void HourPassed();
  66. public delegate void DayPassed();
  67. public delegate void YearPassed();
  68. public delegate void WeatherChanged(EnviroWeatherType weatherType);
  69. public delegate void ZoneWeatherChanged(EnviroWeatherType weatherType, Enviro.EnviroZone zone);
  70. public delegate void SeasonChanged(EnviroEnvironment.Seasons season);
  71. public delegate void isNightEvent();
  72. public delegate void isDayEvent();
  73. public event HourPassed OnHourPassed;
  74. public event DayPassed OnDayPassed;
  75. public event YearPassed OnYearPassed;
  76. public event WeatherChanged OnWeatherChanged;
  77. public event ZoneWeatherChanged OnZoneWeatherChanged;
  78. public event SeasonChanged OnSeasonChanged;
  79. public event isNightEvent OnNightTime;
  80. public event isDayEvent OnDayTime;
  81. public virtual void NotifyHourPassed()
  82. {
  83. if (OnHourPassed != null)
  84. OnHourPassed();
  85. }
  86. public virtual void NotifyDayPassed()
  87. {
  88. if (OnDayPassed != null)
  89. OnDayPassed();
  90. }
  91. public virtual void NotifyYearPassed()
  92. {
  93. if (OnYearPassed != null)
  94. OnYearPassed();
  95. }
  96. public virtual void NotifyWeatherChanged(EnviroWeatherType type)
  97. {
  98. if (OnWeatherChanged != null)
  99. OnWeatherChanged(type);
  100. }
  101. public virtual void NotifyZoneWeatherChanged(EnviroWeatherType type, EnviroZone zone)
  102. {
  103. if (OnZoneWeatherChanged != null)
  104. OnZoneWeatherChanged(type, zone);
  105. }
  106. public virtual void NotifySeasonChanged(EnviroEnvironment.Seasons season)
  107. {
  108. if (OnSeasonChanged != null)
  109. OnSeasonChanged(season);
  110. }
  111. public virtual void NotifyIsNight()
  112. {
  113. if (OnNightTime != null)
  114. OnNightTime();
  115. }
  116. public virtual void NotifyIsDay()
  117. {
  118. if (OnDayTime != null)
  119. OnDayTime();
  120. }
  121. // HDRP
  122. #if ENVIRO_HDRP
  123. public UnityEngine.Rendering.Volume volumeHDRP;
  124. #endif
  125. //////
  126. void OnEnable()
  127. {
  128. #if UNITY_EDITOR
  129. if(UnityEditor.PrefabUtility.IsPartOfAnyPrefab(gameObject))
  130. UnityEditor.PrefabUtility.UnpackPrefabInstance(gameObject,UnityEditor.PrefabUnpackMode.OutermostRoot,UnityEditor.InteractionMode.UserAction);
  131. #endif
  132. CreateGeneralObjects ();
  133. #if ENVIRO_HDRP
  134. CreateHDRPVolume ();
  135. #endif
  136. EnableModules();
  137. #if !ENVIRO_HDRP || !ENVIRO_URP
  138. //Add Enviro Render Component to main camera in builtin rp
  139. AddCameraComponents();
  140. #endif
  141. }
  142. void OnDisable()
  143. {
  144. if(Fog != null)
  145. Fog.Disable();
  146. ReleaseZoneBuffers();
  147. }
  148. private void AddCameraComponents()
  149. {
  150. if(Camera != null)
  151. {
  152. if(Camera.gameObject.GetComponent<EnviroRenderer>() == null)
  153. Camera.gameObject.AddComponent<EnviroRenderer>();
  154. }
  155. for(int i = 0; i < Cameras.Count; i++)
  156. {
  157. if(Cameras[i].camera.gameObject.GetComponent<EnviroRenderer>() == null)
  158. Cameras[i].camera.gameObject.AddComponent<EnviroRenderer>();
  159. }
  160. }
  161. void Start ()
  162. {
  163. // Set dont destroy on load on start
  164. if(dontDestroyOnLoad && Application.isPlaying)
  165. DontDestroyOnLoad(gameObject);
  166. //Set a first frame bool that will be used for events.
  167. notFirstFrame = false;
  168. StartCoroutine(FirstFrame());
  169. StartModules ();
  170. }
  171. //Update modules
  172. void Update()
  173. {
  174. if(!Application.isPlaying)
  175. LoadConfiguration();
  176. UpdateManager ();
  177. //Update all modules
  178. UpdateModules ();
  179. //Update non time case
  180. if(Time == null)
  181. UpdateNonTime();
  182. }
  183. void LateUpdate()
  184. {
  185. if(Camera != null)
  186. {
  187. transform.position = Camera.transform.position;
  188. }
  189. }
  190. void CreateGeneralObjects ()
  191. {
  192. if(Objects.sun == null)
  193. {
  194. Objects.sun = new GameObject();
  195. Objects.sun.name = "Sun";
  196. Objects.sun.transform.SetParent(transform);
  197. Objects.sun.transform.localPosition = Vector3.zero;
  198. }
  199. if(Objects.moon == null)
  200. {
  201. Objects.moon = new GameObject();
  202. Objects.moon.name = "Moon";
  203. Objects.moon.transform.SetParent(transform);
  204. Objects.moon.transform.localPosition = Vector3.zero;
  205. }
  206. if(Objects.stars == null)
  207. {
  208. Objects.stars = new GameObject();
  209. Objects.stars.name = "Stars";
  210. Objects.stars.transform.SetParent(transform);
  211. Objects.stars.transform.localPosition = Vector3.zero;
  212. }
  213. }
  214. // Set the solar and lunar time based on sun rotation.
  215. private void UpdateNonTime()
  216. {
  217. if(Objects.sun != null)
  218. {
  219. Objects.sun.transform.eulerAngles = new Vector3(sunRotationX,sunRotationY,0f);
  220. if(sunRotationX > 0f && sunRotationX <= 90f)
  221. solarTime = EnviroHelper.Remap(sunRotationX, 0f, 90f, 0.5f, 1f);
  222. else if (sunRotationX > 90f && sunRotationX <= 180f)
  223. solarTime = EnviroHelper.Remap(sunRotationX, 90f, 180f, 1f, 0.5f);
  224. else if (sunRotationX > 180f && sunRotationX <= 270f)
  225. solarTime = EnviroHelper.Remap(sunRotationX, 180f, 270f, 0.5f, 0.0f);
  226. else if (sunRotationX > 270f && sunRotationX <= 360f)
  227. solarTime = EnviroHelper.Remap(sunRotationX, 270f, 360f, 0.0f, 0.5f);
  228. else solarTime = 0.5f;
  229. }
  230. if(Objects.moon != null)
  231. {
  232. Objects.moon.transform.eulerAngles = new Vector3(moonRotationX,moonRotationY,0f);
  233. if(moonRotationX > 0f && moonRotationX <= 90f)
  234. lunarTime = EnviroHelper.Remap(moonRotationX, 0f, 90f, 0.5f, 1f);
  235. else if (moonRotationX > 90f && moonRotationX <= 180f)
  236. lunarTime = EnviroHelper.Remap(moonRotationX, 90f, 180f, 1f, 0.5f);
  237. else if (moonRotationX > 180f && moonRotationX <= 270f)
  238. lunarTime = EnviroHelper.Remap(moonRotationX, 180f, 270f, 0.5f, 0.0f);
  239. else if (moonRotationX > 270f && moonRotationX <= 360f)
  240. lunarTime = EnviroHelper.Remap(moonRotationX, 270f, 360f, 0.0f, 0.5f);
  241. else lunarTime = 0.5f;
  242. }
  243. }
  244. //Effect Removal Zones
  245. public bool AddRemovalZone (EnviroEffectRemovalZone zone)
  246. {
  247. removalZones.Add(zone);
  248. return true;
  249. }
  250. public void RemoveRemovaleZone (EnviroEffectRemovalZone zone)
  251. {
  252. if(removalZones.Contains(zone))
  253. removalZones.Remove(zone);
  254. }
  255. private void SetupZoneBuffers()
  256. {
  257. int count = 0;
  258. for (int z = 0; z < EnviroManager.instance.removalZones.Count; z++)
  259. {
  260. if (EnviroManager.instance.removalZones[z] != null && EnviroManager.instance.removalZones[z].enabled && EnviroManager.instance.removalZones[z].gameObject.activeSelf)
  261. count++;
  262. }
  263. Shader.SetGlobalFloat("_EllipsZonesCount", count);
  264. if (count == 0)
  265. {
  266. // Can't not set the buffer
  267. Shader.SetGlobalBuffer("_EllipsZones", clearZoneCB);
  268. return;
  269. }
  270. if (removalZoneParams == null || removalZoneParams.Length != count)
  271. removalZoneParams = new ZoneParams[count];
  272. int zoneID = 0;
  273. for (int i = 0; i < removalZones.Count; i++)
  274. {
  275. Enviro.EnviroEffectRemovalZone fz = removalZones[i];
  276. if (fz == null || !fz.enabled || !fz.gameObject.activeSelf)
  277. continue;
  278. Transform t = fz.transform;
  279. removalZoneParams[zoneID].pos = t.position;
  280. removalZoneParams[zoneID].radius = fz.radius * fz.radius;
  281. removalZoneParams[zoneID].axis = -t.up;
  282. removalZoneParams[zoneID].stretch = 1.0f/fz.stretch - 1.0f;
  283. removalZoneParams[zoneID].density = fz.density;
  284. removalZoneParams[zoneID].feather = 1.0f - fz.feather;
  285. zoneID++;
  286. }
  287. removeZoneParamsCB.SetData(removalZoneParams);
  288. Shader.SetGlobalBuffer("_EllipsZones", removeZoneParamsCB);
  289. }
  290. private void CreateZoneBuffers()
  291. {
  292. EnviroHelper.CreateBuffer(ref removeZoneParamsCB, removalZones.Count, Marshal.SizeOf(typeof(ZoneParams)));
  293. EnviroHelper.CreateBuffer(ref clearZoneCB, 1, 4);
  294. }
  295. private void ReleaseZoneBuffers()
  296. {
  297. if(removeZoneParamsCB != null)
  298. EnviroHelper.ReleaseComputeBuffer(ref removeZoneParamsCB);
  299. if(clearZoneCB != null)
  300. EnviroHelper.ReleaseComputeBuffer(ref clearZoneCB);
  301. }
  302. IEnumerator FirstFrame ()
  303. {
  304. yield return 0;
  305. notFirstFrame = true;
  306. }
  307. ///HDRP Section
  308. public void CreateHDRPVolume ()
  309. {
  310. #if ENVIRO_HDRP
  311. if(volumeHDRP == null)
  312. {
  313. GameObject volume = new GameObject();
  314. volume.name = "Enviro Sky and Fog Volume";
  315. volume.transform.SetParent(transform);
  316. volume.transform.localPosition = Vector3.zero;
  317. volumeHDRP = volume.AddComponent<UnityEngine.Rendering.Volume>();
  318. volumeHDRP.sharedProfile = EnviroHelper.GetDefaultSkyAndFogProfile("Enviro HDRP Sky and Fog Volume");
  319. volumeHDRP.priority = 1;
  320. }
  321. #endif
  322. }
  323. private void CheckCameraSetup ()
  324. {
  325. //Auto assign camera with the camera tag when camera not set.
  326. if(Camera == null)
  327. {
  328. for (int i = 0; i < Camera.allCameras.Length; i++)
  329. {
  330. if (Camera.allCameras[i].tag == CameraTag)
  331. {
  332. Camera = Camera.allCameras[i];
  333. #if !ENVIRO_HDRP || !ENVIRO_URP
  334. AddCameraComponents();
  335. #endif
  336. }
  337. }
  338. }
  339. }
  340. //Updates manager variables.
  341. private void UpdateManager ()
  342. {
  343. if(Application.isPlaying)
  344. CheckCameraSetup ();
  345. if(solarTime > 0.45f)
  346. {
  347. if(isNight == true)
  348. NotifyIsDay();
  349. isNight = false;
  350. }
  351. else
  352. {
  353. if(isNight == false)
  354. NotifyIsNight();
  355. isNight = true;
  356. }
  357. //Effect Removal Zones:
  358. if(Fog != null || Effects != null)
  359. {
  360. CreateZoneBuffers();
  361. SetupZoneBuffers();
  362. }
  363. }
  364. }
  365. }