Unmask.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using UnityEngine;
  2. using UnityEngine.Profiling;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.UI;
  5. namespace Coffee.UIExtensions
  6. {
  7. /// <summary>
  8. /// Reverse masking for parent Mask component.
  9. /// </summary>
  10. [ExecuteInEditMode]
  11. [AddComponentMenu("UI/Unmask/Unmask", 1)]
  12. public class Unmask : MonoBehaviour, IMaterialModifier
  13. {
  14. //################################
  15. // Constant or Static Members.
  16. //################################
  17. private static readonly Vector2 s_Center = new Vector2(0.5f, 0.5f);
  18. //################################
  19. // Serialize Members.
  20. //################################
  21. [Tooltip("Fit graphic's transform to target transform.")]
  22. [SerializeField] private RectTransform m_FitTarget;
  23. [Tooltip("Fit graphic's transform to target transform on LateUpdate every frame.")]
  24. [SerializeField] private bool m_FitOnLateUpdate;
  25. [Tooltip("Unmask affects only for children.")]
  26. [SerializeField] private bool m_OnlyForChildren = false;
  27. [Tooltip("Show the graphic that is associated with the unmask render area.")]
  28. [SerializeField] private bool m_ShowUnmaskGraphic = false;
  29. [Tooltip("Edge smoothing.")]
  30. [Range(0f, 1f)]
  31. [SerializeField] private float m_EdgeSmoothing = 0f;
  32. //################################
  33. // Public Members.
  34. //################################
  35. /// <summary>
  36. /// The graphic associated with the unmask.
  37. /// </summary>
  38. public MaskableGraphic graphic { get { return _graphic ?? (_graphic = GetComponent<MaskableGraphic>()); } }
  39. /// <summary>
  40. /// Fit graphic's transform to target transform.
  41. /// </summary>
  42. public RectTransform fitTarget
  43. {
  44. get { return m_FitTarget; }
  45. set
  46. {
  47. m_FitTarget = value;
  48. FitTo(m_FitTarget);
  49. }
  50. }
  51. /// <summary>
  52. /// Fit graphic's transform to target transform on LateUpdate every frame.
  53. /// </summary>
  54. public bool fitOnLateUpdate { get { return m_FitOnLateUpdate; } set { m_FitOnLateUpdate = value; } }
  55. /// <summary>
  56. /// Show the graphic that is associated with the unmask render area.
  57. /// </summary>
  58. public bool showUnmaskGraphic
  59. {
  60. get { return m_ShowUnmaskGraphic; }
  61. set
  62. {
  63. m_ShowUnmaskGraphic = value;
  64. SetDirty();
  65. }
  66. }
  67. /// <summary>
  68. /// Unmask affects only for children.
  69. /// </summary>
  70. public bool onlyForChildren
  71. {
  72. get { return m_OnlyForChildren; }
  73. set
  74. {
  75. m_OnlyForChildren = value;
  76. SetDirty();
  77. }
  78. }
  79. /// <summary>
  80. /// Edge smooting.
  81. /// </summary>
  82. public float edgeSmoothing
  83. {
  84. get { return m_EdgeSmoothing; }
  85. set { m_EdgeSmoothing = value; }
  86. }
  87. /// <summary>
  88. /// Perform material modification in this function.
  89. /// </summary>
  90. /// <returns>Modified material.</returns>
  91. /// <param name="baseMaterial">Configured Material.</param>
  92. public Material GetModifiedMaterial(Material baseMaterial)
  93. {
  94. if (!isActiveAndEnabled)
  95. {
  96. return baseMaterial;
  97. }
  98. Transform stopAfter = MaskUtilities.FindRootSortOverrideCanvas(transform);
  99. var stencilDepth = MaskUtilities.GetStencilDepth(transform, stopAfter);
  100. var desiredStencilBit = 1 << stencilDepth;
  101. StencilMaterial.Remove(_unmaskMaterial);
  102. _unmaskMaterial = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Invert, CompareFunction.Equal, m_ShowUnmaskGraphic ? ColorWriteMask.All : (ColorWriteMask)0, desiredStencilBit - 1, (1 << 8) - 1);
  103. // Unmask affects only for children.
  104. var canvasRenderer = graphic.canvasRenderer;
  105. if (m_OnlyForChildren)
  106. {
  107. StencilMaterial.Remove(_revertUnmaskMaterial);
  108. _revertUnmaskMaterial = StencilMaterial.Add(baseMaterial, (1 << 7), StencilOp.Invert, CompareFunction.Equal, (ColorWriteMask)0, (1 << 7), (1 << 8) - 1);
  109. canvasRenderer.hasPopInstruction = true;
  110. canvasRenderer.popMaterialCount = 1;
  111. canvasRenderer.SetPopMaterial(_revertUnmaskMaterial, 0);
  112. }
  113. else
  114. {
  115. canvasRenderer.hasPopInstruction = false;
  116. canvasRenderer.popMaterialCount = 0;
  117. }
  118. return _unmaskMaterial;
  119. }
  120. /// <summary>
  121. /// Fit to target transform.
  122. /// </summary>
  123. /// <param name="target">Target transform.</param>
  124. public void FitTo(RectTransform target)
  125. {
  126. var rt = transform as RectTransform;
  127. rt.pivot = target.pivot;
  128. rt.position = target.position;
  129. rt.rotation = target.rotation;
  130. var s1 = target.lossyScale;
  131. var s2 = rt.parent.lossyScale;
  132. rt.localScale = new Vector3(s1.x / s2.x, s1.y / s2.y, s1.z / s2.z);
  133. rt.sizeDelta = target.rect.size;
  134. rt.anchorMax = rt.anchorMin = s_Center;
  135. }
  136. //################################
  137. // Private Members.
  138. //################################
  139. private Material _unmaskMaterial;
  140. private Material _revertUnmaskMaterial;
  141. private MaskableGraphic _graphic;
  142. /// <summary>
  143. /// This function is called when the object becomes enabled and active.
  144. /// </summary>
  145. private void OnEnable()
  146. {
  147. if (m_FitTarget)
  148. {
  149. FitTo(m_FitTarget);
  150. }
  151. SetDirty();
  152. }
  153. /// <summary>
  154. /// This function is called when the behaviour becomes disabled () or inactive.
  155. /// </summary>
  156. private void OnDisable()
  157. {
  158. StencilMaterial.Remove(_unmaskMaterial);
  159. StencilMaterial.Remove(_revertUnmaskMaterial);
  160. _unmaskMaterial = null;
  161. _revertUnmaskMaterial = null;
  162. if (graphic)
  163. {
  164. var canvasRenderer = graphic.canvasRenderer;
  165. canvasRenderer.hasPopInstruction = false;
  166. canvasRenderer.popMaterialCount = 0;
  167. graphic.SetMaterialDirty();
  168. }
  169. SetDirty();
  170. }
  171. /// <summary>
  172. /// LateUpdate is called every frame, if the Behaviour is enabled.
  173. /// </summary>
  174. private void LateUpdate()
  175. {
  176. #if UNITY_EDITOR
  177. if (m_FitTarget && (m_FitOnLateUpdate || !Application.isPlaying))
  178. #else
  179. if (m_FitTarget && m_FitOnLateUpdate)
  180. #endif
  181. {
  182. FitTo(m_FitTarget);
  183. }
  184. Smoothing(graphic, m_EdgeSmoothing);
  185. }
  186. #if UNITY_EDITOR
  187. /// <summary>
  188. /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
  189. /// </summary>
  190. private void OnValidate()
  191. {
  192. SetDirty();
  193. }
  194. #endif
  195. /// <summary>
  196. /// Mark the graphic as dirty.
  197. /// </summary>
  198. void SetDirty()
  199. {
  200. if (graphic)
  201. {
  202. graphic.SetMaterialDirty();
  203. }
  204. }
  205. private static void Smoothing(MaskableGraphic graphic, float smooth)
  206. {
  207. if (!graphic) return;
  208. Profiler.BeginSample("[Unmask] Smoothing");
  209. var canvasRenderer = graphic.canvasRenderer;
  210. var currentColor = canvasRenderer.GetColor();
  211. var targetAlpha = 1f;
  212. if (graphic.maskable && 0 < smooth)
  213. {
  214. var currentAlpha = graphic.color.a * canvasRenderer.GetInheritedAlpha();
  215. if (0 < currentAlpha)
  216. {
  217. targetAlpha = Mathf.Lerp(0.01f, 0.002f, smooth) / currentAlpha;
  218. }
  219. }
  220. if (!Mathf.Approximately(currentColor.a, targetAlpha))
  221. {
  222. currentColor.a = Mathf.Clamp01(targetAlpha);
  223. canvasRenderer.SetColor(currentColor);
  224. }
  225. Profiler.EndSample();
  226. }
  227. }
  228. }