fxvVolumeFogShaderInspector.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace FXV
  5. {
  6. public class fxvVolumeFogShaderInspector : ShaderGUI
  7. {
  8. static class Styles
  9. {
  10. static public readonly GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB)");
  11. static public readonly GUIContent alphaCutoffText = EditorGUIUtility.TrTextContent("Alpha Cutoff", "Threshold for alpha cutoff");
  12. static public readonly GUIContent normalMap = new GUIContent("Normal Map", "Normal Map");
  13. static public readonly GUIContent rmaMap = new GUIContent("RMA Map", "RMA Map");
  14. static public readonly GUIContent occlusion = new GUIContent("Occlusion", "Occlusion (G)");
  15. static public readonly GUIContent detail = new GUIContent("Triplanar Detail", "Detail");
  16. static public readonly GUIContent detailNormalMap = new GUIContent("Detail Normal Map", "Detail Normal Map");
  17. static public readonly GUIContent intersection = new GUIContent("Intersection Map", "Intersection");
  18. static public readonly GUIContent bakeItMainMap = new GUIContent("BakeIt Main Map");
  19. static public readonly GUIContent channelTexture = new GUIContent("Channel Texture", "Color");
  20. static public readonly GUIContent channelRamp = new GUIContent("Channel Ramp");
  21. public static string renderingMode = "Rendering Mode";
  22. public static readonly string[] fogTypeNames = Enum.GetNames(typeof(VolumeFog.FogType));
  23. public static readonly string[] blendNames = Enum.GetNames(typeof(VolumeFog.FogBlendMode));
  24. }
  25. bool _initialized;
  26. public override void OnGUI(MaterialEditor editor, MaterialProperty[] props)
  27. {
  28. Material material = editor.target as Material;
  29. EditorGUI.BeginChangeCheck();
  30. EditorGUILayout.Space();
  31. EditorGUILayout.LabelField("Shader Params:", EditorStyles.boldLabel);
  32. EditorGUI.indentLevel++;
  33. //-----------------------------------------------------------------------------------------------
  34. // FOG TYPE
  35. //-----------------------------------------------------------------------------------------------
  36. MaterialProperty fogType = FindProperty("_FogType", props);
  37. FogTypePopup(editor, fogType);
  38. var fogTypeVal = (VolumeFog.FogType)fogType.floatValue;
  39. //-----------------------------------------------------------------------------------------------s
  40. // BLEND MODE
  41. //-----------------------------------------------------------------------------------------------
  42. MaterialProperty blendMode = FindProperty("_BlendMode", props);
  43. BlendModePopup(editor, blendMode);
  44. //-----------------------------------------------------------------------------------------------
  45. // IN AIR RENDERING
  46. //-----------------------------------------------------------------------------------------------
  47. editor.ShaderProperty(FindProperty("_InAirEnabled", props), "Volumetric Rendering Enabled");
  48. //-----------------------------------------------------------------------------------------------
  49. // AXIS FADE
  50. //-----------------------------------------------------------------------------------------------
  51. if (fogTypeVal == VolumeFog.FogType.Height)
  52. {
  53. editor.ShaderProperty(FindProperty("_AxisFadeEnabled", props), "Axis Fade Enabled");
  54. }
  55. //-----------------------------------------------------------------------------------------------
  56. // ADVANCED
  57. //-----------------------------------------------------------------------------------------------
  58. EditorGUILayout.Space();
  59. EditorGUILayout.Space();
  60. GUILayout.Label("Advanced Options", EditorStyles.boldLabel);
  61. editor.EnableInstancingField();
  62. editor.RenderQueueField();
  63. if (EditorGUI.EndChangeCheck() || !_initialized)
  64. {
  65. foreach (Material m in editor.targets)
  66. {
  67. SetupMaterialWithFogType(m, (VolumeFog.FogType)m.GetFloat("_FogType"));
  68. SetupMaterialWithBlendMode(m, (VolumeFog.FogBlendMode)m.GetFloat("_BlendMode"));
  69. SetMaterialKeywords(m);
  70. }
  71. }
  72. _initialized = true;
  73. }
  74. void FogTypePopup(MaterialEditor editor, MaterialProperty fogType)
  75. {
  76. EditorGUI.showMixedValue = fogType.hasMixedValue;
  77. var mode = (VolumeFog.FogType)fogType.floatValue;
  78. EditorGUI.BeginChangeCheck();
  79. mode = (VolumeFog.FogType)EditorGUILayout.Popup("Fog Type", (int)mode, Styles.fogTypeNames);
  80. if (EditorGUI.EndChangeCheck())
  81. {
  82. editor.RegisterPropertyChangeUndo("Fog Type");
  83. fogType.floatValue = (float)mode;
  84. }
  85. EditorGUI.showMixedValue = false;
  86. }
  87. void BlendModePopup(MaterialEditor editor, MaterialProperty blendMode)
  88. {
  89. EditorGUI.showMixedValue = blendMode.hasMixedValue;
  90. var mode = (VolumeFog.FogBlendMode)blendMode.floatValue;
  91. EditorGUI.BeginChangeCheck();
  92. mode = (VolumeFog.FogBlendMode)EditorGUILayout.Popup("Blend Mode", (int)mode, Styles.blendNames);
  93. if (EditorGUI.EndChangeCheck())
  94. {
  95. editor.RegisterPropertyChangeUndo("Blend Mode");
  96. blendMode.floatValue = (float)mode;
  97. }
  98. EditorGUI.showMixedValue = false;
  99. }
  100. public static void SetupMaterialWithFogType(Material material, VolumeFog.FogType fogType)
  101. {
  102. var types = Enum.GetNames(typeof(VolumeFog.FogType));
  103. foreach (string name in types)
  104. {
  105. material.DisableKeyword("FXV_FOGTYPE_" + name.ToUpper());
  106. }
  107. material.EnableKeyword("FXV_FOGTYPE_" + fogType.ToString().ToUpper());
  108. }
  109. public static void SetupMaterialWithBlendMode(Material material, VolumeFog.FogBlendMode blendMode)
  110. {
  111. if (blendMode == VolumeFog.FogBlendMode.AlphaBlend)
  112. {
  113. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  114. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  115. material.SetInt("_ZWrite", 0);
  116. }
  117. else if (blendMode == VolumeFog.FogBlendMode.Add)
  118. {
  119. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  120. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
  121. material.SetInt("_ZWrite", 0);
  122. }
  123. }
  124. static void SetMaterialKeywords(Material material)
  125. {
  126. SetKeyword(material, "FXV_IN_AIR_FOG", material.GetInt("_InAirEnabled") > 0);
  127. }
  128. static void SetKeyword(Material m, string keyword, bool state)
  129. {
  130. if (state)
  131. m.EnableKeyword(keyword);
  132. else
  133. m.DisableKeyword(keyword);
  134. }
  135. }
  136. }