public void StartTransition(
                RuntimeLightingSettings targetLightingSettings,
                RuntimeRenderSettings targetRenderSettings,
                RuntimeSunlightSettings targetSunlightSettings,
                LightingSceneTransitionType transitionType = LightingSceneTransitionType.None,
                float transitionDuration = 1)
            {
                // Update our target settings
                this.transitionElapsed      = 0;
                this.transitionType         = transitionType;
                this.transitionDuration     = transitionDuration;
                this.targetLightingSettings = targetLightingSettings;
                this.targetRenderSettings   = targetRenderSettings;
                this.targetSunlightSettings = targetSunlightSettings;

                switch (transitionType)
                {
                case LightingSceneTransitionType.None:
                    // Just execute the transition right now
                    // Zap immediately to the new values
                    currentLightingSettings = targetLightingSettings;
                    currentRenderSettings   = targetRenderSettings;
                    currentSunlightSettings = targetSunlightSettings;
                    transitionElapsed       = transitionDuration;
                    ApplySettings();
                    return;
                }

                // Otherwise, copy our old settings so we have something to lerp from
                prevLightingSettings = currentLightingSettings;
                prevRenderSettings   = currentRenderSettings;
                prevSunlightSettings = currentSunlightSettings;
            }
コード例 #2
0
        /// <summary>
        /// Lerps between two settings
        /// </summary>
        /// <param name="t">Value from 0 to 1</param>
        public static RuntimeRenderSettings Lerp(RuntimeRenderSettings from, RuntimeRenderSettings to, float t)
        {
            bool notStarted = t <= 0;

            to.AmbientEquatorColor         = Color.Lerp(from.AmbientEquatorColor, to.AmbientEquatorColor, t);
            to.AmbientGroundColor          = Color.Lerp(from.AmbientGroundColor, to.AmbientGroundColor, t);
            to.AmbientIntensity            = Mathf.Lerp(from.AmbientIntensity, to.AmbientIntensity, t);
            to.AmbientLight                = Color.Lerp(from.AmbientLight, to.AmbientLight, t);
            to.AmbientMode                 = notStarted ? from.AmbientMode : to.AmbientMode;
            to.AmbientSkyColor             = Color.Lerp(from.AmbientSkyColor, to.AmbientSkyColor, t);
            to.CustomReflection            = notStarted ? from.CustomReflection : to.CustomReflection;
            to.DefaultReflectionMode       = notStarted ? from.DefaultReflectionMode : to.DefaultReflectionMode;
            to.DefaultReflectionResolution = notStarted ? from.DefaultReflectionResolution : to.DefaultReflectionResolution;
            to.Fog                     = notStarted ? from.Fog : to.Fog;
            to.FogColor                = Color.Lerp(from.FogColor, to.FogColor, t);
            to.FogDensity              = Mathf.Lerp(from.FogDensity, to.FogDensity, t);
            to.FogMode                 = notStarted ? from.FogMode : to.FogMode;
            to.LinearFogEnd            = Mathf.Lerp(from.LinearFogEnd, to.LinearFogEnd, t);
            to.LinearFogStart          = Mathf.Lerp(from.LinearFogStart, to.LinearFogStart, t);
            to.ReflectionBounces       = notStarted ? from.ReflectionBounces : to.ReflectionBounces;
            to.ReflectionIntensity     = Mathf.Lerp(from.ReflectionIntensity, to.ReflectionIntensity, t);
            to.SkyboxMaterial          = notStarted ? from.SkyboxMaterial : to.SkyboxMaterial;
            to.SubtractiveShadowColor  = Color.Lerp(from.SubtractiveShadowColor, to.SubtractiveShadowColor, t);
            to.UseRadianceAmbientProbe = notStarted ? from.UseRadianceAmbientProbe : to.UseRadianceAmbientProbe;
            return(to);
        }
コード例 #3
0
        /// <inheritdoc />
        public async void SetLightingScene(string newLightingSceneName, LightingSceneTransitionType transitionType = LightingSceneTransitionType.None, float transitionDuration = 1f)
        {
            Debug.Log("Set lighting scene: " + newLightingSceneName);

            if (ActiveLightingScene == newLightingSceneName)
            {   // Nothing to do here
                return;
            }

            if (!CanSceneOpProceed(SceneType.Lighting))
            {
                Debug.LogError("Attempting to perform a scene op when a scene op is already in progress.");
                return;
            }

            SceneInfo lightingScene;
            RuntimeLightingSettings lightingSettings = default(RuntimeLightingSettings);
            RuntimeRenderSettings   renderSettings   = default(RuntimeRenderSettings);
            RuntimeSunlightSettings sunSettings      = default(RuntimeSunlightSettings);

            if (!string.IsNullOrEmpty(newLightingSceneName) && !profile.GetLightingSceneSettings(
                    newLightingSceneName,
                    out lightingScene,
                    out lightingSettings,
                    out renderSettings,
                    out sunSettings))
            {   // Make sure we don't try to load a non-existent scene
                Debug.LogWarning("Couldn't find lighting scene " + newLightingSceneName + " in profile - taking no action.");
                return;
            }

            ActiveLightingScene = newLightingSceneName;

            if (!Application.isPlaying)
            {   // Everything else is runtime-only
                return;
            }

            // Start the lighting executor transition - don't bother waiting for load / unload, it can start right away
            lightingExecutor.StartTransition(lightingSettings, renderSettings, sunSettings, transitionType, transitionDuration);

            List <string> lightingSceneNames = new List <string>();

            // Create a list of lighting scenes to unload
            foreach (SceneInfo lso in LightingScenes)
            {
                if (lso.Name != newLightingSceneName)
                {
                    lightingSceneNames.Add(lso.Name);
                }
            }

            // Load the new lighting scene immediately
            await LoadScenesInternal(new string[] { newLightingSceneName }, SceneType.Lighting, null, 0f, 0.5f, true);

            // Unload the other lighting scenes
            await UnloadScenesInternal(lightingSceneNames, SceneType.Lighting, 0.5f, 1f, false);
        }
コード例 #4
0
 /// <summary>
 /// Sets continuous settings to 'black' without changing any discrete features.
 /// </summary>
 public static RuntimeRenderSettings Black(RuntimeRenderSettings source)
 {
     source.AmbientEquatorColor    = Color.clear;
     source.AmbientGroundColor     = Color.clear;
     source.AmbientIntensity       = 0;
     source.AmbientLight           = Color.clear;
     source.AmbientSkyColor        = Color.clear;
     source.FogColor               = Color.clear;
     source.FogDensity             = 0;
     source.LinearFogEnd           = 0;
     source.LinearFogStart         = 0;
     source.ReflectionIntensity    = 0;
     source.SubtractiveShadowColor = Color.clear;
     return(source);
 }
コード例 #5
0
#pragma warning restore 414

        #endregion

        public bool GetLightingSceneSettings(
            string lightingSceneName,
            out SceneInfo lightingScene,
            out RuntimeLightingSettings lightingSettings,
            out RuntimeRenderSettings renderSettings,
            out RuntimeSunlightSettings sunlightSettings)
        {
            lightingSettings = default(RuntimeLightingSettings);
            renderSettings   = default(RuntimeRenderSettings);
            sunlightSettings = default(RuntimeSunlightSettings);
            lightingScene    = SceneInfo.Empty;

            for (int i = 0; i < lightingScenes.Count; i++)
            {
                if (lightingScenes[i].Name == lightingSceneName)
                {
                    lightingScene = lightingScenes[i];
                    break;
                }
            }

            if (lightingScene.IsEmpty)
            {   // If we didn't find a lighting scene, don't bother looking for a cache
                return(false);
            }

            bool foundCache = false;

            for (int i = 0; i < cachedLightingSettings.Count; i++)
            {
                CachedLightingSettings cache = cachedLightingSettings[i];
                if (cache.SceneName == lightingSceneName)
                {
                    lightingSettings = cache.LightingSettings;
                    renderSettings   = cache.RenderSettings;
                    sunlightSettings = cache.SunlightSettings;
                    foundCache       = true;
                    break;
                }
            }

            return(foundCache);
        }
コード例 #6
0
        /// <summary>
        /// Used to update the cached lighting / render settings.
        /// Since extracting them is complex and requires scene loading, I thought it best to avoid having the profile do it.
        /// </summary>
        /// <param name="sceneInfo">The scene these settings belong to.</param>
        public void SetLightingCache(SceneInfo sceneInfo, RuntimeLightingSettings lightingSettings, RuntimeRenderSettings renderSettings, RuntimeSunlightSettings sunlightSettings)
        {
            CachedLightingSettings settings = new CachedLightingSettings();

            settings.SceneName        = sceneInfo.Name;
            settings.LightingSettings = lightingSettings;
            settings.RenderSettings   = renderSettings;
            settings.SunlightSettings = sunlightSettings;
            settings.TimeStamp        = DateTime.Now;

            cachedLightingSettings.Add(settings);

            editorLightingCacheOutOfDate = false;
        }
            public void UpdateTransition(float deltaTime)
            {
                if (transitionElapsed < transitionDuration)
                {
                    transitionElapsed += deltaTime;
                    if (transitionElapsed >= transitionDuration)
                    {
                        currentLightingSettings = targetLightingSettings;
                        currentRenderSettings   = targetRenderSettings;
                        currentSunlightSettings = targetSunlightSettings;
                        ApplySettings();
                        return;
                    }
                }

                float transitionProgress = Mathf.Clamp01(transitionElapsed / transitionDuration);

                switch (transitionType)
                {
                case LightingSceneTransitionType.None:
                    break;

                case LightingSceneTransitionType.CrossFade:
                    // Just do a straightforward lerp from one setting to the other
                    currentLightingSettings = RuntimeLightingSettings.Lerp(prevLightingSettings, targetLightingSettings, transitionProgress);
                    currentRenderSettings   = RuntimeRenderSettings.Lerp(prevRenderSettings, targetRenderSettings, transitionProgress);
                    currentSunlightSettings = RuntimeSunlightSettings.Lerp(prevSunlightSettings, targetSunlightSettings, transitionProgress);
                    break;

                case LightingSceneTransitionType.FadeToBlack:
                    // If we're in the first half of our transition, fade out to black
                    if (transitionProgress < 0.5f)
                    {
                        float fadeOutProgress = transitionProgress / 0.5f;
                        currentLightingSettings = RuntimeLightingSettings.Lerp(
                            prevLightingSettings,
                            RuntimeLightingSettings.Black(prevLightingSettings),
                            fadeOutProgress);

                        currentRenderSettings = RuntimeRenderSettings.Lerp(
                            prevRenderSettings,
                            RuntimeRenderSettings.Black(prevRenderSettings),
                            fadeOutProgress);

                        currentSunlightSettings = RuntimeSunlightSettings.Lerp(
                            prevSunlightSettings,
                            RuntimeSunlightSettings.Black(prevSunlightSettings),
                            fadeOutProgress);
                    }
                    else
                    {
                        // If we're in the second half, fade in from black
                        float fadeInProgress = (transitionProgress - 0.5f) / 0.5f;
                        currentLightingSettings = RuntimeLightingSettings.Lerp(
                            RuntimeLightingSettings.Black(targetLightingSettings),
                            targetLightingSettings,
                            fadeInProgress);

                        currentRenderSettings = RuntimeRenderSettings.Lerp(
                            RuntimeRenderSettings.Black(targetRenderSettings),
                            targetRenderSettings,
                            fadeInProgress);

                        currentSunlightSettings = RuntimeSunlightSettings.Lerp(
                            RuntimeSunlightSettings.Black(targetSunlightSettings),
                            targetSunlightSettings,
                            fadeInProgress);
                    }
                    break;
                }

                ApplySettings();
            }
コード例 #8
0
        /// <summary>
        /// Loads all lighting scenes, extracts their lighting data, then caches that data in the profile.
        /// </summary>
        private async Task EditorUpdateCachedLighting()
        {
            // Clear out our lighting cache
            profile.ClearLightingCache();
            profile.EditorLightingCacheUpdateRequested = false;

            SceneInfo defaultLightingScene = profile.DefaultLightingScene;

            foreach (SceneInfo lightingScene in profile.LightingScenes)
            {
                // Load all our lighting scenes
                Scene scene;
                EditorSceneUtils.LoadScene(lightingScene, false, out scene);
            }

            // Wait for a moment so all loaded scenes have time to get set up
            await Task.Delay(100);

            foreach (SceneInfo lightingScene in profile.LightingScenes)
            {
                Scene scene;
                EditorSceneUtils.GetSceneIfLoaded(lightingScene, out scene);
                EditorSceneUtils.SetActiveScene(scene);

                SerializedObject lightingSettingsObject;
                SerializedObject renderSettingsObject;
                EditorSceneUtils.GetLightingAndRenderSettings(out lightingSettingsObject, out renderSettingsObject);

                // Copy the serialized objects into new structs
                RuntimeLightingSettings lightingSettings = default(RuntimeLightingSettings);
                RuntimeRenderSettings   renderSettings   = default(RuntimeRenderSettings);
                RuntimeSunlightSettings sunlightSettings = default(RuntimeSunlightSettings);

                lightingSettings = SerializedObjectUtils.CopySerializedObjectToStruct <RuntimeLightingSettings>(lightingSettingsObject, lightingSettings, "m_");
                renderSettings   = SerializedObjectUtils.CopySerializedObjectToStruct <RuntimeRenderSettings>(renderSettingsObject, renderSettings, "m_");

                // Extract sunlight settings based on sunlight object
                SerializedProperty sunProperty = renderSettingsObject.FindProperty("m_Sun");
                if (sunProperty == null)
                {
                    Debug.LogError("Sun settings may not be available in this version of Unity.");
                }
                else
                {
                    Light sunLight = (Light)sunProperty.objectReferenceValue;

                    if (sunLight != null)
                    {
                        sunlightSettings.UseSunlight = true;
                        sunlightSettings.Color       = sunLight.color;
                        sunlightSettings.Intensity   = sunLight.intensity;

                        Vector3 eulerAngles = sunLight.transform.eulerAngles;
                        sunlightSettings.XRotation = eulerAngles.x;
                        sunlightSettings.YRotation = eulerAngles.y;
                        sunlightSettings.ZRotation = eulerAngles.z;
                    }
                }

                profile.SetLightingCache(lightingScene, lightingSettings, renderSettings, sunlightSettings);
            }
        }