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.

117 lines
2.8 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. public class PlanetRing : MonoBehaviour
  6. {
  7. //manual settings
  8. [Range(3, 360)]
  9. public int segments = 3;
  10. public float innerRadius = 0.7f;
  11. public float thickness = 0.5f;
  12. public Material ringMat;
  13. //cached references
  14. GameObject ring;
  15. Mesh ringMesh;
  16. MeshFilter ringMF;
  17. MeshRenderer ringMR;
  18. void OnEnable()
  19. {
  20. if (ring == null || ringMesh == null)
  21. {
  22. SetUpRing();
  23. }
  24. BuildRingMesh();
  25. }
  26. void OnValidate()
  27. {
  28. if (ring == null || ringMesh == null)
  29. {
  30. SetUpRing();
  31. }
  32. BuildRingMesh();
  33. }
  34. void SetUpRing()
  35. {
  36. //check if ring is null and there are no children
  37. if (ring == null && transform.childCount == 0)
  38. {
  39. //create ring object
  40. ring = new GameObject(name + " Ring");
  41. ring.transform.parent = transform;
  42. ring.transform.SetAsFirstSibling();
  43. ring.transform.localScale = Vector3.one;
  44. ring.transform.localPosition = Vector3.zero;
  45. ring.transform.localRotation = Quaternion.identity;
  46. ringMF = ring.AddComponent<MeshFilter>();
  47. ringMR = ring.AddComponent<MeshRenderer>();
  48. ringMR.material = ringMat;
  49. }
  50. else
  51. {
  52. ring = transform.GetChild(0).gameObject;
  53. ringMF = ring.GetComponent<MeshFilter>();
  54. ringMR = ring.GetComponent<MeshRenderer>();
  55. }
  56. ringMesh = new Mesh();
  57. ringMF.sharedMesh = ringMesh;
  58. }
  59. void BuildRingMesh()
  60. {
  61. //build ring mesh
  62. Vector3[] vertices = new Vector3[(segments + 1) * 2 * 2];
  63. int[] triangles = new int[segments * 6 * 2];
  64. Vector2[] uv = new Vector2[(segments + 1) * 2 * 2];
  65. int halfway = (segments + 1) * 2;
  66. for (int i = 0; i < segments + 1; i++)
  67. {
  68. float progress = (float)i / (float)segments;
  69. float angle = Mathf.Deg2Rad * progress * 360;
  70. float x = Mathf.Sin(angle);
  71. float z = Mathf.Cos(angle);
  72. vertices[i * 2] = vertices[i * 2 + halfway] = new Vector3(x, 0f, z) * (innerRadius + thickness);
  73. vertices[i * 2 + 1] = vertices[i * 2 + 1 + halfway] = new Vector3(x, 0f, z) * innerRadius;
  74. uv[i * 2] = uv[i * 2 + halfway] = new Vector2(progress, 0f);
  75. uv[i * 2 + 1] = uv[i * 2 + 1 + halfway] = new Vector2(progress, 1f);
  76. if (i != segments)
  77. {
  78. triangles[i * 12] = i * 2;
  79. triangles[i * 12 + 1] = triangles[i * 12 + 4] = (i + 1) * 2;
  80. triangles[i * 12 + 2] = triangles[i * 12 + 3] = i * 2 + 1;
  81. triangles[i * 12 + 5] = (i + 1) * 2 + 1;
  82. triangles[i * 12 + 6] = i * 2 + halfway;
  83. triangles[i * 12 + 7] = triangles[i * 12 + 10] = i * 2 + 1 + halfway;
  84. triangles[i * 12 + 8] = triangles[i * 12 + 9] = (i + 1) * 2 + halfway;
  85. triangles[i * 12 + 11] = (i + 1) * 2 + 1 + halfway;
  86. }
  87. }
  88. if (vertices.Length < ringMesh.vertices.Length)
  89. {
  90. ringMesh.triangles = triangles;
  91. ringMesh.vertices = vertices;
  92. }
  93. else
  94. {
  95. ringMesh.vertices = vertices;
  96. ringMesh.triangles = triangles;
  97. }
  98. ringMesh.uv = uv;
  99. ringMesh.RecalculateNormals();
  100. }
  101. }