BaseMaterialEffect.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace Coffee.UIEffects
  9. {
  10. /// <summary>
  11. /// Abstract effect base for UI.
  12. /// </summary>
  13. [DisallowMultipleComponent]
  14. public abstract class BaseMaterialEffect : BaseMeshEffect, IParameterTexture, IMaterialModifier
  15. {
  16. protected static readonly Hash128 k_InvalidHash = new Hash128();
  17. protected static readonly List<UIVertex> s_TempVerts = new List<UIVertex>();
  18. private static readonly StringBuilder s_StringBuilder = new StringBuilder();
  19. Hash128 _effectMaterialHash;
  20. /// <summary>
  21. /// Gets or sets the parameter index.
  22. /// </summary>
  23. public int parameterIndex { get; set; }
  24. /// <summary>
  25. /// Gets the parameter texture.
  26. /// </summary>
  27. public virtual ParameterTexture paramTex
  28. {
  29. get { return null; }
  30. }
  31. /// <summary>
  32. /// Mark the vertices as dirty.
  33. /// </summary>
  34. public void SetMaterialDirty()
  35. {
  36. connector.SetMaterialDirty(graphic);
  37. foreach (var effect in syncEffects)
  38. {
  39. effect.SetMaterialDirty();
  40. }
  41. }
  42. public virtual Hash128 GetMaterialHash(Material baseMaterial)
  43. {
  44. return k_InvalidHash;
  45. }
  46. public Material GetModifiedMaterial(Material baseMaterial)
  47. {
  48. return GetModifiedMaterial(baseMaterial, graphic);
  49. }
  50. public virtual Material GetModifiedMaterial(Material baseMaterial, Graphic graphic)
  51. {
  52. if (!isActiveAndEnabled) return baseMaterial;
  53. var oldHash = _effectMaterialHash;
  54. _effectMaterialHash = GetMaterialHash(baseMaterial);
  55. var modifiedMaterial = baseMaterial;
  56. if (_effectMaterialHash.isValid)
  57. {
  58. modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, ModifyMaterial, graphic);
  59. }
  60. MaterialCache.Unregister(oldHash);
  61. return modifiedMaterial;
  62. }
  63. // protected bool isTMProMobile (Material material)
  64. // {
  65. // return material && material.shader && material.shader.name.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal);
  66. // }
  67. public virtual void ModifyMaterial(Material newMaterial, Graphic graphic)
  68. {
  69. if (isActiveAndEnabled && paramTex != null)
  70. paramTex.RegisterMaterial(newMaterial);
  71. }
  72. protected void SetShaderVariants(Material newMaterial, params object[] variants)
  73. {
  74. // Set shader keywords as variants
  75. var keywords = variants.Where(x => 0 < (int) x)
  76. .Select(x => x.ToString().ToUpper())
  77. .Concat(newMaterial.shaderKeywords)
  78. .Distinct()
  79. .ToArray();
  80. newMaterial.shaderKeywords = keywords;
  81. // Add variant name
  82. s_StringBuilder.Length = 0;
  83. s_StringBuilder.Append(Path.GetFileName(newMaterial.shader.name));
  84. foreach (var keyword in keywords)
  85. {
  86. s_StringBuilder.Append("-");
  87. s_StringBuilder.Append(keyword);
  88. }
  89. newMaterial.name = s_StringBuilder.ToString();
  90. }
  91. #if UNITY_EDITOR
  92. protected override void Reset()
  93. {
  94. if (!isActiveAndEnabled) return;
  95. SetMaterialDirty();
  96. SetVerticesDirty();
  97. SetEffectParamsDirty();
  98. }
  99. protected override void OnValidate()
  100. {
  101. if (!isActiveAndEnabled) return;
  102. SetVerticesDirty();
  103. SetEffectParamsDirty();
  104. }
  105. #endif
  106. /// <summary>
  107. /// This function is called when the object becomes enabled and active.
  108. /// </summary>
  109. protected override void OnEnable()
  110. {
  111. base.OnEnable();
  112. if (paramTex != null)
  113. {
  114. paramTex.Register(this);
  115. }
  116. SetMaterialDirty();
  117. SetEffectParamsDirty();
  118. // foreach (var mr in GetComponentsInChildren<UIEffectMaterialResolver> ())
  119. // {
  120. // mr.GetComponent<Graphic> ().SetMaterialDirty ();
  121. // mr.GetComponent<Graphic> ().SetVerticesDirty ();
  122. // }
  123. }
  124. /// <summary>
  125. /// This function is called when the behaviour becomes disabled () or inactive.
  126. /// </summary>
  127. protected override void OnDisable()
  128. {
  129. base.OnDisable();
  130. SetMaterialDirty();
  131. if (paramTex != null)
  132. {
  133. paramTex.Unregister(this);
  134. }
  135. MaterialCache.Unregister(_effectMaterialHash);
  136. _effectMaterialHash = k_InvalidHash;
  137. }
  138. // protected override void OnDidApplyAnimationProperties()
  139. // {
  140. // SetEffectParamsDirty();
  141. // }
  142. // protected override void OnTextChanged (UnityEngine.Object obj)
  143. // {
  144. // base.OnTextChanged (obj);
  145. //
  146. //
  147. // foreach (var sm in GetComponentsInChildren<TMPro.TMP_SubMeshUI> ())
  148. // {
  149. // if(!sm.GetComponent<UIEffectMaterialResolver>())
  150. // {
  151. // var mr = sm.gameObject.AddComponent<UIEffectMaterialResolver> ();
  152. //
  153. // targetGraphic.SetAllDirty ();
  154. // //targetGraphic.SetVerticesDirty ();
  155. //
  156. // //mr.GetComponent<Graphic> ().SetMaterialDirty ();
  157. // //mr.GetComponent<Graphic> ().SetVerticesDirty ();
  158. //
  159. //
  160. // }
  161. // }
  162. // }
  163. }
  164. }