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.

99 lines
3.1 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. #if UNITY_2017_2_OR_NEWER
  5. using UnityEngine.XR;
  6. #else
  7. using UnityEngine.VR;
  8. using XRSettings = UnityEngine.VR.VRSettings;
  9. using XRDevice = UnityEngine.VR.VRDevice;
  10. #endif
  11. /// <summary>
  12. /// Camera script for the main camera for Immersive Mixed Reality.
  13. /// </summary>
  14. [RequireComponent(typeof(Camera))]
  15. public class WindowsMR_Camera : MonoBehaviour
  16. {
  17. [SerializeField]
  18. [Tooltip("Force the Tracking Space Type to be RoomScale (normal VR experiences). If false, Stationary will be forced (e.g. video experiences.")]
  19. private bool forceRoomScaleTracking = true;
  20. public bool ForceRoomScaleTracking
  21. {
  22. get { return forceRoomScaleTracking; }
  23. set { forceRoomScaleTracking = value; }
  24. }
  25. /// <summary>
  26. /// Name of the Windows Mixed Reality Device as listed in XRSettings.
  27. /// </summary>
  28. private const string DEVICE_NAME = "WindowsMR";
  29. protected virtual void Awake()
  30. {
  31. if (CheckForMixedRealitySupport())
  32. {
  33. SetupMRCamera();
  34. }
  35. }
  36. protected virtual void Update()
  37. {
  38. if (XRDevice.GetTrackingSpaceType() != TrackingSpaceType.RoomScale && forceRoomScaleTracking)
  39. {
  40. XRDevice.SetTrackingSpaceType(TrackingSpaceType.RoomScale);
  41. }
  42. if (XRDevice.GetTrackingSpaceType() != TrackingSpaceType.Stationary && !forceRoomScaleTracking)
  43. {
  44. XRDevice.SetTrackingSpaceType(TrackingSpaceType.Stationary);
  45. }
  46. }
  47. /// <summary>
  48. /// Check if the Mixed (Virtual) Reality Settings are properly set.
  49. /// </summary>
  50. /// <returns>Are the settings set.</returns>
  51. protected virtual bool CheckForMixedRealitySupport()
  52. {
  53. if (XRSettings.enabled == false)
  54. {
  55. Debug.LogError("XRSettings are not enabled. Enable in PlayerSettings. Do not forget to add Windows Mixed Reality to Virtual Reality SDKs.");
  56. return false;
  57. }
  58. else
  59. {
  60. foreach (string device in XRSettings.supportedDevices)
  61. {
  62. if (device.Equals("WindowsMR"))
  63. {
  64. return true;
  65. }
  66. }
  67. Debug.LogError("Windows Mixed Reality is not supported in XRSettings, add in PlayerSettings.");
  68. }
  69. return false;
  70. }
  71. /// <summary>
  72. /// Setup the MR camera properly.
  73. /// </summary>
  74. protected virtual void SetupMRCamera()
  75. {
  76. Camera camera = GetComponent<Camera>();
  77. if (camera.tag != "MainCamera")
  78. {
  79. camera.tag = "MainCamera";
  80. }
  81. camera.nearClipPlane = 0.01f;
  82. if (camera.stereoTargetEye != StereoTargetEyeMask.Both)
  83. {
  84. Debug.LogError("Target eye of main camera is not set to both. Are you sure you want to render only one eye?");
  85. }
  86. }
  87. }
  88. }