FillMesh.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using UnityEngine;
  2. namespace FairyGUI
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class FillMesh : IMeshFactory
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public FillMethod method;
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public int origin;
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public float amount;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public bool clockwise;
  25. public FillMesh()
  26. {
  27. clockwise = true;
  28. amount = 1;
  29. }
  30. public void OnPopulateMesh(VertexBuffer vb)
  31. {
  32. float amount = Mathf.Clamp01(this.amount);
  33. switch (method)
  34. {
  35. case FillMethod.Horizontal:
  36. FillHorizontal(vb, vb.contentRect, origin, amount);
  37. break;
  38. case FillMethod.Vertical:
  39. FillVertical(vb, vb.contentRect, origin, amount);
  40. break;
  41. case FillMethod.Radial90:
  42. FillRadial90(vb, vb.contentRect, (Origin90)origin, amount, clockwise);
  43. break;
  44. case FillMethod.Radial180:
  45. FillRadial180(vb, vb.contentRect, (Origin180)origin, amount, clockwise);
  46. break;
  47. case FillMethod.Radial360:
  48. FillRadial360(vb, vb.contentRect, (Origin360)origin, amount, clockwise);
  49. break;
  50. }
  51. }
  52. static void FillHorizontal(VertexBuffer vb, Rect vertRect, int origin, float amount)
  53. {
  54. float a = vertRect.width * amount;
  55. if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
  56. vertRect.x += (vertRect.width - a);
  57. vertRect.width = a;
  58. vb.AddQuad(vertRect);
  59. vb.AddTriangles();
  60. }
  61. static void FillVertical(VertexBuffer vb, Rect vertRect, int origin, float amount)
  62. {
  63. float a = vertRect.height * amount;
  64. if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
  65. vertRect.y += (vertRect.height - a);
  66. vertRect.height = a;
  67. vb.AddQuad(vertRect);
  68. vb.AddTriangles();
  69. }
  70. //4 vertex
  71. static void FillRadial90(VertexBuffer vb, Rect vertRect, Origin90 origin, float amount, bool clockwise)
  72. {
  73. bool flipX = origin == Origin90.TopRight || origin == Origin90.BottomRight;
  74. bool flipY = origin == Origin90.BottomLeft || origin == Origin90.BottomRight;
  75. if (flipX != flipY)
  76. clockwise = !clockwise;
  77. float ratio = clockwise ? amount : (1 - amount);
  78. float tan = Mathf.Tan(Mathf.PI * 0.5f * ratio);
  79. bool thresold = false;
  80. if (ratio != 1)
  81. thresold = (vertRect.height / vertRect.width - tan) > 0;
  82. if (!clockwise)
  83. thresold = !thresold;
  84. float x = vertRect.x + (ratio == 0 ? float.MaxValue : (vertRect.height / tan));
  85. float y = vertRect.y + (ratio == 1 ? float.MaxValue : (vertRect.width * tan));
  86. float x2 = x;
  87. float y2 = y;
  88. if (flipX)
  89. x2 = vertRect.width - x;
  90. if (flipY)
  91. y2 = vertRect.height - y;
  92. float xMin = flipX ? (vertRect.width - vertRect.x) : vertRect.xMin;
  93. float yMin = flipY ? (vertRect.height - vertRect.y) : vertRect.yMin;
  94. float xMax = flipX ? -vertRect.xMin : vertRect.xMax;
  95. float yMax = flipY ? -vertRect.yMin : vertRect.yMax;
  96. vb.AddVert(new Vector3(xMin, yMin, 0));
  97. if (clockwise)
  98. vb.AddVert(new Vector3(xMax, yMin, 0));
  99. if (y > vertRect.yMax)
  100. {
  101. if (thresold)
  102. vb.AddVert(new Vector3(x2, yMax, 0));
  103. else
  104. vb.AddVert(new Vector3(xMax, yMax, 0));
  105. }
  106. else
  107. vb.AddVert(new Vector3(xMax, y2, 0));
  108. if (x > vertRect.xMax)
  109. {
  110. if (thresold)
  111. vb.AddVert(new Vector3(xMax, y2, 0));
  112. else
  113. vb.AddVert(new Vector3(xMax, yMax, 0));
  114. }
  115. else
  116. vb.AddVert(new Vector3(x2, yMax, 0));
  117. if (!clockwise)
  118. vb.AddVert(new Vector3(xMin, yMax, 0));
  119. if (flipX == flipY)
  120. {
  121. vb.AddTriangle(0, 1, 2);
  122. vb.AddTriangle(0, 2, 3);
  123. }
  124. else
  125. {
  126. vb.AddTriangle(2, 1, 0);
  127. vb.AddTriangle(3, 2, 0);
  128. }
  129. }
  130. //8 vertex
  131. static void FillRadial180(VertexBuffer vb, Rect vertRect, Origin180 origin, float amount, bool clockwise)
  132. {
  133. switch (origin)
  134. {
  135. case Origin180.Top:
  136. if (amount <= 0.5f)
  137. {
  138. vertRect.width /= 2;
  139. if (clockwise)
  140. vertRect.x += vertRect.width;
  141. FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.TopRight, amount / 0.5f, clockwise);
  142. Vector3 vec = vb.GetPosition(-4);
  143. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  144. vb.AddTriangles(-4);
  145. }
  146. else
  147. {
  148. vertRect.width /= 2;
  149. if (!clockwise)
  150. vertRect.x += vertRect.width;
  151. FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.TopLeft, (amount - 0.5f) / 0.5f, clockwise);
  152. if (clockwise)
  153. vertRect.x += vertRect.width;
  154. else
  155. vertRect.x -= vertRect.width;
  156. vb.AddQuad(vertRect);
  157. vb.AddTriangles(-4);
  158. }
  159. break;
  160. case Origin180.Bottom:
  161. if (amount <= 0.5f)
  162. {
  163. vertRect.width /= 2;
  164. if (!clockwise)
  165. vertRect.x += vertRect.width;
  166. FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.BottomLeft, amount / 0.5f, clockwise);
  167. Vector3 vec = vb.GetPosition(-4);
  168. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  169. vb.AddTriangles(-4);
  170. }
  171. else
  172. {
  173. vertRect.width /= 2;
  174. if (clockwise)
  175. vertRect.x += vertRect.width;
  176. FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.BottomRight, (amount - 0.5f) / 0.5f, clockwise);
  177. if (clockwise)
  178. vertRect.x -= vertRect.width;
  179. else
  180. vertRect.x += vertRect.width;
  181. vb.AddQuad(vertRect);
  182. vb.AddTriangles(-4);
  183. }
  184. break;
  185. case Origin180.Left:
  186. if (amount <= 0.5f)
  187. {
  188. vertRect.height /= 2;
  189. if (!clockwise)
  190. vertRect.y += vertRect.height;
  191. FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.TopLeft, amount / 0.5f, clockwise);
  192. Vector3 vec = vb.GetPosition(-4);
  193. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  194. vb.AddTriangles(-4);
  195. }
  196. else
  197. {
  198. vertRect.height /= 2;
  199. if (clockwise)
  200. vertRect.y += vertRect.height;
  201. FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.BottomLeft, (amount - 0.5f) / 0.5f, clockwise);
  202. if (clockwise)
  203. vertRect.y -= vertRect.height;
  204. else
  205. vertRect.y += vertRect.height;
  206. vb.AddQuad(vertRect);
  207. vb.AddTriangles(-4);
  208. }
  209. break;
  210. case Origin180.Right:
  211. if (amount <= 0.5f)
  212. {
  213. vertRect.height /= 2;
  214. if (clockwise)
  215. vertRect.y += vertRect.height;
  216. FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.BottomRight, amount / 0.5f, clockwise);
  217. Vector3 vec = vb.GetPosition(-4);
  218. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  219. vb.AddTriangles(-4);
  220. }
  221. else
  222. {
  223. vertRect.height /= 2;
  224. if (!clockwise)
  225. vertRect.y += vertRect.height;
  226. FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.TopRight, (amount - 0.5f) / 0.5f, clockwise);
  227. if (clockwise)
  228. vertRect.y += vertRect.height;
  229. else
  230. vertRect.y -= vertRect.height;
  231. vb.AddQuad(vertRect);
  232. vb.AddTriangles(-4);
  233. }
  234. break;
  235. }
  236. }
  237. //12 vertex
  238. static void FillRadial360(VertexBuffer vb, Rect vertRect, Origin360 origin, float amount, bool clockwise)
  239. {
  240. switch (origin)
  241. {
  242. case Origin360.Top:
  243. if (amount < 0.5f)
  244. {
  245. vertRect.width /= 2;
  246. if (clockwise)
  247. vertRect.x += vertRect.width;
  248. FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, amount / 0.5f, clockwise);
  249. Vector3 vec = vb.GetPosition(-8);
  250. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  251. vb.AddTriangles(-4);
  252. }
  253. else
  254. {
  255. vertRect.width /= 2;
  256. if (!clockwise)
  257. vertRect.x += vertRect.width;
  258. FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, (amount - 0.5f) / 0.5f, clockwise);
  259. if (clockwise)
  260. vertRect.x += vertRect.width;
  261. else
  262. vertRect.x -= vertRect.width;
  263. vb.AddQuad(vertRect);
  264. vb.AddTriangles(-4);
  265. }
  266. break;
  267. case Origin360.Bottom:
  268. if (amount < 0.5f)
  269. {
  270. vertRect.width /= 2;
  271. if (!clockwise)
  272. vertRect.x += vertRect.width;
  273. FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, amount / 0.5f, clockwise);
  274. Vector3 vec = vb.GetPosition(-8);
  275. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  276. vb.AddTriangles(-4);
  277. }
  278. else
  279. {
  280. vertRect.width /= 2;
  281. if (clockwise)
  282. vertRect.x += vertRect.width;
  283. FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, (amount - 0.5f) / 0.5f, clockwise);
  284. if (clockwise)
  285. vertRect.x -= vertRect.width;
  286. else
  287. vertRect.x += vertRect.width;
  288. vb.AddQuad(vertRect);
  289. vb.AddTriangles(-4);
  290. }
  291. break;
  292. case Origin360.Left:
  293. if (amount < 0.5f)
  294. {
  295. vertRect.height /= 2;
  296. if (!clockwise)
  297. vertRect.y += vertRect.height;
  298. FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, amount / 0.5f, clockwise);
  299. Vector3 vec = vb.GetPosition(-8);
  300. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  301. vb.AddTriangles(-4);
  302. }
  303. else
  304. {
  305. vertRect.height /= 2;
  306. if (clockwise)
  307. vertRect.y += vertRect.height;
  308. FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, (amount - 0.5f) / 0.5f, clockwise);
  309. if (clockwise)
  310. vertRect.y -= vertRect.height;
  311. else
  312. vertRect.y += vertRect.height;
  313. vb.AddQuad(vertRect);
  314. vb.AddTriangles(-4);
  315. }
  316. break;
  317. case Origin360.Right:
  318. if (amount < 0.5f)
  319. {
  320. vertRect.height /= 2;
  321. if (clockwise)
  322. vertRect.y += vertRect.height;
  323. FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, amount / 0.5f, clockwise);
  324. Vector3 vec = vb.GetPosition(-8);
  325. vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
  326. vb.AddTriangles(-4);
  327. }
  328. else
  329. {
  330. vertRect.height /= 2;
  331. if (!clockwise)
  332. vertRect.y += vertRect.height;
  333. FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, (amount - 0.5f) / 0.5f, clockwise);
  334. if (clockwise)
  335. vertRect.y += vertRect.height;
  336. else
  337. vertRect.y -= vertRect.height;
  338. vb.AddQuad(vertRect);
  339. vb.AddTriangles(-4);
  340. }
  341. break;
  342. }
  343. }
  344. }
  345. }