8 using System.Collections;
15 public static bool loading {
get {
return _active != null; } }
16 public static float progress
18 get {
return (_active != null && _active.async != null) ? _active.async.progress : 0.0f; }
20 public static Texture progressTexture
22 get {
return (_active != null) ? _active.renderTexture : null; }
26 public string levelName;
29 public string internalProcessPath;
32 public string internalProcessArgs;
35 public bool loadAdditive;
38 public bool loadAsync =
true;
41 public Texture loadingScreen;
44 public Texture progressBarEmpty, progressBarFull;
47 public float loadingScreenWidthInMeters = 6.0f;
48 public float progressBarWidthInMeters = 3.0f;
51 public float loadingScreenDistance = 0.0f;
55 public Transform loadingScreenTransform, progressBarTransform;
58 public Texture front, back, left, right, top, bottom;
61 public Color backgroundColor = Color.black;
65 public bool showGrid =
false;
68 public float fadeOutTime = 0.5f;
69 public float fadeInTime = 0.5f;
74 public float postLoadSettleTime = 0.0f;
77 public float loadingScreenFadeInTime = 1.0f;
78 public float loadingScreenFadeOutTime = 0.25f;
80 float fadeRate = 1.0f;
84 RenderTexture renderTexture;
86 ulong loadingScreenOverlayHandle = OpenVR.k_ulOverlayHandleInvalid;
87 ulong progressBarOverlayHandle = OpenVR.k_ulOverlayHandleInvalid;
89 public bool autoTriggerOnEnable =
false;
93 if (autoTriggerOnEnable)
99 if (!loading && !
string.IsNullOrEmpty(levelName))
100 StartCoroutine(LoadLevel());
104 public static void Begin(
string levelName,
105 bool showGrid =
false,
float fadeOutTime = 0.5f,
106 float r = 0.0f,
float g = 0.0f,
float b = 0.0f,
float a = 1.0f)
109 loader.levelName = levelName;
110 loader.showGrid = showGrid;
111 loader.fadeOutTime = fadeOutTime;
112 loader.backgroundColor =
new Color(r, g, b, a);
123 if (progressBarEmpty != null && progressBarFull != null)
125 if (progressBarOverlayHandle ==
OpenVR.k_ulOverlayHandleInvalid)
126 progressBarOverlayHandle = GetOverlayHandle(
"progressBar", progressBarTransform != null ? progressBarTransform : transform, progressBarWidthInMeters);
128 if (progressBarOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
130 var progress = (async != null) ? async.progress : 0.0f;
133 var w = progressBarFull.width;
134 var h = progressBarFull.height;
137 if (renderTexture == null)
139 renderTexture =
new RenderTexture(w, h, 0);
140 renderTexture.Create();
143 var prevActive = RenderTexture.active;
144 RenderTexture.active = renderTexture;
146 if (Event.current.type == EventType.Repaint)
147 GL.Clear(
false,
true, Color.clear);
149 GUILayout.BeginArea(
new Rect(0, 0, w, h));
151 GUI.DrawTexture(
new Rect(0, 0, w, h), progressBarEmpty);
154 GUI.DrawTextureWithTexCoords(
new Rect(0, 0, progress * w, h), progressBarFull,
new Rect(0.0f, 0.0f, progress, 1.0f));
158 RenderTexture.active = prevActive;
161 var overlay = OpenVR.Overlay;
165 texture.handle = renderTexture.GetNativeTexturePtr();
166 texture.eType = SteamVR.instance.textureType;
167 texture.eColorSpace = EColorSpace.Auto;
168 overlay.SetOverlayTexture(progressBarOverlayHandle, ref texture);
175 if (loadingScreen != null)
177 var screenAspect = (float)Screen.width / Screen.height;
178 var textureAspect = (
float)loadingScreen.width / loadingScreen.height;
181 if (screenAspect < textureAspect)
184 w = Screen.width * 0.9f;
185 h = w / textureAspect;
190 h = Screen.height * 0.9f;
191 w = h * textureAspect;
194 GUILayout.BeginArea(
new Rect(0, 0, Screen.width, Screen.height));
196 var x = Screen.width / 2 - w / 2;
197 var y = Screen.height / 2 - h / 2;
198 GUI.DrawTexture(
new Rect(x, y, w, h), loadingScreen);
203 if (renderTexture != null)
205 var x = Screen.width / 2 - renderTexture.width / 2;
206 var y = Screen.height * 0.9f - renderTexture.height;
207 GUI.DrawTexture(
new Rect(x, y, renderTexture.width, renderTexture.height), renderTexture);
218 alpha = Mathf.Clamp01(alpha + fadeRate * Time.deltaTime);
220 var overlay = OpenVR.Overlay;
223 if (loadingScreenOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
224 overlay.SetOverlayAlpha(loadingScreenOverlayHandle, alpha);
226 if (progressBarOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
227 overlay.SetOverlayAlpha(progressBarOverlayHandle, alpha);
232 IEnumerator LoadLevel()
237 if (loadingScreen != null && loadingScreenDistance > 0.0f)
240 var hmd = SteamVR_Controller.Input((int)
OpenVR.k_unTrackedDeviceIndex_Hmd);
241 while (!hmd.hasTracking)
244 var tloading = hmd.transform;
245 tloading.rot = Quaternion.Euler(0.0f, tloading.rot.eulerAngles.y, 0.0f);
246 tloading.pos += tloading.rot *
new Vector3(0.0f, 0.0f, loadingScreenDistance);
248 var t = loadingScreenTransform != null ? loadingScreenTransform : transform;
249 t.position = tloading.pos;
250 t.rotation = tloading.rot;
255 SteamVR_Events.Loading.Send(
true);
258 if (loadingScreenFadeInTime > 0.0f)
260 fadeRate = 1.0f / loadingScreenFadeInTime;
267 var overlay = OpenVR.Overlay;
270 if (loadingScreen != null && overlay != null)
272 loadingScreenOverlayHandle = GetOverlayHandle(
"loadingScreen", loadingScreenTransform != null ? loadingScreenTransform : transform, loadingScreenWidthInMeters);
273 if (loadingScreenOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
276 texture.handle = loadingScreen.GetNativeTexturePtr();
277 texture.eType = SteamVR.instance.textureType;
278 texture.eColorSpace = EColorSpace.Auto;
279 overlay.SetOverlayTexture(loadingScreenOverlayHandle, ref texture);
283 bool fadedForeground =
false;
286 SteamVR_Events.LoadingFadeOut.Send(fadeOutTime);
289 var compositor = OpenVR.Compositor;
290 if (compositor != null)
294 SteamVR_Skybox.SetOverride(front, back, left, right, top, bottom);
297 compositor.FadeGrid(fadeOutTime,
true);
298 yield
return new WaitForSeconds(fadeOutTime);
300 else if (backgroundColor != Color.clear)
306 compositor.FadeToColor(0.0f, backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a,
true);
307 compositor.FadeGrid(fadeOutTime,
true);
308 yield
return new WaitForSeconds(fadeOutTime);
313 compositor.FadeToColor(fadeOutTime, backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a,
false);
314 yield
return new WaitForSeconds(fadeOutTime + 0.1f);
315 compositor.FadeGrid(0.0f,
true);
316 fadedForeground =
true;
322 SteamVR_Render.pauseRendering =
true;
329 transform.parent = null;
330 DontDestroyOnLoad(gameObject);
332 if (!
string.IsNullOrEmpty(internalProcessPath))
334 Debug.Log(
"Launching external application...");
335 var applications = OpenVR.Applications;
336 if (applications == null)
338 Debug.Log(
"Failed to get OpenVR.Applications interface!");
342 var workingDirectory = Directory.GetCurrentDirectory();
343 var fullPath = Path.Combine(workingDirectory, internalProcessPath);
344 Debug.Log(
"LaunchingInternalProcess");
345 Debug.Log(
"ExternalAppPath = " + internalProcessPath);
346 Debug.Log(
"FullPath = " + fullPath);
347 Debug.Log(
"ExternalAppArgs = " + internalProcessArgs);
348 Debug.Log(
"WorkingDirectory = " + workingDirectory);
349 var error = applications.LaunchInternalProcess(fullPath, internalProcessArgs, workingDirectory);
350 Debug.Log(
"LaunchInternalProcessError: " + error);
352 UnityEditor.EditorApplication.isPlaying =
false;
354 System.Diagnostics.Process.GetCurrentProcess().Kill();
360 var mode = loadAdditive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single;
363 Application.backgroundLoadingPriority = ThreadPriority.Low;
364 async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(levelName, mode);
368 while (!async.isDone)
375 UnityEngine.SceneManagement.SceneManager.LoadScene(levelName, mode);
385 Shader.WarmupAllShaders();
389 yield
return new WaitForSeconds(postLoadSettleTime);
391 SteamVR_Render.pauseRendering =
false;
394 if (loadingScreenFadeOutTime > 0.0f)
396 fadeRate = -1.0f / loadingScreenFadeOutTime;
404 SteamVR_Events.LoadingFadeIn.Send(fadeInTime);
407 compositor = OpenVR.Compositor;
408 if (compositor != null)
413 compositor.FadeGrid(0.0f,
false);
414 compositor.FadeToColor(fadeInTime, 0.0f, 0.0f, 0.0f, 0.0f,
false);
415 yield
return new WaitForSeconds(fadeInTime);
420 compositor.FadeGrid(fadeInTime,
false);
421 yield
return new WaitForSeconds(fadeInTime);
425 SteamVR_Skybox.ClearOverride();
436 if (progressBarOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
437 overlay.HideOverlay(progressBarOverlayHandle);
438 if (loadingScreenOverlayHandle !=
OpenVR.k_ulOverlayHandleInvalid)
439 overlay.HideOverlay(loadingScreenOverlayHandle);
446 SteamVR_Events.Loading.Send(
false);
450 ulong GetOverlayHandle(
string overlayName, Transform transform,
float widthInMeters = 1.0f)
452 ulong handle = OpenVR.k_ulOverlayHandleInvalid;
454 var overlay = OpenVR.Overlay;
458 var key = SteamVR_Overlay.key +
"." + overlayName;
460 var error = overlay.FindOverlay(key, ref handle);
461 if (error != EVROverlayError.None)
462 error = overlay.CreateOverlay(key, overlayName, ref handle);
463 if (error == EVROverlayError.None)
465 overlay.ShowOverlay(handle);
466 overlay.SetOverlayAlpha(handle, alpha);
467 overlay.SetOverlayWidthInMeters(handle, widthInMeters);
470 if (
SteamVR.instance.textureType == ETextureType.DirectX)
473 textureBounds.uMin = 0;
474 textureBounds.vMin = 1;
475 textureBounds.uMax = 1;
476 textureBounds.vMax = 0;
477 overlay.SetOverlayTextureBounds(handle, ref textureBounds);
481 var vrcam = (loadingScreenDistance == 0.0f) ?
SteamVR_Render.Top() : null;
482 if (vrcam != null && vrcam.origin != null)
485 offset.pos.x /= vrcam.origin.localScale.x;
486 offset.pos.y /= vrcam.origin.localScale.y;
487 offset.pos.z /= vrcam.origin.localScale.z;
489 var t = offset.ToHmdMatrix34();
490 overlay.SetOverlayTransformAbsolute(handle, SteamVR_Render.instance.trackingSpace, ref t);
495 overlay.SetOverlayTransformAbsolute(handle, SteamVR_Render.instance.trackingSpace, ref t);