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.

161 lines
8.1 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(FogVolumeDirectionalLight))]
  4. public class FogVolumeDirectionalLightEditor : Editor
  5. {
  6. FogVolumeDirectionalLight _target;
  7. void OnEnable()
  8. {
  9. _target = (FogVolumeDirectionalLight)target;
  10. }
  11. GUIContent VariableField(string VariableName, string Tooltip)
  12. {
  13. return new GUIContent(VariableName, Tooltip);
  14. }
  15. private static bool SHOW_DEBUG_Options
  16. {
  17. get { return EditorPrefs.GetBool("SHOW_FV_DIR_LIGHT_DEBUG_OptionsTab", false); }
  18. set { EditorPrefs.SetBool("SHOW_FV_DIR_LIGHT_DEBUG_OptionsTab", value); }
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. serializedObject.Update();
  23. EditorGUI.BeginChangeCheck();
  24. Undo.RecordObject(_target, "Fog volume directional light modified");
  25. GUILayout.Space(10);
  26. GUILayout.BeginVertical("box");
  27. if (_target._ProminentFogVolume == null)
  28. GUI.color = Color.red;
  29. // _target._TargetFogVolume = (FogVolume)EditorGUILayout.ObjectField("Target Fog Volume", _target._TargetFogVolume, typeof(FogVolume), true);
  30. var FogVolumes = serializedObject.FindProperty("_TargetFogVolumes");
  31. EditorGUI.indentLevel++;
  32. EditorGUILayout.PropertyField(FogVolumes, new GUIContent("Target Fog Volume"), true);
  33. EditorGUI.indentLevel--;
  34. GUI.color = Color.white;
  35. GUI.color = new Color(.9f, 1, .9f);
  36. if (GUILayout.Button("Add all Fog Volumes" /*GUILayout.Width(100)*/))
  37. {
  38. _target.AddAllFogVolumesToThisLight();
  39. _target.Refresh();
  40. _target.Render();
  41. }
  42. if (GUILayout.Button("Remove all Fog Volumes" /*GUILayout.Width(100)*/))
  43. {
  44. _target.RemoveAllFogVolumesFromThisLight();
  45. _target.Refresh();
  46. _target.Render();
  47. }
  48. GUI.color = Color.white;
  49. _target._CameraVerticalPosition = EditorGUILayout.FloatField(VariableField
  50. ("Shadow camera distance", "This is the distance from the shadow camera to the focus point. Increase it if the scene area you want to shade is not completely visible in the shadowmap"), _target._CameraVerticalPosition);
  51. _target.Size = (FogVolumeDirectionalLight.Resolution)EditorGUILayout.EnumPopup("Resolution", _target.Size);
  52. _target._Antialiasing = (FogVolumeDirectionalLight.Antialiasing)EditorGUILayout.EnumPopup("Antialiasing", _target._Antialiasing);
  53. _target._UpdateMode = (FogVolumeDirectionalLight.UpdateMode)EditorGUILayout.EnumPopup(VariableField("Update mode", "OnStart: bake shadowmap on start\nInterleaved: skip frames"), _target._UpdateMode);
  54. if (_target._UpdateMode == FogVolumeDirectionalLight.UpdateMode.Interleaved)
  55. _target.SkipFrames = EditorGUILayout.IntSlider(VariableField("Skip frames", "Instead updating per-frame, we can skip frames before updating the shadow"), _target.SkipFrames, 0, 10);
  56. else
  57. {
  58. GUI.color = new Color(.9f, 1, .9f);
  59. if (GUILayout.Button("Refresh" /*GUILayout.Width(100)*/))
  60. {
  61. _target.Refresh();
  62. _target.Render();
  63. _target.Render();
  64. }
  65. GUI.color = Color.white;
  66. }
  67. Rect LayerFieldRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(20));
  68. if (_target.ShadowCamera != null && _target.ShadowCamera.cullingMask == 0)
  69. GUI.color = Color.red;
  70. _target.LayersToRender = FogVolumeUtilities.EditorExtension.DrawLayerMaskField(LayerFieldRect, _target.LayersToRender, new GUIContent("Layers to render"));
  71. GUI.color = Color.white;
  72. _target._ScaleMode = (FogVolumeDirectionalLight.ScaleMode)EditorGUILayout.EnumPopup(VariableField("Scale mode",
  73. "With VolumeX, the camera will take the volume scale.x to adjust its size\nSet it to manual to adjust this size yourself. Useful when you have a large volume and only want to have good shadows at the focus point"), _target._ScaleMode);
  74. if (_target._ScaleMode == FogVolumeDirectionalLight.ScaleMode.Manual && _target._ProminentFogVolume != null)
  75. _target.Scale = EditorGUILayout.Slider(VariableField("Zoom", "Scale the view range of the camera.\n It will use the volume size from position 0"), _target.Scale, 10, _target._ProminentFogVolume.fogVolumeScale.x);
  76. _target._FocusMode = (FogVolumeDirectionalLight.FocusMode)EditorGUILayout.EnumPopup(VariableField("Focus mode", "The camera will focus on the given position. If the volume is not too large the best option would be Volume center"), _target._FocusMode);
  77. if (_target._FocusMode == FogVolumeDirectionalLight.FocusMode.GameObject)
  78. _target._GameObjectFocus = (Transform)EditorGUILayout.ObjectField(VariableField("Focus point", "Gameobject used to focus the shadow camera"), _target._GameObjectFocus, typeof(Transform), true);
  79. _target._FogVolumeShadowMapEdgeSoftness = EditorGUILayout.Slider(VariableField("Edge softness", "Fading range for the edges of the shadowmap"),
  80. _target._FogVolumeShadowMapEdgeSoftness, .001f, 1);
  81. GUILayout.EndVertical();//end box
  82. #region Debug
  83. GUILayout.BeginVertical("box");
  84. if (GUILayout.Button("Debug options", EditorStyles.toolbarButton))
  85. SHOW_DEBUG_Options = !SHOW_DEBUG_Options;
  86. if (SHOW_DEBUG_Options)
  87. {
  88. _target.CameraVisible = EditorGUILayout.Toggle(VariableField("Show shadow map camera", "Not updating correctly until we click on the hierarchy window :/"), _target.CameraVisible);
  89. if (_target.GOShadowCamera && _target.GOShadowCamera.hideFlags == HideFlags.HideInHierarchy && _target.CameraVisible)
  90. {
  91. _target.GOShadowCamera.hideFlags = HideFlags.None;
  92. EditorApplication.RepaintHierarchyWindow();
  93. EditorApplication.DirtyHierarchyWindowSorting();
  94. }
  95. if (_target.GOShadowCamera && _target.GOShadowCamera.hideFlags == HideFlags.None && !_target.CameraVisible)
  96. {
  97. _target.GOShadowCamera.hideFlags = HideFlags.HideInHierarchy;
  98. EditorApplication.RepaintHierarchyWindow();
  99. EditorApplication.DirtyHierarchyWindowSorting();
  100. }
  101. _target.ShowMiniature = EditorGUILayout.Toggle("View depth map", _target.ShowMiniature);
  102. EditorGUI.indentLevel++;
  103. if (_target.ShowMiniature)
  104. _target.MiniaturePosition = EditorGUILayout.Vector2Field("Position", _target.MiniaturePosition);
  105. EditorGUI.indentLevel--;
  106. }
  107. EditorGUI.EndChangeCheck();
  108. GUI.color = Color.green;
  109. if (_target._ProminentFogVolume == null)
  110. EditorGUILayout.HelpBox("Add a Fog Volume now", MessageType.Info);
  111. if (_target._ProminentFogVolume != null && _target.ShadowCamera != null && _target.ShadowCamera.cullingMask == 0)
  112. EditorGUILayout.HelpBox("Set the layers used to cast shadows", MessageType.Info);
  113. GUI.color = Color.white;
  114. GUI.color = Color.red;
  115. if (_target._TargetFogVolumes != null)
  116. {
  117. foreach (FogVolume fv in _target._TargetFogVolumes)
  118. {
  119. if (fv != null)
  120. if (fv._FogType == FogVolume.FogType.Uniform)
  121. {
  122. EditorGUILayout.HelpBox(fv.name +
  123. " Is not a valid Volume. It has to be textured (must use gradient or noise)",
  124. MessageType.Info);
  125. }
  126. }
  127. }
  128. GUI.color = Color.white;
  129. GUI.color = Color.red;
  130. if (_target._FocusMode == FogVolumeDirectionalLight.FocusMode.GameObject && _target._GameObjectFocus == null)
  131. EditorGUILayout.HelpBox("Set the focus point object", MessageType.Info);
  132. GUI.color = Color.white;
  133. EditorGUILayout.HelpBox("Initial version, experimental", MessageType.Info);
  134. GUILayout.EndVertical();//end box
  135. #endregion
  136. if (GUI.changed)
  137. {
  138. EditorUtility.SetDirty(target);
  139. }
  140. serializedObject.ApplyModifiedProperties();
  141. }
  142. }