GreenScreenEditor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.IO;
  5. /// <summary>
  6. /// Custom Editor that extends the default settings shown in the Inspector.
  7. /// Allows it to add buttons and hidden elements for the garbage matte and config file.
  8. /// </summary>
  9. [CustomEditor(typeof(GreenScreenManager))]
  10. class GreenScreenManagerEditor : Editor
  11. {
  12. private GreenScreenManager greenScreen;
  13. private GUILayoutOption[] optionsButton = { GUILayout.MaxWidth(100) };
  14. private GUILayoutOption[] optionsButtonBrowse = { GUILayout.MaxWidth(30) };
  15. private SerializedProperty keyColors;
  16. private SerializedProperty range;
  17. private SerializedProperty smoothness;
  18. private SerializedProperty whiteclip;
  19. private SerializedProperty blackclip;
  20. private SerializedProperty erosion;
  21. private SerializedProperty sigma;
  22. private SerializedProperty despill;
  23. private SerializedProperty pathfileconfig;
  24. private GarbageMatte matte;
  25. private SerializedProperty enableGrabageMatte;
  26. private static GUIStyle ToggleButtonStyleNormal = null;
  27. private static GUIStyle ToggleButtonStyleToggled = null;
  28. public void OnEnable()
  29. {
  30. greenScreen = (GreenScreenManager)target;
  31. //Create serialized properties for all the GreenScreenManager's fields so they can be modified and saved/serialized properly.
  32. keyColors = serializedObject.FindProperty("keyColors");
  33. erosion = serializedObject.FindProperty("erosion");
  34. range = serializedObject.FindProperty("range");
  35. smoothness = serializedObject.FindProperty("smoothness");
  36. whiteclip = serializedObject.FindProperty("whiteClip");
  37. blackclip = serializedObject.FindProperty("blackClip");
  38. sigma = serializedObject.FindProperty("sigma_");
  39. despill = serializedObject.FindProperty("spill");
  40. pathfileconfig = serializedObject.FindProperty("pathFileConfig");
  41. enableGrabageMatte = serializedObject.FindProperty("enableGarbageMatte");
  42. }
  43. public override void OnInspectorGUI()
  44. {
  45. serializedObject.Update();
  46. greenScreen = (GreenScreenManager)target;
  47. key_colors();
  48. if (ToggleButtonStyleNormal == null)
  49. {
  50. ToggleButtonStyleNormal = "Button";
  51. ToggleButtonStyleToggled = new GUIStyle(ToggleButtonStyleNormal);
  52. ToggleButtonStyleToggled.normal.background = ToggleButtonStyleToggled.active.background;
  53. }
  54. //matte = (ZEDGarbageMatte)target;
  55. matte = greenScreen.garbageMatte;
  56. GUI.enabled = greenScreen.screenManager != null && greenScreen.screenManager.ActualRenderingPath == RenderingPath.Forward;
  57. enableGrabageMatte.boolValue = EditorGUILayout.Toggle("Enable Garbage Matte", enableGrabageMatte.boolValue);
  58. GUI.enabled = true;
  59. //Show the garbage matte section, only if the scene is running and it's enabled.
  60. if (enableGrabageMatte.boolValue && greenScreen.screenManager != null && greenScreen.screenManager.ActualRenderingPath == RenderingPath.Forward)
  61. {
  62. //serializedObject.Update();
  63. EditorGUILayout.BeginHorizontal();
  64. if (GUILayout.Button("Place Markers", matte.editMode ? ToggleButtonStyleToggled : ToggleButtonStyleNormal))
  65. {
  66. matte.editMode = !matte.editMode;
  67. if (matte.editMode)
  68. {
  69. matte.EnterEditMode();
  70. }
  71. }
  72. EditorGUILayout.EndHorizontal();
  73. EditorGUILayout.BeginHorizontal();
  74. if (GUILayout.Button("Remove Last Marker"))
  75. {
  76. matte.RemoveLastPoint();
  77. }
  78. EditorGUILayout.EndHorizontal();
  79. EditorGUILayout.BeginHorizontal();
  80. if (GUILayout.Button("Apply Garbage Matte"))
  81. {
  82. matte.ApplyGarbageMatte();
  83. }
  84. EditorGUILayout.EndHorizontal();
  85. GUI.enabled = true;
  86. if (GUILayout.Button("Reset"))
  87. {
  88. matte.ResetPoints(true);
  89. }
  90. }
  91. //Draw the configuration file path and Save/Load buttons.
  92. GUILayout.Space(20);
  93. EditorGUILayout.BeginHorizontal();
  94. pathfileconfig.stringValue = EditorGUILayout.TextField("Save Config", greenScreen.pathFileConfig);
  95. serializedObject.ApplyModifiedProperties();
  96. if (GUILayout.Button("...", optionsButtonBrowse))
  97. {
  98. pathfileconfig.stringValue = EditorUtility.OpenFilePanel("Load save file", "", "json");
  99. serializedObject.ApplyModifiedProperties();
  100. }
  101. EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal();
  102. GUILayout.FlexibleSpace();
  103. if (GUILayout.Button("Save"))
  104. {
  105. greenScreen.SaveData(greenScreen.RegisterDataChromaKeys(), greenScreen.garbageMatte.RegisterData());
  106. }
  107. GUI.enabled = File.Exists(greenScreen.pathFileConfig);
  108. if (GUILayout.Button("Load"))
  109. {
  110. greenScreen.LoadGreenScreenData(true);
  111. keyColors.colorValue = greenScreen.keyColors;
  112. range.floatValue = greenScreen.range;
  113. smoothness.floatValue = greenScreen.smoothness;
  114. whiteclip.floatValue = greenScreen.whiteClip;
  115. blackclip.floatValue = greenScreen.blackClip;
  116. erosion.intValue = greenScreen.erosion;
  117. sigma.floatValue = greenScreen.sigma_;
  118. despill.floatValue = greenScreen.spill;
  119. }
  120. if (GUILayout.Button ("Clean")) {
  121. matte.CleanSpheres ();
  122. }
  123. GUI.enabled = true;
  124. EditorGUILayout.EndHorizontal();
  125. serializedObject.ApplyModifiedProperties();
  126. }
  127. void key_colors()
  128. {
  129. //GUILayout.Label("Chroma Key", EditorStyles.boldLabel);
  130. EditorGUILayout.Space();
  131. EditorGUI.BeginChangeCheck();
  132. greenScreen.canal = (GreenScreenManager.CANAL)EditorGUILayout.EnumPopup("View", greenScreen.canal);
  133. EditorGUILayout.Space();
  134. if (EditorGUI.EndChangeCheck())
  135. {
  136. greenScreen.UpdateCanal();
  137. }
  138. EditorGUI.BeginChangeCheck();
  139. EditorGUILayout.BeginHorizontal();
  140. EditorGUILayout.EndHorizontal();
  141. GUILayout.Label("Screen", EditorStyles.boldLabel);
  142. keyColors.colorValue = EditorGUILayout.ColorField("Color", keyColors.colorValue);
  143. range.floatValue = EditorGUILayout.Slider("Range", range.floatValue, 0.0f, 1.0f);
  144. smoothness.floatValue = EditorGUILayout.Slider("Smoothness", smoothness.floatValue, 0, 1.0f);
  145. EditorGUILayout.Space();
  146. GUILayout.Label("Foreground", EditorStyles.boldLabel);
  147. whiteclip.floatValue = EditorGUILayout.Slider("Clip White", whiteclip.floatValue, 0.0f, 1.0f);
  148. blackclip.floatValue = EditorGUILayout.Slider("Clip Black", blackclip.floatValue, 0.0f, 1.0f);
  149. erosion.intValue = EditorGUILayout.IntSlider("Erosion", erosion.intValue, 0, 5);
  150. //sigma.floatValue = EditorGUILayout.Slider("Edges Softness", sigma.floatValue, 0.1f, 2.0f);
  151. despill.floatValue = EditorGUILayout.Slider("Despill", despill.floatValue, 0f, 1f);
  152. //Button to reset to default settings.
  153. GUILayout.BeginHorizontal();
  154. GUILayout.FlexibleSpace();
  155. if (GUILayout.Button("Default", optionsButton))
  156. {
  157. greenScreen.SetDefaultValues();
  158. keyColors.colorValue = greenScreen.keyColors;
  159. range.floatValue = greenScreen.range;
  160. smoothness.floatValue = greenScreen.smoothness;
  161. whiteclip.floatValue = greenScreen.whiteClip;
  162. blackclip.floatValue = greenScreen.blackClip;
  163. erosion.intValue = greenScreen.erosion;
  164. sigma.floatValue = greenScreen.sigma_;
  165. despill.floatValue = greenScreen.spill;
  166. Repaint();
  167. }
  168. GUILayout.EndHorizontal();
  169. if (EditorGUI.EndChangeCheck())
  170. {
  171. greenScreen.UpdateShader();
  172. }
  173. }
  174. }
  175. #endif