ProcedurM3Image.cs 7.6 KB

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