LayaUnlitGUI.cs 8.8 KB

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