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.

380 lines
15 KiB

5 years ago
  1. #if GAIA_PRESENT && UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine.Rendering;
  6. namespace Gaia.GX.FogVolume3
  7. {
  8. /// <summary>
  9. /// Fog Volume 3 creator for Gaia.
  10. /// </summary>
  11. public class FogVolumeGaiaIntegration : MonoBehaviour
  12. {
  13. #region Generic informational methods
  14. /// <summary>
  15. /// Returns the publisher name if provided.
  16. /// This will override the publisher name in the namespace ie Gaia.GX.PublisherName
  17. /// </summary>
  18. /// <returns>Publisher name</returns>
  19. public static string GetPublisherName()
  20. {
  21. return "David Miranda";
  22. }
  23. /// <summary>
  24. /// Returns the package name if provided
  25. /// This will override the package name in the class name ie public class PackageName.
  26. /// </summary>
  27. /// <returns>Package name</returns>
  28. public static string GetPackageName()
  29. {
  30. return "Fog Volume 3";
  31. }
  32. #endregion
  33. #region Methods exposed by Gaia as buttons must be prefixed with GX_
  34. public static void GX_About()
  35. {
  36. EditorUtility.DisplayDialog("About Fog Volume 3", "This integration adds Fog Volume 3 to your scene. After adding your Fog Volume components adjust their Y positions to better suit your scene. Also pay attention to the Fog Volume settings on your main camera. For example changing falloff will reduce the blur applied to distant clouds.", "OK");
  37. }
  38. public static void GX_Setup_AddGroundFog()
  39. {
  40. //Pick colour of main light
  41. GameObject goLight = GameObject.Find("Directional Light");
  42. Light mainLight = null;
  43. if (goLight != null)
  44. {
  45. mainLight = goLight.GetComponent<Light>();
  46. }
  47. else
  48. {
  49. mainLight = GameObject.FindObjectOfType<Light>();
  50. }
  51. Color mainLightColor = Color.white;
  52. if (mainLight != null)
  53. {
  54. mainLightColor = mainLight.color;
  55. }
  56. //First make sure its not already in scene
  57. GameObject fvGroundFog = GameObject.Find("Fog Volume [Ground Fog]");
  58. if (fvGroundFog == null)
  59. {
  60. fvGroundFog = new GameObject();
  61. fvGroundFog.name = "Fog Volume [Ground Fog]";
  62. fvGroundFog.AddComponent<MeshRenderer>();
  63. fvGroundFog.AddComponent<FogVolume>();
  64. fvGroundFog.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  65. fvGroundFog.GetComponent<Renderer>().receiveShadows = false;
  66. fvGroundFog.GetComponent<Renderer>().reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  67. fvGroundFog.GetComponent<Renderer>().lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  68. }
  69. //Adjust its position and size
  70. FogVolume fvVolume = fvGroundFog.GetComponent<FogVolume>();
  71. if (fvVolume != null)
  72. {
  73. GaiaSceneInfo info = GaiaSceneInfo.GetSceneInfo();
  74. Debug.Log(info.m_seaLevel);
  75. fvVolume.transform.position = new Vector3(info.m_sceneBounds.center.x, info.m_seaLevel + 0.01f + (info.m_sceneBounds.extents.y / 4f), info.m_sceneBounds.center.z );// info.m_sceneBounds.center;
  76. fvVolume.fogVolumeScale = new Vector3(info.m_sceneBounds.size.x * 3, info.m_sceneBounds.extents.y / 2f, info.m_sceneBounds.size.z * 3);
  77. //And adjust camera far clip as well
  78. float maxClip = Math.Max(info.m_sceneBounds.size.x, info.m_sceneBounds.size.z) * 3f;
  79. Camera mainCamera = Camera.main;
  80. if (mainCamera != null)
  81. {
  82. if (mainCamera.farClipPlane < maxClip)
  83. {
  84. mainCamera.farClipPlane = maxClip + 200f;
  85. }
  86. }
  87. fvVolume.FogMainColor = new Color(53f/255f, 76f/255f, 114f/255f);
  88. //fvVolume.Visibility = maxClip;
  89. fvVolume.Visibility = 800f;
  90. fvVolume.EnableInscattering = true;
  91. fvVolume.InscatteringColor = Color.Lerp(mainLightColor, Color.black, 0.8f);
  92. fvVolume.VolumeFogInscatteringAnisotropy = 0.59f;
  93. fvVolume.InscatteringIntensity = 0.07f;
  94. fvVolume.InscatteringStartDistance = 5f;
  95. fvVolume.InscatteringTransitionWideness = 300f;
  96. //Other
  97. fvVolume.DrawOrder = 3;
  98. fvVolume._PushAlpha = 1.0025f;
  99. fvVolume._ztest = CompareFunction.Always;
  100. }
  101. }
  102. public static void GX_Setup_AddClouds()
  103. {
  104. //Pick colour of main light
  105. Color mainLightColor = Color.white;
  106. GameObject goLight = GameObject.Find("Directional Light");
  107. Light mainLight;
  108. if (goLight != null)
  109. {
  110. mainLight = goLight.GetComponent<Light>();
  111. }
  112. else
  113. {
  114. mainLight = GameObject.FindObjectOfType<Light>();
  115. }
  116. if (mainLight != null)
  117. {
  118. mainLightColor = mainLight.color;
  119. }
  120. //Get the main camera
  121. Camera mainCamera = Camera.main;
  122. //First make sure its not already in scene - if it isnt then add it
  123. FogVolume fvVolume;
  124. GameObject goClouds = GameObject.Find("Fog Volume [Clouds]");
  125. if (goClouds == null)
  126. {
  127. goClouds = new GameObject();
  128. goClouds.name = "Fog Volume [Clouds]";
  129. goClouds.AddComponent<MeshRenderer>();
  130. fvVolume = goClouds.AddComponent<FogVolume>();
  131. goClouds.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  132. goClouds.GetComponent<Renderer>().receiveShadows = false;
  133. goClouds.GetComponent<Renderer>().reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  134. goClouds.GetComponent<Renderer>().lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  135. //Create the horizon
  136. GameObject goHorizon = GameObject.CreatePrimitive(PrimitiveType.Plane);
  137. goHorizon.name = "Horizon";
  138. goHorizon.transform.parent = goClouds.transform;
  139. goHorizon.transform.localPosition = new Vector3(0f, -79f, 0f);
  140. goHorizon.GetComponent<MeshRenderer>().enabled = false;
  141. goHorizon.GetComponent<MeshCollider>().enabled = false;
  142. //Create the priority script
  143. FogVolumePriority fvPriority = goClouds.AddComponent<FogVolumePriority>();
  144. fvPriority.GameCamera = mainCamera;
  145. fvPriority.FogOrderCameraAbove = 4;
  146. fvPriority.FogOrderCameraBelow = -1;
  147. fvPriority.thisFog = fvVolume;
  148. fvPriority.Horizon = goHorizon;
  149. }
  150. //Adjust its position and size
  151. fvVolume = goClouds.GetComponent<FogVolume>();
  152. if (fvVolume != null)
  153. {
  154. GaiaSceneInfo info = GaiaSceneInfo.GetSceneInfo();
  155. //Location and scale
  156. fvVolume.transform.position = new Vector3(info.m_sceneBounds.center.x, info.m_seaLevel + 200f, info.m_sceneBounds.center.z);// info.m_sceneBounds.center;
  157. fvVolume.fogVolumeScale = new Vector3(info.m_sceneBounds.size.x * 3, 100f, info.m_sceneBounds.size.z * 3);
  158. //Camera far clip
  159. float maxClip = Math.Max(info.m_sceneBounds.size.x, info.m_sceneBounds.size.z) * 3f;
  160. if (mainCamera != null)
  161. {
  162. if (mainCamera.farClipPlane < maxClip)
  163. {
  164. mainCamera.farClipPlane = maxClip + 200f;
  165. }
  166. }
  167. //Fog type and blend mode
  168. fvVolume._FogType = FogVolume.FogType.Textured;
  169. fvVolume._BlendMode = FogVolumeRenderer.BlendMode.PremultipliedTransparency;
  170. //Lighting
  171. fvVolume._AmbientColor = Color.Lerp(mainLightColor, Color.black, 0.1f);
  172. fvVolume.useHeightGradient = true;
  173. fvVolume.Absorption = 0.8f;
  174. fvVolume.HeightAbsorption = 0.185f;
  175. fvVolume.bAbsorption = true;
  176. fvVolume.EnableInscattering = true;
  177. fvVolume.InscatteringColor = mainLightColor;
  178. fvVolume.InscatteringShape = 0.05f;
  179. fvVolume.InscatteringIntensity = 0.882f;
  180. fvVolume.InscatteringStartDistance = 0f;
  181. fvVolume.InscatteringTransitionWideness = 1f;
  182. fvVolume._DirectionalLighting = true;
  183. fvVolume.LightExtinctionColor = Color.Lerp(mainLightColor, Color.black, 0.8f);
  184. fvVolume._DirectionalLightingDistance = 0.0008f;
  185. fvVolume.DirectLightingShadowDensity = 6f;
  186. fvVolume.DirectLightingShadowSteps = 1;
  187. //Renderer
  188. fvVolume.NoiseIntensity = 1f;
  189. fvVolume.SceneCollision = false; //Faster i suppose ?
  190. fvVolume.Iterations = 500;
  191. fvVolume.IterationStep = 100;
  192. fvVolume._OptimizationFactor = 0.0000005f;
  193. fvVolume.GradMin = 0.19f;
  194. fvVolume.GradMax = 0.06f;
  195. fvVolume.GradMin2 = -0.25f;
  196. fvVolume.GradMax2 = 0.21f;
  197. //Noise
  198. fvVolume.EnableNoise = true;
  199. fvVolume._3DNoiseScale = 0.15f;
  200. fvVolume.Speed = new Vector4(0.49f, 0f, 0f, 0f);
  201. fvVolume.Vortex = 0.47f;
  202. fvVolume.RotationSpeed = 0f;
  203. fvVolume.rotation = 324f;
  204. fvVolume._VortexAxis = FogVolume.VortexAxis.Y;
  205. fvVolume.Coverage = 2.44f;
  206. fvVolume.NoiseContrast = 12.9f;
  207. fvVolume.NoiseDensity = 0.2f;
  208. fvVolume.Octaves = 3;
  209. fvVolume.BaseTiling = 150f;
  210. fvVolume._BaseRelativeSpeed = 0.85f;
  211. fvVolume.DetailTiling = 285.3f;
  212. fvVolume._DetailRelativeSpeed = 16.6f;
  213. fvVolume.DetailDistance = 5000f;
  214. fvVolume._NoiseDetailRange = 0.337f;
  215. fvVolume._DetailMaskingThreshold = 8f;
  216. fvVolume._Curl = 0.364f;
  217. //Other
  218. fvVolume.DrawOrder = 4;
  219. fvVolume._ztest = CompareFunction.LessEqual;
  220. fvVolume.CreateSurrogate = true;
  221. }
  222. }
  223. public static void GX_Setup_AddPostEffects()
  224. {
  225. //Update renderer settings to dampen things down a bit for newcomers
  226. FogVolumeRenderer fvRenderer = GameObject.FindObjectOfType<FogVolumeRenderer>();
  227. if (fvRenderer != null)
  228. {
  229. fvRenderer._Downsample = 3;
  230. fvRenderer._BlendMode = FogVolumeRenderer.BlendMode.PremultipliedTransparency;
  231. fvRenderer.GenerateDepth = false;
  232. }
  233. //Add screen if missing
  234. FogVolumeScreen fvScreen = GameObject.FindObjectOfType<FogVolumeScreen>();
  235. if (fvScreen == null && Camera.main != null)
  236. {
  237. fvScreen = Camera.main.gameObject.AddComponent<FogVolumeScreen>();
  238. fvScreen.Downsample = 3;
  239. fvScreen.iterations = 3;
  240. fvScreen.blurSpread = 0.2f;
  241. }
  242. }
  243. public static void GX_Quality_Low()
  244. {
  245. //What about ground fog and cloud settings ?
  246. GameObject goGroundFog = GameObject.Find("Fog Volume [Ground Fog]");
  247. if (goGroundFog != null)
  248. {
  249. FogVolume fvGroundFog = goGroundFog.GetComponent<FogVolume>();
  250. if (fvGroundFog != null)
  251. {
  252. //Make adjustments
  253. }
  254. }
  255. GameObject goClouds = GameObject.Find("Fog Volume [Clouds]");
  256. if (goClouds != null)
  257. {
  258. FogVolume fvClouds = goClouds.GetComponent<FogVolume>();
  259. if (fvClouds != null)
  260. {
  261. //Make adjustments
  262. }
  263. }
  264. //Update renderer settings
  265. FogVolumeRenderer fvRenderer = GameObject.FindObjectOfType<FogVolumeRenderer>();
  266. if (fvRenderer != null)
  267. {
  268. fvRenderer._Downsample = 8;
  269. fvRenderer._BlendMode = FogVolumeRenderer.BlendMode.PremultipliedTransparency;
  270. }
  271. //Update screen settings
  272. FogVolumeScreen fvScreen = GameObject.FindObjectOfType<FogVolumeScreen>();
  273. if (fvScreen != null)
  274. {
  275. }
  276. }
  277. public static void GX_Quality_Medium()
  278. {
  279. //What about ground fog and cloud settings ?
  280. //Update renderer settings
  281. FogVolumeRenderer fvRenderer = GameObject.FindObjectOfType<FogVolumeRenderer>();
  282. if (fvRenderer != null)
  283. {
  284. fvRenderer._Downsample = 4;
  285. }
  286. //Update screen settings
  287. FogVolumeScreen fvScreen = GameObject.FindObjectOfType<FogVolumeScreen>();
  288. if (fvScreen != null)
  289. {
  290. }
  291. }
  292. public static void GX_Quality_High()
  293. {
  294. //What about ground fog and cloud settings ?
  295. //Update renderer settings
  296. FogVolumeRenderer fvRenderer = GameObject.FindObjectOfType<FogVolumeRenderer>();
  297. if (fvRenderer != null)
  298. {
  299. fvRenderer._Downsample = 2;
  300. }
  301. //Update screen settings
  302. FogVolumeScreen fvScreen = GameObject.FindObjectOfType<FogVolumeScreen>();
  303. if (fvScreen != null)
  304. {
  305. }
  306. }
  307. public static void GX_Quality_Epic()
  308. {
  309. //What about ground fog and cloud settings ?
  310. //Update renderer settings
  311. FogVolumeRenderer fvRenderer = GameObject.FindObjectOfType<FogVolumeRenderer>();
  312. if (fvRenderer != null)
  313. {
  314. fvRenderer._Downsample = 0;
  315. }
  316. //Update screen settings
  317. FogVolumeScreen fvScreen = GameObject.FindObjectOfType<FogVolumeScreen>();
  318. if (fvScreen != null)
  319. {
  320. }
  321. }
  322. #endregion
  323. }
  324. }
  325. #endif