LayaUnlitGUI.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //#if UNITY_EDITOR
  2. using System;
  3. using UnityEngine;
  4. using UnityEditor;
  5. class LayaUnlitGUI : ShaderGUI
  6. {
  7. public enum RenderMode
  8. {
  9. /**渲染状态_不透明。*/
  10. Opaque = 0,
  11. /**渲染状态_透明测试。*/
  12. Cutout = 1,
  13. /**渲染状态_透明混合。*/
  14. Transparent = 2
  15. }
  16. public enum CullMode
  17. {
  18. CULL_NONE = 0,
  19. CULL_FRONT = 1,
  20. CULL_BACK = 2,
  21. }
  22. MaterialProperty renderMode = null;
  23. MaterialProperty albedoTexture = null;
  24. MaterialProperty albedoColor = null;
  25. MaterialProperty albedoIntensity = null;
  26. MaterialProperty alphaCutoff = null;
  27. MaterialProperty cullMode = null;
  28. MaterialProperty isVertexColor = null;
  29. MaterialEditor m_MaterialEditor;
  30. bool m_FirstTimeApply = true;
  31. public void FindProperties(MaterialProperty[] props)
  32. {
  33. renderMode = FindProperty("_Mode", props);
  34. albedoTexture = FindProperty("_MainTex", props);
  35. albedoColor = FindProperty("_Color", props);
  36. albedoIntensity = FindProperty("_AlbedoIntensity", props);
  37. isVertexColor = FindProperty("_IsVertexColor", props);
  38. alphaCutoff = FindProperty("_Cutoff", props, false);
  39. cullMode = FindProperty("_Cull", props);
  40. }
  41. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  42. {
  43. // render the default gui
  44. FindProperties(props);
  45. m_MaterialEditor = materialEditor;
  46. Material material = materialEditor.target as Material;
  47. if (m_FirstTimeApply)
  48. {
  49. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  50. m_FirstTimeApply = false;
  51. }
  52. ShaderPropertiesGUI(material);
  53. }
  54. public void ShaderPropertiesGUI(Material material)
  55. {
  56. // Use default labelWidth
  57. EditorGUIUtility.labelWidth = 0f;
  58. // Detect any changes to the material
  59. EditorGUI.BeginChangeCheck();
  60. //renderMode
  61. GUILayout.BeginHorizontal();
  62. GUILayout.Label(Styles.renderModeText, GUILayout.Width(120));
  63. var mode = (RenderMode)renderMode.floatValue;
  64. mode = (RenderMode)EditorGUILayout.Popup((int)mode, Styles.renderModeNames);
  65. GUILayout.EndHorizontal();
  66. //enableVertexColor
  67. m_MaterialEditor.ShaderProperty(isVertexColor, Styles.enableVertexColor);
  68. //albedo
  69. m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoTexture, albedoColor);
  70. //albedo Intensity
  71. m_MaterialEditor.ShaderProperty(albedoIntensity, Styles.albedoIntensityText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  72. //alphaTest
  73. if (renderMode.floatValue == 1)
  74. {
  75. m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  76. }
  77. //scaleAndOffset
  78. m_MaterialEditor.TextureScaleOffsetProperty(albedoTexture);
  79. //cullMode
  80. GUILayout.BeginHorizontal();
  81. GUILayout.Label(Styles.cullModeText, GUILayout.Width(120));
  82. var cull = (CullMode)cullMode.floatValue;
  83. cull = (CullMode)EditorGUILayout.Popup((int)cull, Styles.cullModeNames);
  84. GUILayout.EndHorizontal();
  85. if (EditorGUI.EndChangeCheck())
  86. {
  87. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  88. //renderMode
  89. renderMode.floatValue = (float)mode;
  90. if (isVertexColor.floatValue==1.0)
  91. {
  92. material.EnableKeyword("ENABLEVERTEXCOLOR");
  93. //material.SetInt("_IsVertexColor", 1);
  94. }
  95. else
  96. {
  97. material.DisableKeyword("ENABLEVERTEXCOLOR");
  98. //material.SetInt("_IsVertexColor", 0);
  99. }
  100. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  101. //cullMode
  102. cullMode.floatValue = (float)cull;
  103. material.SetInt("_Cull", (int)cull);
  104. }
  105. m_MaterialEditor.RenderQueueField();
  106. }
  107. public void onChangeRender(Material material, RenderMode mode)
  108. {
  109. switch (mode)
  110. {
  111. case RenderMode.Opaque:
  112. material.SetInt("_Mode", 0);
  113. material.SetInt("_AlphaTest", 0);
  114. material.SetInt("_AlphaBlend", 0);
  115. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  116. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  117. material.SetInt("_ZWrite", 1);
  118. material.SetInt("_ZTest", 4);
  119. material.DisableKeyword("_ALPHATEST_ON");
  120. material.DisableKeyword("_ALPHABLEND_ON");
  121. material.DisableKeyword("ADDTIVEFOG");
  122. material.DisableKeyword("EnableAlphaCutoff");
  123. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  124. break;
  125. case RenderMode.Cutout:
  126. material.SetInt("_Mode", 1);
  127. material.SetInt("_AlphaTest", 1);
  128. material.SetInt("_AlphaBlend", 0);
  129. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  130. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  131. material.SetInt("_ZWrite", 1);
  132. material.SetInt("_ZTest", 4);
  133. material.EnableKeyword("_ALPHATEST_ON");
  134. material.DisableKeyword("_ALPHABLEND_ON");
  135. material.DisableKeyword("ADDTIVEFOG");
  136. material.EnableKeyword("EnableAlphaCutoff");
  137. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
  138. break;
  139. case RenderMode.Transparent:
  140. material.SetInt("_Mode", 2);
  141. material.SetInt("_AlphaTest", 0);
  142. material.SetInt("_AlphaBlend", 1);
  143. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  144. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  145. material.SetInt("_ZWrite", 0);
  146. material.SetInt("_ZTest", 4);
  147. material.DisableKeyword("_ALPHATEST_ON");
  148. material.EnableKeyword("_ALPHABLEND_ON");
  149. material.DisableKeyword("ADDTIVEFOG");
  150. material.DisableKeyword("EnableAlphaCutoff");
  151. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  152. break;
  153. default:
  154. material.SetInt("_Mode", 0);
  155. material.SetInt("_AlphaTest", 0);
  156. material.SetInt("_AlphaBlend", 0);
  157. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  158. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  159. material.SetInt("_ZWrite", 1);
  160. material.SetInt("_ZTest", 4);
  161. material.DisableKeyword("_ALPHATEST_ON");
  162. material.DisableKeyword("_ALPHABLEND_ON");
  163. material.DisableKeyword("ADDTIVEFOG");
  164. material.DisableKeyword("EnableAlphaCutoff");
  165. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry;
  166. break;
  167. }
  168. }
  169. public static class Styles
  170. {
  171. public static GUIStyle optionsButton = "PaneOptions";
  172. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  173. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  174. public static string emptyTootip = "";
  175. public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
  176. public static GUIContent albedoIntensityText = new GUIContent("Intensity", "Albedo Intensity");
  177. public static readonly string[] renderModeNames = Enum.GetNames(typeof(RenderMode));
  178. public static GUIContent renderModeText = new GUIContent("RenderMode", "RenderMode");
  179. public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
  180. public static GUIContent cullModeText = new GUIContent("Cull", "CullMode");
  181. public static readonly string[] cullModeNames = Enum.GetNames(typeof(CullMode));
  182. public static GUIContent enableVertexColor = new GUIContent("Enable VertexColor","Enable VertexColor");
  183. }
  184. }
  185. //#endif