LayaEffectGUI.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEngine;
  4. using UnityEditor;
  5. class LayaEffectGUI : ShaderGUI
  6. {
  7. public enum RenderMode
  8. {
  9. Additive = 0,
  10. AlphaBlended = 1
  11. }
  12. MaterialProperty renderMode = null;
  13. MaterialProperty diffuseTexture = null;
  14. MaterialProperty diffuseColor = null;
  15. MaterialEditor m_MaterialEditor;
  16. bool m_FirstTimeApply = true;
  17. public void FindProperties(MaterialProperty[] props)
  18. {
  19. diffuseTexture = FindProperty("_MainTex", props);
  20. diffuseColor = FindProperty("_TintColor", props);
  21. renderMode = FindProperty("_Mode", props);
  22. }
  23. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  24. {
  25. // render the default gui
  26. FindProperties(props);
  27. m_MaterialEditor = materialEditor;
  28. Material material = materialEditor.target as Material;
  29. if (m_FirstTimeApply)
  30. {
  31. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  32. m_FirstTimeApply = false;
  33. }
  34. ShaderPropertiesGUI(material);
  35. }
  36. public void ShaderPropertiesGUI(Material material)
  37. {
  38. // Use default labelWidth
  39. EditorGUIUtility.labelWidth = 0f;
  40. // Detect any changes to the material
  41. EditorGUI.BeginChangeCheck();
  42. {
  43. //renderMode
  44. GUILayout.BeginHorizontal();
  45. GUILayout.Label(Styles.renderModeText, GUILayout.Width(120));
  46. var mode = (RenderMode)renderMode.floatValue;
  47. mode = (RenderMode)EditorGUILayout.Popup((int)mode, Styles.renderModeNames);
  48. GUILayout.EndHorizontal();
  49. //Diffuse
  50. m_MaterialEditor.ShaderProperty(diffuseColor, Styles.MainColorText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  51. //Diffuse
  52. m_MaterialEditor.TexturePropertySingleLine(Styles.DiffuseTextureText, diffuseTexture);
  53. //scaleAndOffset
  54. m_MaterialEditor.TextureScaleOffsetProperty(diffuseTexture);
  55. if (EditorGUI.EndChangeCheck())
  56. {
  57. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  58. //renderMode
  59. renderMode.floatValue = (float)mode;
  60. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  61. }
  62. }
  63. m_MaterialEditor.RenderQueueField();
  64. }
  65. public void onChangeRender(Material material, RenderMode mode)
  66. {
  67. switch (mode)
  68. {
  69. case RenderMode.Additive:
  70. material.SetInt("_Mode", 0);
  71. material.SetInt("_AlphaTest", 0);
  72. material.SetInt("_AlphaBlend", 1);
  73. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  74. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
  75. material.SetInt("_ZWrite", 0);
  76. material.SetInt("_ZTest", 4);
  77. material.DisableKeyword("_ALPHATEST_ON");
  78. material.EnableKeyword("_ALPHABLEND_ON");
  79. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  80. material.EnableKeyword("ADDTIVEFOG");
  81. break;
  82. case RenderMode.AlphaBlended:
  83. material.SetInt("_Mode", 1);
  84. material.SetInt("_AlphaTest", 0);
  85. material.SetInt("_AlphaBlend", 1);
  86. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  87. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  88. material.SetInt("_ZWrite", 0);
  89. material.SetInt("_ZTest", 4);
  90. material.DisableKeyword("_ALPHATEST_ON");
  91. material.EnableKeyword("_ALPHABLEND_ON");
  92. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  93. material.DisableKeyword("ADDTIVEFOG");
  94. break;
  95. default:
  96. material.SetInt("_Mode", 1);
  97. material.SetInt("_AlphaTest", 0);
  98. material.SetInt("_AlphaBlend", 1);
  99. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  100. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  101. material.SetInt("_ZWrite", 0);
  102. material.SetInt("_ZTest", 4);
  103. material.DisableKeyword("_ALPHATEST_ON");
  104. material.EnableKeyword("_ALPHABLEND_ON");
  105. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  106. material.EnableKeyword("ADDTIVEFOG");
  107. break;
  108. }
  109. }
  110. public static class Styles
  111. {
  112. public static GUIStyle optionsButton = "PaneOptions";
  113. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  114. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  115. public static string emptyTootip = "";
  116. public static GUIContent MainColorText = new GUIContent("Color", "Color");
  117. public static GUIContent DiffuseTextureText = new GUIContent("Texture", "Texture");
  118. public static readonly string[] renderModeNames = Enum.GetNames(typeof(RenderMode));
  119. public static GUIContent renderModeText = new GUIContent("RenderMode", "RenderMode");
  120. }
  121. }
  122. #endif