SteamVR_LoadLevel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using Valve.VR;
  8. public class SteamVR_LoadLevel : MonoBehaviour
  9. {
  10. public static bool loading
  11. {
  12. get
  13. {
  14. return SteamVR_LoadLevel._active != null;
  15. }
  16. }
  17. public static float progress
  18. {
  19. get
  20. {
  21. return (!(SteamVR_LoadLevel._active != null) || SteamVR_LoadLevel._active.async == null) ? 0f : SteamVR_LoadLevel._active.async.progress;
  22. }
  23. }
  24. public static Texture progressTexture
  25. {
  26. get
  27. {
  28. return (!(SteamVR_LoadLevel._active != null)) ? null : SteamVR_LoadLevel._active.renderTexture;
  29. }
  30. }
  31. private void OnEnable()
  32. {
  33. if (this.autoTriggerOnEnable)
  34. {
  35. this.Trigger();
  36. }
  37. }
  38. public void Trigger()
  39. {
  40. if (!SteamVR_LoadLevel.loading && !string.IsNullOrEmpty(this.levelName))
  41. {
  42. base.StartCoroutine("LoadLevel");
  43. }
  44. }
  45. public static void Begin(string levelName, bool showGrid = false, float fadeOutTime = 0.5f, float r = 0f, float g = 0f, float b = 0f, float a = 1f)
  46. {
  47. SteamVR_LoadLevel steamVR_LoadLevel = new GameObject("loader").AddComponent<SteamVR_LoadLevel>();
  48. steamVR_LoadLevel.levelName = levelName;
  49. steamVR_LoadLevel.showGrid = showGrid;
  50. steamVR_LoadLevel.fadeOutTime = fadeOutTime;
  51. steamVR_LoadLevel.backgroundColor = new Color(r, g, b, a);
  52. steamVR_LoadLevel.Trigger();
  53. }
  54. private void OnGUI()
  55. {
  56. if (SteamVR_LoadLevel._active != this)
  57. {
  58. return;
  59. }
  60. if (this.progressBarEmpty != null && this.progressBarFull != null)
  61. {
  62. if (this.progressBarOverlayHandle == 0UL)
  63. {
  64. this.progressBarOverlayHandle = this.GetOverlayHandle("progressBar", (!(this.progressBarTransform != null)) ? base.transform : this.progressBarTransform, this.progressBarWidthInMeters);
  65. }
  66. if (this.progressBarOverlayHandle != 0UL)
  67. {
  68. float num = (this.async == null) ? 0f : this.async.progress;
  69. int width = this.progressBarFull.width;
  70. int height = this.progressBarFull.height;
  71. if (this.renderTexture == null)
  72. {
  73. this.renderTexture = new RenderTexture(width, height, 0);
  74. this.renderTexture.Create();
  75. }
  76. RenderTexture active = RenderTexture.active;
  77. RenderTexture.active = this.renderTexture;
  78. if (Event.current.type == EventType.Repaint)
  79. {
  80. GL.Clear(false, true, Color.clear);
  81. }
  82. GUILayout.BeginArea(new Rect(0f, 0f, (float)width, (float)height));
  83. GUI.DrawTexture(new Rect(0f, 0f, (float)width, (float)height), this.progressBarEmpty);
  84. GUI.DrawTextureWithTexCoords(new Rect(0f, 0f, num * (float)width, (float)height), this.progressBarFull, new Rect(0f, 0f, num, 1f));
  85. GUILayout.EndArea();
  86. RenderTexture.active = active;
  87. CVROverlay overlay = OpenVR.Overlay;
  88. if (overlay != null)
  89. {
  90. Texture_t texture_t = default(Texture_t);
  91. texture_t.handle = this.renderTexture.GetNativeTexturePtr();
  92. texture_t.eType = SteamVR.instance.graphicsAPI;
  93. texture_t.eColorSpace = EColorSpace.Auto;
  94. overlay.SetOverlayTexture(this.progressBarOverlayHandle, ref texture_t);
  95. }
  96. }
  97. }
  98. }
  99. private void Update()
  100. {
  101. if (SteamVR_LoadLevel._active != this)
  102. {
  103. return;
  104. }
  105. this.alpha = Mathf.Clamp01(this.alpha + this.fadeRate * Time.deltaTime);
  106. CVROverlay overlay = OpenVR.Overlay;
  107. if (overlay != null)
  108. {
  109. if (this.loadingScreenOverlayHandle != 0UL)
  110. {
  111. overlay.SetOverlayAlpha(this.loadingScreenOverlayHandle, this.alpha);
  112. }
  113. if (this.progressBarOverlayHandle != 0UL)
  114. {
  115. overlay.SetOverlayAlpha(this.progressBarOverlayHandle, this.alpha);
  116. }
  117. }
  118. }
  119. private IEnumerator LoadLevel()
  120. {
  121. if (this.loadingScreen != null && this.loadingScreenDistance > 0f)
  122. {
  123. SteamVR_Controller.Device hmd = SteamVR_Controller.Input(0);
  124. while (!hmd.hasTracking)
  125. {
  126. yield return null;
  127. }
  128. SteamVR_Utils.RigidTransform tloading = hmd.transform;
  129. tloading.rot = Quaternion.Euler(0f, tloading.rot.eulerAngles.y, 0f);
  130. tloading.pos += tloading.rot * new Vector3(0f, 0f, this.loadingScreenDistance);
  131. Transform t = (!(this.loadingScreenTransform != null)) ? base.transform : this.loadingScreenTransform;
  132. t.position = tloading.pos;
  133. t.rotation = tloading.rot;
  134. }
  135. SteamVR_LoadLevel._active = this;
  136. SteamVR_Utils.Event.Send("loading", new object[]
  137. {
  138. true
  139. });
  140. if (this.loadingScreenFadeInTime > 0f)
  141. {
  142. this.fadeRate = 1f / this.loadingScreenFadeInTime;
  143. }
  144. else
  145. {
  146. this.alpha = 1f;
  147. }
  148. CVROverlay overlay = OpenVR.Overlay;
  149. if (this.loadingScreen != null && overlay != null)
  150. {
  151. this.loadingScreenOverlayHandle = this.GetOverlayHandle("loadingScreen", (!(this.loadingScreenTransform != null)) ? base.transform : this.loadingScreenTransform, this.loadingScreenWidthInMeters);
  152. if (this.loadingScreenOverlayHandle != 0UL)
  153. {
  154. Texture_t texture_t = default(Texture_t);
  155. texture_t.handle = this.loadingScreen.GetNativeTexturePtr();
  156. texture_t.eType = SteamVR.instance.graphicsAPI;
  157. texture_t.eColorSpace = EColorSpace.Auto;
  158. overlay.SetOverlayTexture(this.loadingScreenOverlayHandle, ref texture_t);
  159. }
  160. }
  161. bool fadedForeground = false;
  162. SteamVR_Utils.Event.Send("loading_fade_out", new object[]
  163. {
  164. this.fadeOutTime
  165. });
  166. CVRCompositor compositor = OpenVR.Compositor;
  167. if (compositor != null)
  168. {
  169. if (this.front != null)
  170. {
  171. SteamVR_Skybox.SetOverride(this.front, this.back, this.left, this.right, this.top, this.bottom);
  172. compositor.FadeGrid(this.fadeOutTime, true);
  173. yield return new WaitForSeconds(this.fadeOutTime);
  174. }
  175. else if (this.backgroundColor != Color.clear)
  176. {
  177. if (this.showGrid)
  178. {
  179. compositor.FadeToColor(0f, this.backgroundColor.r, this.backgroundColor.g, this.backgroundColor.b, this.backgroundColor.a, true);
  180. compositor.FadeGrid(this.fadeOutTime, true);
  181. yield return new WaitForSeconds(this.fadeOutTime);
  182. }
  183. else
  184. {
  185. compositor.FadeToColor(this.fadeOutTime, this.backgroundColor.r, this.backgroundColor.g, this.backgroundColor.b, this.backgroundColor.a, false);
  186. yield return new WaitForSeconds(this.fadeOutTime + 0.1f);
  187. compositor.FadeGrid(0f, true);
  188. fadedForeground = true;
  189. }
  190. }
  191. }
  192. SteamVR_Render.pauseRendering = true;
  193. while (this.alpha < 1f)
  194. {
  195. yield return null;
  196. }
  197. base.transform.parent = null;
  198. UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
  199. if (this.loadExternalApp)
  200. {
  201. UnityEngine.Debug.Log("Launching external application...");
  202. CVRApplications applications = OpenVR.Applications;
  203. if (applications == null)
  204. {
  205. UnityEngine.Debug.Log("Failed to get OpenVR.Applications interface!");
  206. }
  207. else
  208. {
  209. string currentDirectory = Directory.GetCurrentDirectory();
  210. string text = Path.Combine(currentDirectory, this.externalAppPath);
  211. UnityEngine.Debug.Log("LaunchingInternalProcess");
  212. UnityEngine.Debug.Log("ExternalAppPath = " + this.externalAppPath);
  213. UnityEngine.Debug.Log("FullPath = " + text);
  214. UnityEngine.Debug.Log("ExternalAppArgs = " + this.externalAppArgs);
  215. UnityEngine.Debug.Log("WorkingDirectory = " + currentDirectory);
  216. EVRApplicationError evrapplicationError = applications.LaunchInternalProcess(text, this.externalAppArgs, currentDirectory);
  217. UnityEngine.Debug.Log("LaunchInternalProcessError: " + evrapplicationError);
  218. Process.GetCurrentProcess().Kill();
  219. }
  220. }
  221. else
  222. {
  223. LoadSceneMode mode = (!this.loadAdditive) ? LoadSceneMode.Single : LoadSceneMode.Additive;
  224. if (this.loadAsync)
  225. {
  226. Application.backgroundLoadingPriority = ThreadPriority.Low;
  227. this.async = SceneManager.LoadSceneAsync(this.levelName, mode);
  228. while (!this.async.isDone)
  229. {
  230. yield return null;
  231. }
  232. }
  233. else
  234. {
  235. SceneManager.LoadScene(this.levelName, mode);
  236. }
  237. }
  238. yield return null;
  239. GC.Collect();
  240. yield return null;
  241. Shader.WarmupAllShaders();
  242. yield return new WaitForSeconds(this.postLoadSettleTime);
  243. SteamVR_Render.pauseRendering = false;
  244. if (this.loadingScreenFadeOutTime > 0f)
  245. {
  246. this.fadeRate = -1f / this.loadingScreenFadeOutTime;
  247. }
  248. else
  249. {
  250. this.alpha = 0f;
  251. }
  252. SteamVR_Utils.Event.Send("loading_fade_in", new object[]
  253. {
  254. this.fadeInTime
  255. });
  256. if (compositor != null)
  257. {
  258. if (fadedForeground)
  259. {
  260. compositor.FadeGrid(0f, false);
  261. compositor.FadeToColor(this.fadeInTime, 0f, 0f, 0f, 0f, false);
  262. yield return new WaitForSeconds(this.fadeInTime);
  263. }
  264. else
  265. {
  266. compositor.FadeGrid(this.fadeInTime, false);
  267. yield return new WaitForSeconds(this.fadeInTime);
  268. if (this.front != null)
  269. {
  270. SteamVR_Skybox.ClearOverride();
  271. }
  272. }
  273. }
  274. while (this.alpha > 0f)
  275. {
  276. yield return null;
  277. }
  278. if (overlay != null)
  279. {
  280. if (this.progressBarOverlayHandle != 0UL)
  281. {
  282. overlay.HideOverlay(this.progressBarOverlayHandle);
  283. }
  284. if (this.loadingScreenOverlayHandle != 0UL)
  285. {
  286. overlay.HideOverlay(this.loadingScreenOverlayHandle);
  287. }
  288. }
  289. UnityEngine.Object.Destroy(base.gameObject);
  290. SteamVR_LoadLevel._active = null;
  291. SteamVR_Utils.Event.Send("loading", new object[]
  292. {
  293. false
  294. });
  295. yield break;
  296. }
  297. private ulong GetOverlayHandle(string overlayName, Transform transform, float widthInMeters = 1f)
  298. {
  299. ulong num = 0UL;
  300. CVROverlay overlay = OpenVR.Overlay;
  301. if (overlay == null)
  302. {
  303. return num;
  304. }
  305. string pchOverlayKey = SteamVR_Overlay.key + "." + overlayName;
  306. EVROverlayError evroverlayError = overlay.FindOverlay(pchOverlayKey, ref num);
  307. if (evroverlayError != EVROverlayError.None)
  308. {
  309. evroverlayError = overlay.CreateOverlay(pchOverlayKey, overlayName, ref num);
  310. }
  311. if (evroverlayError == EVROverlayError.None)
  312. {
  313. overlay.ShowOverlay(num);
  314. overlay.SetOverlayAlpha(num, this.alpha);
  315. overlay.SetOverlayWidthInMeters(num, widthInMeters);
  316. if (SteamVR.instance.graphicsAPI == EGraphicsAPIConvention.API_DirectX)
  317. {
  318. VRTextureBounds_t vrtextureBounds_t = default(VRTextureBounds_t);
  319. vrtextureBounds_t.uMin = 0f;
  320. vrtextureBounds_t.vMin = 1f;
  321. vrtextureBounds_t.uMax = 1f;
  322. vrtextureBounds_t.vMax = 0f;
  323. overlay.SetOverlayTextureBounds(num, ref vrtextureBounds_t);
  324. }
  325. SteamVR_Camera steamVR_Camera = (this.loadingScreenDistance != 0f) ? null : SteamVR_Render.Top();
  326. if (steamVR_Camera != null && steamVR_Camera.origin != null)
  327. {
  328. SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(steamVR_Camera.origin, transform);
  329. rigidTransform.pos.x = rigidTransform.pos.x / steamVR_Camera.origin.localScale.x;
  330. rigidTransform.pos.y = rigidTransform.pos.y / steamVR_Camera.origin.localScale.y;
  331. rigidTransform.pos.z = rigidTransform.pos.z / steamVR_Camera.origin.localScale.z;
  332. HmdMatrix34_t hmdMatrix34_t = rigidTransform.ToHmdMatrix34();
  333. overlay.SetOverlayTransformAbsolute(num, SteamVR_Render.instance.trackingSpace, ref hmdMatrix34_t);
  334. }
  335. else
  336. {
  337. SteamVR_Utils.RigidTransform rigidTransform2 = new SteamVR_Utils.RigidTransform(transform);
  338. HmdMatrix34_t hmdMatrix34_t2 = rigidTransform2.ToHmdMatrix34();
  339. overlay.SetOverlayTransformAbsolute(num, SteamVR_Render.instance.trackingSpace, ref hmdMatrix34_t2);
  340. }
  341. }
  342. return num;
  343. }
  344. private static SteamVR_LoadLevel _active;
  345. public string levelName;
  346. public bool loadExternalApp;
  347. public string externalAppPath;
  348. public string externalAppArgs;
  349. public bool loadAdditive;
  350. public bool loadAsync = true;
  351. public Texture loadingScreen;
  352. public Texture progressBarEmpty;
  353. public Texture progressBarFull;
  354. public float loadingScreenWidthInMeters = 6f;
  355. public float progressBarWidthInMeters = 3f;
  356. public float loadingScreenDistance;
  357. public Transform loadingScreenTransform;
  358. public Transform progressBarTransform;
  359. public Texture front;
  360. public Texture back;
  361. public Texture left;
  362. public Texture right;
  363. public Texture top;
  364. public Texture bottom;
  365. public Color backgroundColor = Color.black;
  366. public bool showGrid;
  367. public float fadeOutTime = 0.5f;
  368. public float fadeInTime = 0.5f;
  369. public float postLoadSettleTime;
  370. public float loadingScreenFadeInTime = 1f;
  371. public float loadingScreenFadeOutTime = 0.25f;
  372. private float fadeRate = 1f;
  373. private float alpha;
  374. private AsyncOperation async;
  375. private RenderTexture renderTexture;
  376. private ulong loadingScreenOverlayHandle;
  377. private ulong progressBarOverlayHandle;
  378. public bool autoTriggerOnEnable;
  379. }