Files
GoakeFlax/Source/Game/Console/EngineSubsystem.cs

640 lines
21 KiB
C#

using System;
using System.IO;
using System.Linq;
using FidelityFX;
#if COMPILE_WITH_DLSS
using NVIDIA;
#endif
using FlaxEditor.Content.Settings;
using FlaxEngine;
namespace Game
{
public enum UpscalingMode
{
None,
DLSS,
FSR1,
}
// Holds Console variables and commands to control engine behaviour
public static class EngineSubsystem
{
private static FSR _fsrPlugin;
public static FSR FsrPlugin
{
get
{
if (_fsrPlugin == null)
_fsrPlugin = PluginManager.GetPlugin<FSR>();
return _fsrPlugin;
}
}
#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();
#else
if (false) { }
#endif
else if (FsrPlugin.PostFx?.Enabled ?? false)
return ((int)UpscalingMode.FSR1).ToString();
else
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 (FsrPlugin.PostFx != null)
FsrPlugin.PostFx.Enabled = upscaling == UpscalingMode.FSR1;
}
}
}
#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
}
}
}
[ConsoleVariable("r_fsr_sharpness")]
public static string FsrSharpness
{
get
{
// In shader, the value of 0 is the max sharpness...
float sharpness = 2.0f - FsrPlugin.PostFx.Sharpness;
return sharpness.ToString();
}
set
{
if (float.TryParse(value, out float valueFloat))
{
valueFloat = Mathf.Clamp(valueFloat, 0f, 2.0f);
FsrPlugin.PostFx.Sharpness = 2.0f - valueFloat;
}
}
}
[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 = value;
//GlobalIllumination = value;
}
}
[ConsoleVariable("r_ambientocclusion")]
public static string AmbientOcclusion
{
get
{
return 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;
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
AmbientOcclusionSettings aoSettings = postProcessSettings.AmbientOcclusion;
/*aoSettings.OverrideFlags = (aoSettings.OverrideFlags & ~AmbientOcclusionSettingsOverride.Enabled) |
(boolValue
? AmbientOcclusionSettingsOverride.Enabled
: 0 & AmbientOcclusionSettingsOverride.Enabled);
*/
aoSettings.Enabled = boolValue;
postProcessSettings.AmbientOcclusion = aoSettings;
}
}
[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;
}
}
[ConsoleVariable("r_gi")]
public static string GlobalIllumination
{
get
{
return 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;
PostProcessSettings postProcessSettings = Graphics.PostProcessSettings;
GlobalIlluminationSettings giSettings = postProcessSettings.GlobalIllumination;
giSettings.Mode = boolValue ? 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("playdemo")]
public static void PlayDemoCommand(string[] text)
{
if (text.Length < 1)
return;
string demoName = text[0];
if (!demoName.EndsWith(".gdem"))
demoName += ".gdem";
PlayerActor playerActor = Level.GetActors<PlayerActor>().First(/*x =>
x.GetScript<PlayerMovement>().PlayerId == NetworkManager.LocalPlayerClientId*/);
string demoPath = Path.Combine(AssetManager.DemoPath, demoName);
if (File.Exists(demoPath))
playerActor.GetScript<PlayerMovement>().SetInput(demoPath);
}
private static Action timeDemoUpdate = null;
private static void TimeDemoOnUpdate()
{
if (timeDemoUpdate != null)
timeDemoUpdate();
}
[ConsoleCommand("timedemo")]
public static void TimeDemoCommand(string[] text)
{
if (text.Length < 1)
return;
string demoName = text[0];
if (!demoName.EndsWith(".gdem"))
demoName += ".gdem";
PlayerActor playerActor = Level.GetActors<PlayerActor>().First(/*x =>
x.GetScript<PlayerMovement>().PlayerId == NetworkManager.LocalPlayerClientId*/);
var playerMovement = playerActor.GetScript<PlayerMovement>();
string demoPath = Path.Combine(AssetManager.DemoPath, demoName);
if (File.Exists(demoPath))
playerMovement.SetInput(demoPath);
float accumTime = 0f;
int accumTimes = 0;
timeDemoUpdate = () =>
{
if (playerMovement)
{
var input = playerMovement.input as PlayerInputDemo;
if (input != null)
{
if (!input.IsPlaying)
{
Console.Print($"demo ended, time: {accumTimes} frames, avg: {(accumTime/(float)accumTimes)*1000.0f}");
timeDemoUpdate = null;
return;
}
if (accumTimes == 0)
Console.Print($"timedemo started");
accumTime += Time.DeltaTime;
accumTimes++;
}
}
};
Scripting.Update -= TimeDemoOnUpdate;
Scripting.Update += TimeDemoOnUpdate;
}
[ConsoleCommand("map")]
public static void MapCommand()
{
//NetworkManager.StartServer(true);
GameMode.StartServer(true);
}
[ConsoleCommand("connect")]
public static void ConnectCommand()
{
GameMode.Connect();
}
[ConsoleSubsystemInitializer]
public static void Initialize()
{
}
[ConsoleCommand("quit", "exit")]
public static void ExitCommand()
{
Engine.RequestExit();
}
[ConsoleCommand("debuglog")]
public static void DebugLogCommand(string[] text)
{
Debug.Log(string.Join(" ", text));
}
}
}