From 420eab26d3a57967c1debcd864a5a5cad93bb48b Mon Sep 17 00:00:00 2001 From: JonnyOThan Date: Sun, 13 Aug 2023 02:40:14 -0400 Subject: [PATCH] Fix #41 and #42: disable DOF and TAA on the scaled space camera --- .../TUFX/Addon/TexturesUnlimitedFXLoader.cs | 49 +++++++++++++++---- Plugin/TUFX/TUFXProfile.cs | 2 +- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Plugin/TUFX/Addon/TexturesUnlimitedFXLoader.cs b/Plugin/TUFX/Addon/TexturesUnlimitedFXLoader.cs index 17bf8f2..2887f3f 100644 --- a/Plugin/TUFX/Addon/TexturesUnlimitedFXLoader.cs +++ b/Plugin/TUFX/Addon/TexturesUnlimitedFXLoader.cs @@ -49,7 +49,8 @@ public class Configuration internal static readonly Configuration defaultConfiguration = new Configuration(); - private PostProcessVolume volume; + private PostProcessVolume mainVolume; + private PostProcessVolume scaledSpaceVolume; /// /// The currently active profile. Private field to enforce use of the 'setProfileForScene' method. @@ -70,6 +71,16 @@ public class Configuration /// public static PostProcessResources Resources { get; private set; } + private PostProcessVolume CreateVolume(int layer) + { + var childObject = new GameObject(); + childObject.layer = layer; + childObject.transform.SetParent(transform, false); + var volume = childObject.AddComponent(); + volume.isGlobal = true; + return volume; + } + public void Start() { MonoBehaviour.print("TUFXLoader - Start()"); @@ -78,10 +89,9 @@ public void Start() GameEvents.onLevelWasLoaded.Add(new EventData.OnEvent(onLevelLoaded)); GameEvents.OnCameraChange.Add(new EventData.OnEvent(cameraChange)); - volume = gameObject.AddComponent(); - volume.isGlobal = true; - volume.priority = 100; - } + mainVolume = CreateVolume(0); + scaledSpaceVolume = CreateVolume(1); + } public void ModuleManagerPostLoad() { @@ -528,20 +538,39 @@ internal void enableProfileForCurrentScene() SetCurrentProfile(profileName); } - private void ApplyProfileToCamera(Camera camera, TUFXProfile profile) + private void ApplyProfileToCamera(Camera camera, TUFXProfile tufxProfile) { if (camera == null) return; - camera.allowHDR = profile.HDREnabled; var layer = camera.gameObject.AddOrGetComponent(); layer.Init(Resources); - layer.volumeLayer = ~0; // is this necessary? - layer.antialiasingMode = profile.AntiAliasing; + camera.allowHDR = tufxProfile.HDREnabled; + + // if this is the scaled camera, don't allow TAA because it makes clouds flicker + if (camera == ScaledCamera.Instance?.cam) + { + layer.antialiasingMode = tufxProfile.AntiAliasing == PostProcessLayer.Antialiasing.TemporalAntialiasing ? PostProcessLayer.Antialiasing.None : tufxProfile.AntiAliasing; + layer.volumeLayer = 1 << scaledSpaceVolume.gameObject.layer; + } + else + { + layer.antialiasingMode = tufxProfile.AntiAliasing; + layer.volumeLayer = 1 << mainVolume.gameObject.layer; + } } private void ApplyCurrentProfile() { - volume.sharedProfile = currentProfile.GetPostProcessProfile(); + mainVolume.sharedProfile = currentProfile.CreatePostProcessProfile(); + // clear the copied profile and reset the sharedProfile so we can make a new copy + scaledSpaceVolume.profile = null; + scaledSpaceVolume.sharedProfile = mainVolume.sharedProfile; + + // disallow DoF for the scaledspace camera (note using the `profile` property will clone the sharedProfile into a new copy that we can modify individually) + if (scaledSpaceVolume.profile.TryGetSettings(out var dofSettings)) + { + dofSettings.enabled.Override(false); + } if (HighLogic.LoadedScene == GameScenes.MAINMENU || HighLogic.LoadedScene == GameScenes.SPACECENTER) { diff --git a/Plugin/TUFX/TUFXProfile.cs b/Plugin/TUFX/TUFXProfile.cs index 88c3ff2..4d87fd4 100644 --- a/Plugin/TUFX/TUFXProfile.cs +++ b/Plugin/TUFX/TUFXProfile.cs @@ -217,7 +217,7 @@ public void Enable(PostProcessVolume volume) /// /// Returns a Unity PostProcessProfile instance with the settings contained in this TUFXProfile /// - public PostProcessProfile GetPostProcessProfile() + public PostProcessProfile CreatePostProcessProfile() { PostProcessProfile profile = ScriptableObject.CreateInstance(); int len = Settings.Count;