UnityShaders.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using CommonLang.IO;
  6. using System.Reflection;
  7. using CommonUI_Unity3D.Impl;
  8. namespace CommonUI_Unity3D.Impl
  9. {
  10. public static class UnityShaders
  11. {
  12. public static void InitShaders()
  13. {
  14. Debug.Log("- InitShaders ");
  15. UI_IMG_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_Image"));
  16. UI_IMG_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_ImageGray"));
  17. UI_M3Z_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_ImageMask"));
  18. UI_M3Z_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_ImageMaskGray"));
  19. UI_TXT_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_Text"));
  20. UI_TXT_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_TextGray"));
  21. UI_Shape_Shader = new Material(Shader.Find("iPhone/MFUI_Shape"));
  22. UI_IMG_BASE_Shader.mainTextureScale = new Vector2(1, 1);
  23. UI_IMG_GRAY_Shader.mainTextureScale = new Vector2(1, 1);
  24. UI_M3Z_BASE_Shader.mainTextureScale = new Vector2(1, 1);
  25. UI_M3Z_GRAY_Shader.mainTextureScale = new Vector2(1, 1);
  26. UI_IMG_BASE_Shader.color = UnityEngine.Color.white;
  27. UI_IMG_GRAY_Shader.color = UnityEngine.Color.white;
  28. UI_M3Z_BASE_Shader.color = UnityEngine.Color.white;
  29. UI_M3Z_GRAY_Shader.color = UnityEngine.Color.white;
  30. UI_TXT_BASE_Shader.color = UnityEngine.Color.white;
  31. UI_TXT_GRAY_Shader.color = UnityEngine.Color.white;
  32. UI_Shape_Shader.color = UnityEngine.Color.white;
  33. UI_Image_Shader = Shader.Find("MFUGUI/Image");
  34. UI_ImageM3Z_Shader = Shader.Find("MFUGUI/ImageM3Z");
  35. UI_TextGray_Shader = Shader.Find("MFUGUI/TextGray");
  36. }
  37. public static Shader UI_Image_Shader { get; private set; }
  38. public static Shader UI_ImageM3Z_Shader { get; private set; }
  39. public static Shader UI_TextGray_Shader { get; private set; }
  40. public static Material CreateMaterialUGUI(UnityImage text)
  41. {
  42. if (text.TextureMask == null)
  43. {
  44. Material ret = new Material(UI_Image_Shader);
  45. ret.mainTextureScale = new Vector2(1, 1);
  46. ret.color = UnityEngine.Color.white;
  47. ret.mainTexture = text.Texture;
  48. //ret.SetFloat("_Gray", 1);
  49. ret.SetPass(0);
  50. return ret;
  51. }
  52. else
  53. {
  54. Material ret = new Material(UI_ImageM3Z_Shader);
  55. ret.mainTextureScale = new Vector2(1, 1);
  56. ret.color = UnityEngine.Color.white;
  57. ret.mainTexture = text.Texture;
  58. ret.SetTexture("_MaskTex", text.TextureMask);
  59. //ret.SetFloat("_Gray", 1);
  60. ret.SetPass(0);
  61. return ret;
  62. }
  63. }
  64. public static Material UI_IMG_BASE_Shader { get; private set; }
  65. public static Material UI_IMG_GRAY_Shader { get; private set; }
  66. public static Material UI_M3Z_BASE_Shader { get; private set; }
  67. public static Material UI_M3Z_GRAY_Shader { get; private set; }
  68. public static Material UI_TXT_BASE_Shader { get; private set; }
  69. public static Material UI_TXT_GRAY_Shader { get; private set; }
  70. public static Material UI_Shape_Shader;
  71. private static Color mColorShape = Color.white;
  72. private static Color mColorImage = Color.white;
  73. private static CommonUI.Display.Blend mBlend = CommonUI.Display.Blend.BLEND_MODE_NORMAL;
  74. public static void BeginText(UnityTextLayer text)
  75. {
  76. switch (mBlend)
  77. {
  78. case CommonUI.Display.Blend.BLEND_MODE_GRAY:
  79. UI_TXT_GRAY_Shader.mainTexture = text.mTexture;
  80. UI_TXT_GRAY_Shader.SetPass(0);
  81. break;
  82. default:
  83. UI_TXT_BASE_Shader.mainTexture = text.mTexture;
  84. UI_TXT_BASE_Shader.SetPass(0);
  85. break;
  86. }
  87. }
  88. public static void BeginImage(IUnityImageInterface image)
  89. {
  90. if (image.TextureMask == null)
  91. {
  92. switch (mBlend)
  93. {
  94. case CommonUI.Display.Blend.BLEND_MODE_GRAY:
  95. UI_IMG_GRAY_Shader.mainTexture = image.Texture;
  96. UI_IMG_GRAY_Shader.SetPass(0);
  97. break;
  98. default:
  99. UI_IMG_BASE_Shader.mainTexture = image.Texture;
  100. UI_IMG_BASE_Shader.SetPass(0);
  101. break;
  102. }
  103. }
  104. else
  105. {
  106. switch (mBlend)
  107. {
  108. case CommonUI.Display.Blend.BLEND_MODE_GRAY:
  109. UI_M3Z_GRAY_Shader.mainTexture = image.Texture;
  110. UI_M3Z_GRAY_Shader.SetTexture("_MaskTex", image.TextureMask);
  111. UI_M3Z_GRAY_Shader.SetPass(0);
  112. break;
  113. default:
  114. UI_M3Z_BASE_Shader.mainTexture = image.Texture;
  115. UI_M3Z_BASE_Shader.SetTexture("_MaskTex", image.TextureMask);
  116. UI_M3Z_BASE_Shader.SetPass(0);
  117. break;
  118. }
  119. }
  120. }
  121. public static void BeginShape()
  122. {
  123. UI_Shape_Shader.SetPass(0);
  124. }
  125. public static void SetColor(UnityEngine.Color color)
  126. {
  127. mColorShape = color;
  128. UI_Shape_Shader.SetColor("_clrBase", mColorShape);
  129. }
  130. public static void SetAlpha(float a)
  131. {
  132. mColorShape.a = a;
  133. mColorImage.a = a;
  134. UI_IMG_BASE_Shader.SetColor("_clrBase", mColorImage);
  135. UI_IMG_GRAY_Shader.SetColor("_clrBase", mColorImage);
  136. UI_M3Z_BASE_Shader.SetColor("_clrBase", mColorImage);
  137. UI_M3Z_GRAY_Shader.SetColor("_clrBase", mColorImage);
  138. UI_TXT_BASE_Shader.SetColor("_clrBase", mColorImage);
  139. UI_TXT_GRAY_Shader.SetColor("_clrBase", mColorImage);
  140. UI_Shape_Shader.SetColor("_clrBase", mColorShape);
  141. }
  142. public static void SetBlend(CommonUI.Display.Blend blend)
  143. {
  144. mBlend = blend;
  145. }
  146. }
  147. }