Skip to content

Commit

Permalink
Fix #41 and #42: disable DOF and TAA on the scaled space camera
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyOThan committed Aug 13, 2023
1 parent 67e075b commit 420eab2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
49 changes: 39 additions & 10 deletions Plugin/TUFX/Addon/TexturesUnlimitedFXLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public class Configuration

internal static readonly Configuration defaultConfiguration = new Configuration();

private PostProcessVolume volume;
private PostProcessVolume mainVolume;
private PostProcessVolume scaledSpaceVolume;

/// <summary>
/// The currently active profile. Private field to enforce use of the 'setProfileForScene' method.
Expand All @@ -70,6 +71,16 @@ public class Configuration
/// </summary>
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<PostProcessVolume>();
volume.isGlobal = true;
return volume;
}

public void Start()
{
MonoBehaviour.print("TUFXLoader - Start()");
Expand All @@ -78,10 +89,9 @@ public void Start()
GameEvents.onLevelWasLoaded.Add(new EventData<GameScenes>.OnEvent(onLevelLoaded));
GameEvents.OnCameraChange.Add(new EventData<CameraManager.CameraMode>.OnEvent(cameraChange));

volume = gameObject.AddComponent<PostProcessVolume>();
volume.isGlobal = true;
volume.priority = 100;
}
mainVolume = CreateVolume(0);
scaledSpaceVolume = CreateVolume(1);
}

public void ModuleManagerPostLoad()
{
Expand Down Expand Up @@ -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<PostProcessLayer>();
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<DepthOfField>(out var dofSettings))
{
dofSettings.enabled.Override(false);
}

if (HighLogic.LoadedScene == GameScenes.MAINMENU || HighLogic.LoadedScene == GameScenes.SPACECENTER)
{
Expand Down
2 changes: 1 addition & 1 deletion Plugin/TUFX/TUFXProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void Enable(PostProcessVolume volume)
/// <summary>
/// Returns a Unity PostProcessProfile instance with the settings contained in this TUFXProfile
/// </summary>
public PostProcessProfile GetPostProcessProfile()
public PostProcessProfile CreatePostProcessProfile()
{
PostProcessProfile profile = ScriptableObject.CreateInstance<PostProcessProfile>();
int len = Settings.Count;
Expand Down

0 comments on commit 420eab2

Please sign in to comment.