ConfigEditor.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace VLB
  5. {
  6. [CustomEditor(typeof(Config))]
  7. public class ConfigEditor : EditorCommon
  8. {
  9. SerializedProperty geometryOverrideLayer, geometryLayerID, geometryTag, geometryRenderQueue, renderPipeline, renderingMode;
  10. SerializedProperty sharedMeshSides, sharedMeshSegments;
  11. SerializedProperty globalNoiseScale, globalNoiseVelocity;
  12. SerializedProperty fadeOutCameraTag;
  13. SerializedProperty noise3DData, noise3DSize;
  14. SerializedProperty dustParticlesPrefab;
  15. SerializedProperty ditheringFactor, ditheringNoiseTexture;
  16. SerializedProperty featureEnabledColorGradient, featureEnabledDepthBlend, featureEnabledNoise3D, featureEnabledDynamicOcclusion, featureEnabledMeshSkewing, featureEnabledShaderAccuracyHigh;
  17. bool isRenderQueueCustom = false;
  18. Config m_TargetConfig = null;
  19. protected override void OnEnable()
  20. {
  21. base.OnEnable();
  22. geometryOverrideLayer = FindProperty((Config x) => x.geometryOverrideLayer);
  23. geometryLayerID = FindProperty((Config x) => x.geometryLayerID);
  24. geometryTag = FindProperty((Config x) => x.geometryTag);
  25. geometryRenderQueue = FindProperty((Config x) => x.geometryRenderQueue);
  26. renderPipeline = serializedObject.FindProperty("_RenderPipeline");
  27. renderingMode = serializedObject.FindProperty("_RenderingMode");
  28. sharedMeshSides = FindProperty((Config x) => x.sharedMeshSides);
  29. sharedMeshSegments = FindProperty((Config x) => x.sharedMeshSegments);
  30. globalNoiseScale = FindProperty((Config x) => x.globalNoiseScale);
  31. globalNoiseVelocity = FindProperty((Config x) => x.globalNoiseVelocity);
  32. fadeOutCameraTag = FindProperty((Config x) => x.fadeOutCameraTag);
  33. noise3DData = FindProperty((Config x) => x.noise3DData);
  34. noise3DSize = FindProperty((Config x) => x.noise3DSize);
  35. dustParticlesPrefab = FindProperty((Config x) => x.dustParticlesPrefab);
  36. ditheringFactor = FindProperty((Config x) => x.ditheringFactor);
  37. ditheringNoiseTexture = FindProperty((Config x) => x.ditheringNoiseTexture);
  38. featureEnabledColorGradient = FindProperty((Config x) => x.featureEnabledColorGradient);
  39. featureEnabledDepthBlend = FindProperty((Config x) => x.featureEnabledDepthBlend);
  40. featureEnabledNoise3D = FindProperty((Config x) => x.featureEnabledNoise3D);
  41. featureEnabledDynamicOcclusion = FindProperty((Config x) => x.featureEnabledDynamicOcclusion);
  42. featureEnabledMeshSkewing = FindProperty((Config x) => x.featureEnabledMeshSkewing);
  43. featureEnabledShaderAccuracyHigh = FindProperty((Config x) => x.featureEnabledShaderAccuracyHigh);
  44. RenderQueueGUIInit();
  45. Noise3D.LoadIfNeeded(); // Try to load Noise3D, maybe for the 1st time
  46. m_TargetConfig = this.target as Config;
  47. }
  48. void RenderQueueGUIInit()
  49. {
  50. isRenderQueueCustom = true;
  51. foreach (RenderQueue rq in System.Enum.GetValues(typeof(RenderQueue)))
  52. {
  53. if (rq != RenderQueue.Custom && geometryRenderQueue.intValue == (int)rq)
  54. {
  55. isRenderQueueCustom = false;
  56. break;
  57. }
  58. }
  59. }
  60. void RenderQueueGUIDraw()
  61. {
  62. using (new EditorGUILayout.HorizontalScope())
  63. {
  64. EditorGUI.BeginChangeCheck();
  65. {
  66. EditorGUI.BeginChangeCheck();
  67. RenderQueue rq = isRenderQueueCustom ? RenderQueue.Custom : (RenderQueue)geometryRenderQueue.intValue;
  68. rq = (RenderQueue)EditorGUILayout.EnumPopup(EditorStrings.Config.GeometryRenderQueue, rq);
  69. if (EditorGUI.EndChangeCheck())
  70. {
  71. isRenderQueueCustom = (rq == RenderQueue.Custom);
  72. if (!isRenderQueueCustom)
  73. geometryRenderQueue.intValue = (int)rq;
  74. }
  75. EditorGUI.BeginDisabledGroup(!isRenderQueueCustom);
  76. {
  77. geometryRenderQueue.intValue = EditorGUILayout.IntField(geometryRenderQueue.intValue, GUILayout.MaxWidth(65.0f));
  78. }
  79. EditorGUI.EndDisabledGroup();
  80. }
  81. if (EditorGUI.EndChangeCheck())
  82. {
  83. SetDirty(DirtyFlags.AllBeamGeom);
  84. }
  85. }
  86. }
  87. protected override void OnHeaderGUI()
  88. {
  89. GUILayout.BeginVertical("In BigTitle");
  90. EditorGUILayout.Separator();
  91. var title = string.Format("Volumetric Light Beam - Plugin Configuration");
  92. EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
  93. EditorGUILayout.LabelField(string.Format("Current Version: {0}", Version.Current), EditorStyles.miniBoldLabel);
  94. EditorGUILayout.Separator();
  95. GUILayout.EndVertical();
  96. }
  97. void OnAddConfigPerPlatform(object platform)
  98. {
  99. var p = (RuntimePlatform)platform;
  100. var clone = Object.Instantiate(m_TargetConfig);
  101. Debug.Assert(clone);
  102. var path = AssetDatabase.GetAssetPath(m_TargetConfig).Replace(m_TargetConfig.name + ".asset", "");
  103. ConfigOverride.CreateAsset(clone, path + ConfigOverride.kAssetName + p.ToString() + ".asset");
  104. Selection.activeObject = clone;
  105. }
  106. [System.Flags]
  107. enum DirtyFlags
  108. {
  109. Target = 1 << 1,
  110. Shader = 1 << 2,
  111. Noise = 1 << 3,
  112. GlobalMesh = 1 << 4,
  113. AllBeamGeom = 1 << 5,
  114. AllMeshes = 1 << 6
  115. }
  116. void SetDirty(DirtyFlags flags)
  117. {
  118. if (m_IsUsedInstance)
  119. {
  120. if (flags.HasFlag(DirtyFlags.Target)) EditorUtility.SetDirty(target);
  121. if (flags.HasFlag(DirtyFlags.Shader)) m_NeedToRefreshShader = true;
  122. if (flags.HasFlag(DirtyFlags.Noise)) m_NeedToReloadNoise = true;
  123. if (flags.HasFlag(DirtyFlags.GlobalMesh)) GlobalMesh.Destroy();
  124. if (flags.HasFlag(DirtyFlags.AllBeamGeom)) VolumetricLightBeam._EditorSetAllBeamGeomDirty();
  125. if (flags.HasFlag(DirtyFlags.AllMeshes)) VolumetricLightBeam._EditorSetAllMeshesDirty();
  126. }
  127. }
  128. bool m_NeedToReloadNoise = false;
  129. bool m_NeedToRefreshShader = false;
  130. bool m_IsUsedInstance = false;
  131. public override void OnInspectorGUI()
  132. {
  133. base.OnInspectorGUI();
  134. Debug.Assert(m_TargetConfig != null);
  135. m_NeedToReloadNoise = false;
  136. m_NeedToRefreshShader = false;
  137. m_IsUsedInstance = m_TargetConfig.IsCurrentlyUsedInstance();
  138. // Config per plaftorm
  139. #if UNITY_2018_1_OR_NEWER
  140. {
  141. bool hasValidName = m_TargetConfig.HasValidAssetName();
  142. bool isCurrentPlatformSuffix = m_TargetConfig.GetAssetSuffix() == PlatformHelper.GetCurrentPlatformSuffix();
  143. var platformSuffix = m_TargetConfig.GetAssetSuffix();
  144. string platformStr = "Default Config asset";
  145. if(!string.IsNullOrEmpty(platformSuffix))
  146. platformStr = string.Format("Config asset for platform '{0}'", m_TargetConfig.GetAssetSuffix());
  147. if (!hasValidName)
  148. platformStr += " (INVALID)";
  149. EditorGUILayout.LabelField(platformStr, EditorStyles.boldLabel);
  150. if (GUILayout.Button(EditorStrings.Beam.ButtonCreateOverridePerPlatform, EditorStyles.miniButton))
  151. {
  152. var menu = new GenericMenu();
  153. foreach (var platform in System.Enum.GetValues(typeof(RuntimePlatform)))
  154. menu.AddItem(new GUIContent(platform.ToString()), false, OnAddConfigPerPlatform, platform);
  155. menu.ShowAsContext();
  156. }
  157. if (!hasValidName)
  158. {
  159. EditorGUILayout.Separator();
  160. EditorGUILayout.HelpBox(EditorStrings.Config.InvalidPlatformOverride, MessageType.Error);
  161. ButtonOpenConfig();
  162. }
  163. else if (!m_IsUsedInstance)
  164. {
  165. EditorGUILayout.Separator();
  166. if (isCurrentPlatformSuffix)
  167. EditorGUILayout.HelpBox(EditorStrings.Config.WrongAssetLocation, MessageType.Error);
  168. else
  169. EditorGUILayout.HelpBox(EditorStrings.Config.NotCurrentAssetInUse, MessageType.Warning);
  170. ButtonOpenConfig();
  171. }
  172. DrawLineSeparator();
  173. }
  174. #endif
  175. {
  176. EditorGUI.BeginChangeCheck();
  177. {
  178. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderBeamGeometry))
  179. {
  180. using (new EditorGUILayout.HorizontalScope())
  181. {
  182. geometryOverrideLayer.boolValue = EditorGUILayout.Toggle(EditorStrings.Config.GeometryOverrideLayer, geometryOverrideLayer.boolValue);
  183. using (new EditorGUI.DisabledGroupScope(!geometryOverrideLayer.boolValue))
  184. {
  185. geometryLayerID.intValue = EditorGUILayout.LayerField(geometryLayerID.intValue);
  186. }
  187. }
  188. geometryTag.stringValue = EditorGUILayout.TagField(EditorStrings.Config.GeometryTag, geometryTag.stringValue);
  189. }
  190. FoldableHeader.End();
  191. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderRendering))
  192. {
  193. RenderQueueGUIDraw();
  194. if (BeamGeometry.isCustomRenderPipelineSupported)
  195. {
  196. EditorGUI.BeginChangeCheck();
  197. {
  198. renderPipeline.CustomEnum<RenderPipeline>(EditorStrings.Config.GeometryRenderPipeline, EditorStrings.Config.GeometryRenderPipelineEnumDescriptions);
  199. }
  200. if (EditorGUI.EndChangeCheck())
  201. {
  202. SetDirty(DirtyFlags.AllBeamGeom | DirtyFlags.Shader); // need to fully reset the BeamGeom to update the shader
  203. }
  204. }
  205. if (m_TargetConfig.hasRenderPipelineMismatch)
  206. EditorGUILayout.HelpBox(EditorStrings.Config.ErrorRenderPipelineMismatch, MessageType.Error);
  207. EditorGUI.BeginChangeCheck();
  208. {
  209. EditorGUILayout.PropertyField(renderingMode, EditorStrings.Config.GeometryRenderingMode);
  210. if (renderPipeline.enumValueIndex == (int)RenderPipeline.BuiltIn)
  211. {
  212. if (renderingMode.enumValueIndex == (int)RenderingMode.SRPBatcher)
  213. EditorGUILayout.HelpBox(EditorStrings.Config.ErrorSrpBatcherOnlyCompatibleWithSrp, MessageType.Error);
  214. }
  215. else
  216. {
  217. if (renderingMode.enumValueIndex == (int)RenderingMode.SRPBatcher && SRPHelper.renderPipelineType == SRPHelper.RenderPipeline.LWRP)
  218. EditorGUILayout.HelpBox(EditorStrings.Config.ErrorSrpBatcherNotCompatibleWithLWRP, MessageType.Error);
  219. if (renderingMode.enumValueIndex == (int)RenderingMode.MultiPass)
  220. EditorGUILayout.HelpBox(EditorStrings.Config.ErrorSrpAndMultiPassNotCompatible, MessageType.Error);
  221. }
  222. #pragma warning disable 0162 // warning CS0162: Unreachable code detected
  223. if (renderingMode.enumValueIndex == (int)RenderingMode.GPUInstancing && !BatchingHelper.isGpuInstancingSupported)
  224. EditorGUILayout.HelpBox(EditorStrings.Config.ErrorGeometryGpuInstancingNotSupported, MessageType.Error);
  225. #pragma warning restore 0162
  226. }
  227. if (EditorGUI.EndChangeCheck())
  228. {
  229. SetDirty(DirtyFlags.AllBeamGeom | DirtyFlags.GlobalMesh | DirtyFlags.Shader); // need to fully reset the BeamGeom to update the shader
  230. }
  231. if (m_TargetConfig.beamShader == null)
  232. EditorGUILayout.HelpBox(EditorStrings.Config.GetErrorInvalidShader(), MessageType.Error);
  233. if (ditheringFactor.FloatSlider(EditorStrings.Config.DitheringFactor, 0.0f, 1.0f))
  234. {
  235. SetDirty(DirtyFlags.Shader);
  236. }
  237. }
  238. FoldableHeader.End();
  239. }
  240. if (EditorGUI.EndChangeCheck())
  241. {
  242. VolumetricLightBeam._EditorSetAllMeshesDirty();
  243. }
  244. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderSharedMesh))
  245. {
  246. EditorGUI.BeginChangeCheck();
  247. EditorGUILayout.PropertyField(sharedMeshSides, EditorStrings.Config.SharedMeshSides);
  248. EditorGUILayout.PropertyField(sharedMeshSegments, EditorStrings.Config.SharedMeshSegments);
  249. if (EditorGUI.EndChangeCheck())
  250. {
  251. SetDirty(DirtyFlags.GlobalMesh | DirtyFlags.AllMeshes);
  252. }
  253. var meshInfo = "These properties will change the mesh tessellation of each Volumetric Light Beam with 'Shared' MeshType.\nAdjust them carefully since they could impact performance.";
  254. meshInfo += string.Format("\nShared Mesh stats: {0} vertices, {1} triangles", MeshGenerator.GetSharedMeshVertexCount(), MeshGenerator.GetSharedMeshIndicesCount() / 3);
  255. EditorGUILayout.HelpBox(meshInfo, MessageType.Info);
  256. }
  257. FoldableHeader.End();
  258. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderGlobal3DNoise))
  259. {
  260. EditorGUILayout.PropertyField(globalNoiseScale, EditorStrings.Config.GlobalNoiseScale);
  261. EditorGUILayout.PropertyField(globalNoiseVelocity, EditorStrings.Config.GlobalNoiseVelocity);
  262. }
  263. FoldableHeader.End();
  264. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderFadeOutCamera))
  265. {
  266. EditorGUI.BeginChangeCheck();
  267. fadeOutCameraTag.stringValue = EditorGUILayout.TagField(EditorStrings.Config.FadeOutCameraTag, fadeOutCameraTag.stringValue);
  268. if (EditorGUI.EndChangeCheck() && Application.isPlaying)
  269. m_TargetConfig.ForceUpdateFadeOutCamera();
  270. }
  271. FoldableHeader.End();
  272. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderFeaturesEnabled))
  273. {
  274. EditorGUI.BeginChangeCheck();
  275. {
  276. EditorGUILayout.PropertyField(featureEnabledColorGradient, EditorStrings.Config.FeatureEnabledColorGradient);
  277. EditorGUILayout.PropertyField(featureEnabledDepthBlend, EditorStrings.Config.FeatureEnabledDepthBlend);
  278. EditorGUILayout.PropertyField(featureEnabledNoise3D, EditorStrings.Config.FeatureEnabledNoise3D);
  279. EditorGUILayout.PropertyField(featureEnabledDynamicOcclusion, EditorStrings.Config.FeatureEnabledDynamicOcclusion);
  280. EditorGUILayout.PropertyField(featureEnabledMeshSkewing, EditorStrings.Config.FeatureEnabledMeshSkewing);
  281. EditorGUILayout.PropertyField(featureEnabledShaderAccuracyHigh, EditorStrings.Config.FeatureEnabledShaderAccuracyHigh);
  282. }
  283. if (EditorGUI.EndChangeCheck())
  284. {
  285. SetDirty(DirtyFlags.Shader | DirtyFlags.AllBeamGeom);
  286. }
  287. }
  288. FoldableHeader.End();
  289. if (FoldableHeader.Begin(this, EditorStrings.Config.HeaderInternalData))
  290. {
  291. EditorGUILayout.PropertyField(dustParticlesPrefab, EditorStrings.Config.DustParticlesPrefab);
  292. EditorGUILayout.PropertyField(ditheringNoiseTexture, EditorStrings.Config.DitheringNoiseTexture);
  293. EditorGUI.BeginChangeCheck();
  294. EditorGUILayout.PropertyField(noise3DData, EditorStrings.Config.Noise3DData);
  295. EditorGUILayout.PropertyField(noise3DSize, EditorStrings.Config.Noise3DSize);
  296. if (EditorGUI.EndChangeCheck())
  297. SetDirty(DirtyFlags.Noise);
  298. if (Noise3D.isSupported && !Noise3D.isProperlyLoaded)
  299. EditorGUILayout.HelpBox(EditorStrings.Common.HelpNoiseLoadingFailed, MessageType.Error);
  300. }
  301. FoldableHeader.End();
  302. if (GUILayout.Button(EditorStrings.Config.OpenDocumentation, EditorStyles.miniButton))
  303. {
  304. UnityEditor.Help.BrowseURL(Consts.HelpUrlConfig);
  305. }
  306. using (new EditorGUILayout.HorizontalScope())
  307. {
  308. if (GUILayout.Button(EditorStrings.Config.ResetToDefaultButton, EditorStyles.miniButton))
  309. {
  310. UnityEditor.Undo.RecordObject(target, "Reset Config Properties");
  311. m_TargetConfig.Reset();
  312. SetDirty(DirtyFlags.Target | DirtyFlags.Noise);
  313. }
  314. if (GUILayout.Button(EditorStrings.Config.ResetInternalDataButton, EditorStyles.miniButton))
  315. {
  316. UnityEditor.Undo.RecordObject(target, "Reset Internal Data");
  317. m_TargetConfig.ResetInternalData();
  318. SetDirty(DirtyFlags.Target | DirtyFlags.Noise);
  319. }
  320. }
  321. }
  322. serializedObject.ApplyModifiedProperties();
  323. if (m_NeedToRefreshShader)
  324. m_TargetConfig.RefreshShader(Config.RefreshShaderFlags.All); // need to be done AFTER ApplyModifiedProperties
  325. if (m_NeedToReloadNoise)
  326. Noise3D._EditorForceReloadData(); // Should be called AFTER ApplyModifiedProperties so the Config instance has the proper values when reloading data
  327. }
  328. }
  329. }
  330. #endif