VFXViewPreference.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.VFX;
  5. namespace UnityEditor.VFX
  6. {
  7. static class VFXViewPreference
  8. {
  9. private static bool m_Loaded = false;
  10. private static bool m_GenerateOutputContextWithShaderGraph;
  11. private static bool m_DisplayExperimentalOperator = false;
  12. private static bool m_AllowShaderExternalization = false;
  13. private static bool m_DisplayExtraDebugInfo = false;
  14. private static bool m_ForceEditionCompilation = false;
  15. private static bool m_AdvancedLogs = false;
  16. private static VFXMainCameraBufferFallback m_CameraBuffersFallback = VFXMainCameraBufferFallback.PreferMainCamera;
  17. private static bool m_MultithreadUpdateEnabled = true;
  18. public static bool generateOutputContextWithShaderGraph
  19. {
  20. get
  21. {
  22. LoadIfNeeded();
  23. return m_GenerateOutputContextWithShaderGraph;
  24. }
  25. }
  26. public static bool displayExperimentalOperator
  27. {
  28. get
  29. {
  30. LoadIfNeeded();
  31. return m_DisplayExperimentalOperator;
  32. }
  33. }
  34. public static bool displayExtraDebugInfo
  35. {
  36. get
  37. {
  38. LoadIfNeeded();
  39. return m_DisplayExtraDebugInfo;
  40. }
  41. }
  42. public static bool advancedLogs
  43. {
  44. get
  45. {
  46. LoadIfNeeded();
  47. return m_AdvancedLogs;
  48. }
  49. }
  50. public static bool forceEditionCompilation
  51. {
  52. get
  53. {
  54. LoadIfNeeded();
  55. return m_ForceEditionCompilation;
  56. }
  57. }
  58. public static VFXMainCameraBufferFallback cameraBuffersFallback
  59. {
  60. get
  61. {
  62. LoadIfNeeded();
  63. return m_CameraBuffersFallback;
  64. }
  65. }
  66. public static bool multithreadUpdateEnabled
  67. {
  68. get
  69. {
  70. LoadIfNeeded();
  71. return m_MultithreadUpdateEnabled;
  72. }
  73. }
  74. public const string experimentalOperatorKey = "VFX.displayExperimentalOperatorKey";
  75. public const string extraDebugInfoKey = "VFX.ExtraDebugInfo";
  76. public const string forceEditionCompilationKey = "VFX.ForceEditionCompilation";
  77. public const string allowShaderExternalizationKey = "VFX.allowShaderExternalization";
  78. public const string advancedLogsKey = "VFX.AdvancedLogs";
  79. public const string cameraBuffersFallbackKey = "VFX.CameraBuffersFallback";
  80. public const string multithreadUpdateEnabledKey = "VFX.MultithreadUpdateEnabled";
  81. private static void LoadIfNeeded()
  82. {
  83. if (!m_Loaded)
  84. {
  85. m_GenerateOutputContextWithShaderGraph = true;
  86. m_DisplayExperimentalOperator = EditorPrefs.GetBool(experimentalOperatorKey, false);
  87. m_DisplayExtraDebugInfo = EditorPrefs.GetBool(extraDebugInfoKey, false);
  88. m_ForceEditionCompilation = EditorPrefs.GetBool(forceEditionCompilationKey, false);
  89. m_AllowShaderExternalization = EditorPrefs.GetBool(allowShaderExternalizationKey, false);
  90. m_AdvancedLogs = EditorPrefs.GetBool(advancedLogsKey, false);
  91. m_CameraBuffersFallback = (VFXMainCameraBufferFallback)EditorPrefs.GetInt(cameraBuffersFallbackKey, (int)VFXMainCameraBufferFallback.PreferMainCamera);
  92. m_MultithreadUpdateEnabled = EditorPrefs.GetBool(multithreadUpdateEnabledKey, true);
  93. m_Loaded = true;
  94. }
  95. }
  96. class VFXSettingsProvider : SettingsProvider
  97. {
  98. public VFXSettingsProvider() : base("Preferences/Visual Effects", SettingsScope.User)
  99. {
  100. hasSearchInterestHandler = HasSearchInterestHandler;
  101. }
  102. bool HasSearchInterestHandler(string searchContext)
  103. {
  104. return true;
  105. }
  106. public override void OnGUI(string searchContext)
  107. {
  108. using (new SettingsWindow.GUIScope())
  109. {
  110. LoadIfNeeded();
  111. m_GenerateOutputContextWithShaderGraph = EditorGUILayout.Toggle(new GUIContent("Improved Shader Graph Generation", "When enabled, any shadergraph shaders can be assigned to Visual Effect outputs as long as ‘Support VFX Graph’ is enabled in the ShaderGraph’s Graph Inspector."), m_GenerateOutputContextWithShaderGraph);
  112. m_DisplayExperimentalOperator = EditorGUILayout.Toggle(new GUIContent("Experimental Operators/Blocks", "When enabled, operators and blocks which are still in an experimental state become available to use within the Visual Effect Graph."), m_DisplayExperimentalOperator);
  113. m_DisplayExtraDebugInfo = EditorGUILayout.Toggle(new GUIContent("Show Additional Debug info", "When enabled, additional information becomes available in the inspector when selecting blocks, such as the attributes they use and their shader code."), m_DisplayExtraDebugInfo);
  114. m_AdvancedLogs = EditorGUILayout.Toggle(new GUIContent("Verbose Mode for compilation", "When enabled, additional information about the data, expressions, and generated shaders is displayed in the console whenever a graph is compiled."), m_AdvancedLogs);
  115. m_AllowShaderExternalization = EditorGUILayout.Toggle(new GUIContent("Experimental shader externalization", "When enabled, the generated shaders are stored alongside the Visual Effect asset, enabling their direct modification."), m_AllowShaderExternalization);
  116. bool oldForceEditionCompilation = m_ForceEditionCompilation;
  117. m_ForceEditionCompilation = EditorGUILayout.Toggle(new GUIContent("Force Compilation in Edition Mode", "When enabled, the unoptimized edit version of the Visual Effect is compiled even when the effect is not being edited. Otherwise, an optimized runtime version is compiled."), m_ForceEditionCompilation);
  118. if (m_ForceEditionCompilation != oldForceEditionCompilation)
  119. {
  120. // TODO Factorize that somewhere
  121. var vfxAssets = new HashSet<VisualEffectAsset>();
  122. var vfxAssetsGuid = AssetDatabase.FindAssets("t:VisualEffectAsset");
  123. foreach (var guid in vfxAssetsGuid)
  124. {
  125. string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  126. var vfxAsset = AssetDatabase.LoadAssetAtPath<VisualEffectAsset>(assetPath);
  127. if (vfxAsset != null)
  128. vfxAssets.Add(vfxAsset);
  129. }
  130. foreach (var vfxAsset in vfxAssets)
  131. vfxAsset.GetResource().GetOrCreateGraph().SetCompilationMode(m_ForceEditionCompilation ? VFXCompilationMode.Edition : VFXCompilationMode.Runtime);
  132. }
  133. #if UNITY_2022_1_OR_NEWER
  134. if (Unsupported.IsDeveloperMode())
  135. {
  136. m_MultithreadUpdateEnabled = EditorGUILayout.Toggle(new GUIContent("Multithread Update Enabled", "When enabled, visual effects will be updated in parallel when possible."), m_MultithreadUpdateEnabled);
  137. }
  138. #endif
  139. m_CameraBuffersFallback = (VFXMainCameraBufferFallback)EditorGUILayout.EnumPopup(new GUIContent("Main Camera fallback", "Specifies the camera source for the color and depth buffer that MainCamera Operators use when in the editor."), m_CameraBuffersFallback);
  140. var userTemplateDirectory = EditorGUILayout.DelayedTextField(new GUIContent("User Systems", "Directory for user-generated VFX templates (e.g. Assets/VFX/Templates)"), VFXResources.defaultResources.userTemplateDirectory);
  141. if (GUI.changed)
  142. {
  143. EditorPrefs.SetBool(experimentalOperatorKey, m_DisplayExperimentalOperator);
  144. EditorPrefs.SetBool(extraDebugInfoKey, m_DisplayExtraDebugInfo);
  145. EditorPrefs.SetBool(forceEditionCompilationKey, m_ForceEditionCompilation);
  146. EditorPrefs.SetBool(advancedLogsKey, m_AdvancedLogs);
  147. EditorPrefs.SetBool(allowShaderExternalizationKey, m_AllowShaderExternalization);
  148. EditorPrefs.SetInt(cameraBuffersFallbackKey, (int)m_CameraBuffersFallback);
  149. EditorPrefs.SetBool(multithreadUpdateEnabledKey, m_MultithreadUpdateEnabled);
  150. userTemplateDirectory = userTemplateDirectory.Replace('\\', '/');
  151. userTemplateDirectory = userTemplateDirectory.TrimEnd(new char[] { '/' });
  152. userTemplateDirectory = userTemplateDirectory.TrimStart(new char[] { '/' });
  153. VFXResources.defaultResources.userTemplateDirectory = userTemplateDirectory;
  154. }
  155. }
  156. if ((VFXResources.defaultResources.userTemplateDirectory.Length > 0) && (!System.IO.Directory.Exists(VFXResources.defaultResources.userTemplateDirectory)))
  157. EditorGUILayout.HelpBox("The specified User Systems directory does not exist in the project.", MessageType.Warning);
  158. base.OnGUI(searchContext);
  159. }
  160. }
  161. [SettingsProvider]
  162. public static SettingsProvider PreferenceSettingsProvider()
  163. {
  164. return new VFXSettingsProvider();
  165. }
  166. public static void PreferencesGUI()
  167. {
  168. }
  169. }
  170. }