Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [ExecuteInEditMode]
  6. public class UICircle : Graphic
  7. {
  8. [Range(0, 100)]
  9. public int fillPercent;
  10. public bool fill = true;
  11. public int thickness = 5;
  12. [Range(0, 360)]
  13. public int segments = 360;
  14. [SerializeField]
  15. protected Texture setTexture;
  16. public override Texture mainTexture
  17. {
  18. get
  19. {
  20. return (setTexture == null ? s_WhiteTexture : setTexture);
  21. }
  22. }
  23. // Texture to be used.
  24. public Texture texture
  25. {
  26. get
  27. {
  28. return setTexture;
  29. }
  30. set
  31. {
  32. if (setTexture == value)
  33. {
  34. return;
  35. }
  36. setTexture = value;
  37. SetVerticesDirty();
  38. SetMaterialDirty();
  39. }
  40. }
  41. protected virtual void Update()
  42. {
  43. thickness = (int)Mathf.Clamp(thickness, 0, rectTransform.rect.width / 2);
  44. }
  45. protected virtual UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs)
  46. {
  47. UIVertex[] vbo = new UIVertex[4];
  48. for (int i = 0; i < vertices.Length; i++)
  49. {
  50. UIVertex vert = UIVertex.simpleVert;
  51. vert.color = color;
  52. vert.position = vertices[i];
  53. vert.uv0 = uvs[i];
  54. vbo[i] = vert;
  55. }
  56. return vbo;
  57. }
  58. protected override void OnPopulateMesh(VertexHelper vh)
  59. {
  60. float outer = -rectTransform.pivot.x * rectTransform.rect.width;
  61. float inner = -rectTransform.pivot.x * rectTransform.rect.width + thickness;
  62. vh.Clear();
  63. Vector2 prevX = Vector2.zero;
  64. Vector2 prevY = Vector2.zero;
  65. Vector2 uv0 = new Vector2(0, 0);
  66. Vector2 uv1 = new Vector2(0, 1);
  67. Vector2 uv2 = new Vector2(1, 1);
  68. Vector2 uv3 = new Vector2(1, 0);
  69. Vector2 pos0;
  70. Vector2 pos1;
  71. Vector2 pos2;
  72. Vector2 pos3;
  73. float f = (fillPercent / 100f);
  74. float degrees = 360f / segments;
  75. int fa = (int)((segments + 1) * f);
  76. for (int i = -1 - (fa / 2); i < fa / 2 + 1; i++)
  77. {
  78. float rad = Mathf.Deg2Rad * (i * degrees);
  79. float c = Mathf.Cos(rad);
  80. float s = Mathf.Sin(rad);
  81. uv0 = new Vector2(0, 1);
  82. uv1 = new Vector2(1, 1);
  83. uv2 = new Vector2(1, 0);
  84. uv3 = new Vector2(0, 0);
  85. pos0 = prevX;
  86. pos1 = new Vector2(outer * c, outer * s);
  87. if (fill)
  88. {
  89. pos2 = Vector2.zero;
  90. pos3 = Vector2.zero;
  91. }
  92. else
  93. {
  94. pos2 = new Vector2(inner * c, inner * s);
  95. pos3 = prevY;
  96. }
  97. prevX = pos1;
  98. prevY = pos2;
  99. vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }));
  100. }
  101. }
  102. }
  103. }