VolumetricFogProfileInspector.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. namespace VolumetricFogAndMist {
  9. [CanEditMultipleObjects]
  10. [CustomEditor (typeof(VolumetricFogProfile))]
  11. public class VolumetricFogProfileInspector : Editor {
  12. Texture2D _blackTexture;
  13. static GUIStyle blackStyle;
  14. static GUIStyle disabledStyle;
  15. Color titleColor;
  16. SerializedProperty lightingModel, sunCopyColor, density, noiseStrength, height, baselineHeight, distance, maxFogLength, maxFogLengthFallOff, distanceFallOff;
  17. SerializedProperty baselineRelativeToCamera, baselineRelativeToCameraDelay;
  18. SerializedProperty noiseScale, noiseSparse, alpha, color, specularColor, specularThreshold, specularIntensity, lightDirection, lightIntensity, lightColor;
  19. SerializedProperty speed, windDirection, useRealTime;
  20. SerializedProperty turbulenceStrength, useXYPlane;
  21. SerializedProperty skyColor, skyHaze, skySpeed, skyNoiseStrength, skyAlpha;
  22. SerializedProperty stepping, steppingNear, dithering, ditherStrength;
  23. void OnEnable () {
  24. Color backColor = EditorGUIUtility.isProSkin ? new Color (0.18f, 0.18f, 0.18f) : new Color (0.825f, 0.825f, 0.825f);
  25. titleColor = EditorGUIUtility.isProSkin ? new Color (0.52f, 0.66f, 0.9f) : new Color (0.12f, 0.16f, 0.4f);
  26. _blackTexture = MakeTex (4, 4, backColor);
  27. _blackTexture.hideFlags = HideFlags.DontSave;
  28. blackStyle = new GUIStyle ();
  29. blackStyle.normal.background = _blackTexture;
  30. lightingModel = serializedObject.FindProperty ("lightingModel");
  31. sunCopyColor = serializedObject.FindProperty ("sunCopyColor");
  32. density = serializedObject.FindProperty ("density");
  33. noiseStrength = serializedObject.FindProperty ("noiseStrength");
  34. height = serializedObject.FindProperty ("height");
  35. baselineHeight = serializedObject.FindProperty ("baselineHeight");
  36. distance = serializedObject.FindProperty ("distance");
  37. maxFogLength = serializedObject.FindProperty ("maxFogLength");
  38. maxFogLengthFallOff = serializedObject.FindProperty ("maxFogLengthFallOff");
  39. distanceFallOff = serializedObject.FindProperty ("distanceFallOff");
  40. baselineRelativeToCamera = serializedObject.FindProperty ("baselineRelativeToCamera");
  41. baselineRelativeToCameraDelay = serializedObject.FindProperty ("baselineRelativeToCameraDelay");
  42. noiseScale = serializedObject.FindProperty ("noiseScale");
  43. noiseSparse = serializedObject.FindProperty ("noiseSparse");
  44. alpha = serializedObject.FindProperty ("alpha");
  45. color = serializedObject.FindProperty ("color");
  46. specularColor = serializedObject.FindProperty ("specularColor");
  47. specularThreshold = serializedObject.FindProperty ("specularThreshold");
  48. specularIntensity = serializedObject.FindProperty ("specularIntensity");
  49. lightDirection = serializedObject.FindProperty ("lightDirection");
  50. lightIntensity = serializedObject.FindProperty ("lightIntensity");
  51. lightColor = serializedObject.FindProperty ("lightColor");
  52. speed = serializedObject.FindProperty ("speed");
  53. windDirection = serializedObject.FindProperty ("windDirection");
  54. useRealTime = serializedObject.FindProperty ("useRealTime");
  55. turbulenceStrength = serializedObject.FindProperty ("turbulenceStrength");
  56. useXYPlane = serializedObject.FindProperty ("useXYPlane");
  57. skyColor = serializedObject.FindProperty ("skyColor");
  58. skyHaze = serializedObject.FindProperty ("skyHaze");
  59. skySpeed = serializedObject.FindProperty ("skySpeed");
  60. skyNoiseStrength = serializedObject.FindProperty ("skyNoiseStrength");
  61. skyAlpha = serializedObject.FindProperty ("skyAlpha");
  62. stepping = serializedObject.FindProperty ("stepping");
  63. steppingNear = serializedObject.FindProperty ("steppingNear");
  64. dithering = serializedObject.FindProperty ("dithering");
  65. ditherStrength = serializedObject.FindProperty ("ditherStrength");
  66. }
  67. public override void OnInspectorGUI () {
  68. serializedObject.Update ();
  69. EditorGUILayout.Separator ();
  70. EditorGUILayout.BeginVertical (blackStyle);
  71. EditorGUILayout.BeginHorizontal ();
  72. DrawTitleLabel ("Volumetric Fog Profile");
  73. if (GUILayout.Button ("Help", GUILayout.Width (50)))
  74. EditorUtility.DisplayDialog ("Help", "Move the mouse over each label to show a description of the parameter.", "Ok");
  75. EditorGUILayout.EndHorizontal ();
  76. EditorGUILayout.Separator ();
  77. EditorGUILayout.EndVertical ();
  78. EditorGUILayout.Separator ();
  79. EditorGUILayout.BeginVertical (blackStyle);
  80. DrawTitleLabel ("Geometry");
  81. EditorGUILayout.PropertyField (distance, new GUIContent ("Distance", "Distance in meters from the camera at which the fog starts. It works with Distance FallOff."));
  82. EditorGUILayout.PropertyField (distanceFallOff, new GUIContent (" Fall Off", "When you set a value to Distance > 0, this parameter defines the gradient of the fog to the camera. The higher the value, the shorter the gradient."));
  83. EditorGUILayout.PropertyField (maxFogLength, new GUIContent ("Max. Distance", "Maximum distance from camera at which the fog is rendered. Decrease this value to improve performance."));
  84. EditorGUILayout.PropertyField (maxFogLengthFallOff, new GUIContent (" FallOff", "Blends far range with background."));
  85. EditorGUILayout.PropertyField (height, new GUIContent ("Height", "Maximum height of the fog in meters."));
  86. EditorGUILayout.PropertyField (useXYPlane, new GUIContent ("Use XY Plane", "Switches between normal mode to XY mode."));
  87. EditorGUILayout.BeginHorizontal ();
  88. if (useXYPlane.boolValue) {
  89. EditorGUILayout.PropertyField (baselineHeight, new GUIContent ("Base Z", "Starting Z of the fog in meters."));
  90. } else {
  91. EditorGUILayout.PropertyField (baselineHeight, new GUIContent ("Base Height", "Starting height of the fog in meters. You can set this value above Camera position. Try it!"));
  92. }
  93. EditorGUILayout.EndHorizontal ();
  94. if (useXYPlane.boolValue) {
  95. GUI.enabled = false;
  96. }
  97. EditorGUILayout.PropertyField (baselineRelativeToCamera, new GUIContent ("Relative To Camera", "If set to true, the base height will be added to the camera height. This is useful for cloud styles so they always stay over your head!"));
  98. if (baselineRelativeToCamera.boolValue) {
  99. EditorGUILayout.PropertyField (baselineRelativeToCameraDelay, new GUIContent ("Delay", "Speed factor for transitioning to new camera heights."));
  100. }
  101. GUI.enabled = true;
  102. EditorGUILayout.Separator ();
  103. EditorGUILayout.EndVertical ();
  104. EditorGUILayout.Separator ();
  105. EditorGUILayout.BeginVertical (blackStyle);
  106. DrawTitleLabel ("Appearance");
  107. EditorGUILayout.PropertyField (density, new GUIContent ("Density", "General density of the fog. Higher density fog means darker fog as well."));
  108. EditorGUILayout.PropertyField (noiseStrength, new GUIContent ("Noise Strength", "Randomness of the fog formation. 0 means uniform fog whereas a value towards 1 will make areas of different densities and heights."));
  109. EditorGUILayout.PropertyField (noiseScale, new GUIContent (" Scale", "Increasing this value will expand the size of the noise."));
  110. EditorGUILayout.PropertyField (noiseSparse, new GUIContent (" Sparse", "Increase to make noise sparser."));
  111. EditorGUILayout.PropertyField (alpha, new GUIContent ("Alpha", "Transparency for the fog. You may want to reduce this value if you experiment issues with billboards."));
  112. EditorGUILayout.PropertyField (color, new GUIContent ("Albedo", "Base color of the fog."));
  113. EditorGUILayout.PropertyField (specularColor, new GUIContent ("Specular Color", "This is the color reflected by the fog under direct light exposure (see Light parameters)"));
  114. EditorGUILayout.PropertyField (specularThreshold, new GUIContent ("Specular Threshold", "Area of the fog subject to light reflectancy"));
  115. EditorGUILayout.PropertyField (specularIntensity, new GUIContent ("Specular Intensity", "The intensity of the reflected light."));
  116. EditorGUILayout.PropertyField (lightDirection, new GUIContent ("Light Direction", "The normalized direction of a simulated directional light."));
  117. EditorGUILayout.PropertyField (lightingModel, new GUIContent ("Lighting Model", "The lighting model used to calculate fog color. 'Legacy' is provided for previous version compatibility. 'Natural' uses ambient + light color. 'Single Light' excludes ambient color."));
  118. EditorGUILayout.PropertyField (lightIntensity, new GUIContent ("Light Intensity", "Intensity of the simulated directional light."));
  119. EditorGUILayout.PropertyField (lightColor, new GUIContent ("Light Color", "Color of the simulated direcional light."));
  120. EditorGUILayout.PropertyField (sunCopyColor, new GUIContent ("Copy Sun Color", "Always use Sun light color. Disable this property to allow choosing a custom light color."));
  121. EditorGUILayout.EndVertical ();
  122. EditorGUILayout.Separator ();
  123. EditorGUILayout.BeginVertical (blackStyle);
  124. DrawTitleLabel ("Animation");
  125. EditorGUILayout.BeginHorizontal ();
  126. EditorGUILayout.PropertyField (speed, new GUIContent ("Wind Speed", "Speed factor for the simulated wind effect over the fog."));
  127. if (GUILayout.Button ("Stop", GUILayout.Width (60))) {
  128. speed.floatValue = 0;
  129. }
  130. EditorGUILayout.EndHorizontal ();
  131. EditorGUILayout.PropertyField (windDirection, new GUIContent ("Wind Direction", "Normalized direcional vector for the wind effect."));
  132. EditorGUILayout.PropertyField (turbulenceStrength, new GUIContent ("Turbulence", "Turbulence strength. Set to zero to deactivate. Turbulence adds a render pass to compute fog animation."));
  133. EditorGUILayout.PropertyField (useRealTime, new GUIContent ("Use Real Time", "Uses real elapsed time since last fog rendering instead of Time.deltaTime (elapsed time since last frame) to ensure continuous fog animation irrespective of camera enable state. For example, if you disable the camera and 'Use Real Time' is enabled, when you enable the camera again, the fog animation wil compute the total elapsed time so it shows as if fog continues animating while camera was disabled. If set to false, fog animation will use Time.deltaTime (elapsed time since last frame) which will cause fog to resume previous state."));
  134. EditorGUILayout.EndVertical ();
  135. EditorGUILayout.Separator ();
  136. EditorGUILayout.BeginVertical (blackStyle);
  137. DrawTitleLabel ("Sky");
  138. if (useXYPlane.boolValue) {
  139. EditorGUILayout.HelpBox ("Sky haze is disabled when using XY plane.", MessageType.Info);
  140. }
  141. EditorGUILayout.PropertyField (skyHaze, new GUIContent ("Haze", "Height of the sky haze in meters. Reduce this or alpha to 0 to disable sky haze."));
  142. EditorGUILayout.PropertyField (skyColor, new GUIContent ("Color", "Sky haze color."));
  143. EditorGUILayout.PropertyField (skySpeed, new GUIContent ("Speed", "Speed of the haze animation."));
  144. EditorGUILayout.PropertyField (skyNoiseStrength, new GUIContent ("Noise Strength", "Amount of noise for the sky haze."));
  145. EditorGUILayout.PropertyField (skyAlpha, new GUIContent ("Alpha", "Transparency of the sky haze. Reduce this or Haze value to 0 to disable sky haze."));
  146. EditorGUILayout.EndVertical ();
  147. EditorGUILayout.Separator ();
  148. EditorGUILayout.BeginVertical (blackStyle);
  149. DrawTitleLabel ("Rendering");
  150. EditorGUILayout.PropertyField (stepping, new GUIContent ("Stepping", "Multiplier to the ray-marching algorithm. Values between 8-12 are good. Increasing the stepping will produce more accurate and better quality fog but performance will be reduced. The less the density of the fog the lower you can set this value."));
  151. EditorGUILayout.PropertyField (steppingNear, new GUIContent ("Stepping Near", "Works with Stepping parameter but applies only to short distances from camera. Lowering this value can help to reduce banding effect (performance can be reduced as well)."));
  152. EditorGUILayout.PropertyField (dithering, new GUIContent ("Dithering", "Blends final fog color with a pattern to reduce banding artifacts. Use the slider to choose the intensity of dither."));
  153. if (dithering.boolValue) {
  154. EditorGUILayout.PropertyField (ditherStrength, new GUIContent (" Strength", "Dither strength."));
  155. }
  156. EditorGUILayout.EndVertical ();
  157. EditorGUILayout.Separator ();
  158. if (serializedObject.ApplyModifiedProperties () || (Event.current.type == EventType.ExecuteCommand &&
  159. Event.current.commandName == "UndoRedoPerformed")) {
  160. // Triggers profile reload on all Volumetric Fog scripts
  161. VolumetricFog[] fogs = FindObjectsOfType<VolumetricFog> ();
  162. for (int t = 0; t < targets.Length; t++) {
  163. VolumetricFogProfile profile = (VolumetricFogProfile)targets [t];
  164. for (int k = 0; k < fogs.Length; k++) {
  165. if (fogs [k] != null && fogs [k].profile == profile) {
  166. profile.Load (fogs [k]);
  167. }
  168. }
  169. }
  170. EditorUtility.SetDirty (target);
  171. }
  172. }
  173. Texture2D MakeTex (int width, int height, Color col) {
  174. Color[] pix = new Color[width * height];
  175. for (int i = 0; i < pix.Length; i++)
  176. pix [i] = col;
  177. TextureFormat tf = SystemInfo.SupportsTextureFormat (TextureFormat.RGBAFloat) ? TextureFormat.RGBAFloat : TextureFormat.RGBA32;
  178. Texture2D result = new Texture2D (width, height, tf, false);
  179. result.SetPixels (pix);
  180. result.Apply ();
  181. return result;
  182. }
  183. GUIStyle titleLabelStyle;
  184. void DrawTitleLabel (string s) {
  185. if (titleLabelStyle == null) {
  186. titleLabelStyle = new GUIStyle (GUI.skin.label);
  187. }
  188. titleLabelStyle.normal.textColor = titleColor;
  189. titleLabelStyle.fontStyle = FontStyle.Bold;
  190. GUILayout.Label (s, titleLabelStyle);
  191. }
  192. }
  193. }