WIP pre-reboot checkpoint: Menu Settings Graphics + live VFX res/samples + SDFGI quality
This commit is contained in:
parent
5c471d298b
commit
c6221758e9
12 changed files with 1217 additions and 113 deletions
264
client/GraphicsSettings.cs
Normal file
264
client/GraphicsSettings.cs
Normal file
|
|
@ -0,0 +1,264 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace RustToDust.Client;
|
||||
|
||||
public enum VfxResolutionScale
|
||||
{
|
||||
Half = 0,
|
||||
ThreeQuarter = 1,
|
||||
Full = 2
|
||||
}
|
||||
|
||||
public enum VolumetricSamples
|
||||
{
|
||||
Low = 0,
|
||||
Medium = 1,
|
||||
High = 2,
|
||||
Ultra = 3
|
||||
}
|
||||
|
||||
public enum SdfgiQuality
|
||||
{
|
||||
Off = 0,
|
||||
Balanced = 1,
|
||||
High = 2
|
||||
}
|
||||
|
||||
public readonly record struct SdfgiSettingsConfig(
|
||||
bool Enabled,
|
||||
float MinCellSize,
|
||||
int Cascades,
|
||||
bool HalfResolution,
|
||||
int RayCount);
|
||||
|
||||
public sealed record GraphicsSettingsData
|
||||
{
|
||||
[JsonPropertyName("vfxResolutionScale")]
|
||||
public VfxResolutionScale VfxResolution { get; init; } = VfxResolutionScale.Half;
|
||||
|
||||
[JsonPropertyName("volumetricSamples")]
|
||||
public VolumetricSamples VolumetricSamples { get; init; } = VolumetricSamples.Medium;
|
||||
|
||||
[JsonPropertyName("sdfgiQuality")]
|
||||
public SdfgiQuality SdfgiQuality { get; init; } = SdfgiQuality.Balanced;
|
||||
|
||||
[JsonPropertyName("borderlessFullscreen")]
|
||||
public bool BorderlessFullscreen { get; init; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Single persisted authority for live graphics and display settings (<c>user://graphics_settings.json</c>).
|
||||
/// Pure .NET so unit tests and headless runs share the exact same schema, defaults, and event flow.
|
||||
/// </summary>
|
||||
public sealed class GraphicsSettings
|
||||
{
|
||||
public const string DefaultFileName = "graphics_settings.json";
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter() }
|
||||
};
|
||||
|
||||
private readonly string? _storagePath;
|
||||
|
||||
public GraphicsSettings(string? storagePath = null)
|
||||
{
|
||||
_storagePath = string.IsNullOrWhiteSpace(storagePath) ? null : storagePath;
|
||||
ResetFieldsToDefaults();
|
||||
}
|
||||
|
||||
public VfxResolutionScale VfxResolution { get; private set; }
|
||||
public VolumetricSamples VolumetricSamples { get; private set; }
|
||||
public SdfgiQuality SdfgiQuality { get; private set; }
|
||||
public bool BorderlessFullscreen { get; private set; }
|
||||
|
||||
public string? StoragePath => _storagePath;
|
||||
|
||||
public event Action<GraphicsSettings>? Changed;
|
||||
|
||||
public GraphicsSettingsData ToData() => new()
|
||||
{
|
||||
VfxResolution = VfxResolution,
|
||||
VolumetricSamples = VolumetricSamples,
|
||||
SdfgiQuality = SdfgiQuality,
|
||||
BorderlessFullscreen = BorderlessFullscreen
|
||||
};
|
||||
|
||||
public static GraphicsSettings Load(string? storagePath)
|
||||
{
|
||||
var settings = new GraphicsSettings(storagePath);
|
||||
if (string.IsNullOrWhiteSpace(storagePath))
|
||||
{
|
||||
return settings;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (File.Exists(storagePath))
|
||||
{
|
||||
string json = File.ReadAllText(storagePath);
|
||||
settings.LoadFromJson(json);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
settings.ResetFieldsToDefaults();
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
public bool LoadFromJson(string? json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
ResetFieldsToDefaults();
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize<GraphicsSettingsData>(json, JsonOptions);
|
||||
if (parsed is null)
|
||||
{
|
||||
ResetFieldsToDefaults();
|
||||
return false;
|
||||
}
|
||||
|
||||
VfxResolution = Enum.IsDefined(parsed.VfxResolution)
|
||||
? parsed.VfxResolution
|
||||
: VfxResolutionScale.Half;
|
||||
VolumetricSamples = Enum.IsDefined(parsed.VolumetricSamples)
|
||||
? parsed.VolumetricSamples
|
||||
: VolumetricSamples.Medium;
|
||||
SdfgiQuality = Enum.IsDefined(parsed.SdfgiQuality)
|
||||
? parsed.SdfgiQuality
|
||||
: SdfgiQuality.Balanced;
|
||||
BorderlessFullscreen = parsed.BorderlessFullscreen;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
ResetFieldsToDefaults();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string ToJson() => JsonSerializer.Serialize(ToData(), JsonOptions);
|
||||
|
||||
public bool Save()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_storagePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string? dir = Path.GetDirectoryName(_storagePath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
string json = ToJson();
|
||||
string tempPath = _storagePath + ".tmp";
|
||||
File.WriteAllText(tempPath, json);
|
||||
File.Move(tempPath, _storagePath, overwrite: true);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Apply(
|
||||
VfxResolutionScale vfxResolution,
|
||||
VolumetricSamples volumetricSamples,
|
||||
SdfgiQuality sdfgiQuality,
|
||||
bool borderlessFullscreen,
|
||||
bool persist = true)
|
||||
{
|
||||
VfxResolutionScale safeRes = Enum.IsDefined(vfxResolution) ? vfxResolution : VfxResolutionScale.Half;
|
||||
VolumetricSamples safeSamples = Enum.IsDefined(volumetricSamples) ? volumetricSamples : VolumetricSamples.Medium;
|
||||
SdfgiQuality safeSdfgi = Enum.IsDefined(sdfgiQuality) ? sdfgiQuality : SdfgiQuality.Balanced;
|
||||
|
||||
bool changed = VfxResolution != safeRes
|
||||
|| VolumetricSamples != safeSamples
|
||||
|| SdfgiQuality != safeSdfgi
|
||||
|| BorderlessFullscreen != borderlessFullscreen;
|
||||
|
||||
if (!changed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
VfxResolution = safeRes;
|
||||
VolumetricSamples = safeSamples;
|
||||
SdfgiQuality = safeSdfgi;
|
||||
BorderlessFullscreen = borderlessFullscreen;
|
||||
|
||||
if (persist)
|
||||
{
|
||||
Save();
|
||||
}
|
||||
Changed?.Invoke(this);
|
||||
}
|
||||
|
||||
public void SetVfxResolution(VfxResolutionScale value, bool persist = true) =>
|
||||
Apply(value, VolumetricSamples, SdfgiQuality, BorderlessFullscreen, persist);
|
||||
|
||||
public void SetVolumetricSamples(VolumetricSamples value, bool persist = true) =>
|
||||
Apply(VfxResolution, value, SdfgiQuality, BorderlessFullscreen, persist);
|
||||
|
||||
public void SetSdfgiQuality(SdfgiQuality value, bool persist = true) =>
|
||||
Apply(VfxResolution, VolumetricSamples, value, BorderlessFullscreen, persist);
|
||||
|
||||
public void SetBorderlessFullscreen(bool value, bool persist = true) =>
|
||||
Apply(VfxResolution, VolumetricSamples, SdfgiQuality, value, persist);
|
||||
|
||||
public void ResetToDefaults(bool persist = true) =>
|
||||
Apply(
|
||||
VfxResolutionScale.Half,
|
||||
VolumetricSamples.Medium,
|
||||
SdfgiQuality.Balanced,
|
||||
borderlessFullscreen: true,
|
||||
persist: persist);
|
||||
|
||||
public static float GetResolutionScaleFactor(VfxResolutionScale scale) => scale switch
|
||||
{
|
||||
VfxResolutionScale.Half => 0.50f,
|
||||
VfxResolutionScale.ThreeQuarter => 0.75f,
|
||||
VfxResolutionScale.Full => 1.00f,
|
||||
_ => 0.50f
|
||||
};
|
||||
|
||||
public static (int MaxSteps, int MaxIter) GetVolumetricStepBudget(VolumetricSamples samples) => samples switch
|
||||
{
|
||||
VolumetricSamples.Low => (8, 12),
|
||||
VolumetricSamples.Medium => (10, 15),
|
||||
VolumetricSamples.High => (16, 24),
|
||||
VolumetricSamples.Ultra => (24, 36),
|
||||
_ => (10, 15)
|
||||
};
|
||||
|
||||
public static SdfgiSettingsConfig GetSdfgiConfig(SdfgiQuality quality) => quality switch
|
||||
{
|
||||
SdfgiQuality.Off => new SdfgiSettingsConfig(false, 0.8f, 3, true, 4),
|
||||
SdfgiQuality.Balanced => new SdfgiSettingsConfig(true, 0.8f, 3, true, 4),
|
||||
SdfgiQuality.High => new SdfgiSettingsConfig(true, 0.4f, 4, false, 8),
|
||||
_ => new SdfgiSettingsConfig(true, 0.8f, 3, true, 4)
|
||||
};
|
||||
|
||||
private void ResetFieldsToDefaults()
|
||||
{
|
||||
VfxResolution = VfxResolutionScale.Half;
|
||||
VolumetricSamples = VolumetricSamples.Medium;
|
||||
SdfgiQuality = SdfgiQuality.Balanced;
|
||||
BorderlessFullscreen = true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue