/// <summary> /// Sets continuous settings to 'black' without changing any discrete features. /// </summary> public static RuntimeLightingSettings Black(RuntimeLightingSettings source) { source.AlbedoBoost = 0; source.BounceScale = 0; source.IndirectOutputScale = 0; return(source); }
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; }
/// <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); }
/// <summary> /// Lerps between two settings /// </summary> /// <param name="t">Value from 0 to 1</param> public static RuntimeLightingSettings Lerp(RuntimeLightingSettings from, RuntimeLightingSettings to, float t) { bool notStarted = t <= 0; to.AlbedoBoost = Mathf.Lerp(from.AlbedoBoost, to.AlbedoBoost, t); to.BounceScale = Mathf.Lerp(from.BounceScale, to.BounceScale, t); to.EnableBakedLightmaps = notStarted ? from.EnableBakedLightmaps : to.EnableBakedLightmaps; to.EnabledRealtimeLightmaps = notStarted ? from.EnabledRealtimeLightmaps : to.EnabledRealtimeLightmaps; to.EnvironmentLightingMode = notStarted ? from.EnvironmentLightingMode : to.EnvironmentLightingMode; to.IndirectOutputScale = Mathf.Lerp(from.IndirectOutputScale, to.IndirectOutputScale, t); return(to); }
/// <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; }
#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); }
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(); }
/// <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); } }