OVRManager.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. #if USING_XR_MANAGEMENT && USING_XR_SDK_OCULUS
  14. #define USING_XR_SDK
  15. #endif
  16. #if UNITY_ANDROID && !UNITY_EDITOR
  17. #define OVR_ANDROID_MRC
  18. #endif
  19. #if !UNITY_5_6_OR_NEWER
  20. #error Oculus Utilities require Unity 5.6 or higher.
  21. #endif
  22. using System;
  23. using System.Collections.Generic;
  24. using UnityEngine;
  25. #if UNITY_EDITOR
  26. using UnityEditor;
  27. #endif
  28. #if USING_XR_SDK
  29. using UnityEngine.XR;
  30. using UnityEngine.Experimental.XR;
  31. #endif
  32. #if UNITY_2017_2_OR_NEWER
  33. using Settings = UnityEngine.XR.XRSettings;
  34. using Node = UnityEngine.XR.XRNode;
  35. #else
  36. using Settings = UnityEngine.VR.VRSettings;
  37. using Node = UnityEngine.VR.VRNode;
  38. #endif
  39. /// <summary>
  40. /// Configuration data for Oculus virtual reality.
  41. /// </summary>
  42. public class OVRManager : MonoBehaviour
  43. {
  44. public enum TrackingOrigin
  45. {
  46. EyeLevel = OVRPlugin.TrackingOrigin.EyeLevel,
  47. FloorLevel = OVRPlugin.TrackingOrigin.FloorLevel,
  48. Stage = OVRPlugin.TrackingOrigin.Stage,
  49. }
  50. public enum EyeTextureFormat
  51. {
  52. Default = OVRPlugin.EyeTextureFormat.Default,
  53. R16G16B16A16_FP = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP,
  54. R11G11B10_FP = OVRPlugin.EyeTextureFormat.R11G11B10_FP,
  55. }
  56. public enum FixedFoveatedRenderingLevel
  57. {
  58. Off = OVRPlugin.FixedFoveatedRenderingLevel.Off,
  59. Low = OVRPlugin.FixedFoveatedRenderingLevel.Low,
  60. Medium = OVRPlugin.FixedFoveatedRenderingLevel.Medium,
  61. High = OVRPlugin.FixedFoveatedRenderingLevel.High,
  62. HighTop = OVRPlugin.FixedFoveatedRenderingLevel.HighTop,
  63. }
  64. [Obsolete("Please use FixedFoveatedRenderingLevel instead")]
  65. public enum TiledMultiResLevel
  66. {
  67. Off = OVRPlugin.TiledMultiResLevel.Off,
  68. LMSLow = OVRPlugin.TiledMultiResLevel.LMSLow,
  69. LMSMedium = OVRPlugin.TiledMultiResLevel.LMSMedium,
  70. LMSHigh = OVRPlugin.TiledMultiResLevel.LMSHigh,
  71. LMSHighTop = OVRPlugin.TiledMultiResLevel.LMSHighTop,
  72. }
  73. public enum XRDevice
  74. {
  75. Unknown = 0,
  76. Oculus = 1,
  77. OpenVR = 2,
  78. }
  79. /// <summary>
  80. /// Gets the singleton instance.
  81. /// </summary>
  82. public static OVRManager instance { get; private set; }
  83. /// <summary>
  84. /// Gets a reference to the active display.
  85. /// </summary>
  86. public static OVRDisplay display { get; private set; }
  87. /// <summary>
  88. /// Gets a reference to the active sensor.
  89. /// </summary>
  90. public static OVRTracker tracker { get; private set; }
  91. /// <summary>
  92. /// Gets a reference to the active boundary system.
  93. /// </summary>
  94. public static OVRBoundary boundary { get; private set; }
  95. private static OVRProfile _profile;
  96. /// <summary>
  97. /// Gets the current profile, which contains information about the user's settings and body dimensions.
  98. /// </summary>
  99. public static OVRProfile profile
  100. {
  101. get {
  102. if (_profile == null)
  103. _profile = new OVRProfile();
  104. return _profile;
  105. }
  106. }
  107. private IEnumerable<Camera> disabledCameras;
  108. float prevTimeScale;
  109. /// <summary>
  110. /// Occurs when an HMD attached.
  111. /// </summary>
  112. public static event Action HMDAcquired;
  113. /// <summary>
  114. /// Occurs when an HMD detached.
  115. /// </summary>
  116. public static event Action HMDLost;
  117. /// <summary>
  118. /// Occurs when an HMD is put on the user's head.
  119. /// </summary>
  120. public static event Action HMDMounted;
  121. /// <summary>
  122. /// Occurs when an HMD is taken off the user's head.
  123. /// </summary>
  124. public static event Action HMDUnmounted;
  125. /// <summary>
  126. /// Occurs when VR Focus is acquired.
  127. /// </summary>
  128. public static event Action VrFocusAcquired;
  129. /// <summary>
  130. /// Occurs when VR Focus is lost.
  131. /// </summary>
  132. public static event Action VrFocusLost;
  133. /// <summary>
  134. /// Occurs when Input Focus is acquired.
  135. /// </summary>
  136. public static event Action InputFocusAcquired;
  137. /// <summary>
  138. /// Occurs when Input Focus is lost.
  139. /// </summary>
  140. public static event Action InputFocusLost;
  141. /// <summary>
  142. /// Occurs when the active Audio Out device has changed and a restart is needed.
  143. /// </summary>
  144. public static event Action AudioOutChanged;
  145. /// <summary>
  146. /// Occurs when the active Audio In device has changed and a restart is needed.
  147. /// </summary>
  148. public static event Action AudioInChanged;
  149. /// <summary>
  150. /// Occurs when the sensor gained tracking.
  151. /// </summary>
  152. public static event Action TrackingAcquired;
  153. /// <summary>
  154. /// Occurs when the sensor lost tracking.
  155. /// </summary>
  156. public static event Action TrackingLost;
  157. /// <summary>
  158. /// Occurs when Health & Safety Warning is dismissed.
  159. /// </summary>
  160. //Disable the warning about it being unused. It's deprecated.
  161. #pragma warning disable 0067
  162. [Obsolete]
  163. public static event Action HSWDismissed;
  164. #pragma warning restore
  165. private static bool _isHmdPresentCached = false;
  166. private static bool _isHmdPresent = false;
  167. private static bool _wasHmdPresent = false;
  168. /// <summary>
  169. /// If true, a head-mounted display is connected and present.
  170. /// </summary>
  171. public static bool isHmdPresent
  172. {
  173. get {
  174. if (!_isHmdPresentCached)
  175. {
  176. _isHmdPresentCached = true;
  177. _isHmdPresent = OVRNodeStateProperties.IsHmdPresent();
  178. }
  179. return _isHmdPresent;
  180. }
  181. private set {
  182. _isHmdPresentCached = true;
  183. _isHmdPresent = value;
  184. }
  185. }
  186. /// <summary>
  187. /// Gets the audio output device identifier.
  188. /// </summary>
  189. /// <description>
  190. /// On Windows, this is a string containing the GUID of the IMMDevice for the Windows audio endpoint to use.
  191. /// </description>
  192. public static string audioOutId
  193. {
  194. get { return OVRPlugin.audioOutId; }
  195. }
  196. /// <summary>
  197. /// Gets the audio input device identifier.
  198. /// </summary>
  199. /// <description>
  200. /// On Windows, this is a string containing the GUID of the IMMDevice for the Windows audio endpoint to use.
  201. /// </description>
  202. public static string audioInId
  203. {
  204. get { return OVRPlugin.audioInId; }
  205. }
  206. private static bool _hasVrFocusCached = false;
  207. private static bool _hasVrFocus = false;
  208. private static bool _hadVrFocus = false;
  209. /// <summary>
  210. /// If true, the app has VR Focus.
  211. /// </summary>
  212. public static bool hasVrFocus
  213. {
  214. get {
  215. if (!_hasVrFocusCached)
  216. {
  217. _hasVrFocusCached = true;
  218. _hasVrFocus = OVRPlugin.hasVrFocus;
  219. }
  220. return _hasVrFocus;
  221. }
  222. private set {
  223. _hasVrFocusCached = true;
  224. _hasVrFocus = value;
  225. }
  226. }
  227. private static bool _hadInputFocus = true;
  228. /// <summary>
  229. /// If true, the app has Input Focus.
  230. /// </summary>
  231. public static bool hasInputFocus
  232. {
  233. get
  234. {
  235. return OVRPlugin.hasInputFocus;
  236. }
  237. }
  238. /// <summary>
  239. /// If true, chromatic de-aberration will be applied, improving the image at the cost of texture bandwidth.
  240. /// </summary>
  241. public bool chromatic
  242. {
  243. get {
  244. if (!isHmdPresent)
  245. return false;
  246. return OVRPlugin.chromatic;
  247. }
  248. set {
  249. if (!isHmdPresent)
  250. return;
  251. OVRPlugin.chromatic = value;
  252. }
  253. }
  254. [Header("Performance/Quality")]
  255. /// <summary>
  256. /// If true, distortion rendering work is submitted a quarter-frame early to avoid pipeline stalls and increase CPU-GPU parallelism.
  257. /// </summary>
  258. [Tooltip("If true, distortion rendering work is submitted a quarter-frame early to avoid pipeline stalls and increase CPU-GPU parallelism.")]
  259. public bool queueAhead = true;
  260. /// <summary>
  261. /// If true, Unity will use the optimal antialiasing level for quality/performance on the current hardware.
  262. /// </summary>
  263. [Tooltip("If true, Unity will use the optimal antialiasing level for quality/performance on the current hardware.")]
  264. public bool useRecommendedMSAALevel = true;
  265. /// <summary>
  266. /// If true, both eyes will see the same image, rendered from the center eye pose, saving performance.
  267. /// </summary>
  268. [SerializeField]
  269. [Tooltip("If true, both eyes will see the same image, rendered from the center eye pose, saving performance.")]
  270. private bool _monoscopic = false;
  271. public bool monoscopic
  272. {
  273. get
  274. {
  275. if (!isHmdPresent)
  276. return _monoscopic;
  277. return OVRPlugin.monoscopic;
  278. }
  279. set
  280. {
  281. if (!isHmdPresent)
  282. return;
  283. OVRPlugin.monoscopic = value;
  284. _monoscopic = value;
  285. }
  286. }
  287. /// <summary>
  288. /// If true, dynamic resolution will be enabled
  289. /// </summary>
  290. [Tooltip("If true, dynamic resolution will be enabled On PC")]
  291. public bool enableAdaptiveResolution = false;
  292. /// <summary>
  293. /// Adaptive Resolution is based on Unity engine's renderViewportScale/eyeTextureResolutionScale feature
  294. /// But renderViewportScale was broken in an array of Unity engines, this function help to filter out those broken engines
  295. /// </summary>
  296. public static bool IsAdaptiveResSupportedByEngine()
  297. {
  298. #if UNITY_2017_1_OR_NEWER
  299. return Application.unityVersion != "2017.1.0f1";
  300. #else
  301. return false;
  302. #endif
  303. }
  304. /// <summary>
  305. /// Min RenderScale the app can reach under adaptive resolution mode ( enableAdaptiveResolution = true );
  306. /// </summary>
  307. [RangeAttribute(0.5f, 2.0f)]
  308. [Tooltip("Min RenderScale the app can reach under adaptive resolution mode")]
  309. public float minRenderScale = 0.7f;
  310. /// <summary>
  311. /// Max RenderScale the app can reach under adaptive resolution mode ( enableAdaptiveResolution = true );
  312. /// </summary>
  313. [RangeAttribute(0.5f, 2.0f)]
  314. [Tooltip("Max RenderScale the app can reach under adaptive resolution mode")]
  315. public float maxRenderScale = 1.0f;
  316. /// <summary>
  317. /// Set the relative offset rotation of head poses
  318. /// </summary>
  319. [SerializeField]
  320. [Tooltip("Set the relative offset rotation of head poses")]
  321. private Vector3 _headPoseRelativeOffsetRotation;
  322. public Vector3 headPoseRelativeOffsetRotation
  323. {
  324. get
  325. {
  326. return _headPoseRelativeOffsetRotation;
  327. }
  328. set
  329. {
  330. OVRPlugin.Quatf rotation;
  331. OVRPlugin.Vector3f translation;
  332. if (OVRPlugin.GetHeadPoseModifier(out rotation, out translation))
  333. {
  334. Quaternion finalRotation = Quaternion.Euler(value);
  335. rotation = finalRotation.ToQuatf();
  336. OVRPlugin.SetHeadPoseModifier(ref rotation, ref translation);
  337. }
  338. _headPoseRelativeOffsetRotation = value;
  339. }
  340. }
  341. /// <summary>
  342. /// Set the relative offset translation of head poses
  343. /// </summary>
  344. [SerializeField]
  345. [Tooltip("Set the relative offset translation of head poses")]
  346. private Vector3 _headPoseRelativeOffsetTranslation;
  347. public Vector3 headPoseRelativeOffsetTranslation
  348. {
  349. get
  350. {
  351. return _headPoseRelativeOffsetTranslation;
  352. }
  353. set
  354. {
  355. OVRPlugin.Quatf rotation;
  356. OVRPlugin.Vector3f translation;
  357. if (OVRPlugin.GetHeadPoseModifier(out rotation, out translation))
  358. {
  359. if (translation.FromFlippedZVector3f() != value)
  360. {
  361. translation = value.ToFlippedZVector3f();
  362. OVRPlugin.SetHeadPoseModifier(ref rotation, ref translation);
  363. }
  364. }
  365. _headPoseRelativeOffsetTranslation = value;
  366. }
  367. }
  368. /// <summary>
  369. /// The TCP listening port of Oculus Profiler Service, which will be activated in Debug/Developerment builds
  370. /// When the app is running on editor or device, open "Tools/Oculus/Oculus Profiler Panel" to view the realtime system metrics
  371. /// </summary>
  372. public int profilerTcpPort = OVRSystemPerfMetrics.TcpListeningPort;
  373. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
  374. /// <summary>
  375. /// If true, the MixedRealityCapture properties will be displayed
  376. /// </summary>
  377. [HideInInspector]
  378. public bool expandMixedRealityCapturePropertySheet = false;
  379. /// <summary>
  380. /// If true, Mixed Reality mode will be enabled
  381. /// </summary>
  382. [HideInInspector, Tooltip("If true, Mixed Reality mode will be enabled. It would be always set to false when the game is launching without editor")]
  383. public bool enableMixedReality = false;
  384. public enum CompositionMethod
  385. {
  386. External,
  387. Direct
  388. }
  389. /// <summary>
  390. /// Composition method
  391. /// </summary>
  392. [HideInInspector]
  393. public CompositionMethod compositionMethod = CompositionMethod.External;
  394. /// <summary>
  395. /// Extra hidden layers
  396. /// </summary>
  397. [HideInInspector, Tooltip("Extra hidden layers")]
  398. public LayerMask extraHiddenLayers;
  399. /// <summary>
  400. /// The backdrop color will be used when rendering the foreground frames (on Rift). It only applies to External Composition.
  401. /// </summary>
  402. [HideInInspector, Tooltip("Backdrop color for Rift (External Compositon)")]
  403. public Color externalCompositionBackdropColorRift = Color.green;
  404. /// <summary>
  405. /// The backdrop color will be used when rendering the foreground frames (on Quest). It only applies to External Composition.
  406. /// </summary>
  407. [HideInInspector, Tooltip("Backdrop color for Quest (External Compositon)")]
  408. public Color externalCompositionBackdropColorQuest = Color.clear;
  409. /// <summary>
  410. /// If true, Mixed Reality mode will use direct composition from the first web camera
  411. /// </summary>
  412. public enum CameraDevice
  413. {
  414. WebCamera0,
  415. WebCamera1,
  416. ZEDCamera
  417. }
  418. /// <summary>
  419. /// The camera device for direct composition
  420. /// </summary>
  421. [HideInInspector, Tooltip("The camera device for direct composition")]
  422. public CameraDevice capturingCameraDevice = CameraDevice.WebCamera0;
  423. /// <summary>
  424. /// Flip the camera frame horizontally
  425. /// </summary>
  426. [HideInInspector, Tooltip("Flip the camera frame horizontally")]
  427. public bool flipCameraFrameHorizontally = false;
  428. /// <summary>
  429. /// Flip the camera frame vertically
  430. /// </summary>
  431. [HideInInspector, Tooltip("Flip the camera frame vertically")]
  432. public bool flipCameraFrameVertically = false;
  433. /// <summary>
  434. /// Delay the touch controller pose by a short duration (0 to 0.5 second) to match the physical camera latency
  435. /// </summary>
  436. [HideInInspector, Tooltip("Delay the touch controller pose by a short duration (0 to 0.5 second) to match the physical camera latency")]
  437. public float handPoseStateLatency = 0.0f;
  438. /// <summary>
  439. /// Delay the foreground / background image in the sandwich composition to match the physical camera latency. The maximum duration is sandwichCompositionBufferedFrames / {Game FPS}
  440. /// </summary>
  441. [HideInInspector, Tooltip("Delay the foreground / background image in the sandwich composition to match the physical camera latency. The maximum duration is sandwichCompositionBufferedFrames / {Game FPS}")]
  442. public float sandwichCompositionRenderLatency = 0.0f;
  443. /// <summary>
  444. /// The number of frames are buffered in the SandWich composition. The more buffered frames, the more memory it would consume.
  445. /// </summary>
  446. [HideInInspector, Tooltip("The number of frames are buffered in the SandWich composition. The more buffered frames, the more memory it would consume.")]
  447. public int sandwichCompositionBufferedFrames = 8;
  448. /// <summary>
  449. /// Chroma Key Color
  450. /// </summary>
  451. [HideInInspector, Tooltip("Chroma Key Color")]
  452. public Color chromaKeyColor = Color.green;
  453. /// <summary>
  454. /// Chroma Key Similarity
  455. /// </summary>
  456. [HideInInspector, Tooltip("Chroma Key Similarity")]
  457. public float chromaKeySimilarity = 0.60f;
  458. /// <summary>
  459. /// Chroma Key Smooth Range
  460. /// </summary>
  461. [HideInInspector, Tooltip("Chroma Key Smooth Range")]
  462. public float chromaKeySmoothRange = 0.03f;
  463. /// <summary>
  464. /// Chroma Key Spill Range
  465. /// </summary>
  466. [HideInInspector, Tooltip("Chroma Key Spill Range")]
  467. public float chromaKeySpillRange = 0.06f;
  468. /// <summary>
  469. /// Use dynamic lighting (Depth sensor required)
  470. /// </summary>
  471. [HideInInspector, Tooltip("Use dynamic lighting (Depth sensor required)")]
  472. public bool useDynamicLighting = false;
  473. public enum DepthQuality
  474. {
  475. Low,
  476. Medium,
  477. High
  478. }
  479. /// <summary>
  480. /// The quality level of depth image. The lighting could be more smooth and accurate with high quality depth, but it would also be more costly in performance.
  481. /// </summary>
  482. [HideInInspector, Tooltip("The quality level of depth image. The lighting could be more smooth and accurate with high quality depth, but it would also be more costly in performance.")]
  483. public DepthQuality depthQuality = DepthQuality.Medium;
  484. /// <summary>
  485. /// Smooth factor in dynamic lighting. Larger is smoother
  486. /// </summary>
  487. [HideInInspector, Tooltip("Smooth factor in dynamic lighting. Larger is smoother")]
  488. public float dynamicLightingSmoothFactor = 8.0f;
  489. /// <summary>
  490. /// The maximum depth variation across the edges. Make it smaller to smooth the lighting on the edges.
  491. /// </summary>
  492. [HideInInspector, Tooltip("The maximum depth variation across the edges. Make it smaller to smooth the lighting on the edges.")]
  493. public float dynamicLightingDepthVariationClampingValue = 0.001f;
  494. public enum VirtualGreenScreenType
  495. {
  496. Off,
  497. OuterBoundary,
  498. PlayArea
  499. }
  500. /// <summary>
  501. /// Set the current type of the virtual green screen
  502. /// </summary>
  503. [HideInInspector, Tooltip("Type of virutal green screen ")]
  504. public VirtualGreenScreenType virtualGreenScreenType = VirtualGreenScreenType.Off;
  505. /// <summary>
  506. /// Top Y of virtual screen
  507. /// </summary>
  508. [HideInInspector, Tooltip("Top Y of virtual green screen")]
  509. public float virtualGreenScreenTopY = 10.0f;
  510. /// <summary>
  511. /// Bottom Y of virtual screen
  512. /// </summary>
  513. [HideInInspector, Tooltip("Bottom Y of virtual green screen")]
  514. public float virtualGreenScreenBottomY = -10.0f;
  515. /// <summary>
  516. /// When using a depth camera (e.g. ZED), whether to use the depth in virtual green screen culling.
  517. /// </summary>
  518. [HideInInspector, Tooltip("When using a depth camera (e.g. ZED), whether to use the depth in virtual green screen culling.")]
  519. public bool virtualGreenScreenApplyDepthCulling = false;
  520. /// <summary>
  521. /// The tolerance value (in meter) when using the virtual green screen with a depth camera. Make it bigger if the foreground objects got culled incorrectly.
  522. /// </summary>
  523. [HideInInspector, Tooltip("The tolerance value (in meter) when using the virtual green screen with a depth camera. Make it bigger if the foreground objects got culled incorrectly.")]
  524. public float virtualGreenScreenDepthTolerance = 0.2f;
  525. public enum MrcActivationMode
  526. {
  527. Automatic,
  528. Disabled
  529. }
  530. /// <summary>
  531. /// (Quest-only) control if the mixed reality capture mode can be activated automatically through remote network connection.
  532. /// </summary>
  533. [HideInInspector, Tooltip("(Quest-only) control if the mixed reality capture mode can be activated automatically through remote network connection.")]
  534. public MrcActivationMode mrcActivationMode;
  535. #endif
  536. /// <summary>
  537. /// The number of expected display frames per rendered frame.
  538. /// </summary>
  539. public int vsyncCount
  540. {
  541. get {
  542. if (!isHmdPresent)
  543. return 1;
  544. return OVRPlugin.vsyncCount;
  545. }
  546. set {
  547. if (!isHmdPresent)
  548. return;
  549. OVRPlugin.vsyncCount = value;
  550. }
  551. }
  552. public static string OCULUS_UNITY_NAME_STR = "Oculus";
  553. public static string OPENVR_UNITY_NAME_STR = "OpenVR";
  554. public static XRDevice loadedXRDevice;
  555. /// <summary>
  556. /// Gets the current battery level.
  557. /// </summary>
  558. /// <returns><c>battery level in the range [0.0,1.0]</c>
  559. /// <param name="batteryLevel">Battery level.</param>
  560. public static float batteryLevel
  561. {
  562. get {
  563. if (!isHmdPresent)
  564. return 1f;
  565. return OVRPlugin.batteryLevel;
  566. }
  567. }
  568. /// <summary>
  569. /// Gets the current battery temperature.
  570. /// </summary>
  571. /// <returns><c>battery temperature in Celsius</c>
  572. /// <param name="batteryTemperature">Battery temperature.</param>
  573. public static float batteryTemperature
  574. {
  575. get {
  576. if (!isHmdPresent)
  577. return 0f;
  578. return OVRPlugin.batteryTemperature;
  579. }
  580. }
  581. /// <summary>
  582. /// Gets the current battery status.
  583. /// </summary>
  584. /// <returns><c>battery status</c>
  585. /// <param name="batteryStatus">Battery status.</param>
  586. public static int batteryStatus
  587. {
  588. get {
  589. if (!isHmdPresent)
  590. return -1;
  591. return (int)OVRPlugin.batteryStatus;
  592. }
  593. }
  594. /// <summary>
  595. /// Gets the current volume level.
  596. /// </summary>
  597. /// <returns><c>volume level in the range [0,1].</c>
  598. public static float volumeLevel
  599. {
  600. get {
  601. if (!isHmdPresent)
  602. return 0f;
  603. return OVRPlugin.systemVolume;
  604. }
  605. }
  606. /// <summary>
  607. /// Gets or sets the current CPU performance level (0-2). Lower performance levels save more power.
  608. /// </summary>
  609. public static int cpuLevel
  610. {
  611. get {
  612. if (!isHmdPresent)
  613. return 2;
  614. return OVRPlugin.cpuLevel;
  615. }
  616. set {
  617. if (!isHmdPresent)
  618. return;
  619. OVRPlugin.cpuLevel = value;
  620. }
  621. }
  622. /// <summary>
  623. /// Gets or sets the current GPU performance level (0-2). Lower performance levels save more power.
  624. /// </summary>
  625. public static int gpuLevel
  626. {
  627. get {
  628. if (!isHmdPresent)
  629. return 2;
  630. return OVRPlugin.gpuLevel;
  631. }
  632. set {
  633. if (!isHmdPresent)
  634. return;
  635. OVRPlugin.gpuLevel = value;
  636. }
  637. }
  638. /// <summary>
  639. /// If true, the CPU and GPU are currently throttled to save power and/or reduce the temperature.
  640. /// </summary>
  641. public static bool isPowerSavingActive
  642. {
  643. get {
  644. if (!isHmdPresent)
  645. return false;
  646. return OVRPlugin.powerSaving;
  647. }
  648. }
  649. /// <summary>
  650. /// Gets or sets the eye texture format.
  651. /// </summary>
  652. public static EyeTextureFormat eyeTextureFormat
  653. {
  654. get
  655. {
  656. return (OVRManager.EyeTextureFormat)OVRPlugin.GetDesiredEyeTextureFormat();
  657. }
  658. set
  659. {
  660. OVRPlugin.SetDesiredEyeTextureFormat((OVRPlugin.EyeTextureFormat)value);
  661. }
  662. }
  663. /// <summary>
  664. /// Gets if tiled-based multi-resolution technique is supported
  665. /// This feature is only supported on QCOMM-based Android devices
  666. /// </summary>
  667. public static bool fixedFoveatedRenderingSupported
  668. {
  669. get
  670. {
  671. return OVRPlugin.fixedFoveatedRenderingSupported;
  672. }
  673. }
  674. /// <summary>
  675. /// Gets or sets the tiled-based multi-resolution level
  676. /// This feature is only supported on QCOMM-based Android devices
  677. /// </summary>
  678. public static FixedFoveatedRenderingLevel fixedFoveatedRenderingLevel
  679. {
  680. get
  681. {
  682. if (!OVRPlugin.fixedFoveatedRenderingSupported)
  683. {
  684. Debug.LogWarning("Fixed Foveated Rendering feature is not supported");
  685. }
  686. return (FixedFoveatedRenderingLevel)OVRPlugin.fixedFoveatedRenderingLevel;
  687. }
  688. set
  689. {
  690. if (!OVRPlugin.fixedFoveatedRenderingSupported)
  691. {
  692. Debug.LogWarning("Fixed Foveated Rendering feature is not supported");
  693. }
  694. OVRPlugin.fixedFoveatedRenderingLevel = (OVRPlugin.FixedFoveatedRenderingLevel)value;
  695. }
  696. }
  697. [Obsolete("Please use fixedFoveatedRenderingSupported instead", false)]
  698. public static bool tiledMultiResSupported
  699. {
  700. get
  701. {
  702. return OVRPlugin.tiledMultiResSupported;
  703. }
  704. }
  705. [Obsolete("Please use fixedFoveatedRenderingLevel instead", false)]
  706. public static TiledMultiResLevel tiledMultiResLevel
  707. {
  708. get
  709. {
  710. if (!OVRPlugin.tiledMultiResSupported)
  711. {
  712. Debug.LogWarning("Tiled-based Multi-resolution feature is not supported");
  713. }
  714. return (TiledMultiResLevel)OVRPlugin.tiledMultiResLevel;
  715. }
  716. set
  717. {
  718. if (!OVRPlugin.tiledMultiResSupported)
  719. {
  720. Debug.LogWarning("Tiled-based Multi-resolution feature is not supported");
  721. }
  722. OVRPlugin.tiledMultiResLevel = (OVRPlugin.TiledMultiResLevel)value;
  723. }
  724. }
  725. /// <summary>
  726. /// Gets if the GPU Utility is supported
  727. /// This feature is only supported on QCOMM-based Android devices
  728. /// </summary>
  729. public static bool gpuUtilSupported
  730. {
  731. get
  732. {
  733. return OVRPlugin.gpuUtilSupported;
  734. }
  735. }
  736. /// <summary>
  737. /// Gets the GPU Utilised Level (0.0 - 1.0)
  738. /// This feature is only supported on QCOMM-based Android devices
  739. /// </summary>
  740. public static float gpuUtilLevel
  741. {
  742. get
  743. {
  744. if (!OVRPlugin.gpuUtilSupported)
  745. {
  746. Debug.LogWarning("GPU Util is not supported");
  747. }
  748. return OVRPlugin.gpuUtilLevel;
  749. }
  750. }
  751. /// <summary>
  752. /// Sets the Color Scale and Offset which is commonly used for effects like fade-to-black.
  753. /// In our compositor, once a given frame is rendered, warped, and ready to be displayed, we then multiply
  754. /// each pixel by colorScale and add it to colorOffset, whereby newPixel = oldPixel * colorScale + colorOffset.
  755. /// Note that for mobile devices (Quest, Go, etc.), colorOffset is not supported, so colorScale is all that can
  756. /// be used. A colorScale of (1, 1, 1, 1) and colorOffset of (0, 0, 0, 0) will lead to an identity multiplication
  757. /// and have no effect.
  758. /// </summary>
  759. public static void SetColorScaleAndOffset(Vector4 colorScale, Vector4 colorOffset, bool applyToAllLayers)
  760. {
  761. OVRPlugin.SetColorScaleAndOffset(colorScale, colorOffset, applyToAllLayers);
  762. }
  763. /// <summary>
  764. /// Specifies OpenVR pose local to tracking space
  765. /// </summary>
  766. public static void SetOpenVRLocalPose(Vector3 leftPos, Vector3 rightPos, Quaternion leftRot, Quaternion rightRot)
  767. {
  768. if (loadedXRDevice == XRDevice.OpenVR)
  769. OVRInput.SetOpenVRLocalPose(leftPos, rightPos, leftRot, rightRot);
  770. }
  771. //Series of offsets that line up the virtual controllers to the phsyical world.
  772. private static Vector3 OpenVRTouchRotationOffsetEulerLeft = new Vector3(40.0f, 0.0f, 0.0f);
  773. private static Vector3 OpenVRTouchRotationOffsetEulerRight = new Vector3(40.0f, 0.0f, 0.0f);
  774. private static Vector3 OpenVRTouchPositionOffsetLeft = new Vector3(0.0075f, -0.005f, -0.0525f);
  775. private static Vector3 OpenVRTouchPositionOffsetRight = new Vector3(-0.0075f, -0.005f, -0.0525f);
  776. /// <summary>
  777. /// Specifies the pose offset required to make an OpenVR controller's reported pose match the virtual pose.
  778. /// Currently we only specify this offset for Oculus Touch on OpenVR.
  779. /// </summary>
  780. public static OVRPose GetOpenVRControllerOffset(Node hand)
  781. {
  782. OVRPose poseOffset = OVRPose.identity;
  783. if ((hand == Node.LeftHand || hand == Node.RightHand) && loadedXRDevice == XRDevice.OpenVR)
  784. {
  785. int index = (hand == Node.LeftHand) ? 0 : 1;
  786. if (OVRInput.openVRControllerDetails[index].controllerType == OVRInput.OpenVRController.OculusTouch)
  787. {
  788. Vector3 offsetOrientation = (hand == Node.LeftHand) ? OpenVRTouchRotationOffsetEulerLeft : OpenVRTouchRotationOffsetEulerRight;
  789. poseOffset.orientation = Quaternion.Euler(offsetOrientation.x, offsetOrientation.y, offsetOrientation.z);
  790. poseOffset.position = (hand == Node.LeftHand) ? OpenVRTouchPositionOffsetLeft : OpenVRTouchPositionOffsetRight;
  791. }
  792. }
  793. return poseOffset;
  794. }
  795. [Header("Tracking")]
  796. [SerializeField]
  797. [Tooltip("Defines the current tracking origin type.")]
  798. private OVRManager.TrackingOrigin _trackingOriginType = OVRManager.TrackingOrigin.EyeLevel;
  799. /// <summary>
  800. /// Defines the current tracking origin type.
  801. /// </summary>
  802. public OVRManager.TrackingOrigin trackingOriginType
  803. {
  804. get {
  805. if (!isHmdPresent)
  806. return _trackingOriginType;
  807. return (OVRManager.TrackingOrigin)OVRPlugin.GetTrackingOriginType();
  808. }
  809. set {
  810. if (!isHmdPresent)
  811. return;
  812. if (OVRPlugin.SetTrackingOriginType((OVRPlugin.TrackingOrigin)value))
  813. {
  814. // Keep the field exposed in the Unity Editor synchronized with any changes.
  815. _trackingOriginType = value;
  816. }
  817. }
  818. }
  819. /// <summary>
  820. /// If true, head tracking will affect the position of each OVRCameraRig's cameras.
  821. /// </summary>
  822. [Tooltip("If true, head tracking will affect the position of each OVRCameraRig's cameras.")]
  823. public bool usePositionTracking = true;
  824. /// <summary>
  825. /// If true, head tracking will affect the rotation of each OVRCameraRig's cameras.
  826. /// </summary>
  827. [HideInInspector]
  828. public bool useRotationTracking = true;
  829. /// <summary>
  830. /// If true, the distance between the user's eyes will affect the position of each OVRCameraRig's cameras.
  831. /// </summary>
  832. [Tooltip("If true, the distance between the user's eyes will affect the position of each OVRCameraRig's cameras.")]
  833. public bool useIPDInPositionTracking = true;
  834. /// <summary>
  835. /// If true, each scene load will cause the head pose to reset.
  836. /// </summary>
  837. [Tooltip("If true, each scene load will cause the head pose to reset.")]
  838. public bool resetTrackerOnLoad = false;
  839. /// <summary>
  840. /// If true, the Reset View in the universal menu will cause the pose to be reset. This should generally be
  841. /// enabled for applications with a stationary position in the virtual world and will allow the View Reset
  842. /// command to place the person back to a predefined location (such as a cockpit seat).
  843. /// Set this to false if you have a locomotion system because resetting the view would effectively teleport
  844. /// the player to potentially invalid locations.
  845. /// </summary>
  846. [Tooltip("If true, the Reset View in the universal menu will cause the pose to be reset. This should generally be enabled for applications with a stationary position in the virtual world and will allow the View Reset command to place the person back to a predefined location (such as a cockpit seat). Set this to false if you have a locomotion system because resetting the view would effectively teleport the player to potentially invalid locations.")]
  847. public bool AllowRecenter = true;
  848. [SerializeField]
  849. [Tooltip("Specifies HMD recentering behavior when controller recenter is performed. True recenters the HMD as well, false does not.")]
  850. private bool _reorientHMDOnControllerRecenter = true;
  851. /// <summary>
  852. /// Defines the recentering mode specified in the tooltip above.
  853. /// </summary>
  854. public bool reorientHMDOnControllerRecenter
  855. {
  856. get
  857. {
  858. if (!isHmdPresent)
  859. return false;
  860. return OVRPlugin.GetReorientHMDOnControllerRecenter();
  861. }
  862. set
  863. {
  864. if (!isHmdPresent)
  865. return;
  866. OVRPlugin.SetReorientHMDOnControllerRecenter(value);
  867. }
  868. }
  869. /// <summary>
  870. /// If true, a lower-latency update will occur right before rendering. If false, the only controller pose update will occur at the start of simulation for a given frame.
  871. /// Selecting this option lowers rendered latency for controllers and is often a net positive; however, it also creates a slight disconnect between rendered and simulated controller poses.
  872. /// Visit online Oculus documentation to learn more.
  873. /// </summary>
  874. [Tooltip("If true, rendered controller latency is reduced by several ms, as the left/right controllers will have their positions updated right before rendering.")]
  875. public bool LateControllerUpdate = true;
  876. /// <summary>
  877. /// True if the current platform supports virtual reality.
  878. /// </summary>
  879. public bool isSupportedPlatform { get; private set; }
  880. private static bool _isUserPresentCached = false;
  881. private static bool _isUserPresent = false;
  882. private static bool _wasUserPresent = false;
  883. /// <summary>
  884. /// True if the user is currently wearing the display.
  885. /// </summary>
  886. public bool isUserPresent
  887. {
  888. get {
  889. if (!_isUserPresentCached)
  890. {
  891. _isUserPresentCached = true;
  892. _isUserPresent = OVRPlugin.userPresent;
  893. }
  894. return _isUserPresent;
  895. }
  896. private set {
  897. _isUserPresentCached = true;
  898. _isUserPresent = value;
  899. }
  900. }
  901. private static bool prevAudioOutIdIsCached = false;
  902. private static bool prevAudioInIdIsCached = false;
  903. private static string prevAudioOutId = string.Empty;
  904. private static string prevAudioInId = string.Empty;
  905. private static bool wasPositionTracked = false;
  906. public static System.Version utilitiesVersion
  907. {
  908. get { return OVRPlugin.wrapperVersion; }
  909. }
  910. public static System.Version pluginVersion
  911. {
  912. get { return OVRPlugin.version; }
  913. }
  914. public static System.Version sdkVersion
  915. {
  916. get { return OVRPlugin.nativeSDKVersion; }
  917. }
  918. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
  919. private static bool MixedRealityEnabledFromCmd()
  920. {
  921. var args = System.Environment.GetCommandLineArgs();
  922. for (int i = 0; i < args.Length; i++)
  923. {
  924. if (args[i].ToLower() == "-mixedreality")
  925. return true;
  926. }
  927. return false;
  928. }
  929. private static bool UseDirectCompositionFromCmd()
  930. {
  931. var args = System.Environment.GetCommandLineArgs();
  932. for (int i = 0; i < args.Length; i++)
  933. {
  934. if (args[i].ToLower() == "-directcomposition")
  935. return true;
  936. }
  937. return false;
  938. }
  939. private static bool UseExternalCompositionFromCmd()
  940. {
  941. var args = System.Environment.GetCommandLineArgs();
  942. for (int i = 0; i < args.Length; i++)
  943. {
  944. if (args[i].ToLower() == "-externalcomposition")
  945. return true;
  946. }
  947. return false;
  948. }
  949. private static bool CreateMixedRealityCaptureConfigurationFileFromCmd()
  950. {
  951. var args = System.Environment.GetCommandLineArgs();
  952. for (int i = 0; i < args.Length; i++)
  953. {
  954. if (args[i].ToLower() == "-create_mrc_config")
  955. return true;
  956. }
  957. return false;
  958. }
  959. private static bool LoadMixedRealityCaptureConfigurationFileFromCmd()
  960. {
  961. var args = System.Environment.GetCommandLineArgs();
  962. for (int i = 0; i < args.Length; i++)
  963. {
  964. if (args[i].ToLower() == "-load_mrc_config")
  965. return true;
  966. }
  967. return false;
  968. }
  969. #endif
  970. public static bool IsUnityAlphaOrBetaVersion()
  971. {
  972. string ver = Application.unityVersion;
  973. int pos = ver.Length - 1;
  974. while (pos >= 0 && ver[pos] >= '0' && ver[pos] <= '9')
  975. {
  976. --pos;
  977. }
  978. if (pos >= 0 && (ver[pos] == 'a' || ver[pos] == 'b'))
  979. return true;
  980. return false;
  981. }
  982. public static string UnityAlphaOrBetaVersionWarningMessage = "WARNING: It's not recommended to use Unity alpha/beta release in Oculus development. Use a stable release if you encounter any issue.";
  983. #region Unity Messages
  984. public static bool OVRManagerinitialized = false;
  985. private void InitOVRManager()
  986. {
  987. // Only allow one instance at runtime.
  988. if (instance != null)
  989. {
  990. enabled = false;
  991. DestroyImmediate(this);
  992. return;
  993. }
  994. instance = this;
  995. // uncomment the following line to disable the callstack printed to log
  996. //Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); // TEMPORARY
  997. Debug.Log("Unity v" + Application.unityVersion + ", " +
  998. "Oculus Utilities v" + OVRPlugin.wrapperVersion + ", " +
  999. "OVRPlugin v" + OVRPlugin.version + ", " +
  1000. "SDK v" + OVRPlugin.nativeSDKVersion + ".");
  1001. #if !UNITY_EDITOR
  1002. if (IsUnityAlphaOrBetaVersion())
  1003. {
  1004. Debug.LogWarning(UnityAlphaOrBetaVersionWarningMessage);
  1005. }
  1006. #endif
  1007. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  1008. var supportedTypes =
  1009. UnityEngine.Rendering.GraphicsDeviceType.Direct3D11.ToString() + ", " +
  1010. UnityEngine.Rendering.GraphicsDeviceType.Direct3D12.ToString();
  1011. if (!supportedTypes.Contains(SystemInfo.graphicsDeviceType.ToString()))
  1012. Debug.LogWarning("VR rendering requires one of the following device types: (" + supportedTypes + "). Your graphics device: " + SystemInfo.graphicsDeviceType.ToString());
  1013. #endif
  1014. // Detect whether this platform is a supported platform
  1015. RuntimePlatform currPlatform = Application.platform;
  1016. if (currPlatform == RuntimePlatform.Android ||
  1017. // currPlatform == RuntimePlatform.LinuxPlayer ||
  1018. currPlatform == RuntimePlatform.OSXEditor ||
  1019. currPlatform == RuntimePlatform.OSXPlayer ||
  1020. currPlatform == RuntimePlatform.WindowsEditor ||
  1021. currPlatform == RuntimePlatform.WindowsPlayer)
  1022. {
  1023. isSupportedPlatform = true;
  1024. }
  1025. else
  1026. {
  1027. isSupportedPlatform = false;
  1028. }
  1029. if (!isSupportedPlatform)
  1030. {
  1031. Debug.LogWarning("This platform is unsupported");
  1032. return;
  1033. }
  1034. #if UNITY_ANDROID && !UNITY_EDITOR
  1035. // Turn off chromatic aberration by default to save texture bandwidth.
  1036. chromatic = false;
  1037. #endif
  1038. #if (UNITY_STANDALONE_WIN || UNITY_ANDROID) && !UNITY_EDITOR
  1039. enableMixedReality = false; // we should never start the standalone game in MxR mode, unless the command-line parameter is provided
  1040. #endif
  1041. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  1042. if (!staticMixedRealityCaptureInitialized)
  1043. {
  1044. bool loadMrcConfig = LoadMixedRealityCaptureConfigurationFileFromCmd();
  1045. bool createMrcConfig = CreateMixedRealityCaptureConfigurationFileFromCmd();
  1046. if (loadMrcConfig || createMrcConfig)
  1047. {
  1048. OVRMixedRealityCaptureSettings mrcSettings = ScriptableObject.CreateInstance<OVRMixedRealityCaptureSettings>();
  1049. mrcSettings.ReadFrom(this);
  1050. if (loadMrcConfig)
  1051. {
  1052. mrcSettings.CombineWithConfigurationFile();
  1053. mrcSettings.ApplyTo(this);
  1054. }
  1055. if (createMrcConfig)
  1056. {
  1057. mrcSettings.WriteToConfigurationFile();
  1058. }
  1059. ScriptableObject.Destroy(mrcSettings);
  1060. }
  1061. if (MixedRealityEnabledFromCmd())
  1062. {
  1063. enableMixedReality = true;
  1064. }
  1065. if (enableMixedReality)
  1066. {
  1067. Debug.Log("OVR: Mixed Reality mode enabled");
  1068. if (UseDirectCompositionFromCmd())
  1069. {
  1070. compositionMethod = CompositionMethod.Direct;
  1071. }
  1072. if (UseExternalCompositionFromCmd())
  1073. {
  1074. compositionMethod = CompositionMethod.External;
  1075. }
  1076. Debug.Log("OVR: CompositionMethod : " + compositionMethod);
  1077. }
  1078. }
  1079. #endif
  1080. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  1081. StaticInitializeMixedRealityCapture(this);
  1082. #endif
  1083. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  1084. if (enableAdaptiveResolution && !OVRManager.IsAdaptiveResSupportedByEngine())
  1085. {
  1086. enableAdaptiveResolution = false;
  1087. UnityEngine.Debug.LogError("Your current Unity Engine " + Application.unityVersion + " might have issues to support adaptive resolution, please disable it under OVRManager");
  1088. }
  1089. #endif
  1090. Initialize();
  1091. if (resetTrackerOnLoad)
  1092. display.RecenterPose();
  1093. if (Debug.isDebugBuild)
  1094. {
  1095. // Activate system metrics collection in Debug/Developerment build
  1096. if (GetComponent<OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer>() == null)
  1097. {
  1098. gameObject.AddComponent<OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer>();
  1099. }
  1100. OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer perfTcpServer = GetComponent<OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer>();
  1101. perfTcpServer.listeningPort = profilerTcpPort;
  1102. if (!perfTcpServer.enabled)
  1103. {
  1104. perfTcpServer.enabled = true;
  1105. }
  1106. OVRPlugin.SetDeveloperMode(OVRPlugin.Bool.True);
  1107. }
  1108. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  1109. // Force OcculusionMesh on all the time, you can change the value to false if you really need it be off for some reasons,
  1110. // be aware there are performance drops if you don't use occlusionMesh.
  1111. OVRPlugin.occlusionMesh = true;
  1112. #endif
  1113. OVRManagerinitialized = true;
  1114. }
  1115. private void Awake()
  1116. {
  1117. #if !USING_XR_SDK
  1118. //For legacy, we should initialize OVRManager in all cases.
  1119. //For now, in XR SDK, only initialize if OVRPlugin is initialized.
  1120. InitOVRManager();
  1121. #else
  1122. if (OVRPlugin.initialized)
  1123. InitOVRManager();
  1124. #endif
  1125. }
  1126. #if UNITY_EDITOR
  1127. private static bool _scriptsReloaded;
  1128. [UnityEditor.Callbacks.DidReloadScripts]
  1129. static void ScriptsReloaded()
  1130. {
  1131. _scriptsReloaded = true;
  1132. }
  1133. #endif
  1134. void SetCurrentXRDevice()
  1135. {
  1136. #if USING_XR_SDK
  1137. XRDisplaySubsystem currentDisplaySubsystem = GetCurrentDisplaySubsystem();
  1138. XRDisplaySubsystemDescriptor currentDisplaySubsystemDescriptor = GetCurrentDisplaySubsystemDescriptor();
  1139. #endif
  1140. if (OVRPlugin.initialized)
  1141. {
  1142. loadedXRDevice = XRDevice.Oculus;
  1143. }
  1144. #if USING_XR_SDK
  1145. else if (currentDisplaySubsystem != null && currentDisplaySubsystemDescriptor != null && currentDisplaySubsystem.running)
  1146. #else
  1147. else if (Settings.enabled)
  1148. #endif
  1149. {
  1150. #if USING_XR_SDK
  1151. string loadedXRDeviceName = currentDisplaySubsystemDescriptor.id;
  1152. #else
  1153. string loadedXRDeviceName = Settings.loadedDeviceName;
  1154. #endif
  1155. if (loadedXRDeviceName == OPENVR_UNITY_NAME_STR)
  1156. loadedXRDevice = XRDevice.OpenVR;
  1157. else
  1158. loadedXRDevice = XRDevice.Unknown;
  1159. }
  1160. else
  1161. {
  1162. loadedXRDevice = XRDevice.Unknown;
  1163. }
  1164. }
  1165. #if USING_XR_SDK
  1166. public static XRDisplaySubsystem GetCurrentDisplaySubsystem()
  1167. {
  1168. List<XRDisplaySubsystem> displaySubsystems = new List<XRDisplaySubsystem>();
  1169. SubsystemManager.GetInstances(displaySubsystems);
  1170. //Note: Here we are making the assumption that there will always be one valid display subsystem. If there is not, then submitFrame isn't being called,
  1171. //so for now this is a fine assumption to make.
  1172. if (displaySubsystems.Count > 0)
  1173. return displaySubsystems[0];
  1174. return null;
  1175. }
  1176. public static XRDisplaySubsystemDescriptor GetCurrentDisplaySubsystemDescriptor()
  1177. {
  1178. List<XRDisplaySubsystemDescriptor> displaySubsystemDescriptors = new List<XRDisplaySubsystemDescriptor>();
  1179. SubsystemManager.GetSubsystemDescriptors(displaySubsystemDescriptors);
  1180. if (displaySubsystemDescriptors.Count > 0)
  1181. return displaySubsystemDescriptors[0];
  1182. return null;
  1183. }
  1184. #endif
  1185. void Initialize()
  1186. {
  1187. if (display == null)
  1188. display = new OVRDisplay();
  1189. if (tracker == null)
  1190. tracker = new OVRTracker();
  1191. if (boundary == null)
  1192. boundary = new OVRBoundary();
  1193. reorientHMDOnControllerRecenter = _reorientHMDOnControllerRecenter;
  1194. SetCurrentXRDevice();
  1195. }
  1196. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  1197. private bool suppressDisableMixedRealityBecauseOfNoMainCameraWarning = false;
  1198. #endif
  1199. private void Update()
  1200. {
  1201. //Only if we're using the XR SDK do we have to check if OVRManager isn't yet initialized, and init it.
  1202. //If we're on legacy, we know initialization occurred properly in Awake()
  1203. #if USING_XR_SDK
  1204. if (!OVRManagerinitialized)
  1205. {
  1206. XRDisplaySubsystem currentDisplaySubsystem = GetCurrentDisplaySubsystem();
  1207. XRDisplaySubsystemDescriptor currentDisplaySubsystemDescriptor = GetCurrentDisplaySubsystemDescriptor();
  1208. if (currentDisplaySubsystem == null || currentDisplaySubsystemDescriptor == null || !OVRPlugin.initialized)
  1209. return;
  1210. //If we're using the XR SDK and the display subsystem is present, and OVRPlugin is initialized, we can init OVRManager
  1211. InitOVRManager();
  1212. }
  1213. #endif
  1214. #if UNITY_EDITOR
  1215. if (_scriptsReloaded)
  1216. {
  1217. _scriptsReloaded = false;
  1218. instance = this;
  1219. Initialize();
  1220. }
  1221. #endif
  1222. SetCurrentXRDevice();
  1223. if (OVRPlugin.shouldQuit)
  1224. {
  1225. Debug.Log("[OVRManager] OVRPlugin.shouldQuit detected");
  1226. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  1227. StaticShutdownMixedRealityCapture(instance);
  1228. #endif
  1229. Application.Quit();
  1230. }
  1231. if (AllowRecenter && OVRPlugin.shouldRecenter)
  1232. {
  1233. OVRManager.display.RecenterPose();
  1234. }
  1235. if (trackingOriginType != _trackingOriginType)
  1236. trackingOriginType = _trackingOriginType;
  1237. tracker.isEnabled = usePositionTracking;
  1238. OVRPlugin.rotation = useRotationTracking;
  1239. OVRPlugin.useIPDInPositionTracking = useIPDInPositionTracking;
  1240. // Dispatch HMD events.
  1241. isHmdPresent = OVRNodeStateProperties.IsHmdPresent();
  1242. if (useRecommendedMSAALevel && QualitySettings.antiAliasing != display.recommendedMSAALevel)
  1243. {
  1244. Debug.Log("The current MSAA level is " + QualitySettings.antiAliasing +
  1245. ", but the recommended MSAA level is " + display.recommendedMSAALevel +
  1246. ". Switching to the recommended level.");
  1247. QualitySettings.antiAliasing = display.recommendedMSAALevel;
  1248. }
  1249. if (monoscopic != _monoscopic)
  1250. {
  1251. monoscopic = _monoscopic;
  1252. }
  1253. if (headPoseRelativeOffsetRotation != _headPoseRelativeOffsetRotation)
  1254. {
  1255. headPoseRelativeOffsetRotation = _headPoseRelativeOffsetRotation;
  1256. }
  1257. if (headPoseRelativeOffsetTranslation != _headPoseRelativeOffsetTranslation)
  1258. {
  1259. headPoseRelativeOffsetTranslation = _headPoseRelativeOffsetTranslation;
  1260. }
  1261. if (_wasHmdPresent && !isHmdPresent)
  1262. {
  1263. try
  1264. {
  1265. Debug.Log("[OVRManager] HMDLost event");
  1266. if (HMDLost != null)
  1267. HMDLost();
  1268. }
  1269. catch (Exception e)
  1270. {
  1271. Debug.LogError("Caught Exception: " + e);
  1272. }
  1273. }
  1274. if (!_wasHmdPresent && isHmdPresent)
  1275. {
  1276. try
  1277. {
  1278. Debug.Log("[OVRManager] HMDAcquired event");
  1279. if (HMDAcquired != null)
  1280. HMDAcquired();
  1281. }
  1282. catch (Exception e)
  1283. {
  1284. Debug.LogError("Caught Exception: " + e);
  1285. }
  1286. }
  1287. _wasHmdPresent = isHmdPresent;
  1288. // Dispatch HMD mounted events.
  1289. isUserPresent = OVRPlugin.userPresent;
  1290. if (_wasUserPresent && !isUserPresent)
  1291. {
  1292. try
  1293. {
  1294. Debug.Log("[OVRManager] HMDUnmounted event");
  1295. if (HMDUnmounted != null)
  1296. HMDUnmounted();
  1297. }
  1298. catch (Exception e)
  1299. {
  1300. Debug.LogError("Caught Exception: " + e);
  1301. }
  1302. }
  1303. if (!_wasUserPresent && isUserPresent)
  1304. {
  1305. try
  1306. {
  1307. Debug.Log("[OVRManager] HMDMounted event");
  1308. if (HMDMounted != null)
  1309. HMDMounted();
  1310. }
  1311. catch (Exception e)
  1312. {
  1313. Debug.LogError("Caught Exception: " + e);
  1314. }
  1315. }
  1316. _wasUserPresent = isUserPresent;
  1317. // Dispatch VR Focus events.
  1318. hasVrFocus = OVRPlugin.hasVrFocus;
  1319. if (_hadVrFocus && !hasVrFocus)
  1320. {
  1321. try
  1322. {
  1323. Debug.Log("[OVRManager] VrFocusLost event");
  1324. if (VrFocusLost != null)
  1325. VrFocusLost();
  1326. }
  1327. catch (Exception e)
  1328. {
  1329. Debug.LogError("Caught Exception: " + e);
  1330. }
  1331. }
  1332. if (!_hadVrFocus && hasVrFocus)
  1333. {
  1334. try
  1335. {
  1336. Debug.Log("[OVRManager] VrFocusAcquired event");
  1337. if (VrFocusAcquired != null)
  1338. VrFocusAcquired();
  1339. }
  1340. catch (Exception e)
  1341. {
  1342. Debug.LogError("Caught Exception: " + e);
  1343. }
  1344. }
  1345. _hadVrFocus = hasVrFocus;
  1346. // Dispatch VR Input events.
  1347. bool hasInputFocus = OVRPlugin.hasInputFocus;
  1348. if (_hadInputFocus && !hasInputFocus)
  1349. {
  1350. try
  1351. {
  1352. Debug.Log("[OVRManager] InputFocusLost event");
  1353. if (InputFocusLost != null)
  1354. InputFocusLost();
  1355. }
  1356. catch (Exception e)
  1357. {
  1358. Debug.LogError("Caught Exception: " + e);
  1359. }
  1360. }
  1361. if (!_hadInputFocus && hasInputFocus)
  1362. {
  1363. try
  1364. {
  1365. Debug.Log("[OVRManager] InputFocusAcquired event");
  1366. if (InputFocusAcquired != null)
  1367. InputFocusAcquired();
  1368. }
  1369. catch (Exception e)
  1370. {
  1371. Debug.LogError("Caught Exception: " + e);
  1372. }
  1373. }
  1374. _hadInputFocus = hasInputFocus;
  1375. // Changing effective rendering resolution dynamically according performance
  1376. #if (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN)
  1377. if (enableAdaptiveResolution)
  1378. {
  1379. #if UNITY_2017_2_OR_NEWER
  1380. if (Settings.eyeTextureResolutionScale < maxRenderScale)
  1381. {
  1382. // Allocate renderScale to max to avoid re-allocation
  1383. Settings.eyeTextureResolutionScale = maxRenderScale;
  1384. }
  1385. else
  1386. {
  1387. // Adjusting maxRenderScale in case app started with a larger renderScale value
  1388. maxRenderScale = Mathf.Max(maxRenderScale, Settings.eyeTextureResolutionScale);
  1389. }
  1390. minRenderScale = Mathf.Min(minRenderScale, maxRenderScale);
  1391. float minViewportScale = minRenderScale / Settings.eyeTextureResolutionScale;
  1392. float recommendedViewportScale = Mathf.Clamp(Mathf.Sqrt(OVRPlugin.GetAdaptiveGPUPerformanceScale()) * Settings.eyeTextureResolutionScale * Settings.renderViewportScale, 0.5f, 2.0f);
  1393. recommendedViewportScale /= Settings.eyeTextureResolutionScale;
  1394. recommendedViewportScale = Mathf.Clamp(recommendedViewportScale, minViewportScale, 1.0f);
  1395. Settings.renderViewportScale = recommendedViewportScale;
  1396. #else
  1397. if (UnityEngine.VR.VRSettings.renderScale < maxRenderScale)
  1398. {
  1399. // Allocate renderScale to max to avoid re-allocation
  1400. UnityEngine.VR.VRSettings.renderScale = maxRenderScale;
  1401. }
  1402. else
  1403. {
  1404. // Adjusting maxRenderScale in case app started with a larger renderScale value
  1405. maxRenderScale = Mathf.Max(maxRenderScale, UnityEngine.VR.VRSettings.renderScale);
  1406. }
  1407. minRenderScale = Mathf.Min(minRenderScale, maxRenderScale);
  1408. float minViewportScale = minRenderScale / UnityEngine.VR.VRSettings.renderScale;
  1409. float recommendedViewportScale = OVRPlugin.GetEyeRecommendedResolutionScale() / UnityEngine.VR.VRSettings.renderScale;
  1410. recommendedViewportScale = Mathf.Clamp(recommendedViewportScale, minViewportScale, 1.0f);
  1411. UnityEngine.VR.VRSettings.renderViewportScale = recommendedViewportScale;
  1412. #endif
  1413. }
  1414. #endif
  1415. // Dispatch Audio Device events.
  1416. string audioOutId = OVRPlugin.audioOutId;
  1417. if (!prevAudioOutIdIsCached)
  1418. {
  1419. prevAudioOutId = audioOutId;
  1420. prevAudioOutIdIsCached = true;
  1421. }
  1422. else if (audioOutId != prevAudioOutId)
  1423. {
  1424. try
  1425. {
  1426. Debug.Log("[OVRManager] AudioOutChanged event");
  1427. if (AudioOutChanged != null)
  1428. AudioOutChanged();
  1429. }
  1430. catch (Exception e)
  1431. {
  1432. Debug.LogError("Caught Exception: " + e);
  1433. }
  1434. prevAudioOutId = audioOutId;
  1435. }
  1436. string audioInId = OVRPlugin.audioInId;
  1437. if (!prevAudioInIdIsCached)
  1438. {
  1439. prevAudioInId = audioInId;
  1440. prevAudioInIdIsCached = true;
  1441. }
  1442. else if (audioInId != prevAudioInId)
  1443. {
  1444. try
  1445. {
  1446. Debug.Log("[OVRManager] AudioInChanged event");
  1447. if (AudioInChanged != null)
  1448. AudioInChanged();
  1449. }
  1450. catch (Exception e)
  1451. {
  1452. Debug.LogError("Caught Exception: " + e);
  1453. }
  1454. prevAudioInId = audioInId;
  1455. }
  1456. // Dispatch tracking events.
  1457. if (wasPositionTracked && !tracker.isPositionTracked)
  1458. {
  1459. try
  1460. {
  1461. Debug.Log("[OVRManager] TrackingLost event");
  1462. if (TrackingLost != null)
  1463. TrackingLost();
  1464. }
  1465. catch (Exception e)
  1466. {
  1467. Debug.LogError("Caught Exception: " + e);
  1468. }
  1469. }
  1470. if (!wasPositionTracked && tracker.isPositionTracked)
  1471. {
  1472. try
  1473. {
  1474. Debug.Log("[OVRManager] TrackingAcquired event");
  1475. if (TrackingAcquired != null)
  1476. TrackingAcquired();
  1477. }
  1478. catch (Exception e)
  1479. {
  1480. Debug.LogError("Caught Exception: " + e);
  1481. }
  1482. }
  1483. wasPositionTracked = tracker.isPositionTracked;
  1484. display.Update();
  1485. OVRInput.Update();
  1486. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  1487. StaticUpdateMixedRealityCapture(this);
  1488. #endif
  1489. }
  1490. private bool multipleMainCameraWarningPresented = false;
  1491. private Camera lastFoundMainCamera = null;
  1492. private Camera FindMainCamera()
  1493. {
  1494. if (lastFoundMainCamera != null && lastFoundMainCamera.CompareTag("MainCamera"))
  1495. {
  1496. return lastFoundMainCamera;
  1497. }
  1498. Camera result = null;
  1499. GameObject[] objects = GameObject.FindGameObjectsWithTag("MainCamera");
  1500. List<Camera> cameras = new List<Camera>(4);
  1501. foreach (GameObject obj in objects)
  1502. {
  1503. Camera camera = obj.GetComponent<Camera>();
  1504. if (camera != null && camera.enabled)
  1505. {
  1506. OVRCameraRig cameraRig = camera.GetComponentInParent<OVRCameraRig>();
  1507. if (cameraRig != null && cameraRig.trackingSpace != null)
  1508. {
  1509. cameras.Add(camera);
  1510. }
  1511. }
  1512. }
  1513. if (cameras.Count == 0)
  1514. {
  1515. result = Camera.main; // pick one of the cameras which tagged as "MainCamera"
  1516. }
  1517. else if (cameras.Count == 1)
  1518. {
  1519. result = cameras[0];
  1520. }
  1521. else
  1522. {
  1523. if (!multipleMainCameraWarningPresented)
  1524. {
  1525. Debug.LogWarning("Multiple MainCamera found. Assume the real MainCamera is the camera with the least depth");
  1526. multipleMainCameraWarningPresented = true;
  1527. }
  1528. // return the camera with least depth
  1529. cameras.Sort((Camera c0, Camera c1) => { return c0.depth < c1.depth ? -1 : (c0.depth > c1.depth ? 1 : 0); });
  1530. result = cameras[0];
  1531. }
  1532. if (result != null)
  1533. {
  1534. Debug.LogFormat("[OVRManager] mainCamera found for MRC: ", result.gameObject.name);
  1535. }
  1536. else
  1537. {
  1538. Debug.Log("[OVRManager] unable to find a vaild camera");
  1539. }
  1540. lastFoundMainCamera = result;
  1541. return result;
  1542. }
  1543. private void OnDisable()
  1544. {
  1545. OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer perfTcpServer = GetComponent<OVRSystemPerfMetrics.OVRSystemPerfMetricsTcpServer>();
  1546. if (perfTcpServer != null)
  1547. {
  1548. perfTcpServer.enabled = false;
  1549. }
  1550. }
  1551. private void LateUpdate()
  1552. {
  1553. OVRHaptics.Process();
  1554. }
  1555. private void FixedUpdate()
  1556. {
  1557. OVRInput.FixedUpdate();
  1558. }
  1559. private void OnDestroy()
  1560. {
  1561. Debug.Log("[OVRManager] OnDestroy");
  1562. OVRManagerinitialized = false;
  1563. }
  1564. private void OnApplicationPause(bool pause)
  1565. {
  1566. if (pause)
  1567. {
  1568. Debug.Log("[OVRManager] OnApplicationPause(true)");
  1569. }
  1570. else
  1571. {
  1572. Debug.Log("[OVRManager] OnApplicationPause(false)");
  1573. }
  1574. }
  1575. private void OnApplicationFocus(bool focus)
  1576. {
  1577. if (focus)
  1578. {
  1579. Debug.Log("[OVRManager] OnApplicationFocus(true)");
  1580. }
  1581. else
  1582. {
  1583. Debug.Log("[OVRManager] OnApplicationFocus(false)");
  1584. }
  1585. }
  1586. private void OnApplicationQuit()
  1587. {
  1588. Debug.Log("[OVRManager] OnApplicationQuit");
  1589. }
  1590. #endregion // Unity Messages
  1591. /// <summary>
  1592. /// Leaves the application/game and returns to the launcher/dashboard
  1593. /// </summary>
  1594. public void ReturnToLauncher()
  1595. {
  1596. // show the platform UI quit prompt
  1597. OVRManager.PlatformUIConfirmQuit();
  1598. }
  1599. public static void PlatformUIConfirmQuit()
  1600. {
  1601. if (!isHmdPresent)
  1602. return;
  1603. OVRPlugin.ShowUI(OVRPlugin.PlatformUI.ConfirmQuit);
  1604. }
  1605. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  1606. public static bool staticMixedRealityCaptureInitialized = false;
  1607. public static bool staticPrevEnableMixedRealityCapture = false;
  1608. public static OVRMixedRealityCaptureSettings staticMrcSettings = null;
  1609. public static void StaticInitializeMixedRealityCapture(OVRManager instance)
  1610. {
  1611. if (!staticMixedRealityCaptureInitialized)
  1612. {
  1613. staticMrcSettings = ScriptableObject.CreateInstance<OVRMixedRealityCaptureSettings>();
  1614. staticMrcSettings.ReadFrom(OVRManager.instance);
  1615. #if OVR_ANDROID_MRC
  1616. bool mediaInitialized = OVRPlugin.Media.Initialize();
  1617. Debug.Log(mediaInitialized ? "OVRPlugin.Media initialized" : "OVRPlugin.Media not initialized");
  1618. if (mediaInitialized)
  1619. {
  1620. OVRPlugin.Media.SetMrcAudioSampleRate(AudioSettings.outputSampleRate);
  1621. Debug.LogFormat("[MRC] SetMrcAudioSampleRate({0})", AudioSettings.outputSampleRate);
  1622. OVRPlugin.Media.SetMrcInputVideoBufferType(OVRPlugin.Media.InputVideoBufferType.TextureHandle);
  1623. Debug.LogFormat("[MRC] Active InputVideoBufferType:{0}", OVRPlugin.Media.GetMrcInputVideoBufferType());
  1624. if (instance.mrcActivationMode == MrcActivationMode.Automatic)
  1625. {
  1626. OVRPlugin.Media.SetMrcActivationMode(OVRPlugin.Media.MrcActivationMode.Automatic);
  1627. Debug.LogFormat("[MRC] ActivateMode: Automatic");
  1628. }
  1629. else if (instance.mrcActivationMode == MrcActivationMode.Disabled)
  1630. {
  1631. OVRPlugin.Media.SetMrcActivationMode(OVRPlugin.Media.MrcActivationMode.Disabled);
  1632. Debug.LogFormat("[MRC] ActivateMode: Disabled");
  1633. }
  1634. }
  1635. #endif
  1636. staticPrevEnableMixedRealityCapture = false;
  1637. staticMixedRealityCaptureInitialized = true;
  1638. }
  1639. else
  1640. {
  1641. staticMrcSettings.ApplyTo(instance);
  1642. }
  1643. }
  1644. public static void StaticUpdateMixedRealityCapture(OVRManager instance)
  1645. {
  1646. if (!staticMixedRealityCaptureInitialized)
  1647. {
  1648. return;
  1649. }
  1650. #if OVR_ANDROID_MRC
  1651. instance.enableMixedReality = OVRPlugin.Media.GetInitialized() && OVRPlugin.Media.IsMrcActivated();
  1652. instance.compositionMethod = CompositionMethod.External; // force external composition on Android MRC
  1653. if (OVRPlugin.Media.GetInitialized())
  1654. {
  1655. OVRPlugin.Media.Update();
  1656. }
  1657. #endif
  1658. if (instance.enableMixedReality && !staticPrevEnableMixedRealityCapture)
  1659. {
  1660. OVRPlugin.SendEvent("mixed_reality_capture", "activated");
  1661. Debug.Log("MixedRealityCapture: activate");
  1662. }
  1663. if (!instance.enableMixedReality && staticPrevEnableMixedRealityCapture)
  1664. {
  1665. Debug.Log("MixedRealityCapture: deactivate");
  1666. }
  1667. if (instance.enableMixedReality || staticPrevEnableMixedRealityCapture)
  1668. {
  1669. Camera mainCamera = instance.FindMainCamera();
  1670. if (Camera.main != null)
  1671. {
  1672. instance.suppressDisableMixedRealityBecauseOfNoMainCameraWarning = false;
  1673. if (instance.enableMixedReality)
  1674. {
  1675. OVRMixedReality.Update(instance.gameObject, mainCamera, instance.compositionMethod, instance.useDynamicLighting, instance.capturingCameraDevice, instance.depthQuality);
  1676. }
  1677. if (staticPrevEnableMixedRealityCapture && !instance.enableMixedReality)
  1678. {
  1679. OVRMixedReality.Cleanup();
  1680. }
  1681. staticPrevEnableMixedRealityCapture = instance.enableMixedReality;
  1682. }
  1683. else
  1684. {
  1685. if (!instance.suppressDisableMixedRealityBecauseOfNoMainCameraWarning)
  1686. {
  1687. Debug.LogWarning("Main Camera is not set, Mixed Reality disabled");
  1688. instance.suppressDisableMixedRealityBecauseOfNoMainCameraWarning = true;
  1689. }
  1690. }
  1691. }
  1692. staticMrcSettings.ReadFrom(OVRManager.instance);
  1693. }
  1694. public static void StaticShutdownMixedRealityCapture(OVRManager instance)
  1695. {
  1696. if (staticMixedRealityCaptureInitialized)
  1697. {
  1698. ScriptableObject.Destroy(staticMrcSettings);
  1699. staticMrcSettings = null;
  1700. OVRMixedReality.Cleanup();
  1701. #if OVR_ANDROID_MRC
  1702. if (OVRPlugin.Media.GetInitialized())
  1703. {
  1704. OVRPlugin.Media.Shutdown();
  1705. }
  1706. #endif
  1707. staticMixedRealityCaptureInitialized = false;
  1708. }
  1709. }
  1710. #endif
  1711. }