ProcedurM2Image.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Sprites;
  4. using UnityEngine.UI;
  5. /* Author: Josh H.
  6. * Procedural UI Image
  7. * assetstore.joshh@gmail.com for feedback or questions
  8. */
  9. namespace UnityEngine.UI
  10. {
  11. public class ProcedurM2Image : RawImage
  12. {
  13. protected override void OnPopulateMesh(VertexHelper toFill)
  14. {
  15. //note: Sliced and Tiled have no effect to this currently.
  16. GenerateSimpleSprite(toFill);
  17. }
  18. private Vector4 GetDrawingDimensions(bool shouldPreserveAspect)
  19. {
  20. var padding = sprite == null ? Vector4.zero : DataUtility.GetPadding(sprite);
  21. Rect r = GetPixelAdjustedRect();
  22. var size = sprite == null ? new Vector2(r.width, r.height) : new Vector2(sprite.rect.width, sprite.rect.height);
  23. //Debug.Log(string.Format("r:{2}, size:{0}, padding:{1}", size, padding, r));
  24. int spriteW = Mathf.RoundToInt(size.x);
  25. int spriteH = Mathf.RoundToInt(size.y);
  26. if (shouldPreserveAspect && size.sqrMagnitude > 0.0f)
  27. {
  28. var spriteRatio = size.x / size.y;
  29. var rectRatio = r.width / r.height;
  30. if (spriteRatio > rectRatio)
  31. {
  32. var oldHeight = r.height;
  33. r.height = r.width * (1.0f / spriteRatio);
  34. r.y += (oldHeight - r.height) * rectTransform.pivot.y;
  35. }
  36. else
  37. {
  38. var oldWidth = r.width;
  39. r.width = r.height * spriteRatio;
  40. r.x += (oldWidth - r.width) * rectTransform.pivot.x;
  41. }
  42. }
  43. var v = new Vector4(
  44. padding.x / spriteW,
  45. padding.y / spriteH,
  46. (spriteW - padding.z) / spriteW,
  47. (spriteH - padding.w) / spriteH);
  48. v = new Vector4(
  49. r.x + r.width * v.x,
  50. r.y + r.height * v.y,
  51. r.x + r.width * v.z,
  52. r.y + r.height * v.w
  53. );
  54. return v;
  55. }
  56. //每个角最大的三角形数,一般5-8个就有不错的圆角效果,设置Max防止不必要的性能浪费
  57. const int MaxTriangleNum = 20;
  58. const int MinTriangleNum = 1;
  59. [SerializeField]
  60. public float Radius = 10;
  61. //使用几个三角形去填充每个角的四分之一圆
  62. [Range(MinTriangleNum, MaxTriangleNum)]
  63. int TriangleNum = 4;
  64. private Texture2D TextureToTexture2D(Texture texture)
  65. {
  66. Texture2D texture2d = texture as Texture2D;
  67. return texture2d;
  68. }
  69. static Sprite sprite;
  70. private VertexHelper nvh;
  71. /// <summary>
  72. /// Generates the Verticies needed.
  73. /// </summary>
  74. /// <param name="vh">vertex helper</param>
  75. void GenerateSimpleSprite(VertexHelper vh)
  76. {
  77. if (texture != null && sprite == null)
  78. {
  79. sprite = Sprite.Create(TextureToTexture2D(texture), new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  80. }
  81. Vector4 v = GetDrawingDimensions(false);
  82. Vector4 uv = sprite != null ? DataUtility.GetOuterUV(sprite) : Vector4.zero;
  83. var color32 = color;
  84. vh.Clear();
  85. if(this.GetComponent<RawImageDeviceManager>())
  86. {
  87. Radius = this.GetComponent<RawImageDeviceManager>().Radius;
  88. }
  89. //对radius的值做限制,必须在0-较小的边的1/2的范围内
  90. float radius = Radius;
  91. if (radius > (v.z - v.x) / 2) radius = (v.z - v.x) / 2;
  92. if (radius > (v.w - v.y) / 2) radius = (v.w - v.y) / 2;
  93. if (radius < 0) radius = 0;
  94. //计算出uv中对应的半径值坐标轴的半径
  95. float uvRadiusX = radius / (v.z - v.x);
  96. float uvRadiusY = radius / (v.w - v.y);
  97. //0,1
  98. vh.AddVert(new Vector3(v.x, v.w - radius), color32, new Vector2(uv.x, uv.w - uvRadiusY));
  99. vh.AddVert(new Vector3(v.x, v.y + radius), color32, new Vector2(uv.x, uv.y + uvRadiusY));
  100. //2,3,4,5
  101. vh.AddVert(new Vector3(v.x + radius, v.w), color32, new Vector2(uv.x + uvRadiusX, uv.w));
  102. vh.AddVert(new Vector3(v.x + radius, v.w - radius), color32, new Vector2(uv.x + uvRadiusX, uv.w - uvRadiusY));
  103. vh.AddVert(new Vector3(v.x + radius, v.y + radius), color32, new Vector2(uv.x + uvRadiusX, uv.y + uvRadiusY));
  104. vh.AddVert(new Vector3(v.x + radius, v.y), color32, new Vector2(uv.x + uvRadiusX, uv.y));
  105. //6,7,8,9
  106. vh.AddVert(new Vector3(v.z - radius, v.w), color32, new Vector2(uv.z - uvRadiusX, uv.w));
  107. vh.AddVert(new Vector3(v.z - radius, v.w - radius), color32, new Vector2(uv.z - uvRadiusX, uv.w - uvRadiusY));
  108. vh.AddVert(new Vector3(v.z - radius, v.y + radius), color32, new Vector2(uv.z - uvRadiusX, uv.y + uvRadiusY));
  109. vh.AddVert(new Vector3(v.z - radius, v.y), color32, new Vector2(uv.z - uvRadiusX, uv.y));
  110. //10,11
  111. vh.AddVert(new Vector3(v.z, v.w - radius), color32, new Vector2(uv.z, uv.w - uvRadiusY));
  112. vh.AddVert(new Vector3(v.z, v.y + radius), color32, new Vector2(uv.z, uv.y + uvRadiusY));
  113. //左边的矩形
  114. vh.AddTriangle(1, 0, 3);
  115. vh.AddTriangle(1, 3, 4);
  116. //中间的矩形
  117. vh.AddTriangle(5, 2, 6);
  118. vh.AddTriangle(5, 6, 9);
  119. //右边的矩形
  120. vh.AddTriangle(8, 7, 10);
  121. vh.AddTriangle(8, 10, 11);
  122. //开始构造四个角
  123. List<Vector2> vCenterList = new List<Vector2>();
  124. List<Vector2> uvCenterList = new List<Vector2>();
  125. List<int> vCenterVertList = new List<int>();
  126. //右上角的圆心
  127. vCenterList.Add(new Vector2(v.z - radius, v.w - radius));
  128. uvCenterList.Add(new Vector2(uv.z - uvRadiusX, uv.w - uvRadiusY));
  129. vCenterVertList.Add(7);
  130. //左上角的圆心
  131. vCenterList.Add(new Vector2(v.x + radius, v.w - radius));
  132. uvCenterList.Add(new Vector2(uv.x + uvRadiusX, uv.w - uvRadiusY));
  133. vCenterVertList.Add(3);
  134. //左下角的圆心
  135. vCenterList.Add(new Vector2(v.x + radius, v.y + radius));
  136. uvCenterList.Add(new Vector2(uv.x + uvRadiusX, uv.y + uvRadiusY));
  137. vCenterVertList.Add(4);
  138. //右下角的圆心
  139. vCenterList.Add(new Vector2(v.z - radius, v.y + radius));
  140. uvCenterList.Add(new Vector2(uv.z - uvRadiusX, uv.y + uvRadiusY));
  141. vCenterVertList.Add(8);
  142. //每个三角形的顶角
  143. float degreeDelta = (float)(Mathf.PI / 2 / TriangleNum);
  144. //当前的角度
  145. float curDegree = 0;
  146. for (int i = 0; i < vCenterVertList.Count; i++)
  147. {
  148. int preVertNum = vh.currentVertCount;
  149. for (int j = 0; j <= TriangleNum; j++)
  150. {
  151. float cosA = Mathf.Cos(curDegree);
  152. float sinA = Mathf.Sin(curDegree);
  153. Vector3 vPosition = new Vector3(vCenterList[i].x + cosA * radius, vCenterList[i].y + sinA * radius);
  154. Vector3 uvPosition = new Vector2(uvCenterList[i].x + cosA * uvRadiusX, uvCenterList[i].y + sinA * uvRadiusY);
  155. vh.AddVert(vPosition, color32, uvPosition);
  156. curDegree += degreeDelta;
  157. }
  158. curDegree -= degreeDelta;
  159. for (int j = 0; j <= TriangleNum - 1; j++)
  160. {
  161. vh.AddTriangle(vCenterVertList[i], preVertNum + j + 1, preVertNum + j);
  162. }
  163. }
  164. }
  165. }
  166. }