OVRManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.VR;
  6. public class OVRManager : MonoBehaviour
  7. {
  8. public static OVRManager instance { get; private set; }
  9. public static OVRDisplay display { get; private set; }
  10. public static OVRTracker tracker { get; private set; }
  11. public static OVRBoundary boundary { get; private set; }
  12. public static OVRProfile profile
  13. {
  14. get
  15. {
  16. if (OVRManager._profile == null)
  17. {
  18. OVRManager._profile = new OVRProfile();
  19. }
  20. return OVRManager._profile;
  21. }
  22. }
  23. private bool paused
  24. {
  25. get
  26. {
  27. return this._isPaused;
  28. }
  29. set
  30. {
  31. if (value == this._isPaused)
  32. {
  33. return;
  34. }
  35. this._isPaused = value;
  36. }
  37. }
  38. public static event Action HMDAcquired;
  39. public static event Action HMDLost;
  40. public static event Action HMDMounted;
  41. public static event Action HMDUnmounted;
  42. public static event Action VrFocusAcquired;
  43. public static event Action VrFocusLost;
  44. public static event Action AudioOutChanged;
  45. public static event Action AudioInChanged;
  46. public static event Action TrackingAcquired;
  47. public static event Action TrackingLost;
  48. [Obsolete]
  49. public static event Action HSWDismissed;
  50. public static bool isHmdPresent
  51. {
  52. get
  53. {
  54. if (!OVRManager._isHmdPresentCached)
  55. {
  56. OVRManager._isHmdPresentCached = true;
  57. OVRManager._isHmdPresent = OVRPlugin.hmdPresent;
  58. }
  59. return OVRManager._isHmdPresent;
  60. }
  61. private set
  62. {
  63. OVRManager._isHmdPresentCached = true;
  64. OVRManager._isHmdPresent = value;
  65. }
  66. }
  67. public static string audioOutId
  68. {
  69. get
  70. {
  71. return OVRPlugin.audioOutId;
  72. }
  73. }
  74. public static string audioInId
  75. {
  76. get
  77. {
  78. return OVRPlugin.audioInId;
  79. }
  80. }
  81. public static bool hasVrFocus
  82. {
  83. get
  84. {
  85. if (!OVRManager._hasVrFocusCached)
  86. {
  87. OVRManager._hasVrFocusCached = true;
  88. OVRManager._hasVrFocus = OVRPlugin.hasVrFocus;
  89. }
  90. return OVRManager._hasVrFocus;
  91. }
  92. private set
  93. {
  94. OVRManager._hasVrFocusCached = true;
  95. OVRManager._hasVrFocus = value;
  96. }
  97. }
  98. [Obsolete]
  99. public static bool isHSWDisplayed
  100. {
  101. get
  102. {
  103. return false;
  104. }
  105. }
  106. [Obsolete]
  107. public static void DismissHSWDisplay()
  108. {
  109. }
  110. public bool chromatic
  111. {
  112. get
  113. {
  114. return OVRManager.isHmdPresent && OVRPlugin.chromatic;
  115. }
  116. set
  117. {
  118. if (!OVRManager.isHmdPresent)
  119. {
  120. return;
  121. }
  122. OVRPlugin.chromatic = value;
  123. }
  124. }
  125. public bool monoscopic
  126. {
  127. get
  128. {
  129. return !OVRManager.isHmdPresent || OVRPlugin.monoscopic;
  130. }
  131. set
  132. {
  133. if (!OVRManager.isHmdPresent)
  134. {
  135. return;
  136. }
  137. OVRPlugin.monoscopic = value;
  138. }
  139. }
  140. public int vsyncCount
  141. {
  142. get
  143. {
  144. if (!OVRManager.isHmdPresent)
  145. {
  146. return 1;
  147. }
  148. return OVRPlugin.vsyncCount;
  149. }
  150. set
  151. {
  152. if (!OVRManager.isHmdPresent)
  153. {
  154. return;
  155. }
  156. OVRPlugin.vsyncCount = value;
  157. }
  158. }
  159. public static float batteryLevel
  160. {
  161. get
  162. {
  163. if (!OVRManager.isHmdPresent)
  164. {
  165. return 1f;
  166. }
  167. return OVRPlugin.batteryLevel;
  168. }
  169. }
  170. public static float batteryTemperature
  171. {
  172. get
  173. {
  174. if (!OVRManager.isHmdPresent)
  175. {
  176. return 0f;
  177. }
  178. return OVRPlugin.batteryTemperature;
  179. }
  180. }
  181. public static int batteryStatus
  182. {
  183. get
  184. {
  185. if (!OVRManager.isHmdPresent)
  186. {
  187. return -1;
  188. }
  189. return (int)OVRPlugin.batteryStatus;
  190. }
  191. }
  192. public static float volumeLevel
  193. {
  194. get
  195. {
  196. if (!OVRManager.isHmdPresent)
  197. {
  198. return 0f;
  199. }
  200. return OVRPlugin.systemVolume;
  201. }
  202. }
  203. public static int cpuLevel
  204. {
  205. get
  206. {
  207. if (!OVRManager.isHmdPresent)
  208. {
  209. return 2;
  210. }
  211. return OVRPlugin.cpuLevel;
  212. }
  213. set
  214. {
  215. if (!OVRManager.isHmdPresent)
  216. {
  217. return;
  218. }
  219. OVRPlugin.cpuLevel = value;
  220. }
  221. }
  222. public static int gpuLevel
  223. {
  224. get
  225. {
  226. if (!OVRManager.isHmdPresent)
  227. {
  228. return 2;
  229. }
  230. return OVRPlugin.gpuLevel;
  231. }
  232. set
  233. {
  234. if (!OVRManager.isHmdPresent)
  235. {
  236. return;
  237. }
  238. OVRPlugin.gpuLevel = value;
  239. }
  240. }
  241. public static bool isPowerSavingActive
  242. {
  243. get
  244. {
  245. return OVRManager.isHmdPresent && OVRPlugin.powerSaving;
  246. }
  247. }
  248. public OVRManager.TrackingOrigin trackingOriginType
  249. {
  250. get
  251. {
  252. if (!OVRManager.isHmdPresent)
  253. {
  254. return this._trackingOriginType;
  255. }
  256. return (OVRManager.TrackingOrigin)OVRPlugin.GetTrackingOriginType();
  257. }
  258. set
  259. {
  260. if (!OVRManager.isHmdPresent)
  261. {
  262. return;
  263. }
  264. if (OVRPlugin.SetTrackingOriginType((OVRPlugin.TrackingOrigin)value))
  265. {
  266. this._trackingOriginType = value;
  267. }
  268. }
  269. }
  270. public bool isSupportedPlatform { get; private set; }
  271. public bool isUserPresent
  272. {
  273. get
  274. {
  275. if (!OVRManager._isUserPresentCached)
  276. {
  277. OVRManager._isUserPresentCached = true;
  278. OVRManager._isUserPresent = OVRPlugin.userPresent;
  279. }
  280. return OVRManager._isUserPresent;
  281. }
  282. private set
  283. {
  284. OVRManager._isUserPresentCached = true;
  285. OVRManager._isUserPresent = value;
  286. }
  287. }
  288. private void Awake()
  289. {
  290. if (OVRManager.instance != null)
  291. {
  292. base.enabled = false;
  293. UnityEngine.Object.DestroyImmediate(this);
  294. return;
  295. }
  296. OVRManager.instance = this;
  297. Debug.Log(string.Concat(new object[]
  298. {
  299. "Unity v",
  300. Application.unityVersion,
  301. ", Oculus Utilities v",
  302. OVRPlugin.wrapperVersion,
  303. ", OVRPlugin v",
  304. OVRPlugin.version,
  305. ", SDK v",
  306. OVRPlugin.nativeSDKVersion,
  307. "."
  308. }));
  309. if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D11)
  310. {
  311. Debug.LogWarning("VR rendering requires Direct3D11. Your graphics device: " + SystemInfo.graphicsDeviceType);
  312. }
  313. RuntimePlatform platform = Application.platform;
  314. this.isSupportedPlatform |= (platform == RuntimePlatform.Android);
  315. this.isSupportedPlatform |= (platform == RuntimePlatform.OSXEditor);
  316. this.isSupportedPlatform |= (platform == RuntimePlatform.OSXPlayer);
  317. this.isSupportedPlatform |= (platform == RuntimePlatform.WindowsEditor);
  318. this.isSupportedPlatform |= (platform == RuntimePlatform.WindowsPlayer);
  319. if (!this.isSupportedPlatform)
  320. {
  321. Debug.LogWarning("This platform is unsupported");
  322. return;
  323. }
  324. if (OVRManager.display == null)
  325. {
  326. OVRManager.display = new OVRDisplay();
  327. }
  328. if (OVRManager.tracker == null)
  329. {
  330. OVRManager.tracker = new OVRTracker();
  331. }
  332. if (OVRManager.boundary == null)
  333. {
  334. OVRManager.boundary = new OVRBoundary();
  335. }
  336. if (this.resetTrackerOnLoad)
  337. {
  338. OVRManager.display.RecenterPose();
  339. }
  340. OVRPlugin.occlusionMesh = false;
  341. OVRPlugin.ignoreVrFocus = OVRManager.runInBackground;
  342. }
  343. private void Update()
  344. {
  345. this.paused = !OVRPlugin.hasVrFocus;
  346. if (OVRPlugin.shouldQuit)
  347. {
  348. Application.Quit();
  349. }
  350. if (OVRPlugin.shouldRecenter)
  351. {
  352. OVRManager.display.RecenterPose();
  353. }
  354. if (this.trackingOriginType != this._trackingOriginType)
  355. {
  356. this.trackingOriginType = this._trackingOriginType;
  357. }
  358. OVRManager.tracker.isEnabled = this.usePositionTracking;
  359. OVRPlugin.useIPDInPositionTracking = this.useIPDInPositionTracking;
  360. OVRManager.isHmdPresent = OVRPlugin.hmdPresent;
  361. if (this.useRecommendedMSAALevel && QualitySettings.antiAliasing != OVRManager.display.recommendedMSAALevel)
  362. {
  363. Debug.Log(string.Concat(new object[]
  364. {
  365. "The current MSAA level is ",
  366. QualitySettings.antiAliasing,
  367. ", but the recommended MSAA level is ",
  368. OVRManager.display.recommendedMSAALevel,
  369. ". Switching to the recommended level."
  370. }));
  371. QualitySettings.antiAliasing = OVRManager.display.recommendedMSAALevel;
  372. }
  373. if (OVRManager._wasHmdPresent && !OVRManager.isHmdPresent)
  374. {
  375. try
  376. {
  377. if (OVRManager.HMDLost != null)
  378. {
  379. OVRManager.HMDLost();
  380. }
  381. }
  382. catch (Exception arg)
  383. {
  384. Debug.LogError("Caught Exception: " + arg);
  385. }
  386. }
  387. if (!OVRManager._wasHmdPresent && OVRManager.isHmdPresent)
  388. {
  389. try
  390. {
  391. if (OVRManager.HMDAcquired != null)
  392. {
  393. OVRManager.HMDAcquired();
  394. }
  395. }
  396. catch (Exception arg2)
  397. {
  398. Debug.LogError("Caught Exception: " + arg2);
  399. }
  400. }
  401. OVRManager._wasHmdPresent = OVRManager.isHmdPresent;
  402. this.isUserPresent = OVRPlugin.userPresent;
  403. if (OVRManager._wasUserPresent && !this.isUserPresent)
  404. {
  405. try
  406. {
  407. if (OVRManager.HMDUnmounted != null)
  408. {
  409. OVRManager.HMDUnmounted();
  410. }
  411. }
  412. catch (Exception arg3)
  413. {
  414. Debug.LogError("Caught Exception: " + arg3);
  415. }
  416. }
  417. if (!OVRManager._wasUserPresent && this.isUserPresent)
  418. {
  419. try
  420. {
  421. if (OVRManager.HMDMounted != null)
  422. {
  423. OVRManager.HMDMounted();
  424. }
  425. }
  426. catch (Exception arg4)
  427. {
  428. Debug.LogError("Caught Exception: " + arg4);
  429. }
  430. }
  431. OVRManager._wasUserPresent = this.isUserPresent;
  432. OVRManager.hasVrFocus = OVRPlugin.hasVrFocus;
  433. if (OVRManager._hadVrFocus && !OVRManager.hasVrFocus)
  434. {
  435. try
  436. {
  437. if (OVRManager.VrFocusLost != null)
  438. {
  439. OVRManager.VrFocusLost();
  440. }
  441. }
  442. catch (Exception arg5)
  443. {
  444. Debug.LogError("Caught Exception: " + arg5);
  445. }
  446. }
  447. if (!OVRManager._hadVrFocus && OVRManager.hasVrFocus)
  448. {
  449. try
  450. {
  451. if (OVRManager.VrFocusAcquired != null)
  452. {
  453. OVRManager.VrFocusAcquired();
  454. }
  455. }
  456. catch (Exception arg6)
  457. {
  458. Debug.LogError("Caught Exception: " + arg6);
  459. }
  460. }
  461. OVRManager._hadVrFocus = OVRManager.hasVrFocus;
  462. if (this.enableAdaptiveResolution)
  463. {
  464. if (VRSettings.renderScale < this.maxRenderScale)
  465. {
  466. VRSettings.renderScale = this.maxRenderScale;
  467. }
  468. else
  469. {
  470. this.maxRenderScale = Mathf.Max(this.maxRenderScale, VRSettings.renderScale);
  471. }
  472. float min = this.minRenderScale / VRSettings.renderScale;
  473. float num = OVRPlugin.GetEyeRecommendedResolutionScale() / VRSettings.renderScale;
  474. num = Mathf.Clamp(num, min, 1f);
  475. VRSettings.renderViewportScale = num;
  476. }
  477. string audioOutId = OVRPlugin.audioOutId;
  478. if (!OVRManager.prevAudioOutIdIsCached)
  479. {
  480. OVRManager.prevAudioOutId = audioOutId;
  481. OVRManager.prevAudioOutIdIsCached = true;
  482. }
  483. else if (audioOutId != OVRManager.prevAudioOutId)
  484. {
  485. try
  486. {
  487. if (OVRManager.AudioOutChanged != null)
  488. {
  489. OVRManager.AudioOutChanged();
  490. }
  491. }
  492. catch (Exception arg7)
  493. {
  494. Debug.LogError("Caught Exception: " + arg7);
  495. }
  496. OVRManager.prevAudioOutId = audioOutId;
  497. }
  498. string audioInId = OVRPlugin.audioInId;
  499. if (!OVRManager.prevAudioInIdIsCached)
  500. {
  501. OVRManager.prevAudioInId = audioInId;
  502. OVRManager.prevAudioInIdIsCached = true;
  503. }
  504. else if (audioInId != OVRManager.prevAudioInId)
  505. {
  506. try
  507. {
  508. if (OVRManager.AudioInChanged != null)
  509. {
  510. OVRManager.AudioInChanged();
  511. }
  512. }
  513. catch (Exception arg8)
  514. {
  515. Debug.LogError("Caught Exception: " + arg8);
  516. }
  517. OVRManager.prevAudioInId = audioInId;
  518. }
  519. if (OVRManager.wasPositionTracked && !OVRManager.tracker.isPositionTracked)
  520. {
  521. try
  522. {
  523. if (OVRManager.TrackingLost != null)
  524. {
  525. OVRManager.TrackingLost();
  526. }
  527. }
  528. catch (Exception arg9)
  529. {
  530. Debug.LogError("Caught Exception: " + arg9);
  531. }
  532. }
  533. if (!OVRManager.wasPositionTracked && OVRManager.tracker.isPositionTracked)
  534. {
  535. try
  536. {
  537. if (OVRManager.TrackingAcquired != null)
  538. {
  539. OVRManager.TrackingAcquired();
  540. }
  541. }
  542. catch (Exception arg10)
  543. {
  544. Debug.LogError("Caught Exception: " + arg10);
  545. }
  546. }
  547. OVRManager.wasPositionTracked = OVRManager.tracker.isPositionTracked;
  548. OVRManager.display.Update();
  549. OVRInput.Update();
  550. }
  551. private void LateUpdate()
  552. {
  553. OVRHaptics.Process();
  554. }
  555. private void FixedUpdate()
  556. {
  557. OVRInput.FixedUpdate();
  558. }
  559. public void ReturnToLauncher()
  560. {
  561. OVRManager.PlatformUIConfirmQuit();
  562. }
  563. public static void PlatformUIConfirmQuit()
  564. {
  565. if (!OVRManager.isHmdPresent)
  566. {
  567. return;
  568. }
  569. OVRPlugin.ShowUI(OVRPlugin.PlatformUI.ConfirmQuit);
  570. }
  571. public static void PlatformUIGlobalMenu()
  572. {
  573. if (!OVRManager.isHmdPresent)
  574. {
  575. return;
  576. }
  577. OVRPlugin.ShowUI(OVRPlugin.PlatformUI.GlobalMenu);
  578. }
  579. private static OVRProfile _profile;
  580. private bool _isPaused;
  581. private IEnumerable<Camera> disabledCameras;
  582. private float prevTimeScale;
  583. private static bool _isHmdPresentCached = false;
  584. private static bool _isHmdPresent = false;
  585. private static bool _wasHmdPresent = false;
  586. private static bool _hasVrFocusCached = false;
  587. private static bool _hasVrFocus = false;
  588. private static bool _hadVrFocus = false;
  589. public bool queueAhead = true;
  590. public bool useRecommendedMSAALevel;
  591. public bool enableAdaptiveResolution;
  592. [Range(0.5f, 2f)]
  593. public float maxRenderScale = 1f;
  594. [Range(0.5f, 2f)]
  595. public float minRenderScale = 0.7f;
  596. [SerializeField]
  597. private OVRManager.TrackingOrigin _trackingOriginType;
  598. public bool usePositionTracking = true;
  599. public bool useIPDInPositionTracking = true;
  600. public bool resetTrackerOnLoad;
  601. private static bool _isUserPresentCached = false;
  602. private static bool _isUserPresent = false;
  603. private static bool _wasUserPresent = false;
  604. private static bool prevAudioOutIdIsCached = false;
  605. private static bool prevAudioInIdIsCached = false;
  606. private static string prevAudioOutId = string.Empty;
  607. private static string prevAudioInId = string.Empty;
  608. private static bool wasPositionTracked = false;
  609. [SerializeField]
  610. [HideInInspector]
  611. internal static bool runInBackground = false;
  612. public enum TrackingOrigin
  613. {
  614. EyeLevel,
  615. FloorLevel
  616. }
  617. }