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.

158 lines
5.8 KiB

  1. // SteamVR Boundaries|SDK_SteamVR|005
  2. namespace VRTK
  3. {
  4. #if VRTK_DEFINE_SDK_STEAMVR
  5. using UnityEngine;
  6. #endif
  7. /// <summary>
  8. /// The SteamVR Boundaries SDK script provides a bridge to the SteamVR SDK play area.
  9. /// </summary>
  10. [SDK_Description(typeof(SDK_SteamVRSystem))]
  11. public class SDK_SteamVRBoundaries
  12. #if VRTK_DEFINE_SDK_STEAMVR
  13. : SDK_BaseBoundaries
  14. #else
  15. : SDK_FallbackBoundaries
  16. #endif
  17. {
  18. #if VRTK_DEFINE_SDK_STEAMVR
  19. protected SteamVR_PlayArea cachedSteamVRPlayArea;
  20. /// <summary>
  21. /// The InitBoundaries method is run on start of scene and can be used to initialse anything on game start.
  22. /// </summary>
  23. public override void InitBoundaries()
  24. {
  25. #if UNITY_5_6 && !VRTK_DEFINE_STEAMVR_PLUGIN_1_2_2_OR_NEWER
  26. Transform headsetCamera = VRTK_DeviceFinder.HeadsetCamera();
  27. if (headsetCamera != null && headsetCamera.GetComponent<SteamVR_UpdatePoses>() == null)
  28. {
  29. headsetCamera.gameObject.AddComponent<SteamVR_UpdatePoses>();
  30. }
  31. #endif
  32. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  33. if (area != null)
  34. {
  35. area.BuildMesh();
  36. }
  37. }
  38. /// <summary>
  39. /// The GetPlayArea method returns the Transform of the object that is used to represent the play area in the scene.
  40. /// </summary>
  41. /// <returns>A transform of the object representing the play area in the scene.</returns>
  42. public override Transform GetPlayArea()
  43. {
  44. cachedPlayArea = GetSDKManagerPlayArea();
  45. if (cachedPlayArea == null)
  46. {
  47. SteamVR_PlayArea steamVRPlayArea = VRTK_SharedMethods.FindEvenInactiveComponent<SteamVR_PlayArea>(true);
  48. if (steamVRPlayArea != null)
  49. {
  50. cachedSteamVRPlayArea = steamVRPlayArea;
  51. cachedPlayArea = steamVRPlayArea.transform;
  52. }
  53. }
  54. return cachedPlayArea;
  55. }
  56. /// <summary>
  57. /// The GetPlayAreaVertices method returns the points of the play area boundaries.
  58. /// </summary>
  59. /// <returns>A Vector3 array of the points in the scene that represent the play area boundaries.</returns>
  60. public override Vector3[] GetPlayAreaVertices()
  61. {
  62. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  63. if (area != null)
  64. {
  65. return ProcessVertices(area.vertices);
  66. }
  67. return null;
  68. }
  69. /// <summary>
  70. /// The GetPlayAreaBorderThickness returns the thickness of the drawn border for the given play area.
  71. /// </summary>
  72. /// <returns>The thickness of the drawn border.</returns>
  73. public override float GetPlayAreaBorderThickness()
  74. {
  75. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  76. if (area != null)
  77. {
  78. return area.borderThickness;
  79. }
  80. return 0f;
  81. }
  82. /// <summary>
  83. /// The IsPlayAreaSizeCalibrated method returns whether the given play area size has been auto calibrated by external sensors.
  84. /// </summary>
  85. /// <returns>Returns true if the play area size has been auto calibrated and set by external sensors.</returns>
  86. public override bool IsPlayAreaSizeCalibrated()
  87. {
  88. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  89. return (area != null && area.size == SteamVR_PlayArea.Size.Calibrated);
  90. }
  91. /// <summary>
  92. /// The GetDrawAtRuntime method returns whether the given play area drawn border is being displayed.
  93. /// </summary>
  94. /// <returns>Returns true if the drawn border is being displayed.</returns>
  95. public override bool GetDrawAtRuntime()
  96. {
  97. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  98. return (area != null ? area.drawInGame : false);
  99. }
  100. /// <summary>
  101. /// The SetDrawAtRuntime method sets whether the given play area drawn border should be displayed at runtime.
  102. /// </summary>
  103. /// <param name="value">The state of whether the drawn border should be displayed or not.</param>
  104. public override void SetDrawAtRuntime(bool value)
  105. {
  106. SteamVR_PlayArea area = GetCachedSteamVRPlayArea();
  107. if (area != null)
  108. {
  109. area.drawInGame = value;
  110. area.enabled = true;
  111. }
  112. }
  113. protected virtual SteamVR_PlayArea GetCachedSteamVRPlayArea()
  114. {
  115. if (cachedSteamVRPlayArea == null)
  116. {
  117. Transform checkPlayArea = GetPlayArea();
  118. if (checkPlayArea != null)
  119. {
  120. cachedSteamVRPlayArea = checkPlayArea.GetComponent<SteamVR_PlayArea>();
  121. }
  122. }
  123. return cachedSteamVRPlayArea;
  124. }
  125. protected virtual Vector3[] ProcessVertices(Vector3[] vertices)
  126. {
  127. #if VRTK_DEFINE_STEAMVR_PLUGIN_1_2_2_OR_NEWER
  128. return vertices;
  129. #else
  130. //If there aren't enough vertices or the play area is calibrated then just return
  131. if (vertices.Length < 8 || IsPlayAreaSizeCalibrated())
  132. {
  133. return vertices;
  134. }
  135. //Go through the existing vertices and swap them around so they're in the correct expected location
  136. Vector3[] modifiedVertices = new Vector3[8];
  137. int[] verticeIndexes = new int[] { 3, 0, 1, 2, 7, 4, 5, 6 };
  138. for (int i = 0; i < modifiedVertices.Length; i++)
  139. {
  140. modifiedVertices[i] = vertices[verticeIndexes[i]];
  141. }
  142. return modifiedVertices;
  143. #endif
  144. }
  145. #endif
  146. }
  147. }