MaterialManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace FairyGUI
  6. {
  7. [Flags]
  8. public enum MaterialFlags
  9. {
  10. Clipped = 1,
  11. SoftClipped = 2,
  12. StencilTest = 4,
  13. AlphaMask = 8,
  14. Grayed = 16,
  15. ColorFilter = 32
  16. }
  17. /// <summary>
  18. /// Every texture-shader combination has a MaterialManager.
  19. /// </summary>
  20. public class MaterialManager
  21. {
  22. public event Action<Material> onCreateNewMaterial;
  23. public bool firstMaterialInFrame;
  24. NTexture _texture;
  25. Shader _shader;
  26. List<string> _addKeywords;
  27. Dictionary<int, List<MaterialRef>> _materials;
  28. bool _combineTexture;
  29. class MaterialRef
  30. {
  31. public Material material;
  32. public int frame;
  33. public BlendMode blendMode;
  34. public uint group;
  35. }
  36. const int internalKeywordsCount = 6;
  37. static string[] internalKeywords = new[] { "CLIPPED", "SOFT_CLIPPED", null, "ALPHA_MASK", "GRAYED", "COLOR_FILTER" };
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="texture"></param>
  42. /// <param name="shader"></param>
  43. internal MaterialManager(NTexture texture, Shader shader)
  44. {
  45. _texture = texture;
  46. _shader = shader;
  47. _materials = new Dictionary<int, List<MaterialRef>>();
  48. _combineTexture = texture.alphaTexture != null;
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. /// <param name="keywords"></param>
  54. /// <returns></returns>
  55. public int GetFlagsByKeywords(IList<string> keywords)
  56. {
  57. if (_addKeywords == null)
  58. _addKeywords = new List<string>();
  59. int flags = 0;
  60. for (int i = 0; i < keywords.Count; i++)
  61. {
  62. string s = keywords[i];
  63. if (string.IsNullOrEmpty(s))
  64. continue;
  65. int j = _addKeywords.IndexOf(s);
  66. if (j == -1)
  67. {
  68. j = _addKeywords.Count;
  69. _addKeywords.Add(s);
  70. }
  71. flags += (1 << (j + internalKeywordsCount));
  72. }
  73. return flags;
  74. }
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. /// <param name="flags"></param>
  79. /// <param name="blendMode"></param>
  80. /// <param name="group"></param>
  81. /// <returns></returns>
  82. public Material GetMaterial(int flags, BlendMode blendMode, uint group)
  83. {
  84. if (blendMode != BlendMode.Normal && BlendModeUtils.Factors[(int)blendMode].pma)
  85. flags |= (int)MaterialFlags.ColorFilter;
  86. List<MaterialRef> items;
  87. if (!_materials.TryGetValue(flags, out items))
  88. {
  89. items = new List<MaterialRef>();
  90. _materials[flags] = items;
  91. }
  92. int frameId = Time.frameCount;
  93. int cnt = items.Count;
  94. MaterialRef result = null;
  95. for (int i = 0; i < cnt; i++)
  96. {
  97. MaterialRef item = items[i];
  98. if (item.group == group && item.blendMode == blendMode)
  99. {
  100. if (item.frame != frameId)
  101. {
  102. firstMaterialInFrame = true;
  103. item.frame = frameId;
  104. }
  105. else
  106. firstMaterialInFrame = false;
  107. if (_combineTexture)
  108. item.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
  109. return item.material;
  110. }
  111. else if (result == null && (item.frame > frameId || item.frame < frameId - 1)) //collect materials if it is unused in last frame
  112. result = item;
  113. }
  114. if (result == null)
  115. {
  116. result = new MaterialRef() { material = CreateMaterial(flags) };
  117. items.Add(result);
  118. }
  119. else if (_combineTexture)
  120. result.material.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
  121. if (result.blendMode != blendMode)
  122. {
  123. BlendModeUtils.Apply(result.material, blendMode);
  124. result.blendMode = blendMode;
  125. }
  126. result.group = group;
  127. result.frame = frameId;
  128. firstMaterialInFrame = true;
  129. return result.material;
  130. }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. /// <returns></returns>
  135. Material CreateMaterial(int flags)
  136. {
  137. Material mat = new Material(_shader);
  138. mat.mainTexture = _texture.nativeTexture;
  139. if (_texture.alphaTexture != null)
  140. {
  141. mat.EnableKeyword("COMBINED");
  142. mat.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
  143. }
  144. for (int i = 0; i < internalKeywordsCount; i++)
  145. {
  146. if ((flags & (1 << i)) != 0)
  147. {
  148. string s = internalKeywords[i];
  149. if (s != null)
  150. mat.EnableKeyword(s);
  151. }
  152. }
  153. if (_addKeywords != null)
  154. {
  155. int keywordCnt = _addKeywords.Count;
  156. for (int i = 0; i < keywordCnt; i++)
  157. {
  158. if ((flags & (1 << (i + internalKeywordsCount))) != 0)
  159. mat.EnableKeyword(_addKeywords[i]);
  160. }
  161. }
  162. mat.hideFlags = DisplayObject.hideFlags;
  163. if (onCreateNewMaterial != null)
  164. onCreateNewMaterial(mat);
  165. return mat;
  166. }
  167. /// <summary>
  168. ///
  169. /// </summary>
  170. public void DestroyMaterials()
  171. {
  172. var iter = _materials.GetEnumerator();
  173. while (iter.MoveNext())
  174. {
  175. List<MaterialRef> items = iter.Current.Value;
  176. if (Application.isPlaying)
  177. {
  178. int cnt = items.Count;
  179. for (int j = 0; j < cnt; j++)
  180. Object.Destroy(items[j].material);
  181. }
  182. else
  183. {
  184. int cnt = items.Count;
  185. for (int j = 0; j < cnt; j++)
  186. Object.DestroyImmediate(items[j].material);
  187. }
  188. items.Clear();
  189. }
  190. iter.Dispose();
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. public void RefreshMaterials()
  196. {
  197. _combineTexture = _texture.alphaTexture != null;
  198. var iter = _materials.GetEnumerator();
  199. while (iter.MoveNext())
  200. {
  201. List<MaterialRef> items = iter.Current.Value;
  202. int cnt = items.Count;
  203. for (int j = 0; j < cnt; j++)
  204. {
  205. Material mat = items[j].material;
  206. mat.mainTexture = _texture.nativeTexture;
  207. if (_combineTexture)
  208. {
  209. mat.EnableKeyword("COMBINED");
  210. mat.SetTexture(ShaderConfig.ID_AlphaTex, _texture.alphaTexture);
  211. }
  212. }
  213. }
  214. iter.Dispose();
  215. }
  216. }
  217. }