RegularPolygonMesh.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using UnityEngine;
  2. namespace FairyGUI
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class RegularPolygonMesh : IMeshFactory, IHitTest
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public Rect? drawRect;
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public int sides;
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public float lineWidth;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public Color32 lineColor;
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public Color32? centerColor;
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. public Color32? fillColor;
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public float[] distances;
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public float rotation;
  41. public RegularPolygonMesh()
  42. {
  43. sides = 3;
  44. lineColor = Color.black;
  45. }
  46. public void OnPopulateMesh(VertexBuffer vb)
  47. {
  48. if (distances != null && distances.Length < sides)
  49. {
  50. Debug.LogError("distances.Length<sides");
  51. return;
  52. }
  53. Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
  54. Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
  55. float angleDelta = 2 * Mathf.PI / sides;
  56. float angle = rotation * Mathf.Deg2Rad;
  57. float radius = Mathf.Min(rect.width / 2, rect.height / 2);
  58. float centerX = radius + rect.x;
  59. float centerY = radius + rect.y;
  60. vb.AddVert(new Vector3(centerX, centerY, 0), centerColor == null ? color : (Color32)centerColor);
  61. for (int i = 0; i < sides; i++)
  62. {
  63. float r = radius;
  64. if (distances != null)
  65. r *= distances[i];
  66. float xv = Mathf.Cos(angle) * (r - lineWidth);
  67. float yv = Mathf.Sin(angle) * (r - lineWidth);
  68. Vector3 vec = new Vector3(xv + centerX, yv + centerY, 0);
  69. vb.AddVert(vec, color);
  70. if (lineWidth > 0)
  71. {
  72. vb.AddVert(vec, lineColor);
  73. xv = Mathf.Cos(angle) * r + centerX;
  74. yv = Mathf.Sin(angle) * r + centerY;
  75. vb.AddVert(new Vector3(xv, yv, 0), lineColor);
  76. }
  77. angle += angleDelta;
  78. }
  79. if (lineWidth > 0)
  80. {
  81. int tmp = sides * 3;
  82. for (int i = 0; i < tmp; i += 3)
  83. {
  84. if (i != tmp - 3)
  85. {
  86. vb.AddTriangle(0, i + 1, i + 4);
  87. vb.AddTriangle(i + 5, i + 2, i + 3);
  88. vb.AddTriangle(i + 3, i + 6, i + 5);
  89. }
  90. else
  91. {
  92. vb.AddTriangle(0, i + 1, 1);
  93. vb.AddTriangle(2, i + 2, i + 3);
  94. vb.AddTriangle(i + 3, 3, 2);
  95. }
  96. }
  97. }
  98. else
  99. {
  100. for (int i = 0; i < sides; i++)
  101. vb.AddTriangle(0, i + 1, (i == sides - 1) ? 1 : i + 2);
  102. }
  103. }
  104. public bool HitTest(Rect contentRect, Vector2 point)
  105. {
  106. if (drawRect != null)
  107. return ((Rect)drawRect).Contains(point);
  108. else
  109. return contentRect.Contains(point);
  110. }
  111. }
  112. }