OVRManager.cs 14 KB

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