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.

219 lines
6.1 KiB

  1. /************************************************************************************
  2. See SampleFramework license.txt for license terms. Unless required by applicable law
  3. or agreed to in writing, the sample code is provided AS IS WITHOUT WARRANTIES OR
  4. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  5. language governing permissions and limitations under the license.
  6. ************************************************************************************/
  7. using UnityEngine;
  8. using System.Collections.Generic;
  9. /// <summary>
  10. /// Procedurally generated capsule mesh which matches the character controller size.
  11. /// This was originally created for visualizing the capsule in the HMD but it could be adapted to other purposes.
  12. /// </summary>
  13. [ExecuteInEditMode]
  14. public class CharacterCapsule : MonoBehaviour
  15. {
  16. private CharacterController _character;
  17. private MeshFilter _meshFilter;
  18. private float _height;
  19. private float _radius;
  20. [Range(4,32)]
  21. public int SubdivisionsU;
  22. [Range(4, 32)]
  23. public int SubdivisionsV;
  24. private int _subdivisionU;
  25. private int _subdivisionV;
  26. private Vector3[] _vertices;
  27. private int[] _triangles;
  28. // Update is called once per frame
  29. void Update ()
  30. {
  31. if (_character == null)
  32. {
  33. _character = GetComponentInParent<CharacterController>();
  34. if (_character == null)
  35. {
  36. return;
  37. }
  38. }
  39. if (_height == _character.height
  40. && _radius == _character.radius
  41. && _subdivisionU == SubdivisionsU
  42. && _subdivisionV == SubdivisionsV)
  43. {
  44. return;
  45. }
  46. _height = _character.height;
  47. _radius = _character.radius;
  48. _subdivisionU = SubdivisionsU;
  49. _subdivisionV = SubdivisionsV;
  50. List<Vector3> verts = new List<Vector3>();
  51. var vector = new Vector3(1,0,0);
  52. // Generate the mesh
  53. var topOffset = new Vector3(0, _height/2.0f - _radius, 0);
  54. var bottomOffset = new Vector3(0, _radius - _height/2.0f, 0);
  55. // Add all the necessary vertices
  56. verts.Add(new Vector3(0, _height/2.0f, 0));
  57. for (int u = SubdivisionsU - 1; u >= 0; u--)
  58. {
  59. float uf = (float) u / (float) SubdivisionsU;
  60. for (int v = 0; v < SubdivisionsV; v++)
  61. {
  62. float vf = (float) v / (float) SubdivisionsV;
  63. var q = Quaternion.Euler(0, vf * 360.0f, uf * 90.0f);
  64. var vert = q * vector;
  65. vert *= _radius;
  66. var v1 = vert + topOffset;
  67. verts.Add(v1);
  68. }
  69. }
  70. for (int u = 0; u < SubdivisionsU; u++)
  71. {
  72. float uf = (float)u / (float)SubdivisionsU;
  73. for (int v = 0; v < SubdivisionsV; v++)
  74. {
  75. float vf = (float)v / (float)SubdivisionsV;
  76. var q = Quaternion.Euler(0, vf * 360.0f + 180.0f, uf * 90.0f);
  77. var vert = q * vector;
  78. vert *= _radius;
  79. var v2 = bottomOffset - vert;
  80. verts.Add(v2);
  81. }
  82. }
  83. verts.Add(new Vector3(0, -_height / 2.0f, 0));
  84. // Setup all the triangles
  85. List<int> tris = new List<int>();
  86. int index;
  87. int i;
  88. // top cap
  89. for (int v = 0; v < SubdivisionsV; v++)
  90. {
  91. i = 0;
  92. tris.Add(i);
  93. tris.Add(v);
  94. tris.Add(v+1);
  95. }
  96. tris.Add(0);
  97. tris.Add(SubdivisionsV);
  98. tris.Add(1);
  99. // top hemisphere
  100. for (int u = 0; u < SubdivisionsU - 1; u++)
  101. {
  102. index = u * SubdivisionsV + 1;
  103. for (int v = 0; v < SubdivisionsV - 1; v++)
  104. {
  105. i = index + v;
  106. tris.Add(i);
  107. tris.Add(i + SubdivisionsV);
  108. tris.Add(i + 1);
  109. tris.Add(i + 1);
  110. tris.Add(i + SubdivisionsV);
  111. tris.Add(i + SubdivisionsV + 1);
  112. }
  113. i = index + SubdivisionsV - 1;
  114. tris.Add(i);
  115. tris.Add(i + SubdivisionsV);
  116. tris.Add(i + 1 - SubdivisionsV);
  117. tris.Add(i + 1 - SubdivisionsV);
  118. tris.Add(i + SubdivisionsV);
  119. tris.Add(i + 1);
  120. }
  121. // center tube
  122. index = (SubdivisionsU - 1) * SubdivisionsV + 1;
  123. for (int v = 0; v < SubdivisionsV - 1; v++)
  124. {
  125. i = index + v;
  126. tris.Add(i);
  127. tris.Add(i + SubdivisionsV);
  128. tris.Add(i + 1);
  129. tris.Add(i + 1);
  130. tris.Add(i + SubdivisionsV);
  131. tris.Add(i + SubdivisionsV + 1);
  132. }
  133. i = index + SubdivisionsV - 1;
  134. tris.Add(i);
  135. tris.Add(i + SubdivisionsV);
  136. tris.Add(i + 1 - SubdivisionsV);
  137. tris.Add(i + 1 - SubdivisionsV);
  138. tris.Add(i + SubdivisionsV);
  139. tris.Add(i + 1);
  140. // bottom hemisphere
  141. for (int u = 0; u < SubdivisionsU - 1; u++)
  142. {
  143. index = u * SubdivisionsV + (SubdivisionsU * SubdivisionsV) + 1;
  144. for (int v = 0; v < SubdivisionsV - 1; v++)
  145. {
  146. i = index + v;
  147. tris.Add(i);
  148. tris.Add(i + SubdivisionsV);
  149. tris.Add(i + 1);
  150. tris.Add(i + 1);
  151. tris.Add(i + SubdivisionsV);
  152. tris.Add(i + SubdivisionsV + 1);
  153. }
  154. i = index + SubdivisionsV - 1;
  155. tris.Add(i);
  156. tris.Add(i + SubdivisionsV);
  157. tris.Add(i + 1 - SubdivisionsV);
  158. tris.Add(i + 1 - SubdivisionsV);
  159. tris.Add(i + SubdivisionsV);
  160. tris.Add(i + 1);
  161. }
  162. // bottom cap
  163. var last = verts.Count - 1;
  164. var lastRow = last - SubdivisionsV;
  165. for (int v = 0; v < SubdivisionsV; v++)
  166. {
  167. i = 0;
  168. tris.Add(last);
  169. tris.Add(lastRow + v + 1);
  170. tris.Add(lastRow + v);
  171. }
  172. tris.Add(last);
  173. tris.Add(lastRow);
  174. tris.Add(last - 1);
  175. _vertices = verts.ToArray();
  176. _triangles = tris.ToArray();
  177. _meshFilter = gameObject.GetComponent<MeshFilter>();
  178. _meshFilter.mesh = new Mesh();
  179. _meshFilter.sharedMesh.vertices = _vertices;
  180. _meshFilter.sharedMesh.triangles = _triangles;
  181. _meshFilter.sharedMesh.RecalculateNormals();
  182. }
  183. }