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.

520 lines
23 KiB

  1. // Device Finder|Utilities|90050
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. #if UNITY_2017_2_OR_NEWER
  6. using UnityEngine.XR;
  7. #else
  8. using XRDevice = UnityEngine.VR.VRDevice;
  9. #endif
  10. /// <summary>
  11. /// The Device Finder offers a collection of static methods that can be called to find common game devices such as the headset or controllers, or used to determine key information about the connected devices.
  12. /// </summary>
  13. public static class VRTK_DeviceFinder
  14. {
  15. /// <summary>
  16. /// Possible devices.
  17. /// </summary>
  18. public enum Devices
  19. {
  20. /// <summary>
  21. /// The headset.
  22. /// </summary>
  23. Headset,
  24. /// <summary>
  25. /// The left hand controller.
  26. /// </summary>
  27. LeftController,
  28. /// <summary>
  29. /// The right hand controller.
  30. /// </summary>
  31. RightController,
  32. }
  33. /// <summary>
  34. /// Possible headsets
  35. /// </summary>
  36. [System.Obsolete("`VRTK_DeviceFinder.Headsets` has been deprecated and has been replaced with a manufacturer string. This enum will be removed in a future version of VRTK.")]
  37. public enum Headsets
  38. {
  39. /// <summary>
  40. /// An unknown headset.
  41. /// </summary>
  42. Unknown,
  43. /// <summary>
  44. /// A summary of all Oculus Rift headset versions.
  45. /// </summary>
  46. OculusRift,
  47. /// <summary>
  48. /// A specific version of the Oculus Rift headset, the Consumer Version 1.
  49. /// </summary>
  50. OculusRiftCV1,
  51. /// <summary>
  52. /// A summary of all HTC Vive headset versions.
  53. /// </summary>
  54. Vive,
  55. /// <summary>
  56. /// A specific version of the HTC Vive headset, the first consumer version.
  57. /// </summary>
  58. ViveMV,
  59. /// <summary>
  60. /// A specific version of the HTC Vive headset, the first consumer version.
  61. /// </summary>
  62. ViveDVT,
  63. /// <summary>
  64. /// A specific version of the Oculus Rift headset, the rare ES07.
  65. /// </summary>
  66. OculusRiftES07
  67. }
  68. /// <summary>
  69. /// Obsolete
  70. /// </summary>
  71. private static string cachedHeadsetType = "";
  72. /// <summary>
  73. /// The GetCurrentControllerType method returns the current used ControllerType based on the SDK and headset being used.
  74. /// </summary>
  75. /// <param name="controllerReference">The reference to the controller to get type of.</param>
  76. /// <returns>The ControllerType based on the SDK and headset being used.</returns>
  77. public static SDK_BaseController.ControllerType GetCurrentControllerType(VRTK_ControllerReference controllerReference = null)
  78. {
  79. return VRTK_SDK_Bridge.GetCurrentControllerType(controllerReference);
  80. }
  81. /// <summary>
  82. /// The GetControllerIndex method is used to find the index of a given controller object.
  83. /// </summary>
  84. /// <param name="controller">The controller object to get the index of a controller.</param>
  85. /// <returns>The index of the given controller.</returns>
  86. public static uint GetControllerIndex(GameObject controller)
  87. {
  88. return VRTK_SDK_Bridge.GetControllerIndex(controller);
  89. }
  90. /// <summary>
  91. /// The GetControllerByIndex method is used to find a controller based on it's unique index.
  92. /// </summary>
  93. /// <param name="index">The index of the actual controller to find.</param>
  94. /// <param name="getActual">An optional parameter that if true will return the game object that the SDK controller is attached to.</param>
  95. /// <returns>The actual controller GameObject that matches the given index.</returns>
  96. public static GameObject GetControllerByIndex(uint index, bool getActual)
  97. {
  98. return VRTK_SDK_Bridge.GetControllerByIndex(index, getActual);
  99. }
  100. /// <summary>
  101. /// The GetControllerOrigin method is used to find the controller's origin.
  102. /// </summary>
  103. /// <param name="controllerReference">The reference to the controller to get the origin for.</param>
  104. /// <returns>The transform of the controller origin or if an origin is not set then the transform parent.</returns>
  105. public static Transform GetControllerOrigin(VRTK_ControllerReference controllerReference)
  106. {
  107. return VRTK_SDK_Bridge.GetControllerOrigin(controllerReference);
  108. }
  109. /// <summary>
  110. /// The DeviceTransform method returns the transform for a given Devices enum.
  111. /// </summary>
  112. /// <param name="device">The Devices enum to get the transform for.</param>
  113. /// <returns>The transform for the given Devices enum.</returns>
  114. public static Transform DeviceTransform(Devices device)
  115. {
  116. switch (device)
  117. {
  118. case Devices.Headset:
  119. return HeadsetTransform();
  120. case Devices.LeftController:
  121. return GetControllerLeftHand().transform;
  122. case Devices.RightController:
  123. return GetControllerRightHand().transform;
  124. }
  125. return null;
  126. }
  127. /// <summary>
  128. /// The GetControllerHandType method is used for getting the enum representation of ControllerHand from a given string.
  129. /// </summary>
  130. /// <param name="hand">The string representation of the hand to retrieve the type of. `left` or `right`.</param>
  131. /// <returns>A ControllerHand representing either the Left or Right hand.</returns>
  132. public static SDK_BaseController.ControllerHand GetControllerHandType(string hand)
  133. {
  134. switch (hand.ToLower())
  135. {
  136. case "left":
  137. return SDK_BaseController.ControllerHand.Left;
  138. case "right":
  139. return SDK_BaseController.ControllerHand.Right;
  140. default:
  141. return SDK_BaseController.ControllerHand.None;
  142. }
  143. }
  144. /// <summary>
  145. /// The GetControllerHand method is used for getting the enum representation of ControllerHand for the given controller game object.
  146. /// </summary>
  147. /// <param name="controller">The controller game object to check the hand of.</param>
  148. /// <returns>A ControllerHand representing either the Left or Right hand.</returns>
  149. public static SDK_BaseController.ControllerHand GetControllerHand(GameObject controller)
  150. {
  151. if (VRTK_SDK_Bridge.IsControllerLeftHand(controller))
  152. {
  153. return SDK_BaseController.ControllerHand.Left;
  154. }
  155. else if (VRTK_SDK_Bridge.IsControllerRightHand(controller))
  156. {
  157. return SDK_BaseController.ControllerHand.Right;
  158. }
  159. else
  160. {
  161. return SDK_BaseController.ControllerHand.None;
  162. }
  163. }
  164. /// <summary>
  165. /// The GetControllerLeftHand method retrieves the game object for the left hand controller.
  166. /// </summary>
  167. /// <param name="getActual">An optional parameter that if true will return the game object that the SDK controller is attached to.</param>
  168. /// <returns>The left hand controller.</returns>
  169. public static GameObject GetControllerLeftHand(bool getActual = false)
  170. {
  171. return VRTK_SDK_Bridge.GetControllerLeftHand(getActual);
  172. }
  173. /// <summary>
  174. /// The GetControllerRightHand method retrieves the game object for the right hand controller.
  175. /// </summary>
  176. /// <param name="getActual">An optional parameter that if true will return the game object that the SDK controller is attached to.</param>
  177. /// <returns>The right hand controller.</returns>
  178. public static GameObject GetControllerRightHand(bool getActual = false)
  179. {
  180. return VRTK_SDK_Bridge.GetControllerRightHand(getActual);
  181. }
  182. /// <summary>
  183. /// The GetControllerReferenceLeftHand returns a Controller Reference for the left hand controller.
  184. /// </summary>
  185. /// <returns>The Controller Reference for the left hand controller.</returns>
  186. public static VRTK_ControllerReference GetControllerReferenceLeftHand()
  187. {
  188. return VRTK_ControllerReference.GetControllerReference(SDK_BaseController.ControllerHand.Left);
  189. }
  190. /// <summary>
  191. /// The GetControllerReferenceRightHand returns a Controller Reference for the right hand controller.
  192. /// </summary>
  193. /// <returns>The Controller Reference for the right hand controller.</returns>
  194. public static VRTK_ControllerReference GetControllerReferenceRightHand()
  195. {
  196. return VRTK_ControllerReference.GetControllerReference(SDK_BaseController.ControllerHand.Right);
  197. }
  198. /// <summary>
  199. /// The GetControllerReferenceForHand returns a Controller Reference for the given hand controller.
  200. /// </summary>
  201. /// <returns>The Controller Reference for the given hand controller.</returns>
  202. public static VRTK_ControllerReference GetControllerReferenceForHand(SDK_BaseController.ControllerHand hand)
  203. {
  204. return VRTK_ControllerReference.GetControllerReference(hand);
  205. }
  206. /// <summary>
  207. /// The IsControllerOfHand method is used to check if a given controller game object is of the hand type provided.
  208. /// </summary>
  209. /// <param name="checkController">The actual controller object that is being checked.</param>
  210. /// <param name="hand">The representation of a hand to check if the given controller matches.</param>
  211. /// <returns>Is true if the given controller matches the given hand.</returns>
  212. public static bool IsControllerOfHand(GameObject checkController, SDK_BaseController.ControllerHand hand)
  213. {
  214. switch (hand)
  215. {
  216. case SDK_BaseController.ControllerHand.Left:
  217. return (IsControllerLeftHand(checkController));
  218. case SDK_BaseController.ControllerHand.Right:
  219. return (IsControllerRightHand(checkController));
  220. }
  221. return false;
  222. }
  223. /// <summary>
  224. /// The IsControllerLeftHand method is used to check if a given controller game object is the left handed controller.
  225. /// </summary>
  226. /// <param name="checkController">The controller object that is being checked.</param>
  227. /// <returns>Is true if the given controller is the left controller.</returns>
  228. public static bool IsControllerLeftHand(GameObject checkController)
  229. {
  230. return VRTK_SDK_Bridge.IsControllerLeftHand(checkController);
  231. }
  232. /// <summary>
  233. /// The IsControllerRightHand method is used to check if a given controller game object is the right handed controller.
  234. /// </summary>
  235. /// <param name="checkController">The controller object that is being checked.</param>
  236. /// <returns>Is true if the given controller is the right controller.</returns>
  237. public static bool IsControllerRightHand(GameObject checkController)
  238. {
  239. return VRTK_SDK_Bridge.IsControllerRightHand(checkController);
  240. }
  241. /// <summary>
  242. /// The GetOppositeHand method returns the other hand type from the current hand given.
  243. /// </summary>
  244. /// <param name="currentHand">The current hand.</param>
  245. /// <returns>The opposite hand.</returns>
  246. public static SDK_BaseController.ControllerHand GetOppositeHand(SDK_BaseController.ControllerHand currentHand)
  247. {
  248. switch (currentHand)
  249. {
  250. case SDK_BaseController.ControllerHand.Left:
  251. return SDK_BaseController.ControllerHand.Right;
  252. case SDK_BaseController.ControllerHand.Right:
  253. return SDK_BaseController.ControllerHand.Left;
  254. }
  255. return currentHand;
  256. }
  257. /// <summary>
  258. /// The GetActualController method will attempt to get the actual SDK controller object.
  259. /// </summary>
  260. /// <param name="givenController">The GameObject of the controller.</param>
  261. /// <returns>The GameObject that is the actual controller.</returns>
  262. public static GameObject GetActualController(GameObject givenController)
  263. {
  264. if (VRTK_SDK_Bridge.IsControllerLeftHand(givenController, true) || VRTK_SDK_Bridge.IsControllerRightHand(givenController, true))
  265. {
  266. return givenController;
  267. }
  268. if (VRTK_SDK_Bridge.IsControllerLeftHand(givenController, false))
  269. {
  270. return VRTK_SDK_Bridge.GetControllerLeftHand(true);
  271. }
  272. if (VRTK_SDK_Bridge.IsControllerRightHand(givenController, false))
  273. {
  274. return VRTK_SDK_Bridge.GetControllerRightHand(true);
  275. }
  276. return null;
  277. }
  278. /// <summary>
  279. /// The GetScriptAliasController method will attempt to get the object that contains the scripts for the controller.
  280. /// </summary>
  281. /// <param name="givenController">The GameObject of the controller.</param>
  282. /// <returns>The GameObject that is the alias controller containing the scripts.</returns>
  283. public static GameObject GetScriptAliasController(GameObject givenController)
  284. {
  285. if (VRTK_SDK_Bridge.IsControllerLeftHand(givenController, false) || VRTK_SDK_Bridge.IsControllerRightHand(givenController, false))
  286. {
  287. return givenController;
  288. }
  289. if (VRTK_SDK_Bridge.IsControllerLeftHand(givenController, true))
  290. {
  291. return VRTK_SDK_Bridge.GetControllerLeftHand(false);
  292. }
  293. if (VRTK_SDK_Bridge.IsControllerRightHand(givenController, true))
  294. {
  295. return VRTK_SDK_Bridge.GetControllerRightHand(false);
  296. }
  297. return null;
  298. }
  299. /// <summary>
  300. /// The GetModelAliasController method will attempt to get the object that contains the model for the controller.
  301. /// </summary>
  302. /// <param name="givenController">The GameObject of the controller.</param>
  303. /// <returns>The GameObject that is the alias controller containing the controller model.</returns>
  304. public static GameObject GetModelAliasController(GameObject givenController)
  305. {
  306. return VRTK_SDK_Bridge.GetControllerModel(givenController);
  307. }
  308. /// <summary>
  309. /// The GetModelAliasControllerHand method will return the hand that the given model alias GameObject is for.
  310. /// </summary>
  311. /// <param name="givenObject">The GameObject that may represent a model alias.</param>
  312. /// <returns>The enum of the ControllerHand that the given GameObject may represent.</returns>
  313. public static SDK_BaseController.ControllerHand GetModelAliasControllerHand(GameObject givenObject)
  314. {
  315. if (GetModelAliasController(GetControllerLeftHand()) == givenObject)
  316. {
  317. return SDK_BaseController.ControllerHand.Left;
  318. }
  319. else if (GetModelAliasController(GetControllerRightHand()) == givenObject)
  320. {
  321. return SDK_BaseController.ControllerHand.Right;
  322. }
  323. return SDK_BaseController.ControllerHand.None;
  324. }
  325. /// <summary>
  326. /// The GetControllerVelocity method is used for getting the current velocity of the physical game controller. This can be useful to determine the speed at which the controller is being swung or the direction it is being moved in.
  327. /// </summary>
  328. /// <param name="controllerReference">The reference to the controller.</param>
  329. /// <returns>A 3 dimensional vector containing the current real world physical controller velocity.</returns>
  330. public static Vector3 GetControllerVelocity(VRTK_ControllerReference controllerReference)
  331. {
  332. return VRTK_SDK_Bridge.GetControllerVelocity(controllerReference);
  333. }
  334. /// <summary>
  335. /// The GetControllerAngularVelocity method is used for getting the current rotational velocity of the physical game controller. This can be useful for determining which way the controller is being rotated and at what speed the rotation is occurring.
  336. /// </summary>
  337. /// <param name="controllerReference">The reference to the controller.</param>
  338. /// <returns>A 3 dimensional vector containing the current real world physical controller angular (rotational) velocity.</returns>
  339. public static Vector3 GetControllerAngularVelocity(VRTK_ControllerReference controllerReference)
  340. {
  341. return VRTK_SDK_Bridge.GetControllerAngularVelocity(controllerReference);
  342. }
  343. /// <summary>
  344. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  345. /// </summary>
  346. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  347. public static Vector3 GetHeadsetVelocity()
  348. {
  349. return VRTK_SDK_Bridge.GetHeadsetVelocity();
  350. }
  351. /// <summary>
  352. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  353. /// </summary>
  354. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  355. public static Vector3 GetHeadsetAngularVelocity()
  356. {
  357. return VRTK_SDK_Bridge.GetHeadsetAngularVelocity();
  358. }
  359. /// <summary>
  360. /// The HeadsetTransform method is used to retrieve the transform for the VR Headset in the scene. It can be useful to determine the position of the user's head in the game world.
  361. /// </summary>
  362. /// <returns>The transform of the VR Headset component.</returns>
  363. public static Transform HeadsetTransform()
  364. {
  365. return VRTK_SDK_Bridge.GetHeadset();
  366. }
  367. /// <summary>
  368. /// The HeadsetCamera method is used to retrieve the transform for the VR Camera in the scene.
  369. /// </summary>
  370. /// <returns>The transform of the VR Camera component.</returns>
  371. public static Transform HeadsetCamera()
  372. {
  373. return VRTK_SDK_Bridge.GetHeadsetCamera();
  374. }
  375. /// <summary>
  376. /// The ResetHeadsetTypeCache resets the cache holding the current headset type value.
  377. /// </summary>
  378. [System.Obsolete("`VRTK_DeviceFinder.ResetHeadsetTypeCache()` has been deprecated. This method will be removed in a future version of VRTK.")]
  379. public static void ResetHeadsetTypeCache()
  380. {
  381. cachedHeadsetType = "";
  382. }
  383. /// <summary>
  384. /// The GetHeadsetType method returns the type of headset connected to the computer.
  385. /// </summary>
  386. /// <param name="summary">If this is `true`, then the generic name for the headset is returned not including the version type (e.g. OculusRift will be returned for DK2 and CV1).</param>
  387. /// <returns>The Headset type that is connected.</returns>
  388. [System.Obsolete("`VRTK_DeviceFinder.GetHeadsetType(summary) -> VRTK_DeviceFinder.Headsets` has been replaced with `VRTK_DeviceFinder.GetHeadsetType() -> SDK_BaseHeadset.HeadsetType`. This method will be removed in a future version of VRTK.")]
  389. public static Headsets GetHeadsetType(bool summary = false)
  390. {
  391. Headsets returnValue = Headsets.Unknown;
  392. cachedHeadsetType = (cachedHeadsetType == "" ? XRDevice.model.Replace(" ", "").Replace(".", "").ToLowerInvariant() : cachedHeadsetType);
  393. switch (cachedHeadsetType)
  394. {
  395. case "oculusriftcv1":
  396. returnValue = (summary ? Headsets.OculusRift : Headsets.OculusRiftCV1);
  397. break;
  398. case "oculusriftes07":
  399. returnValue = (summary ? Headsets.OculusRift : Headsets.OculusRiftES07);
  400. break;
  401. case "vivemv":
  402. returnValue = (summary ? Headsets.Vive : Headsets.ViveMV);
  403. break;
  404. case "vivedvt":
  405. returnValue = (summary ? Headsets.Vive : Headsets.ViveDVT);
  406. break;
  407. }
  408. if (returnValue == Headsets.Unknown)
  409. {
  410. VRTK_Logger.Warn(
  411. string.Format("Your headset is of type '{0}' which VRTK doesn't know about yet. Please report this headset type to the maintainers of VRTK."
  412. + (summary ? " Falling back to a slower check to summarize the headset type now." : ""),
  413. cachedHeadsetType)
  414. );
  415. if (summary)
  416. {
  417. if (cachedHeadsetType.Contains("rift"))
  418. {
  419. return Headsets.OculusRift;
  420. }
  421. if (cachedHeadsetType.Contains("vive"))
  422. {
  423. return Headsets.Vive;
  424. }
  425. }
  426. }
  427. return returnValue;
  428. }
  429. /// <summary>
  430. /// The GetHeadsetTypeAsString method returns a string representing the type of headset connected.
  431. /// </summary>
  432. /// <returns>The string of the headset connected.</returns>
  433. public static string GetHeadsetTypeAsString()
  434. {
  435. return VRTK_SDK_Bridge.GetHeadsetType();
  436. }
  437. /// <summary>
  438. /// The GetHeadsetType method returns the type of headset currently connected.
  439. /// </summary>
  440. /// <returns>The Headset type that is connected.</returns>
  441. public static SDK_BaseHeadset.HeadsetType GetHeadsetType()
  442. {
  443. switch (GetHeadsetTypeAsString())
  444. {
  445. case "simulator":
  446. return SDK_BaseHeadset.HeadsetType.Simulator;
  447. case "htcvive":
  448. return SDK_BaseHeadset.HeadsetType.HTCVive;
  449. case "oculusrift":
  450. return SDK_BaseHeadset.HeadsetType.OculusRift;
  451. case "oculusgearvr":
  452. return SDK_BaseHeadset.HeadsetType.OculusGearVR;
  453. case "googledaydream":
  454. return SDK_BaseHeadset.HeadsetType.GoogleDaydream;
  455. case "googlecardboard":
  456. return SDK_BaseHeadset.HeadsetType.GoogleCardboard;
  457. case "hyperealvr":
  458. return SDK_BaseHeadset.HeadsetType.HyperealVR;
  459. case "oculusriftdk1":
  460. return SDK_BaseHeadset.HeadsetType.OculusRiftDK1;
  461. case "oculusriftdk2":
  462. return SDK_BaseHeadset.HeadsetType.OculusRiftDK2;
  463. case "windowsmixedreality":
  464. return SDK_BaseHeadset.HeadsetType.WindowsMixedReality;
  465. }
  466. return SDK_BaseHeadset.HeadsetType.Undefined;
  467. }
  468. /// <summary>
  469. /// The PlayAreaTransform method is used to retrieve the transform for the play area in the scene.
  470. /// </summary>
  471. /// <returns>The transform of the VR Play Area component.</returns>
  472. public static Transform PlayAreaTransform()
  473. {
  474. return VRTK_SDK_Bridge.GetPlayArea();
  475. }
  476. }
  477. }