OvrMgr.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. array[i].GetComponent<Camera>().targetTexture = this.m_rtRTarget;
  124. }
  125. GameObject gameObject3 = GameObject.Find("UI Root");
  126. if (gameObject3 != null)
  127. {
  128. UICamera[] componentsInChildren = gameObject3.GetComponentsInChildren<UICamera>(true);
  129. if (componentsInChildren != null)
  130. {
  131. foreach (UICamera uicamera in componentsInChildren)
  132. {
  133. if (uicamera.GetComponent<Camera>() != null)
  134. {
  135. uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Color;
  136. }
  137. }
  138. }
  139. }
  140. UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
  141. for (int k = 0; k < array3.Length; k++)
  142. {
  143. array3[k].scalingStyle = UIRoot.Scaling.Constrained;
  144. array3[k].minimumHeight = 1080;
  145. array3[k].maximumHeight = 1080;
  146. array3[k].fitWidth = false;
  147. array3[k].fitHeight = true;
  148. }
  149. this.m_goAudioMgr = UTY.GetChildObject(GameMain.Instance.gameObject, "Camera/CameraMain/CameraMain/AudioMgr", false);
  150. this.m_goAudioMgr.transform.SetParent(this.m_trEyeAnchor, false);
  151. this.m_coLeapConnectWait = base.StartCoroutine(this.CoLeapLoad(f_trParent));
  152. }
  153. private IEnumerator CoLeapLoad(Transform f_trParent)
  154. {
  155. UnityEngine.Object objLeap = Resources.Load("LeapMotion/LMHeadMountedRig");
  156. this.m_goLeapMotion = (UnityEngine.Object.Instantiate(objLeap) as GameObject);
  157. this.m_goLeapMotion.transform.SetParent(GameMain.Instance.gameObject.transform);
  158. this.m_LeapMgr = this.m_goLeapMotion.GetComponent<LeapMgr>();
  159. this.m_trLeapHandL = this.m_LeapMgr.m_trHandHitL;
  160. this.m_trLeapHandR = this.m_LeapMgr.m_trHandHitR;
  161. LeapServiceProvider rsp = this.m_goLeapMotion.GetComponentInChildren<LeapServiceProvider>(true);
  162. float time = Time.realtimeSinceStartup;
  163. bool bTimeout = false;
  164. Debug.Log("Leap Connect wait1 loop start.");
  165. while (rsp.GetLeapController() == null)
  166. {
  167. yield return null;
  168. if (5f < Time.realtimeSinceStartup - time)
  169. {
  170. Debug.Log("Leap Unconnected timeout");
  171. bTimeout = true;
  172. break;
  173. }
  174. }
  175. if (!bTimeout)
  176. {
  177. time = Time.realtimeSinceStartup;
  178. Debug.Log("Leap Connect wait2 loop start.");
  179. while (!rsp.IsConnected())
  180. {
  181. yield return null;
  182. if (5f < Time.realtimeSinceStartup - time)
  183. {
  184. Debug.Log("Leap Unconnected timeout");
  185. bTimeout = true;
  186. break;
  187. }
  188. }
  189. if (!bTimeout)
  190. {
  191. Debug.Log("Leap Connected !!");
  192. this.m_goLeapMotion.transform.SetParent(f_trParent, false);
  193. this.m_goLeapMotion.transform.localPosition = new Vector3(0f, 0f, 0.1f);
  194. this.m_bEnableLeapMotion = true;
  195. }
  196. }
  197. if (bTimeout)
  198. {
  199. UnityEngine.Object.Destroy(this.m_goLeapMotion);
  200. this.m_goLeapMotion = null;
  201. }
  202. this.m_coLeapConnectWait = null;
  203. yield break;
  204. }
  205. public void Uninit()
  206. {
  207. if (GameMain.Instance == null)
  208. {
  209. return;
  210. }
  211. this.OvrCamera.Uninit();
  212. Transform transform = base.gameObject.transform.Find("SystemUI Root");
  213. if (transform != null)
  214. {
  215. UIRoot component = transform.GetComponent<UIRoot>();
  216. component.scalingStyle = UIRoot.Scaling.Constrained;
  217. component.minimumHeight = 1080;
  218. component.maximumHeight = 1080;
  219. component.fitWidth = true;
  220. component.fitHeight = true;
  221. Transform transform2 = transform.Find("Camera");
  222. UICamera component2 = transform2.GetComponent<UICamera>();
  223. component2.DeleteVirtualCursorObj();
  224. component2.GetComponent<Camera>().targetTexture = null;
  225. }
  226. UICamera[] array = UnityEngine.Object.FindObjectsOfType<UICamera>();
  227. for (int i = 0; i < array.Length; i++)
  228. {
  229. array[i].GetComponent<Camera>().targetTexture = null;
  230. }
  231. GameObject gameObject = GameObject.Find("UI Root");
  232. if (gameObject != null)
  233. {
  234. UICamera[] componentsInChildren = gameObject.GetComponentsInChildren<UICamera>(true);
  235. if (componentsInChildren != null)
  236. {
  237. foreach (UICamera uicamera in componentsInChildren)
  238. {
  239. if (uicamera.GetComponent<Camera>() != null)
  240. {
  241. uicamera.GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
  242. }
  243. }
  244. }
  245. }
  246. UIRoot[] array3 = UnityEngine.Object.FindObjectsOfType<UIRoot>();
  247. for (int k = 0; k < array3.Length; k++)
  248. {
  249. array3[k].scalingStyle = UIRoot.Scaling.Constrained;
  250. array3[k].minimumHeight = 1080;
  251. array3[k].maximumHeight = 1080;
  252. array3[k].fitWidth = true;
  253. array3[k].fitHeight = true;
  254. }
  255. Debug.Log("OvrMgr Uninit");
  256. if (this.m_goAudioMgr != null)
  257. {
  258. Transform transform3 = GameMain.Instance.transform.Find("Camera/CameraMain/CameraMain");
  259. if (transform3 != null)
  260. {
  261. this.m_goAudioMgr.transform.SetParent(transform3, false);
  262. }
  263. this.m_goAudioMgr = null;
  264. }
  265. if (this.m_coLeapConnectWait != null)
  266. {
  267. base.StopCoroutine(this.m_coLeapConnectWait);
  268. this.m_coLeapConnectWait = null;
  269. }
  270. if (this.m_goLeapMotion != null)
  271. {
  272. UnityEngine.Object.DestroyImmediate(this.m_goLeapMotion);
  273. this.m_goLeapMotion = null;
  274. }
  275. if (this.m_goOvrRig != null)
  276. {
  277. UnityEngine.Object.DestroyImmediate(this.m_goOvrRig);
  278. }
  279. this.OvrCamera = null;
  280. this.m_trEyeAnchor = null;
  281. this.ovr_obj_ = null;
  282. if (GameMain.Instance.MainCamera != null)
  283. {
  284. GameMain.Instance.MainCamera.enabled = true;
  285. GameMain.Instance.MainCamera.GetComponent<Camera>().enabled = true;
  286. GameMain.Instance.MainCamera.GetComponent<CameraMain>().enabled = true;
  287. GameMain.Instance.MainCamera.GetComponent<UltimateOrbitCamera>().enabled = true;
  288. GameMain.Instance.MainCamera.GetComponent<FadeInAndFadeOutOnGUI>().enabled = true;
  289. GameMain.Instance.MainCamera.GetComponent<AudioListener>().enabled = true;
  290. }
  291. }
  292. public void OnLevelWasLoaded(int level)
  293. {
  294. Debug.Log("OvrMgr OnLevelWasLoaded!");
  295. }
  296. private GameObject m_goOvrRig;
  297. private Transform m_trEyeAnchor;
  298. private RenderTexture m_rtRTarget;
  299. public GameObject m_goLeapMotion;
  300. public bool m_bEnableLeapMotion;
  301. public LeapMgr m_LeapMgr;
  302. public Transform m_trLeapHandL;
  303. public Transform m_trLeapHandR;
  304. private AVRTrackingObjectsMgr m_TrackingObjectsMgr;
  305. private GameObject m_goAudioMgr;
  306. private Coroutine m_coLeapConnectWait;
  307. private UICamera m_SysUiCam;
  308. private OvrMgr.OvrObject ovr_obj_;
  309. public class OvrObject
  310. {
  311. public OvrMgr.OvrObject.Controller right_controller;
  312. public OvrMgr.OvrObject.Controller left_controller;
  313. public class Controller
  314. {
  315. public OvrGripCollider grip_collider;
  316. public Transform hand_trans;
  317. public Transform hand_trans_hit;
  318. public AVRController controller;
  319. public AVRControllerButtons controller_buttons;
  320. }
  321. }
  322. }