UIGradient.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Coffee.UIEffects
  4. {
  5. /// <summary>
  6. /// UIGradient.
  7. /// </summary>
  8. [DisallowMultipleComponent]
  9. [AddComponentMenu("UI/UIEffects/UIGradient", 101)]
  10. public class UIGradient : BaseMeshEffect
  11. {
  12. static readonly Vector2[] s_SplitedCharacterPosition = {Vector2.up, Vector2.one, Vector2.right, Vector2.zero};
  13. /// <summary>
  14. /// Gradient direction.
  15. /// </summary>
  16. public enum Direction
  17. {
  18. Horizontal,
  19. Vertical,
  20. Angle,
  21. Diagonal,
  22. }
  23. /// <summary>
  24. /// Gradient space for Text.
  25. /// </summary>
  26. public enum GradientStyle
  27. {
  28. Rect,
  29. Fit,
  30. Split,
  31. }
  32. [Tooltip("Gradient Direction.")] [SerializeField]
  33. Direction m_Direction;
  34. [Tooltip("Color1: Top or Left.")] [SerializeField]
  35. Color m_Color1 = Color.white;
  36. [Tooltip("Color2: Bottom or Right.")] [SerializeField]
  37. Color m_Color2 = Color.white;
  38. [Tooltip("Color3: For diagonal.")] [SerializeField]
  39. Color m_Color3 = Color.white;
  40. [Tooltip("Color4: For diagonal.")] [SerializeField]
  41. Color m_Color4 = Color.white;
  42. [Tooltip("Gradient rotation.")] [SerializeField] [Range(-180, 180)]
  43. float m_Rotation;
  44. [Tooltip("Gradient offset for Horizontal, Vertical or Angle.")] [SerializeField] [Range(-1, 1)]
  45. float m_Offset1;
  46. [Tooltip("Gradient offset for Diagonal.")] [SerializeField] [Range(-1, 1)]
  47. float m_Offset2;
  48. [Tooltip("Gradient style for Text.")] [SerializeField]
  49. GradientStyle m_GradientStyle;
  50. [Tooltip("Color space to correct color.")] [SerializeField]
  51. ColorSpace m_ColorSpace = ColorSpace.Uninitialized;
  52. [Tooltip("Ignore aspect ratio.")] [SerializeField]
  53. bool m_IgnoreAspectRatio = true;
  54. /// <summary>
  55. /// Gradient Direction.
  56. /// </summary>
  57. public Direction direction
  58. {
  59. get { return m_Direction; }
  60. set
  61. {
  62. if (m_Direction == value) return;
  63. m_Direction = value;
  64. SetVerticesDirty();
  65. }
  66. }
  67. /// <summary>
  68. /// Color1: Top or Left.
  69. /// </summary>
  70. public Color color1
  71. {
  72. get { return m_Color1; }
  73. set
  74. {
  75. if (m_Color1 == value) return;
  76. m_Color1 = value;
  77. SetVerticesDirty();
  78. }
  79. }
  80. /// <summary>
  81. /// Color2: Bottom or Right.
  82. /// </summary>
  83. public Color color2
  84. {
  85. get { return m_Color2; }
  86. set
  87. {
  88. if (m_Color2 == value) return;
  89. m_Color2 = value;
  90. SetVerticesDirty();
  91. }
  92. }
  93. /// <summary>
  94. /// Color3: For diagonal.
  95. /// </summary>
  96. public Color color3
  97. {
  98. get { return m_Color3; }
  99. set
  100. {
  101. if (m_Color3 == value) return;
  102. m_Color3 = value;
  103. SetVerticesDirty();
  104. }
  105. }
  106. /// <summary>
  107. /// Color4: For diagonal.
  108. /// </summary>
  109. public Color color4
  110. {
  111. get { return m_Color4; }
  112. set
  113. {
  114. if (m_Color4 == value) return;
  115. m_Color4 = value;
  116. SetVerticesDirty();
  117. }
  118. }
  119. /// <summary>
  120. /// Gradient rotation.
  121. /// </summary>
  122. public float rotation
  123. {
  124. get
  125. {
  126. return m_Direction == Direction.Horizontal ? -90
  127. : m_Direction == Direction.Vertical ? 0
  128. : m_Rotation;
  129. }
  130. set
  131. {
  132. if (Mathf.Approximately(m_Rotation, value)) return;
  133. m_Rotation = value;
  134. SetVerticesDirty();
  135. }
  136. }
  137. /// <summary>
  138. /// Gradient offset for Horizontal, Vertical or Angle.
  139. /// </summary>
  140. public float offset
  141. {
  142. get { return m_Offset1; }
  143. set
  144. {
  145. if (Mathf.Approximately(m_Offset1, value)) return;
  146. m_Offset1 = value;
  147. SetVerticesDirty();
  148. }
  149. }
  150. /// <summary>
  151. /// Gradient offset for Diagonal.
  152. /// </summary>
  153. public Vector2 offset2
  154. {
  155. get { return new Vector2(m_Offset2, m_Offset1); }
  156. set
  157. {
  158. if (Mathf.Approximately(m_Offset1, value.y) && Mathf.Approximately(m_Offset2, value.x)) return;
  159. m_Offset1 = value.y;
  160. m_Offset2 = value.x;
  161. SetVerticesDirty();
  162. }
  163. }
  164. /// <summary>
  165. /// Gradient style for Text.
  166. /// </summary>
  167. public GradientStyle gradientStyle
  168. {
  169. get { return m_GradientStyle; }
  170. set
  171. {
  172. if (m_GradientStyle == value) return;
  173. m_GradientStyle = value;
  174. SetVerticesDirty();
  175. }
  176. }
  177. /// <summary>
  178. /// Color space to correct color.
  179. /// </summary>
  180. public ColorSpace colorSpace
  181. {
  182. get { return m_ColorSpace; }
  183. set
  184. {
  185. if (m_ColorSpace == value) return;
  186. m_ColorSpace = value;
  187. SetVerticesDirty();
  188. }
  189. }
  190. /// <summary>
  191. /// Ignore aspect ratio.
  192. /// </summary>
  193. public bool ignoreAspectRatio
  194. {
  195. get { return m_IgnoreAspectRatio; }
  196. set
  197. {
  198. if (m_IgnoreAspectRatio == value) return;
  199. m_IgnoreAspectRatio = value;
  200. SetVerticesDirty();
  201. }
  202. }
  203. /// <summary>
  204. /// Call used to modify mesh.
  205. /// </summary>
  206. public override void ModifyMesh(VertexHelper vh, Graphic graphic)
  207. {
  208. if (!isActiveAndEnabled)
  209. return;
  210. // Gradient space.
  211. var rect = default(Rect);
  212. var vertex = default(UIVertex);
  213. switch (m_GradientStyle)
  214. {
  215. case GradientStyle.Rect:
  216. // RectTransform.
  217. rect = graphic.rectTransform.rect;
  218. break;
  219. case GradientStyle.Split:
  220. // Each characters.
  221. rect.Set(0, 0, 1, 1);
  222. break;
  223. case GradientStyle.Fit:
  224. {
  225. // Fit to contents.
  226. rect.xMin = rect.yMin = float.MaxValue;
  227. rect.xMax = rect.yMax = float.MinValue;
  228. for (var i = 0; i < vh.currentVertCount; i++)
  229. {
  230. vh.PopulateUIVertex(ref vertex, i);
  231. rect.xMin = Mathf.Min(rect.xMin, vertex.position.x);
  232. rect.yMin = Mathf.Min(rect.yMin, vertex.position.y);
  233. rect.xMax = Mathf.Max(rect.xMax, vertex.position.x);
  234. rect.yMax = Mathf.Max(rect.yMax, vertex.position.y);
  235. }
  236. break;
  237. }
  238. }
  239. // Gradient rotation.
  240. var rad = rotation * Mathf.Deg2Rad;
  241. var dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
  242. if (!m_IgnoreAspectRatio && Direction.Angle <= m_Direction)
  243. {
  244. dir.x *= rect.height / rect.width;
  245. dir = dir.normalized;
  246. }
  247. // Calculate vertex color.
  248. var localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix.
  249. for (var i = 0; i < vh.currentVertCount; i++)
  250. {
  251. vh.PopulateUIVertex(ref vertex, i);
  252. // Normalize vertex position by local matrix.
  253. Vector2 normalizedPos;
  254. if (m_GradientStyle == GradientStyle.Split)
  255. {
  256. // Each characters.
  257. normalizedPos = localMatrix * s_SplitedCharacterPosition[i % 4] + offset2;
  258. }
  259. else
  260. {
  261. normalizedPos = localMatrix * vertex.position + offset2;
  262. }
  263. // Interpolate vertex color.
  264. Color color;
  265. if (direction == Direction.Diagonal)
  266. {
  267. color = Color.LerpUnclamped(
  268. Color.LerpUnclamped(m_Color1, m_Color2, normalizedPos.x),
  269. Color.LerpUnclamped(m_Color3, m_Color4, normalizedPos.x),
  270. normalizedPos.y);
  271. }
  272. else
  273. {
  274. color = Color.LerpUnclamped(m_Color2, m_Color1, normalizedPos.y);
  275. }
  276. // Correct color.
  277. vertex.color *= (m_ColorSpace == ColorSpace.Gamma) ? color.gamma
  278. : (m_ColorSpace == ColorSpace.Linear) ? color.linear
  279. : color;
  280. vh.SetUIVertex(vertex, i);
  281. }
  282. }
  283. }
  284. }