Files
GoakeFlax/Source/Game/Console/Commands/EngineConsoleCommands.cs
2024-04-06 16:53:29 +03:00

586 lines
17 KiB
C#

using System;
using System.IO;
using System.Linq;
#if COMPILE_WITH_FSR1
using FidelityFX;
#endif
#if COMPILE_WITH_DLSS
using NVIDIA;
#endif
using FlaxEngine;
using FlaxEngine.Networking;
namespace Game;
public enum UpscalingMode
{
None,
FSR2,
DLSS,
FSR1,
}
// Holds Console variables and commands to control engine behaviour
public static class EngineConsoleCommands
{
[ConsoleSubsystemInitializer]
public static void Initialize()
{
}
#if COMPILE_WITH_FSR1
private static FSR _fsrPlugin;
public static FSR FsrPlugin
{
get
{
if (_fsrPlugin == null)
_fsrPlugin = PluginManager.GetPlugin<FSR>();
return _fsrPlugin;
}
}
#endif
#if COMPILE_WITH_DLSS
private static DLSS _dlssPlugin;
public static DLSS DlssPlugin
{
get
{
if (_dlssPlugin == null)
_dlssPlugin = PluginManager.GetPlugin<DLSS>();
return _dlssPlugin;
}
}
#endif
// TODO: this should manually set all postprocessing values to 0 or disabled
/*[ConsoleVariable("r_postprocessing")]
public static string PostProcessing
{
get
{
PostFxVolume postFx = Level.FindActor<PostFxVolume>();
if (postFx != null)
return postFx.CameraArtifacts.OverrideFlags.ToString();
return "";
}
set
{
bool valueBoolean = false;
if (int.TryParse(value, out int valueInt))
valueBoolean = valueInt != 0;
else
return;
PostFxVolume postFx = Level.FindActor<PostFxVolume>();
if (postFx != null)
{
var cameraArtifacts = postFx.CameraArtifacts;
cameraArtifacts.OverrideFlags = valueBoolean ? CameraArtifactsSettingsOverride.None : CameraArtifactsSettingsOverride.All;
postFx.CameraArtifacts = cameraArtifacts;
}
}
}*/
[ConsoleVariable("r_vignette")]
public static string Vignette
{
get
{
return Graphics.PostProcessSettings.CameraArtifacts.VignetteIntensity.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.0f, 2.0f);
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
CameraArtifactsSettings cameraArtifacts = postProcessSettings.CameraArtifacts;
cameraArtifacts.VignetteIntensity = valueFloat;
postProcessSettings.CameraArtifacts = cameraArtifacts;
Graphics.PostProcessSettings = postProcessSettings;
}
}
}
[ConsoleVariable("cl_maxfps")]
public static string MaxFps
{
get => Time.UpdateFPS.ToString();
set
{
if (float.TryParse(value, out float valueFloat))
{
if (valueFloat <= 0.0f)
valueFloat = 0.0f;
else
valueFloat = Mathf.Clamp(valueFloat, 10f, 99999999999.0f);
if (Time.UpdateFPS != valueFloat)
Time.UpdateFPS = valueFloat;
if (Time.DrawFPS != valueFloat)
Time.DrawFPS = valueFloat;
}
}
}
[ConsoleVariable("r_renderscale")]
public static string RenderScale
{
get => MainRenderTask.Instance.RenderingPercentage.ToString();
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.00001f, 4.0f);
MainRenderTask.Instance.RenderingPercentage = valueFloat;
}
}
}
private static UpscalingMode GetUpscalingMode()
{
return (UpscalingMode)int.Parse(Upscaling);
}
[ConsoleVariable("r_upscaling")]
public static string Upscaling
{
get
{
#if COMPILE_WITH_DLSS
if (DlssPlugin.PostFx?.Enabled ?? false)
return ((int)UpscalingMode.DLSS).ToString();
#endif
#if COMPILE_WITH_FSR1
if (FsrPlugin.PostFx?.Enabled ?? false)
return ((int)UpscalingMode.FSR1).ToString();
#endif
return ((int)UpscalingMode.None).ToString();
}
set
{
if (int.TryParse(value, out int intValue))
{
UpscalingMode upscaling = (UpscalingMode)intValue;
#if COMPILE_WITH_DLSS
if (DlssPlugin.PostFx != null)
{
if (upscaling == UpscalingMode.DLSS)
{
if (DlssPlugin.Support == DLSSSupport.Supported)
DlssPlugin.PostFx.Enabled = true;
else
{
DlssPlugin.PostFx.Enabled = false;
Console.Print("DLSS not supported");
}
}
else
DlssPlugin.PostFx.Enabled = false;
}
#else
if (upscaling == UpscalingMode.DLSS)
{
Console.Print("DLSS not supported: compiled without COMPILE_WITH_DLSS");
upscaling = UpscalingMode.None;
}
#endif
#if COMPILE_WITH_FSR1
if (FsrPlugin.PostFx != null)
FsrPlugin.PostFx.Enabled = upscaling == UpscalingMode.FSR1;
#else
if (upscaling == UpscalingMode.FSR1)
{
Console.Print("FSR1 not supported: compiled without COMPILE_WITH_FSR1");
upscaling = UpscalingMode.None;
}
#endif
}
}
}
#if !COMPILE_WITH_DLSS
private static float dummy_DlssSharpness;
private static int dummy_DlssQuality;
#endif
[ConsoleVariable("r_dlss_sharpness")]
public static string DlssSharpness
{
get
{
#if COMPILE_WITH_DLSS
return DlssPlugin.Sharpness.ToString();
#else
return dummy_DlssSharpness.ToString();
#endif
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, -1f, 1f);
#if COMPILE_WITH_DLSS
DlssPlugin.Sharpness = valueFloat;
#else
dummy_DlssSharpness = valueFloat;
#endif
}
}
}
[ConsoleVariable("r_dlss_quality")]
public static string DlssQuality
{
get
{
#if COMPILE_WITH_DLSS
return ((int)DlssPlugin.Quality).ToString();
#else
return dummy_DlssQuality.ToString();
#endif
}
set
{
if (int.TryParse(value, out int intValue))
{
#if COMPILE_WITH_DLSS
#if USE_NETCORE
intValue = Math.Clamp(intValue, 0, (int)DLSSQuality.MAX-1);
#else
intValue = (intValue > (int)DLSSQuality.MAX-1) ? ((int)DLSSQuality.MAX-1) : (intValue < 0 ? 0 : intValue);
#endif
DlssPlugin.Quality = (DLSSQuality)intValue;
#else
//intValue = Math.Clamp(intValue, 0, 4);
intValue = (intValue > 4) ? 4 : (intValue < 0 ? 0 : intValue);
dummy_DlssQuality = intValue;
#endif
}
}
}
#if !COMPILE_WITH_FSR1
private static float dummy_Fsr1Sharpness;
#endif
[ConsoleVariable("r_fsr_sharpness")]
public static string FsrSharpness
{
get
{
// In shader, the value of 0 is the max sharpness...
#if COMPILE_WITH_FSR1
float sharpness = 2.0f - FsrPlugin.PostFx.Sharpness;
#else
float sharpness = 2.0f - dummy_Fsr1Sharpness;
#endif
return sharpness.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0f, 2.0f);
#if COMPILE_WITH_FSR1
FsrPlugin.PostFx.Sharpness = 2.0f - valueFloat;
#else
dummy_Fsr1Sharpness = 2.0f - valueFloat;
#endif
}
}
}
[ConsoleVariable("cl_showweapon")]
public static string ShowWeapon
{
get => Level.FindActor("ViewModelCamera").IsActive ? "1" : "0";
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
Level.FindActor("ViewModelCamera").IsActive = boolValue;
}
}
// Horizontal field of view of the Camera
[ConsoleVariable("fov")]
public static string CameraFov
{
get
{
float valueFloat = Level.FindActor("PlayerCamera").As<Camera>().FieldOfView;
float horizontalFov = (float)(180.0f / Math.PI *
(2 * Math.Atan(4f / 3f *
Math.Tan(Math.PI / 180.0f * valueFloat / 2.0f))));
return horizontalFov.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.01f, 360.0f);
float verticalFov = (float)(180.0f / Math.PI *
(2 * Math.Atan(3f / 4f *
Math.Tan(Math.PI / 180.0f * valueFloat / 2.0f))));
Level.FindActor("PlayerCamera").As<Camera>().FieldOfView = verticalFov;
}
}
}
[ConsoleVariable("r_lighting")]
public static string SceneLighting
{
get
{
return ((bool)AssetManager.Globals.GetValue("Scene Lighting") ? "1" : "0");
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
AssetManager.Globals.SetValue("Scene Lighting", boolValue);
AmbientOcclusion = AmbientOcclusion;
GlobalIllumination = GlobalIllumination;
//GlobalIllumination = value;
}
}
private static bool _AmbientOcclusion;
[ConsoleVariable("r_ambientocclusion")]
public static string AmbientOcclusion
{
get
{
return _AmbientOcclusion ? "1" : "0";//Graphics.PostProcessSettings.AmbientOcclusion.Enabled ? "1" : "0";
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
_AmbientOcclusion = boolValue;
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
AmbientOcclusionSettings aoSettings = postProcessSettings.AmbientOcclusion;
/*aoSettings.OverrideFlags = (aoSettings.OverrideFlags & ~AmbientOcclusionSettingsOverride.Enabled) |
(boolValue
? AmbientOcclusionSettingsOverride.Enabled
: 0 & AmbientOcclusionSettingsOverride.Enabled);*/
aoSettings.Enabled = boolValue && SceneLighting == "1";
postProcessSettings.AmbientOcclusion = aoSettings;
Graphics.PostProcessSettings = postProcessSettings;
}
}
[ConsoleVariable("r_vsync")]
public static string Vsync
{
get
{
return Graphics.UseVSync ? "1" : "0";
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
Graphics.UseVSync = boolValue;
}
}
private static bool _GlobalIllumination;
[ConsoleVariable("r_gi")]
public static string GlobalIllumination
{
get
{
return _GlobalIllumination ? "1" : "0";//Graphics.PostProcessSettings.GlobalIllumination.Mode == GlobalIlluminationMode.DDGI ? "1" : "0";
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
_GlobalIllumination = boolValue;
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
GlobalIlluminationSettings giSettings = postProcessSettings.GlobalIllumination;
giSettings.Mode = (boolValue && SceneLighting == "1") ? GlobalIlluminationMode.DDGI : GlobalIlluminationMode.None;
postProcessSettings.GlobalIllumination = giSettings;
Graphics.PostProcessSettings = postProcessSettings;
//Graphics.EnableGlobalSDF = boolValue;
}
}
[ConsoleVariable("r_gi_bounce")]
public static string GlobalIlluminationBounce
{
get
{
return Graphics.PostProcessSettings.GlobalIllumination.BounceIntensity.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.0f, 999.0f);
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
GlobalIlluminationSettings giSettings = postProcessSettings.GlobalIllumination;
giSettings.BounceIntensity = valueFloat;
postProcessSettings.GlobalIllumination = giSettings;
Graphics.PostProcessSettings = postProcessSettings;
}
}
}
/*[ConsoleVariable("r_gi_spacing")]
public static string GlobalIlluminationProbeSpacing
{
get
{
return Graphics.GIProbesSpacing.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.0f, 999.0f);
Graphics.GIProbesSpacing = valueFloat;
}
}
}*/
[ConsoleVariable("r_gi_time")]
public static string GlobalIlluminationTime
{
get
{
return Graphics.PostProcessSettings.GlobalIllumination.TemporalResponse.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.0f, 10.0f);
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
GlobalIlluminationSettings giSettings = postProcessSettings.GlobalIllumination;
giSettings.TemporalResponse = valueFloat;
postProcessSettings.GlobalIllumination = giSettings;
Graphics.PostProcessSettings = postProcessSettings;
}
}
}
[ConsoleVariable("r_gi_distance")]
public static string GlobalIlluminationDistance
{
get
{
return Graphics.PostProcessSettings.GlobalIllumination.Distance.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0.0f, 10000000.0f);
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
GlobalIlluminationSettings giSettings = postProcessSettings.GlobalIllumination;
giSettings.Distance = valueFloat;
postProcessSettings.GlobalIllumination = giSettings;
Graphics.PostProcessSettings = postProcessSettings;
}
}
}
[ConsoleVariable("r_shadows")]
public static string SceneShadows
{
get
{
return (bool)AssetManager.Globals.GetValue("Scene Shadows") ? "1" : "0";
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
AssetManager.Globals.SetValue("Scene Shadows", boolValue);
}
}
[ConsoleVariable("r_staticbatch")]
public static string StaticBatch
{
get
{
return (bool)AssetManager.Globals.GetValue("Static Batching") ? "1" : "0";
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
AssetManager.Globals.SetValue("Static Batching", boolValue);
}
}
[ConsoleCommand("quit", "exit")]
public static void ExitCommand()
{
Engine.RequestExit();
}
[ConsoleCommand("debuglog")]
public static void DebugLogCommand(string[] text)
{
Debug.Log(string.Join(" ", text));
}
}