You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.0 KiB

5 years ago
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class FogVolumeData : MonoBehaviour
  5. {
  6. [SerializeField]
  7. bool _ForceNoRenderer;
  8. public bool ForceNoRenderer
  9. {
  10. get { return _ForceNoRenderer; }
  11. set
  12. {
  13. if (_ForceNoRenderer != value)
  14. {
  15. _ForceNoRenderer = value;
  16. ToggleFogVolumeRenderers();
  17. }
  18. }
  19. }
  20. [SerializeField]
  21. Camera _GameCamera;
  22. public Camera GameCamera
  23. {
  24. get { return _GameCamera; }
  25. set
  26. {
  27. if (_GameCamera != value)
  28. {
  29. _GameCamera = value;
  30. RefreshCamera();
  31. }
  32. }
  33. }
  34. public void setDownsample(int val)
  35. {
  36. if (_GameCamera.GetComponent<FogVolumeRenderer>())
  37. _GameCamera.GetComponent<FogVolumeRenderer>()._Downsample = val;
  38. }
  39. void RefreshCamera()
  40. {
  41. //refresh all fog folumes assigned camera
  42. //print("Refresh");
  43. FindFogVolumes();
  44. foreach (FogVolume _FogVolumes in SceneFogVolumes)
  45. {
  46. _FogVolumes.AssignCamera();
  47. }
  48. ToggleFogVolumeRenderers();
  49. }
  50. [SerializeField]
  51. List<Camera> FoundCameras;
  52. void OnEnable()
  53. {
  54. Initialize();
  55. }
  56. void Initialize()
  57. {
  58. if (FoundCameras == null)
  59. FoundCameras = new List<Camera>();
  60. FindCamera();
  61. RefreshCamera();
  62. if (FoundCameras.Count == 0)
  63. Debug.Log("Definetly, no camera available for Fog Volume");
  64. }
  65. [SerializeField]
  66. FogVolume[] SceneFogVolumes;
  67. public void FindFogVolumes()
  68. {
  69. SceneFogVolumes = (FogVolume[])FindObjectsOfType(typeof(FogVolume));
  70. }
  71. void Update()
  72. {
  73. if (GameCamera == null)
  74. {
  75. Debug.Log("No Camera available for Fog Volume. Trying to find another one");
  76. Initialize();
  77. }
  78. #if UNITY_EDITOR
  79. for (int i = 0; i < SceneFogVolumes.Length; i++)
  80. {
  81. FogVolume SlotFogVolume = SceneFogVolumes[i];
  82. if(SlotFogVolume==null)
  83. {
  84. //reset and rebuild
  85. SceneFogVolumes = null;
  86. FindFogVolumes();
  87. }
  88. }
  89. if (SceneFogVolumes.Length == 0)
  90. DestroyImmediate(gameObject);
  91. #endif
  92. }
  93. void ToggleFogVolumeRenderers()
  94. {
  95. if (FoundCameras != null)
  96. for (int i = 0; i < FoundCameras.Count; i++)
  97. {
  98. if (FoundCameras[i] != _GameCamera)
  99. {
  100. if (FoundCameras[i].GetComponent<FogVolumeRenderer>())
  101. FoundCameras[i].GetComponent<FogVolumeRenderer>().enabled = false;
  102. }
  103. else if (FoundCameras[i].GetComponent<FogVolumeRenderer>() &&
  104. !_ForceNoRenderer)
  105. {
  106. FoundCameras[i].GetComponent<FogVolumeRenderer>().enabled = true;
  107. }
  108. else
  109. {
  110. var FVRenderer = FoundCameras[i].GetComponent<FogVolumeRenderer>();
  111. if (FVRenderer == null)
  112. {
  113. if (ForceNoRenderer) { continue; }
  114. FVRenderer = FoundCameras[i].gameObject.AddComponent<FogVolumeRenderer>();
  115. }
  116. if(ForceNoRenderer)
  117. {
  118. FVRenderer.enabled = false;
  119. }
  120. }
  121. }
  122. }
  123. public void FindCamera()
  124. {
  125. //We will try to assign the typical MainCamera first. This search will be performed only when the field is null
  126. //This is just an initial attempt on assigning any camera available when the field 'Camera' is null.
  127. //We will be able to select any other camera later
  128. if (FoundCameras != null && FoundCameras.Count > 0) FoundCameras.Clear();
  129. //Find all cameras in scene and store
  130. Camera[] CamerasFound = (Camera[])FindObjectsOfType(typeof(Camera));
  131. for (int i = 0; i < CamerasFound.Length; i++)
  132. if (
  133. !CamerasFound[i].name.Contains("FogVolumeCamera")
  134. &&
  135. !CamerasFound[i].name.Contains("Shadow Camera")
  136. &&
  137. CamerasFound[i].gameObject.hideFlags == HideFlags.None)//not you!
  138. FoundCameras.Add(CamerasFound[i]);
  139. if (GameCamera == null)
  140. GameCamera = Camera.main;
  141. //No MainCamera? Try to find any!
  142. if (GameCamera == null)
  143. {
  144. foreach (Camera FoundCamera in FoundCameras)
  145. {
  146. // Many effects may use hidden cameras, so let's filter a little bit until we get something valid
  147. if (FoundCamera.isActiveAndEnabled)
  148. if (FoundCamera.gameObject.activeInHierarchy)
  149. if (FoundCamera.gameObject.hideFlags == HideFlags.None)
  150. {
  151. GameCamera = FoundCamera;
  152. break;
  153. }
  154. }
  155. }
  156. if (GameCamera != null)
  157. {
  158. // Debug.Log("Fog Volume has been assigned with camera: " + GameCamera);
  159. //if (FindObjectOfType<FogVolumeCamera>())
  160. // FindObjectOfType<FogVolumeCamera>().SceneCamera = GameCamera;
  161. //NOTE: This makes sure we have a depth texture which will be either free (deferred, etc) or internally generated through a replacement shader
  162. //Now, objects must be able to do shadow casting. If you’re using surface shaders, add the "addshadow" directive
  163. //only “opaque” objects (that which have their materials and shaders setup to use render queue <= 2500) are rendered into the depth texture.
  164. //GameCamera.depthTextureMode = DepthTextureMode.Depth;
  165. }
  166. }
  167. public Camera GetFogVolumeCamera
  168. {
  169. get
  170. {
  171. return GameCamera;
  172. }
  173. }
  174. void OnDisable()
  175. {
  176. FoundCameras.Clear();
  177. SceneFogVolumes = null;
  178. }
  179. }