Ejemplo n.º 1
0
        private async Task CaptureCubemap(PhotosphereJig photosphere)
        {
            try
            {
                var fileRef = photosphere.name + codec.ContentType;
                if (cache.IsCached(fileRef))
                {
                    Debug.Log("Cubemap already cached");
                }
                else
                {
                    loadingBar.Activate();

                    const int dim     = 2048;
                    Cubemap   cubemap = null;
                    Texture2D img     = null;

                    await loadingBar.RunAsync(
                        ("Rendering cubemap", async(prog) =>
                    {
                        cubemap = await JuniperSystem.OnMainThreadAsync(() =>
                        {
                            prog.Report(0);
                            var cb = new Cubemap(dim, TextureFormat.RGB24, false);
                            cb.Apply();

                            var curMask = DisplayManager.MainCamera.cullingMask;
                            DisplayManager.MainCamera.cullingMask = LayerMask.GetMask(Photosphere.PHOTOSPHERE_LAYER_ARR);

                            var curRotation = DisplayManager.MainCamera.transform.rotation;
                            DisplayManager.MainCamera.transform.rotation = Quaternion.identity;

                            DisplayManager.MainCamera.RenderToCubemap(cb, 63);

                            DisplayManager.MainCamera.cullingMask = curMask;
                            DisplayManager.MainCamera.transform.rotation = curRotation;
                            prog.Report(1);

                            return(cb);
                        });
                    }
                        ),
                        ("Copying cubemap faces", async(prog) =>
                    {
                        for (var f = 0; f < CAPTURE_CUBEMAP_FACES.Length; ++f)
                        {
                            await JuniperSystem.OnMainThreadAsync(() =>
                            {
                                prog.Report(f, CAPTURE_CUBEMAP_FACES.Length, CAPTURE_CUBEMAP_FACES[f].ToString());
                                var pixels = cubemap.GetPixels(CAPTURE_CUBEMAP_FACES[f]);
                                var texture = new Texture2D(cubemap.width, cubemap.height);
                                texture.SetPixels(pixels);
                                texture.Apply();
                                CAPTURE_CUBEMAP_SUB_IMAGES[f] = texture;
                                prog.Report(f + 1, CAPTURE_CUBEMAP_FACES.Length);
                            });
                        }
                    }
                        ),
                        ("Concatenating faces", async(prog) =>
                    {
                        img = await JuniperSystem.OnMainThreadAsync(() =>
                                                                    processor.Concatenate(ImageData.CubeCross(CAPTURE_CUBEMAP_SUB_IMAGES), prog));
                    }
                        ),
                        ("Saving image", (prog) =>
                         JuniperSystem.OnMainThreadAsync(() => cache.Save(codec, fileRef, img))));

                    loadingBar.Deactivate();
                }

                await JuniperSystem.OnMainThreadAsync(photosphere.DestroyJig);

                var anyDestroyed = await JuniperSystem.OnMainThreadAsync(() =>
                {
                    var any = false;
                    foreach (var texture in CAPTURE_CUBEMAP_SUB_IMAGES)
                    {
                        if (texture != null)
                        {
                            any = true;
                            Destroy(texture);
                        }
                    }

                    return(any);
                });

                if (anyDestroyed)
                {
                    Resources.UnloadUnusedAssets();
                    GC.Collect();
                }

                photosphere.enabled = false;
                await Task.Yield();

                photosphere.enabled = true;
            }
            catch (Exception exp)
            {
                Debug.LogError("Cubemap capture error");
                Debug.LogException(exp, this);
                throw;
            }
        }
Ejemplo n.º 2
0
        private IEnumerator SwitchToSceneCoroutine(string subSceneName, bool skipFadeOut, bool skipLoadingScreen, bool unloadOtherScenes, bool fromView, IProgress prog)
        {
            var scenePath = GetScenePathFromName(subSceneName);

            if (scenePath == null)
            {
                ScreenDebugger.Print($"Couldn't find scene: {subSceneName}");
            }
            else
            {
                if (unloadOtherScenes)
                {
                    yield return(ExitAllSubScenesExcept(GetOpenSubScenes(scenePath), scenePath));
                }

                var showFader = !skipFadeOut &&
                                fader != null &&
                                fader.CanEnter;

                if (showFader && fromView)
                {
                    showFader &= !IsSceneLoaded(scenePath);
                    if (!showFader)
                    {
                        var scene     = SceneManager.GetSceneByPath(scenePath);
                        var subScenes = scene.FindAll <SubSceneController>();
                        foreach (var subScene in subScenes)
                        {
                            showFader |= subScene.IsExited || !subScene.isActiveAndEnabled;
                        }
                    }
                }

                if (showFader)
                {
                    Resources.UnloadUnusedAssets();

                    yield return(fader.EnterAsync().AsCoroutine());

                    if (input != null)
                    {
                        input.paused = true;
                    }

                    if (loadingBar != null && !skipLoadingScreen)
                    {
                        loadingBar.Activate();
                        prog.Report(0, subSceneName);
                    }
                }

                if (unloadOtherScenes)
                {
                    foreach (var subScene in GetOpenSubScenes(scenePath))
                    {
                        if (subScene.unloadSceneOnExit)
                        {
                            var scene = subScene.gameObject.scene;
                            yield return(SceneManager.UnloadSceneAsync(scene).AsCoroutine());
                        }
                    }
                }

                yield return(LoadScenePathCoroutine(scenePath, prog));

                for (var i = 1; i < SceneManager.sceneCount; ++i)
                {
                    var scene = SceneManager.GetSceneAt(i);

#if UNITY_MODULES_UI
                    var canvases = scene.FindAll <Canvas>((c) =>
                                                          c.renderMode == RenderMode.WorldSpace &&
                                                          (c.worldCamera == null ||
                                                           c.worldCamera != DisplayManager.MainCamera));
                    foreach (var canvas in canvases)
                    {
                        canvas.worldCamera = DisplayManager.EventCamera;
                    }
#endif

#if UNITY_MODULES_AUDIO
                    var audioSources = scene.FindAll <AudioSource>((a) => a.spatialize);
                    foreach (var audioSource in audioSources)
                    {
                        if (audioSource.spatialize)
                        {
                            interaction.Spatialize(audioSource);
                        }
                        else
                        {
                            interaction.SetDefaultMixerGroup(audioSource);
                        }
                    }
#endif
                }

                if (loadingBar != null)
                {
                    var end = DateTime.Now.AddSeconds(1);
                    while (DateTime.Now < end || loadingBar.Progress >= 1)
                    {
                        yield return(null);
                    }
                    loadingBar.Deactivate();
                }

                if (splash != null)
                {
                    splash.Deactivate();
                }

                if (fader != null &&
                    fader.CanExit)
                {
                    if (input != null)
                    {
                        input.paused = false;
                    }

                    if (skipLoadingScreen)
                    {
                        fader.SkipExit();
                    }
                    else
                    {
                        yield return(fader.ExitAsync().AsCoroutine());
                    }
                }
            }
        }