MPImageBasic.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace MPUIKIT {
  5. [AddComponentMenu("UI/MPUI/MPImageBasic")]
  6. public class MPImageBasic : Image {
  7. #region SerializedFields
  8. [SerializeField] private DrawShape m_DrawShape = DrawShape.None;
  9. [SerializeField] private Type m_ImageType = Type.Simple; // Mapping in Vertex Stream
  10. [SerializeField] private float m_StrokeWidth; // MapTo -> UV2.x
  11. [SerializeField] private float m_FalloffDistance = 0.5f; // MapTo -> UV2.y
  12. [SerializeField] private float m_OutlineWidth; // MapTo -> Normal.x
  13. [SerializeField] private Color m_OutlineColor = Color.black; // MapTo -> Tangent.x, Tangent.y, Tangent.z, Tangent.w
  14. [SerializeField] private float m_ShapeRotation; // MapTo -> UV3.x Compressed
  15. [SerializeField] private bool m_ConstrainRotation = true; // MapTo -> UV3.x Compressed
  16. [SerializeField] private bool m_FlipHorizontal; // MapTo -> UV3.x Compressed
  17. [SerializeField] private bool m_FlipVertical; // MapTo -> UV3.x Compressed
  18. [SerializeField] private CornerStyleType m_CornerStyle; // MapTo -> UV3.y
  19. [SerializeField] private Vector4 m_RectangleCornerRadius; // MapTo -> Normal.y, Normal.z compressed
  20. [SerializeField] private Vector3 m_TriangleCornerRadius; // MapTo -> Normal.y, Normal.z compressed
  21. #pragma warning disable
  22. // ReSharper disable once NotAccessedField.Local
  23. [SerializeField] private bool m_TriangleUniformCornerRadius = true;
  24. // ReSharper disable once NotAccessedField.Local
  25. [SerializeField] private bool m_RectangleUniformCornerRadius = true;
  26. #pragma warning restore
  27. [SerializeField] private float m_CircleRadius; // MapTo -> Normal.y
  28. [SerializeField] private bool m_CircleFitToRect = true; // MapTo -> Normal.z
  29. [SerializeField] private int m_NStarPolygonSideCount = 3; // MapTo -> Normal.y compressed
  30. [SerializeField] private float m_NStarPolygonInset = 2f; // MapTo -> Normal.y compressed
  31. [SerializeField] private float m_NStarPolygonCornerRadius; // MapTo -> Normal.z
  32. #endregion
  33. #region Public Accessors
  34. public DrawShape Shape { get => m_DrawShape;
  35. set {
  36. m_DrawShape = value;
  37. m_Material = null;
  38. base.SetMaterialDirty();
  39. base.SetVerticesDirty();
  40. }
  41. }
  42. public float StrokeWidth {
  43. get => m_StrokeWidth;
  44. set {
  45. Vector2 size = GetPixelAdjustedRect().size;
  46. m_StrokeWidth = Mathf.Clamp(value, 0, Mathf.Min(size.x, size.y) * 0.5f);
  47. base.SetVerticesDirty();
  48. }
  49. }
  50. public float FallOffDistance {
  51. get => m_FalloffDistance;
  52. set {
  53. m_FalloffDistance = Mathf.Max(0, value);
  54. base.SetVerticesDirty();
  55. }
  56. }
  57. public float OutlineWidth {
  58. get => m_OutlineWidth;
  59. set {
  60. m_OutlineWidth = Mathf.Max(0,value);
  61. base.SetVerticesDirty();
  62. }
  63. }
  64. public Color OutlineColor {
  65. get => m_OutlineColor;
  66. set {
  67. m_OutlineColor = value;
  68. base.SetVerticesDirty();
  69. }
  70. }
  71. public float ShapeRotation {
  72. get => m_ShapeRotation;
  73. set {
  74. m_ShapeRotation = value % 360;
  75. ConstrainRotationValue();
  76. base.SetVerticesDirty();
  77. }
  78. }
  79. public bool ConstrainRotation {
  80. get => m_ConstrainRotation;
  81. set {
  82. m_ConstrainRotation = value;
  83. ConstrainRotationValue();
  84. base.SetVerticesDirty();
  85. }
  86. }
  87. public bool FlipHorizontal {
  88. get => m_FlipHorizontal;
  89. set {
  90. m_FlipHorizontal = value;
  91. base.SetVerticesDirty();
  92. }
  93. }
  94. public bool FlipVertical {
  95. get => m_FlipVertical;
  96. set {
  97. m_FlipVertical = value;
  98. base.SetVerticesDirty();
  99. }
  100. }
  101. /// <summary>
  102. /// Type of the image. Only two types are supported. Simple and Filled.
  103. /// Default and fallback value is Simple.
  104. /// </summary>
  105. public new Type type {
  106. get => m_ImageType;
  107. set {
  108. if (m_ImageType != value) {
  109. switch (value) {
  110. case Type.Simple:
  111. case Type.Filled:
  112. if (sprite) m_ImageType = value;
  113. break;
  114. case Type.Tiled:
  115. case Type.Sliced:
  116. break;
  117. default:
  118. throw new ArgumentOutOfRangeException(value.ToString(), value, null);
  119. }
  120. }
  121. if(base.type != m_ImageType) base.type = m_ImageType;
  122. base.SetAllDirty();
  123. }
  124. }
  125. public CornerStyleType CornerStyle {
  126. get => m_CornerStyle;
  127. set {
  128. m_CornerStyle = value;
  129. base.SetVerticesDirty();
  130. }
  131. }
  132. public Vector3 TriangleCornerRadius {
  133. get => m_TriangleCornerRadius;
  134. set {
  135. Vector2 size = GetPixelAdjustedRect().size;
  136. float zMax = size.x * 0.5f;
  137. m_TriangleCornerRadius.z = Mathf.Clamp(value.z, 0, zMax);
  138. float hMax = Mathf.Min(size.x, size.y) * 0.3f;
  139. m_TriangleCornerRadius.x = Mathf.Clamp(value.x, 0, hMax);
  140. m_TriangleCornerRadius.y = Mathf.Clamp(value.y, 0, hMax);
  141. base.SetVerticesDirty();
  142. }
  143. }
  144. public Vector4 RectangleCornerRadius {
  145. get => m_RectangleCornerRadius;
  146. set
  147. {
  148. m_RectangleCornerRadius = value;
  149. base.SetVerticesDirty();
  150. }
  151. }
  152. public float CircleRadius {
  153. get => m_CircleRadius;
  154. set {
  155. m_CircleRadius = Mathf.Clamp(value, 0, GetMinSize());
  156. base.SetVerticesDirty();
  157. }
  158. }
  159. public bool CircleFitToRect {
  160. get => m_CircleFitToRect;
  161. set {
  162. m_CircleFitToRect = value;
  163. base.SetVerticesDirty();
  164. }
  165. }
  166. public float NStarPolygonCornerRadius {
  167. get => m_NStarPolygonCornerRadius;
  168. set {
  169. float halfHeight = GetPixelAdjustedRect().height * 0.5f;
  170. m_NStarPolygonCornerRadius = Mathf.Clamp(value, m_NStarPolygonSideCount == 2? 0.1f : 0f, halfHeight);
  171. base.SetVerticesDirty();
  172. }
  173. }
  174. public float NStarPolygonInset {
  175. get => m_NStarPolygonInset;
  176. set {
  177. m_NStarPolygonInset = Mathf.Clamp(value, 2f, m_NStarPolygonSideCount);
  178. base.SetVerticesDirty();
  179. }
  180. }
  181. public int NStarPolygonSideCount {
  182. get => m_NStarPolygonSideCount;
  183. set {
  184. m_NStarPolygonSideCount = Mathf.Clamp(value, 2, 10);
  185. base.SetVerticesDirty();
  186. }
  187. }
  188. #endregion
  189. public override Material material {
  190. get
  191. {
  192. switch (m_DrawShape)
  193. {
  194. case DrawShape.None:
  195. return Canvas.GetDefaultCanvasMaterial();
  196. case DrawShape.Circle:
  197. case DrawShape.Triangle:
  198. case DrawShape.Rectangle:
  199. return MPMaterials.GetMaterial((int)m_DrawShape - 1, m_StrokeWidth > 0f, m_OutlineWidth > 0f);
  200. case DrawShape.Pentagon:
  201. case DrawShape.Hexagon:
  202. case DrawShape.NStarPolygon:
  203. return MPMaterials.GetMaterial(3, m_StrokeWidth > 0f, m_OutlineWidth > 0f);
  204. default:
  205. throw new ArgumentOutOfRangeException();
  206. }
  207. }
  208. set => Debug.LogWarning("Setting Material of MPImageBasic has no effect.");
  209. }
  210. public override float preferredWidth => sprite == MPImageUtility.EmptySprite ? 0 : base.preferredWidth;
  211. public override float preferredHeight => sprite == MPImageUtility.EmptySprite ? 0 : base.preferredHeight;
  212. protected override void OnEnable() {
  213. base.OnEnable();
  214. MPImageUtility.FixAdditionalShaderChannelsInCanvas(canvas);
  215. if (sprite == null) sprite = MPImageUtility.EmptySprite;
  216. }
  217. #if UNITY_EDITOR
  218. protected override void OnValidate() {
  219. base.OnValidate();
  220. Shape = m_DrawShape;
  221. if (sprite == null) sprite = MPImageUtility.EmptySprite;
  222. type = m_ImageType;
  223. StrokeWidth = m_StrokeWidth;
  224. FallOffDistance = m_FalloffDistance;
  225. OutlineWidth = m_OutlineWidth;
  226. OutlineColor = m_OutlineColor;
  227. ShapeRotation = m_ShapeRotation;
  228. ConstrainRotation = m_ConstrainRotation;
  229. FlipHorizontal = m_FlipHorizontal;
  230. FlipVertical = m_FlipVertical;
  231. CornerStyle = m_CornerStyle;
  232. }
  233. #endif
  234. private float GetMinSizeHalf() {
  235. return GetMinSize() * 0.5f;
  236. }
  237. private float GetMinSize() {
  238. Vector2 size = GetPixelAdjustedRect().size;
  239. return Mathf.Min(size.x, size.y);
  240. }
  241. private void ConstrainRotationValue() {
  242. if (!m_ConstrainRotation) return;
  243. float finalRotation = m_ShapeRotation - (m_ShapeRotation % 90);
  244. if (Mathf.Abs(finalRotation) >= 360) finalRotation = 0;
  245. m_ShapeRotation = finalRotation;
  246. }
  247. protected override void OnTransformParentChanged() {
  248. base.OnTransformParentChanged();
  249. MPImageUtility.FixAdditionalShaderChannelsInCanvas(canvas);
  250. base.SetVerticesDirty();
  251. }
  252. private MPVertexStream CreateVertexStream() {
  253. MPVertexStream stream = new MPVertexStream();
  254. RectTransform rectT = rectTransform;
  255. stream.RectTransform = rectT;
  256. Rect r = GetPixelAdjustedRect();
  257. stream.Uv1 = new Vector2(r.width + m_FalloffDistance, r.height + m_FalloffDistance);
  258. float packedRotData =
  259. PackRotationData(m_ShapeRotation, m_ConstrainRotation, m_FlipHorizontal, m_FlipVertical);
  260. stream.Uv3 = new Vector2(packedRotData, (float)m_CornerStyle);
  261. stream.Tangent = QualitySettings.activeColorSpace == ColorSpace.Linear? m_OutlineColor.linear : m_OutlineColor;
  262. Vector3 normal = new Vector3();
  263. normal.x = m_OutlineWidth;
  264. normal.y = m_StrokeWidth;
  265. normal.z = m_FalloffDistance;
  266. Vector4 data;
  267. Vector2 shapeProps;
  268. switch (m_DrawShape) {
  269. case DrawShape.Circle:
  270. shapeProps = new Vector2(m_CircleRadius, m_CircleFitToRect ? 1 : 0);
  271. break;
  272. case DrawShape.Triangle:
  273. data = m_TriangleCornerRadius;
  274. data = data / Mathf.Min(r.width, r.height);
  275. shapeProps = MPImageUtility.Encode_0_1_16(data);
  276. break;
  277. case DrawShape.Rectangle:
  278. data = FixRadius(m_RectangleCornerRadius);
  279. data = data / Mathf.Min(r.width, r.height);
  280. shapeProps = MPImageUtility.Encode_0_1_16(data);
  281. break;
  282. case DrawShape.NStarPolygon:
  283. data = new Vector4(m_NStarPolygonSideCount, m_NStarPolygonCornerRadius, m_NStarPolygonInset);
  284. data = data / Mathf.Min(r.width, r.height);
  285. shapeProps = MPImageUtility.Encode_0_1_16(data);
  286. break;
  287. default:
  288. shapeProps = Vector2.zero;
  289. break;
  290. }
  291. stream.Uv2 = shapeProps;
  292. stream.Normal = normal;
  293. return stream;
  294. }
  295. private float PackRotationData(float rotation, bool constrainRotation, bool flipH, bool flipV) {
  296. int c = constrainRotation ? 1 : 0;
  297. c += flipH ? 10 : 0;
  298. c += flipV ? 100 : 0;
  299. float cr = rotation % 360f;
  300. float sign = cr >= 0 ? 1 : -1;
  301. cr = Mathf.Abs(cr) / 360f;
  302. cr = (cr + c) * sign;
  303. return cr;
  304. }
  305. void UnPackRotation(float f) {
  306. float r = 0, x = 0, y = 0, z = 0;
  307. float sign = f >= 0.0f ? 1 : -1;
  308. f = Mathf.Abs(f);
  309. r = fract(f) * 360f * sign;
  310. f = Mathf.Floor(f);
  311. float p = f / 100f;
  312. z = Mathf.Floor(p);
  313. p = fract(p) * 10f;
  314. y = Mathf.Floor(p);
  315. p = fract(p) * 10f;
  316. x = Mathf.Round(p);
  317. // Debug.Log($"Rotation: {r}, X: {x}, Y: {y}, Z: {z}");
  318. float fract(float val) {
  319. val = Mathf.Abs(val);
  320. float ret = val - Mathf.Floor(val);
  321. return ret;
  322. }
  323. }
  324. protected override void OnRectTransformDimensionsChange()
  325. {
  326. base.OnRectTransformDimensionsChange();
  327. base.SetVerticesDirty();
  328. }
  329. private Vector4 FixRadius(Vector4 radius)
  330. {
  331. Rect rect = rectTransform.rect;
  332. radius = Vector4.Max(radius, Vector4.zero);
  333. radius = Vector4.Min(radius, Vector4.one * Mathf.Min(rect.width, rect.height));
  334. float scaleFactor =
  335. Mathf.Min (
  336. Mathf.Min (
  337. Mathf.Min (
  338. Mathf.Min (
  339. rect.width / (radius.x + radius.y),
  340. rect.width / (radius.z + radius.w)),
  341. rect.height / (radius.x + radius.w)),
  342. rect.height / (radius.z + radius.y)),
  343. 1f);
  344. return radius * scaleFactor;
  345. }
  346. protected override void OnPopulateMesh(VertexHelper toFill) {
  347. base.OnPopulateMesh(toFill);
  348. MPVertexStream stream = CreateVertexStream();
  349. UIVertex uiVert = new UIVertex();
  350. for (int i = 0; i < toFill.currentVertCount; i++) {
  351. toFill.PopulateUIVertex(ref uiVert, i);
  352. //uiVert.position += ((Vector3)uiVert.uv0 - new Vector3(0.5f, 0.5f)) * m_FalloffDistance;
  353. uiVert.uv1 = stream.Uv1;
  354. uiVert.uv2 = stream.Uv2;
  355. uiVert.uv3 = stream.Uv3;
  356. uiVert.normal = stream.Normal;
  357. uiVert.tangent = stream.Tangent;
  358. toFill.SetUIVertex(uiVert, i);
  359. }
  360. }
  361. #if UNITY_EDITOR
  362. protected override void Reset() {
  363. base.Reset();
  364. if (sprite == null) sprite = MPImageUtility.EmptySprite;
  365. }
  366. #else
  367. void Reset() {
  368. if (sprite == null) sprite = MPImageUtility.EmptySprite;
  369. }
  370. #endif
  371. }
  372. }