CameraOffset.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. #if ENABLE_VR || ENABLE_AR
  6. using UnityEngine.XR;
  7. namespace UnityEditor.XR.LegacyInputHelpers
  8. {
  9. public enum UserRequestedTrackingMode
  10. {
  11. Default,
  12. Device,
  13. Floor,
  14. }
  15. [AddComponentMenu("XR/Camera Offset")]
  16. public class CameraOffset : MonoBehaviour
  17. {
  18. const float k_DefaultCameraYOffset = 1.36144f;
  19. [SerializeField]
  20. [Tooltip("GameObject to move to desired height off the floor (defaults to this object if none provided).")]
  21. GameObject m_CameraFloorOffsetObject = null;
  22. /// <summary>Gets or sets the GameObject to move to desired height off the floor (defaults to this object if none provided).</summary>
  23. public GameObject cameraFloorOffsetObject { get { return m_CameraFloorOffsetObject; } set { m_CameraFloorOffsetObject = value; SetupCamera(); } }
  24. [SerializeField]
  25. [Tooltip("What the user wants the tracking origin mode to be")]
  26. UserRequestedTrackingMode m_RequestedTrackingMode = UserRequestedTrackingMode.Default;
  27. public UserRequestedTrackingMode requestedTrackingMode { get { return m_RequestedTrackingMode; } set { m_RequestedTrackingMode = value; TryInitializeCamera(); } }
  28. #if UNITY_2019_3_OR_NEWER
  29. [SerializeField]
  30. [Tooltip("Sets the type of tracking origin to use for this Rig. Tracking origins identify where 0,0,0 is in the world of tracking.")]
  31. /// <summary>Gets or sets the type of tracking origin to use for this Rig. Tracking origins identify where 0,0,0 is in the world of tracking. Not all devices support all tracking spaces; if the selected tracking space is not set it will fall back to Stationary.</summary>
  32. TrackingOriginModeFlags m_TrackingOriginMode = TrackingOriginModeFlags.Unknown;
  33. public TrackingOriginModeFlags TrackingOriginMode { get { return m_TrackingOriginMode; } set { m_TrackingOriginMode = value; TryInitializeCamera(); } }
  34. #endif
  35. // Disable Obsolete warnings for TrackingSpaceType, explicitly to read in old data and upgrade.
  36. #pragma warning disable 0618
  37. [SerializeField]
  38. [Tooltip("Set if the XR experience is Room Scale or Stationary.")]
  39. TrackingSpaceType m_TrackingSpace = TrackingSpaceType.Stationary;
  40. /// <summary>Gets or sets if the experience is rooms scale or stationary. Not all devices support all tracking spaces; if the selected tracking space is not set it will fall back to Stationary.</summary>
  41. #if UNITY_2019_3_OR_NEWER
  42. [Obsolete("CameraOffset.trackingSpace is obsolete. Please use CameraOffset.trackingOriginMode.")]
  43. #endif
  44. public TrackingSpaceType trackingSpace { get { return m_TrackingSpace; } set { m_TrackingSpace = value; TryInitializeCamera(); } }
  45. #pragma warning restore 0618
  46. [SerializeField]
  47. [Tooltip("Camera Height to be used when in Device tracking space.")]
  48. float m_CameraYOffset = k_DefaultCameraYOffset;
  49. /// <summary>Gets or sets the amount the camera is offset from the floor (by moving the camera offset object).</summary>
  50. public float cameraYOffset { get { return m_CameraYOffset; } set { m_CameraYOffset = value; TryInitializeCamera(); } }
  51. // Bookkeeping to track lazy initialization of the tracking space type.
  52. bool m_CameraInitialized = false;
  53. bool m_CameraInitializing = false;
  54. #if UNITY_2019_3_OR_NEWER
  55. /// <summary>
  56. /// Used to cache the input subsystems without creating additional garbage.
  57. /// </summary>
  58. static List<XRInputSubsystem> s_InputSubsystems = new List<XRInputSubsystem>();
  59. #endif
  60. /// Utility helper to migrate from TrackingSpace to TrackingOrigin seamlessly
  61. void UpgradeTrackingSpaceToTrackingOriginMode()
  62. {
  63. #if UNITY_2019_3_OR_NEWER
  64. // Disable Obsolete warnings for TrackingSpaceType, explicitly to allow a proper upgrade path.
  65. #pragma warning disable 0618
  66. if (m_TrackingOriginMode == TrackingOriginModeFlags.Unknown && m_TrackingSpace <= TrackingSpaceType.RoomScale)
  67. {
  68. switch (m_TrackingSpace)
  69. {
  70. case TrackingSpaceType.RoomScale:
  71. {
  72. m_TrackingOriginMode = TrackingOriginModeFlags.Floor;
  73. break;
  74. }
  75. case TrackingSpaceType.Stationary:
  76. {
  77. m_TrackingOriginMode = TrackingOriginModeFlags.Device;
  78. break;
  79. }
  80. default:
  81. break;
  82. }
  83. // Tag is Invalid not to be used.
  84. m_TrackingSpace = (TrackingSpaceType)3;
  85. #if UNITY_EDITOR
  86. EditorUtility.SetDirty(this);
  87. #endif //UNITY_EDITOR
  88. #pragma warning restore 0618
  89. }
  90. #endif //UNITY_2019_3_OR_NEWER
  91. }
  92. void Awake()
  93. {
  94. if (!m_CameraFloorOffsetObject)
  95. {
  96. Debug.LogWarning("No camera container specified for XR Rig, using attached GameObject");
  97. m_CameraFloorOffsetObject = this.gameObject;
  98. }
  99. }
  100. void Start()
  101. {
  102. TryInitializeCamera();
  103. }
  104. void OnValidate()
  105. {
  106. UpgradeTrackingSpaceToTrackingOriginMode();
  107. TryInitializeCamera();
  108. }
  109. void TryInitializeCamera()
  110. {
  111. m_CameraInitialized = SetupCamera();
  112. if (!m_CameraInitialized & !m_CameraInitializing)
  113. StartCoroutine(RepeatInitializeCamera());
  114. }
  115. /// <summary>
  116. /// Repeatedly attempt to initialize the camera.
  117. /// </summary>
  118. /// <returns></returns>
  119. IEnumerator RepeatInitializeCamera()
  120. {
  121. m_CameraInitializing = true;
  122. yield return null;
  123. while (!m_CameraInitialized)
  124. {
  125. m_CameraInitialized = SetupCamera();
  126. yield return null;
  127. }
  128. m_CameraInitializing = false;
  129. }
  130. /// <summary>
  131. /// Handles re-centering and off-setting the camera in space depending on which tracking space it is setup in.
  132. /// </summary>
  133. #if UNITY_2019_3_OR_NEWER
  134. bool SetupCamera()
  135. {
  136. SubsystemManager.GetInstances<XRInputSubsystem>(s_InputSubsystems);
  137. bool initialized = true;
  138. if (s_InputSubsystems.Count != 0)
  139. {
  140. for (int i = 0; i < s_InputSubsystems.Count; i++)
  141. {
  142. initialized &= SetupCamera(s_InputSubsystems[i]);
  143. }
  144. }
  145. else
  146. {
  147. // Disable Obsolete warnings for TrackingSpaceType, explicitly to allow a proper upgrade path.
  148. #pragma warning disable 0618
  149. if (m_RequestedTrackingMode == UserRequestedTrackingMode.Floor)
  150. {
  151. SetupCameraLegacy(TrackingSpaceType.RoomScale);
  152. }
  153. else
  154. {
  155. SetupCameraLegacy(TrackingSpaceType.Stationary);
  156. }
  157. #pragma warning restore 0618
  158. }
  159. return initialized;
  160. }
  161. bool SetupCamera(XRInputSubsystem subsystem)
  162. {
  163. if (subsystem == null)
  164. return false;
  165. bool trackingSettingsSet = false;
  166. float desiredOffset = cameraYOffset;
  167. var currentMode = subsystem.GetTrackingOriginMode();
  168. var supportedModes = subsystem.GetSupportedTrackingOriginModes();
  169. TrackingOriginModeFlags requestedMode = TrackingOriginModeFlags.Unknown;
  170. // map between the user requested options, and the actual options.
  171. if (m_RequestedTrackingMode == UserRequestedTrackingMode.Default)
  172. {
  173. requestedMode = currentMode;
  174. }
  175. else if(m_RequestedTrackingMode == UserRequestedTrackingMode.Device)
  176. {
  177. requestedMode = TrackingOriginModeFlags.Device;
  178. }
  179. else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Floor)
  180. {
  181. requestedMode = TrackingOriginModeFlags.Floor;
  182. }
  183. else
  184. {
  185. Debug.LogWarning("Unknown Requested Tracking Mode");
  186. }
  187. // now we've mapped em. actually go set em.
  188. if (requestedMode == TrackingOriginModeFlags.Floor)
  189. {
  190. // We need to check for Unknown because we may not be in a state where we can read this data yet.
  191. if ((supportedModes & (TrackingOriginModeFlags.Floor | TrackingOriginModeFlags.Unknown)) == 0)
  192. {
  193. Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Floor, but that is not supported by the SDK.");
  194. m_TrackingOriginMode = subsystem.GetTrackingOriginMode();
  195. return true;
  196. }
  197. if (subsystem.TrySetTrackingOriginMode(requestedMode))
  198. {
  199. desiredOffset = 0.0f;
  200. trackingSettingsSet = true;
  201. }
  202. }
  203. else if (requestedMode == TrackingOriginModeFlags.Device)
  204. {
  205. // We need to check for Unknown because we may not be in a state where we can read this data yet.
  206. if ((supportedModes & (TrackingOriginModeFlags.Device | TrackingOriginModeFlags.Unknown)) == 0)
  207. {
  208. Debug.LogWarning("CameraOffset.SetupCamera: Attempting to set the tracking space to Device, but that is not supported by the SDK.");
  209. m_TrackingOriginMode = subsystem.GetTrackingOriginMode();
  210. return true;
  211. }
  212. if (subsystem.TrySetTrackingOriginMode(requestedMode))
  213. {
  214. trackingSettingsSet = subsystem.TryRecenter();
  215. }
  216. }
  217. // what did we actually set?
  218. m_TrackingOriginMode = subsystem.GetTrackingOriginMode();
  219. if (trackingSettingsSet)
  220. {
  221. // Move camera to correct height
  222. if (m_CameraFloorOffsetObject)
  223. m_CameraFloorOffsetObject.transform.localPosition = new Vector3(m_CameraFloorOffsetObject.transform.localPosition.x, desiredOffset, m_CameraFloorOffsetObject.transform.localPosition.z);
  224. }
  225. return trackingSettingsSet;
  226. }
  227. #else
  228. bool SetupCamera()
  229. {
  230. if (m_RequestedTrackingMode == UserRequestedTrackingMode.Floor)
  231. {
  232. SetupCameraLegacy(TrackingSpaceType.RoomScale);
  233. }
  234. else if(m_RequestedTrackingMode == UserRequestedTrackingMode.Device)
  235. {
  236. SetupCameraLegacy(TrackingSpaceType.Stationary);
  237. }
  238. else if (m_RequestedTrackingMode == UserRequestedTrackingMode.Default)
  239. {
  240. TrackingSpaceType tst = XRDevice.GetTrackingSpaceType();
  241. SetupCameraLegacy(tst);
  242. }
  243. else
  244. {
  245. Debug.LogWarning("CameraOffset.SetupCamera: Unknown requested ");
  246. }
  247. return true;
  248. }
  249. #endif
  250. // Disable Obsolete warnings for TrackingSpaceType, explicitly to allow for using legacy data if available.
  251. #pragma warning disable 0618
  252. void SetupCameraLegacy(TrackingSpaceType trackingSpace)
  253. {
  254. float cameraYOffset = m_CameraYOffset;
  255. XRDevice.SetTrackingSpaceType(trackingSpace);
  256. if (trackingSpace == TrackingSpaceType.Stationary)
  257. InputTracking.Recenter();
  258. else if (trackingSpace == TrackingSpaceType.RoomScale)
  259. cameraYOffset = 0;
  260. m_TrackingSpace = trackingSpace;
  261. // Move camera to correct height
  262. if (m_CameraFloorOffsetObject)
  263. m_CameraFloorOffsetObject.transform.localPosition = new Vector3(m_CameraFloorOffsetObject.transform.localPosition.x, cameraYOffset, m_CameraFloorOffsetObject.transform.localPosition.z);
  264. }
  265. #pragma warning restore 0618
  266. }
  267. }
  268. #endif