OvrMgr.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. using System.Collections;
  3. using Leap.Unity;
  4. using UnityEngine;
  5. public class OvrMgr : MonoBehaviour
  6. {
  7. public OvrCamera OvrCamera { get; private set; }
  8. public Transform EyeAnchor
  9. {
  10. get
  11. {
  12. return this.m_trEyeAnchor;
  13. }
  14. }
  15. public RenderTexture UIRTarget
  16. {
  17. get
  18. {
  19. return this.m_rtRTarget;
  20. }
  21. }
  22. public OvrMgr.OvrObject ovr_obj
  23. {
  24. get
  25. {
  26. return this.ovr_obj_;
  27. }
  28. }
  29. public Transform GetVRControllerTransform(bool f_bLeft)
  30. {
  31. return (!f_bLeft) ? this.ovr_obj_.right_controller.hand_trans : this.ovr_obj_.left_controller.hand_trans;
  32. }
  33. public AVRControllerButtons GetVRControllerButtons(bool f_bLeft)
  34. {
  35. return (!f_bLeft) ? this.ovr_obj_.right_controller.controller_buttons : this.ovr_obj_.left_controller.controller_buttons;
  36. }
  37. public UICamera SystemUICamera
  38. {
  39. get
  40. {
  41. return this.m_SysUiCam;
  42. }
  43. }
  44. public AVRTrackingObjectsMgr TrackingObjectsMgr
  45. {
  46. get
  47. {
  48. return this.m_TrackingObjectsMgr;
  49. }
  50. }
  51. public void Init()
  52. {
  53. this.ovr_obj_ = new OvrMgr.OvrObject();
  54. GameMain.Instance.MainCamera.GetComponent<AudioListener>().enabled = false;
  55. GameMain.Instance.MainCamera.enabled = false;
  56. GameMain.Instance.MainCamera.GetComponent<Camera>().enabled = false;
  57. GameMain.Instance.MainCamera.GetComponent<CameraMain>().enabled = false;
  58. GameMain.Instance.MainCamera.GetComponent<UltimateOrbitCamera>().enabled = false;
  59. GameMain.Instance.MainCamera.GetComponent<Bloom>().enabled = false;
  60. GameMain.Instance.MainCamera.GetComponent<ScreenOverlay>().enabled = false;
  61. GameMain.Instance.MainCamera.GetComponent<FadeInAndFadeOutOnGUI>().enabled = false;
  62. if (this.m_rtRTarget == null)
  63. {
  64. this.m_rtRTarget = Resources.Load<RenderTexture>("OVR/rtOVRUI");
  65. }
  66. Transform transform = base.gameObject.transform.Find("SystemUI Root");
  67. this.m_SysUiCam = transform.Find("Camera").GetComponent<UICamera>();
  68. this.m_SysUiCam.CreateVirtualCursorObj();
  69. Transform f_trParent = null;
  70. if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.Oculus)
  71. {
  72. this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
  73. this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
  74. this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("OVR/OVRCameraRig")) as GameObject);
  75. this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
  76. this.OvrCamera = this.m_goOvrRig.GetComponent<OvrCamera>();
  77. this.ovr_obj_.right_controller.grip_collider.pair_object = this.ovr_obj_.left_controller.grip_collider;
  78. this.ovr_obj_.left_controller.grip_collider.pair_object = this.ovr_obj_.right_controller.grip_collider;
  79. this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
  80. f_trParent = this.m_trEyeAnchor;
  81. }
  82. else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.HTC)
  83. {
  84. this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
  85. this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
  86. GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("SteamVR/[SteamVR]")) as GameObject;
  87. gameObject.transform.parent = GameMain.Instance.gameObject.transform;
  88. GameObject gameObject2 = UnityEngine.Object.Instantiate(Resources.Load("SteamVR/[Status]")) as GameObject;
  89. gameObject2.transform.parent = GameMain.Instance.gameObject.transform;
  90. this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("SteamVR/ViveCameraRig")) as GameObject);
  91. this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
  92. this.OvrCamera = this.m_goOvrRig.GetComponent<ViveCamera>();
  93. this.ovr_obj_.right_controller.grip_collider.pair_object = this.ovr_obj_.left_controller.grip_collider;
  94. this.ovr_obj_.left_controller.grip_collider.pair_object = this.ovr_obj_.right_controller.grip_collider;
  95. this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
  96. f_trParent = this.m_goOvrRig.transform.Find("Main Camera (head)");
  97. this.m_TrackingObjectsMgr = this.m_goOvrRig.GetComponentInChildren<ViveBaseStationHide>();
  98. }
  99. else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.FOVE)
  100. {
  101. this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
  102. this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
  103. this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("Fove/Fove Rig")) as GameObject);
  104. this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
  105. this.OvrCamera = this.m_goOvrRig.GetComponent<FoveCamera>();
  106. this.m_trEyeAnchor = this.m_goOvrRig.transform.Find("Fove Interface");
  107. f_trParent = this.m_trEyeAnchor;
  108. }
  109. else if (GameMain.Instance.VRFamily == GameMain.VRFamilyType.NON)
  110. {
  111. this.ovr_obj_.right_controller = new OvrMgr.OvrObject.Controller();
  112. this.ovr_obj_.left_controller = new OvrMgr.OvrObject.Controller();
  113. this.m_goOvrRig = (UnityEngine.Object.Instantiate(Resources.Load("DummyVR/DummyVRCameraRig")) as GameObject);
  114. this.m_goOvrRig.transform.parent = GameMain.Instance.gameObject.transform;
  115. this.OvrCamera = this.m_goOvrRig.GetComponent<DummyVRCamera>();
  116. this.m_trEyeAnchor = this.OvrCamera.m_trCenterEye;
  117. f_trParent = this.m_trEyeAnchor;
  118. this.m_SysUiCam.HideVirtualCursorObj();
  119. }
  120. UICamera[] array = UnityEngine.Object.FindObjectsOfType<UICamera>();
  121. for (int i = 0; i < array.Length; i++)
  122. {
  123. if (!array[i].VRExclusionUI)
  124. {
  125. array[i].GetComponent<Camera>().targetTexture = this.m_rtRTarget;
  126. }
  127. }
  128. GameObject gameObject3 = GameObject.Find("UI Root");
  129. if (gameObject3 != null)
  130. {
  131. UICamera[] componentsInChildren = gameObject3.GetComponentsInChildren<UICamera>(true);
  132. if (componentsInChildren != null)
  133. {
  134. foreach (UICamera uicamera in componentsInChildren)
  135. {
  136. if (uicamera.GetComponent<Camera>() != null)
  137. {
  138. uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Color;
  139. }
  140. }
  141. }
  142. }
  143. UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
  144. for (int k = 0; k < array3.Length; k++)
  145. {
  146. array3[k].scalingStyle = UIRoot.Scaling.Constrained;
  147. array3[k].minimumHeight = 1080;
  148. array3[k].maximumHeight = 1080;
  149. array3[k].fitWidth = false;
  150. array3[k].fitHeight = true;
  151. }
  152. this.m_goAudioMgr = UTY.GetChildObject(GameMain.Instance.gameObject, "Camera/CameraMain/CameraMain/AudioMgr", false);
  153. this.m_goAudioMgr.transform.SetParent(this.m_trEyeAnchor, false);
  154. this.m_coLeapConnectWait = base.StartCoroutine(this.CoLeapLoad(f_trParent));
  155. }
  156. private IEnumerator CoLeapLoad(Transform f_trParent)
  157. {
  158. UnityEngine.Object objLeap = Resources.Load("LeapMotion/LMHeadMountedRig");
  159. this.m_goLeapMotion = (UnityEngine.Object.Instantiate(objLeap) as GameObject);
  160. this.m_goLeapMotion.transform.SetParent(GameMain.Instance.gameObject.transform);
  161. this.m_LeapMgr = this.m_goLeapMotion.GetComponent<LeapMgr>();
  162. this.m_trLeapHandL = this.m_LeapMgr.m_trHandHitL;
  163. this.m_trLeapHandR = this.m_LeapMgr.m_trHandHitR;
  164. LeapServiceProvider rsp = this.m_goLeapMotion.GetComponentInChildren<LeapServiceProvider>(true);
  165. float time = Time.realtimeSinceStartup;
  166. bool bTimeout = false;
  167. Debug.Log("Leap Connect wait1 loop start.");
  168. while (rsp.GetLeapController() == null)
  169. {
  170. yield return null;
  171. if (5f < Time.realtimeSinceStartup - time)
  172. {
  173. Debug.Log("Leap Unconnected timeout");
  174. bTimeout = true;
  175. break;
  176. }
  177. }
  178. if (!bTimeout)
  179. {
  180. time = Time.realtimeSinceStartup;
  181. Debug.Log("Leap Connect wait2 loop start.");
  182. while (!rsp.IsConnected())
  183. {
  184. yield return null;
  185. if (5f < Time.realtimeSinceStartup - time)
  186. {
  187. Debug.Log("Leap Unconnected timeout");
  188. bTimeout = true;
  189. break;
  190. }
  191. }
  192. if (!bTimeout)
  193. {
  194. Debug.Log("Leap Connected !!");
  195. this.m_goLeapMotion.transform.SetParent(f_trParent, false);
  196. this.m_goLeapMotion.transform.localPosition = new Vector3(0f, 0f, 0.1f);
  197. this.m_bEnableLeapMotion = true;
  198. }
  199. }
  200. if (bTimeout)
  201. {
  202. UnityEngine.Object.Destroy(this.m_goLeapMotion);
  203. this.m_goLeapMotion = null;
  204. }
  205. this.m_coLeapConnectWait = null;
  206. yield break;
  207. }
  208. public void Uninit()
  209. {
  210. if (GameMain.Instance == null)
  211. {
  212. return;
  213. }
  214. this.OvrCamera.Uninit();
  215. Transform transform = base.gameObject.transform.Find("SystemUI Root");
  216. if (transform != null)
  217. {
  218. UIRoot component = transform.GetComponent<UIRoot>();
  219. component.scalingStyle = UIRoot.Scaling.Constrained;
  220. component.minimumHeight = 1080;
  221. component.maximumHeight = 1080;
  222. component.fitWidth = true;
  223. component.fitHeight = true;
  224. Transform transform2 = transform.Find("Camera");
  225. UICamera component2 = transform2.GetComponent<UICamera>();
  226. component2.DeleteVirtualCursorObj();
  227. component2.GetComponent<Camera>().targetTexture = null;
  228. }
  229. UICamera[] array = UnityEngine.Object.FindObjectsOfType<UICamera>();
  230. for (int i = 0; i < array.Length; i++)
  231. {
  232. array[i].GetComponent<Camera>().targetTexture = null;
  233. }
  234. GameObject gameObject = GameObject.Find("UI Root");
  235. if (gameObject != null)
  236. {
  237. UICamera[] componentsInChildren = gameObject.GetComponentsInChildren<UICamera>(true);
  238. if (componentsInChildren != null)
  239. {
  240. foreach (UICamera uicamera in componentsInChildren)
  241. {
  242. if (uicamera.GetComponent<Camera>() != null)
  243. {
  244. uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
  245. }
  246. }
  247. }
  248. }
  249. UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
  250. for (int k = 0; k < array3.Length; k++)
  251. {
  252. array3[k].scalingStyle = UIRoot.Scaling.Constrained;
  253. array3[k].minimumHeight = 1080;
  254. array3[k].maximumHeight = 1080;
  255. array3[k].fitWidth = true;
  256. array3[k].fitHeight = true;
  257. }
  258. Debug.Log("OvrMgr Uninit");
  259. if (this.m_goAudioMgr != null)
  260. {
  261. Transform transform3 = GameMain.Instance.transform.Find("Camera/CameraMain/CameraMain");
  262. if (transform3 != null)
  263. {
  264. this.m_goAudioMgr.transform.SetParent(transform3, false);
  265. }
  266. this.m_goAudioMgr = null;
  267. }
  268. if (this.m_coLeapConnectWait != null)
  269. {
  270. base.StopCoroutine(this.m_coLeapConnectWait);
  271. this.m_coLeapConnectWait = null;
  272. }
  273. if (this.m_goLeapMotion != null)
  274. {
  275. UnityEngine.Object.DestroyImmediate(this.m_goLeapMotion);
  276. this.m_goLeapMotion = null;
  277. }
  278. if (this.m_goOvrRig != null)
  279. {
  280. UnityEngine.Object.DestroyImmediate(this.m_goOvrRig);
  281. }
  282. this.OvrCamera = null;
  283. this.m_trEyeAnchor = null;
  284. this.ovr_obj_ = null;
  285. if (GameMain.Instance.MainCamera != null)
  286. {
  287. GameMain.Instance.MainCamera.enabled = true;
  288. GameMain.Instance.MainCamera.GetComponent<Camera>().enabled = true;
  289. GameMain.Instance.MainCamera.GetComponent<CameraMain>().enabled = true;
  290. GameMain.Instance.MainCamera.GetComponent<UltimateOrbitCamera>().enabled = true;
  291. GameMain.Instance.MainCamera.GetComponent<FadeInAndFadeOutOnGUI>().enabled = true;
  292. GameMain.Instance.MainCamera.GetComponent<AudioListener>().enabled = true;
  293. }
  294. }
  295. public void OnLevelWasLoaded(int level)
  296. {
  297. Debug.Log("OvrMgr OnLevelWasLoaded!");
  298. }
  299. private GameObject m_goOvrRig;
  300. private Transform m_trEyeAnchor;
  301. private RenderTexture m_rtRTarget;
  302. public GameObject m_goLeapMotion;
  303. public bool m_bEnableLeapMotion;
  304. public LeapMgr m_LeapMgr;
  305. public Transform m_trLeapHandL;
  306. public Transform m_trLeapHandR;
  307. private AVRTrackingObjectsMgr m_TrackingObjectsMgr;
  308. private GameObject m_goAudioMgr;
  309. private Coroutine m_coLeapConnectWait;
  310. private UICamera m_SysUiCam;
  311. private OvrMgr.OvrObject ovr_obj_;
  312. public class OvrObject
  313. {
  314. public OvrMgr.OvrObject.Controller right_controller;
  315. public OvrMgr.OvrObject.Controller left_controller;
  316. public class Controller
  317. {
  318. public OvrGripCollider grip_collider;
  319. public Transform hand_trans;
  320. public Transform hand_trans_hit;
  321. public AVRController controller;
  322. public AVRControllerButtons controller_buttons;
  323. }
  324. }
  325. }