Parallax refactors (#7654)

This commit is contained in:
20kdc
2022-05-04 17:55:21 +01:00
committed by GitHub
parent 9bc965409a
commit 3d606f4316
24 changed files with 857 additions and 179 deletions

View File

@@ -118,7 +118,6 @@ namespace Content.Client.Entry
factory.GenerateNetIds(); factory.GenerateNetIds();
IoCManager.Resolve<IClientAdminManager>().Initialize(); IoCManager.Resolve<IClientAdminManager>().Initialize();
IoCManager.Resolve<IParallaxManager>().LoadParallax();
IoCManager.Resolve<IBaseClient>().PlayerJoinedServer += SubscribePlayerAttachmentEvents; IoCManager.Resolve<IBaseClient>().PlayerJoinedServer += SubscribePlayerAttachmentEvents;
IoCManager.Resolve<IStylesheetManager>().Initialize(); IoCManager.Resolve<IStylesheetManager>().Initialize();
IoCManager.Resolve<IScreenshotHook>().Initialize(); IoCManager.Resolve<IScreenshotHook>().Initialize();
@@ -180,6 +179,7 @@ namespace Content.Client.Entry
ContentContexts.SetupContexts(inputMan.Contexts); ContentContexts.SetupContexts(inputMan.Contexts);
IoCManager.Resolve<IGameHud>().Initialize(); IoCManager.Resolve<IGameHud>().Initialize();
IoCManager.Resolve<IParallaxManager>().LoadParallax(); // Have to do this later because prototypes are needed.
var overlayMgr = IoCManager.Resolve<IOverlayManager>(); var overlayMgr = IoCManager.Resolve<IOverlayManager>();
overlayMgr.AddOverlay(new ParallaxOverlay()); overlayMgr.AddOverlay(new ParallaxOverlay());

View File

@@ -35,6 +35,7 @@
Text="{Loc 'ui-options-vp-integer-scaling'}" Text="{Loc 'ui-options-vp-integer-scaling'}"
ToolTip="{Loc 'ui-options-vp-integer-scaling-tooltip'}" /> ToolTip="{Loc 'ui-options-vp-integer-scaling-tooltip'}" />
<CheckBox Name="ViewportLowResCheckBox" Text="{Loc 'ui-options-vp-low-res'}" /> <CheckBox Name="ViewportLowResCheckBox" Text="{Loc 'ui-options-vp-low-res'}" />
<CheckBox Name="ParallaxLowQualityCheckBox" Text="{Loc 'ui-options-parallax-low-quality'}" />
<CheckBox Name="FpsCounterCheckBox" Text="{Loc 'ui-options-fps-counter'}" /> <CheckBox Name="FpsCounterCheckBox" Text="{Loc 'ui-options-fps-counter'}" />
</BoxContainer> </BoxContainer>
<hudUi:StripeBack HasBottomEdge="False" HasMargins="False"> <hudUi:StripeBack HasBottomEdge="False" HasMargins="False">

View File

@@ -76,6 +76,7 @@ namespace Content.Client.EscapeMenu.UI.Tabs
ShowHeldItemCheckBox.OnToggled += OnCheckBoxToggled; ShowHeldItemCheckBox.OnToggled += OnCheckBoxToggled;
IntegerScalingCheckBox.OnToggled += OnCheckBoxToggled; IntegerScalingCheckBox.OnToggled += OnCheckBoxToggled;
ViewportLowResCheckBox.OnToggled += OnCheckBoxToggled; ViewportLowResCheckBox.OnToggled += OnCheckBoxToggled;
ParallaxLowQualityCheckBox.OnToggled += OnCheckBoxToggled;
FpsCounterCheckBox.OnToggled += OnCheckBoxToggled; FpsCounterCheckBox.OnToggled += OnCheckBoxToggled;
ApplyButton.OnPressed += OnApplyButtonPressed; ApplyButton.OnPressed += OnApplyButtonPressed;
VSyncCheckBox.Pressed = _cfg.GetCVar(CVars.DisplayVSync); VSyncCheckBox.Pressed = _cfg.GetCVar(CVars.DisplayVSync);
@@ -87,6 +88,7 @@ namespace Content.Client.EscapeMenu.UI.Tabs
ViewportStretchCheckBox.Pressed = _cfg.GetCVar(CCVars.ViewportStretch); ViewportStretchCheckBox.Pressed = _cfg.GetCVar(CCVars.ViewportStretch);
IntegerScalingCheckBox.Pressed = _cfg.GetCVar(CCVars.ViewportSnapToleranceMargin) != 0; IntegerScalingCheckBox.Pressed = _cfg.GetCVar(CCVars.ViewportSnapToleranceMargin) != 0;
ViewportLowResCheckBox.Pressed = !_cfg.GetCVar(CCVars.ViewportScaleRender); ViewportLowResCheckBox.Pressed = !_cfg.GetCVar(CCVars.ViewportScaleRender);
ParallaxLowQualityCheckBox.Pressed = _cfg.GetCVar(CCVars.ParallaxLowQuality);
FpsCounterCheckBox.Pressed = _cfg.GetCVar(CCVars.HudFpsCounterVisible); FpsCounterCheckBox.Pressed = _cfg.GetCVar(CCVars.HudFpsCounterVisible);
ShowHeldItemCheckBox.Pressed = _cfg.GetCVar(CCVars.HudHeldItemShow); ShowHeldItemCheckBox.Pressed = _cfg.GetCVar(CCVars.HudHeldItemShow);
@@ -123,6 +125,7 @@ namespace Content.Client.EscapeMenu.UI.Tabs
_cfg.SetCVar(CCVars.ViewportSnapToleranceMargin, _cfg.SetCVar(CCVars.ViewportSnapToleranceMargin,
IntegerScalingCheckBox.Pressed ? CCVars.ViewportSnapToleranceMargin.DefaultValue : 0); IntegerScalingCheckBox.Pressed ? CCVars.ViewportSnapToleranceMargin.DefaultValue : 0);
_cfg.SetCVar(CCVars.ViewportScaleRender, !ViewportLowResCheckBox.Pressed); _cfg.SetCVar(CCVars.ViewportScaleRender, !ViewportLowResCheckBox.Pressed);
_cfg.SetCVar(CCVars.ParallaxLowQuality, ParallaxLowQualityCheckBox.Pressed);
_cfg.SetCVar(CCVars.HudHeldItemShow, ShowHeldItemCheckBox.Pressed); _cfg.SetCVar(CCVars.HudHeldItemShow, ShowHeldItemCheckBox.Pressed);
_cfg.SetCVar(CCVars.HudFpsCounterVisible, FpsCounterCheckBox.Pressed); _cfg.SetCVar(CCVars.HudFpsCounterVisible, FpsCounterCheckBox.Pressed);
_cfg.SaveToFile(); _cfg.SaveToFile();
@@ -151,6 +154,7 @@ namespace Content.Client.EscapeMenu.UI.Tabs
var isVPScaleSame = (int) ViewportScaleSlider.Value == _cfg.GetCVar(CCVars.ViewportFixedScaleFactor); var isVPScaleSame = (int) ViewportScaleSlider.Value == _cfg.GetCVar(CCVars.ViewportFixedScaleFactor);
var isIntegerScalingSame = IntegerScalingCheckBox.Pressed == (_cfg.GetCVar(CCVars.ViewportSnapToleranceMargin) != 0); var isIntegerScalingSame = IntegerScalingCheckBox.Pressed == (_cfg.GetCVar(CCVars.ViewportSnapToleranceMargin) != 0);
var isVPResSame = ViewportLowResCheckBox.Pressed == !_cfg.GetCVar(CCVars.ViewportScaleRender); var isVPResSame = ViewportLowResCheckBox.Pressed == !_cfg.GetCVar(CCVars.ViewportScaleRender);
var isPLQSame = ParallaxLowQualityCheckBox.Pressed == _cfg.GetCVar(CCVars.ParallaxLowQuality);
var isShowHeldItemSame = ShowHeldItemCheckBox.Pressed == _cfg.GetCVar(CCVars.HudHeldItemShow); var isShowHeldItemSame = ShowHeldItemCheckBox.Pressed == _cfg.GetCVar(CCVars.HudHeldItemShow);
var isFpsCounterVisibleSame = FpsCounterCheckBox.Pressed == _cfg.GetCVar(CCVars.HudFpsCounterVisible); var isFpsCounterVisibleSame = FpsCounterCheckBox.Pressed == _cfg.GetCVar(CCVars.HudFpsCounterVisible);
@@ -162,6 +166,7 @@ namespace Content.Client.EscapeMenu.UI.Tabs
isVPScaleSame && isVPScaleSame &&
isIntegerScalingSame && isIntegerScalingSame &&
isVPResSame && isVPResSame &&
isPLQSame &&
isHudThemeSame && isHudThemeSame &&
isShowHeldItemSame && isShowHeldItemSame &&
isFpsCounterVisibleSame; isFpsCounterVisibleSame;

View File

@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using JetBrains.Annotations;
using Nett;
using Content.Shared;
using Content.Shared.CCVar;
using Content.Client.Resources;
using Content.Client.IoC;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace Content.Client.Parallax.Data;
[UsedImplicitly]
[DataDefinition]
public sealed class GeneratedParallaxTextureSource : IParallaxTextureSource
{
/// <summary>
/// Parallax config path (the TOML file).
/// In client resources.
/// </summary>
[DataField("configPath")]
public ResourcePath ParallaxConfigPath { get; } = new("/parallax_config.toml");
/// <summary>
/// ID for debugging, caching, and so forth.
/// The empty string here is reserved for the original parallax.
/// It is advisible to provide a roughly unique ID for any unique config contents.
/// </summary>
[DataField("id")]
public string Identifier { get; } = "other";
/// <summary>
/// Cached path.
/// In user directory.
/// </summary>
private ResourcePath ParallaxCachedImagePath => new($"/parallax_{Identifier}cache.png");
/// <summary>
/// Old parallax config path (for checking for parallax updates).
/// In user directory.
/// </summary>
private ResourcePath PreviousParallaxConfigPath => new($"/parallax_{Identifier}config_old");
async Task<Texture> IParallaxTextureSource.GenerateTexture(CancellationToken cancel = default)
{
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
{
Logger.ErrorS("parallax", $"Parallax config not found or unreadable: {ParallaxConfigPath}");
// The show must go on.
return Texture.Transparent;
}
var debugParallax = IoCManager.Resolve<IConfigurationManager>().GetCVar(CCVars.ParallaxDebug);
if (debugParallax
|| !StaticIoC.ResC.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{
var table = Toml.ReadString(parallaxConfig);
await UpdateCachedTexture(table, debugParallax, cancel);
//Update the previous config
using var writer = StaticIoC.ResC.UserData.OpenWriteText(PreviousParallaxConfigPath);
writer.Write(parallaxConfig);
}
try
{
return GetCachedTexture();
}
catch (Exception ex)
{
Logger.ErrorS("parallax", $"Couldn't retrieve parallax cached texture: {ex}");
// The show must go on.
try
{
// Also try to at least sort of fix this if we've been fooled by a config backup
StaticIoC.ResC.UserData.Delete(PreviousParallaxConfigPath);
}
catch (Exception)
{
}
return Texture.Transparent;
}
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers, CancellationToken cancel = default)
{
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
var sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("parallax");
// Generate the parallax in the thread pool.
using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages, cancel), cancel);
// And load it in the main thread for safety reasons.
// But before spending time saving it, make sure to exit out early if it's not wanted.
cancel.ThrowIfCancellationRequested();
// Store it and CRC so further game starts don't need to regenerate it.
using var imageStream = StaticIoC.ResC.UserData.OpenWrite(ParallaxCachedImagePath);
newParallexImage.SaveAsPng(imageStream);
if (saveDebugLayers)
{
for (var i = 0; i < debugImages!.Count; i++)
{
var debugImage = debugImages[i];
using var debugImageStream = StaticIoC.ResC.UserData.OpenWrite(new ResourcePath($"/parallax_{Identifier}debug_{i}.png"));
debugImage.SaveAsPng(debugImageStream);
}
}
}
private Texture GetCachedTexture()
{
using var imageStream = StaticIoC.ResC.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
}
private string? GetParallaxConfig()
{
if (!StaticIoC.ResC.TryContentFileRead(ParallaxConfigPath, out var configStream))
{
return null;
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
}
}

View File

@@ -0,0 +1,19 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Client.Graphics;
namespace Content.Client.Parallax.Data
{
[ImplicitDataDefinitionForInheritors]
public interface IParallaxTextureSource
{
/// <summary>
/// Generates or loads the texture.
/// Note that this should be cached, but not necessarily *here*.
/// </summary>
Task<Texture> GenerateTexture(CancellationToken cancel = default);
}
}

View File

@@ -0,0 +1,29 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using JetBrains.Annotations;
using Content.Client.Resources;
using Content.Client.IoC;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Client.Parallax.Data;
[UsedImplicitly]
[DataDefinition]
public sealed class ImageParallaxTextureSource : IParallaxTextureSource
{
/// <summary>
/// Texture path.
/// </summary>
[DataField("path", required: true)]
public ResourcePath Path { get; } = default!;
async Task<Texture> IParallaxTextureSource.GenerateTexture(CancellationToken cancel = default)
{
return StaticIoC.ResC.GetTexture(Path);
}
}

View File

@@ -0,0 +1,66 @@
using System;
using Robust.Client.Graphics;
using Content.Client.Parallax.Data;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Client.Parallax.Data;
/// <summary>
/// The configuration for a parallax layer.
/// </summary>
[DataDefinition]
public sealed class ParallaxLayerConfig
{
/// <summary>
/// The texture source for this layer.
/// </summary>
[DataField("texture", required: true)]
public IParallaxTextureSource Texture { get; set; } = default!;
/// <summary>
/// A scaling factor for the texture.
/// In the interest of simplifying maths, this is rounded down to integer for ParallaxControl, so be careful.
/// </summary>
[DataField("scale")]
public Vector2 Scale { get; set; } = Vector2.One;
/// <summary>
/// If true, this layer is tiled as the camera scrolls around.
/// If false, this layer only shows up around it's home position.
/// </summary>
[DataField("tiled")]
public bool Tiled { get; set; } = true;
/// <summary>
/// A position relative to the centre of a ParallaxControl that this parallax should be drawn at, in pixels.
/// Used for menus.
/// Note that this is ignored if the parallax layer is tiled - in that event a random pixel offset is used and slowness is applied.
/// </summary>
[DataField("controlHomePosition")]
public Vector2 ControlHomePosition { get; set; }
/// <summary>
/// The "relative to ParallaxAnchor" starting world position for this layer.
/// Essentially, an unclamped lerp occurs between here and the eye position, with Slowness as the factor.
/// Used for in-game.
/// </summary>
[DataField("worldHomePosition")]
public Vector2 WorldHomePosition { get; set; }
/// <summary>
/// An adjustment performed to the world position of this layer after parallax shifting.
/// Used for in-game.
/// Useful for moving around Slowness = 1.0 objects (which can't otherwise be moved from screen centre).
/// </summary>
[DataField("worldAdjustPosition")]
public Vector2 WorldAdjustPosition { get; set; }
/// <summary>
/// Multiplier of parallax shift.
/// A slowness of 0.0f anchors this layer to the world.
/// A slowness of 1.0f anchors this layer to the camera.
/// </summary>
[DataField("slowness")]
public float Slowness { get; set; } = 0.5f;
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.Utility;
namespace Content.Client.Parallax.Data;
/// <summary>
/// Prototype data for a parallax.
/// </summary>
[Prototype("parallax")]
public sealed class ParallaxPrototype : IPrototype
{
/// <inheritdoc/>
[IdDataFieldAttribute]
public string ID { get; } = default!;
/// <summary>
/// Parallax layers.
/// </summary>
[DataField("layers")]
public List<ParallaxLayerConfig> Layers { get; } = new();
/// <summary>
/// Parallax layers, low-quality.
/// </summary>
[DataField("layersLQ")]
public List<ParallaxLayerConfig> LayersLQ { get; } = new();
/// <summary>
/// If low-quality layers don't exist for this parallax and high-quality should be used instead.
/// </summary>
[DataField("layersLQUseHQ")]
public bool LayersLQUseHQ { get; } = true;
}

View File

@@ -1,12 +1,35 @@
using System; using System;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Content.Client.Parallax;
namespace Content.Client.Parallax.Managers namespace Content.Client.Parallax.Managers;
public interface IParallaxManager
{ {
public interface IParallaxManager /// <summary>
{ /// The current parallax.
event Action<Texture>? OnTextureLoaded; /// Changing this causes a new parallax to be loaded (eventually).
Texture? ParallaxTexture { get; } /// Do not alter until prototype manager is available.
/// Useful "csi" input for testing new parallaxes:
/// using Content.Client.Parallax.Managers; IoCManager.Resolve<IParallaxManager>().ParallaxName = "test";
/// </summary>
string ParallaxName { get; set; }
/// <summary>
/// All WorldHomePosition values are offset by this.
/// </summary>
Vector2 ParallaxAnchor { get; set; }
/// <summary>
/// The layers of the currently loaded parallax.
/// This will change on a whim without notification.
/// </summary>
ParallaxLayerPrepared[] ParallaxLayers { get; }
/// <summary>
/// Used to initialize the manager.
/// Do not call until prototype manager is available.
/// </summary>
void LoadParallax(); void LoadParallax();
}
} }

View File

@@ -1,12 +1,16 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Client.Parallax.Data;
using Content.Shared; using Content.Shared;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Nett; using Nett;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.ResourceManagement; using Robust.Client.ResourceManagement;
using Robust.Shared.Prototypes;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.ContentPack; using Robust.Shared.ContentPack;
using Robust.Shared.IoC; using Robust.Shared.IoC;
@@ -15,91 +19,115 @@ using Robust.Shared.Utility;
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace Content.Client.Parallax.Managers namespace Content.Client.Parallax.Managers;
internal sealed class ParallaxManager : IParallaxManager
{ {
internal sealed class ParallaxManager : IParallaxManager [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!; [Dependency] private readonly IConfigurationManager _configurationManager = default!;
private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml"); private string _parallaxName = "";
public string ParallaxName
{
get => _parallaxName;
set
{
LoadParallaxByName(value);
}
}
// Both of these below are in the user directory. public Vector2 ParallaxAnchor { get; set; }
private static readonly ResourcePath ParallaxCachedImagePath = new("/parallax_cache.png");
private static readonly ResourcePath PreviousParallaxConfigPath = new("/parallax_config_old");
public event Action<Texture>? OnTextureLoaded; private CancellationTokenSource? _presentParallaxLoadCancel;
public Texture? ParallaxTexture { get; private set; }
private ParallaxLayerPrepared[] _parallaxLayersHQ = {};
private ParallaxLayerPrepared[] _parallaxLayersLQ = {};
public ParallaxLayerPrepared[] ParallaxLayers => _configurationManager.GetCVar(CCVars.ParallaxLowQuality) ? _parallaxLayersLQ : _parallaxLayersHQ;
public async void LoadParallax() public async void LoadParallax()
{ {
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled)) await LoadParallaxByName("default");
}
private async Task LoadParallaxByName(string name)
{
// Update _parallaxName
if (_parallaxName == name)
{
return; return;
}
_parallaxName = name;
var parallaxConfig = GetParallaxConfig(); // Cancel any existing load and setup the new cancellation token
if (parallaxConfig == null) _presentParallaxLoadCancel?.Cancel();
_presentParallaxLoadCancel = new CancellationTokenSource();
var cancel = _presentParallaxLoadCancel.Token;
// Empty parallax name = no layers (this is so that the initial "" parallax name is consistent)
if (_parallaxName == "")
{
_parallaxLayersHQ = _parallaxLayersLQ = new ParallaxLayerPrepared[] {};
return; return;
}
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug); // Begin (for real)
Logger.InfoS("parallax", $"Loading parallax {name}");
if (debugParallax try
|| !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{ {
var table = Toml.ReadString(parallaxConfig); var parallaxPrototype = _prototypeManager.Index<ParallaxPrototype>(name);
await UpdateCachedTexture(table, debugParallax);
//Update the previous config ParallaxLayerPrepared[] hq;
using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath); ParallaxLayerPrepared[] lq;
writer.Write(parallaxConfig);
}
ParallaxTexture = GetCachedTexture(); if (parallaxPrototype.LayersLQUseHQ)
OnTextureLoaded?.Invoke(ParallaxTexture);
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers)
{ {
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null; lq = hq = await LoadParallaxLayers(parallaxPrototype.Layers, cancel);
}
var sawmill = _logManager.GetSawmill("parallax"); else
// Generate the parallax in the thread pool.
using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages));
// And load it in the main thread for safety reasons.
// Store it and CRC so further game starts don't need to regenerate it.
using var imageStream = _resourceCache.UserData.OpenWrite(ParallaxCachedImagePath);
newParallexImage.SaveAsPng(imageStream);
if (saveDebugLayers)
{ {
for (var i = 0; i < debugImages!.Count; i++) var results = await Task.WhenAll(
{ LoadParallaxLayers(parallaxPrototype.Layers, cancel),
var debugImage = debugImages[i]; LoadParallaxLayers(parallaxPrototype.LayersLQ, cancel)
using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png")); );
debugImage.SaveAsPng(debugImageStream); hq = results[0];
lq = results[1];
} }
// Still keeping this check just in case.
if (_parallaxName == name)
{
_parallaxLayersHQ = hq;
_parallaxLayersLQ = lq;
Logger.InfoS("parallax", $"Loaded parallax {name}");
}
}
catch (Exception ex)
{
Logger.ErrorS("parallax", $"Failed to loaded parallax {name}: {ex}");
} }
} }
private Texture GetCachedTexture() private async Task<ParallaxLayerPrepared[]> LoadParallaxLayers(List<ParallaxLayerConfig> layersIn, CancellationToken cancel = default)
{ {
using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath); // Because this is async, make sure it doesn't change (prototype reloads could muck this up)
return Texture.LoadFromPNGStream(imageStream, "Parallax"); // Since the tasks aren't awaited until the end, this should be fine
var tasks = new Task<ParallaxLayerPrepared>[layersIn.Count];
for (var i = 0; i < layersIn.Count; i++)
{
tasks[i] = LoadParallaxLayer(layersIn[i], cancel);
}
return await Task.WhenAll(tasks);
} }
private string? GetParallaxConfig() private async Task<ParallaxLayerPrepared> LoadParallaxLayer(ParallaxLayerConfig config, CancellationToken cancel = default)
{ {
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream)) return new ParallaxLayerPrepared()
{ {
Logger.ErrorS("parallax", "Parallax config not found."); Texture = await config.Texture.GenerateTexture(cancel),
return null; Config = config
} };
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
}
} }
} }

View File

@@ -6,13 +6,13 @@ using Robust.Shared.Maths;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
namespace Content.Client.Parallax namespace Content.Client.Parallax;
/// <summary>
/// Renders the parallax background as a UI control.
/// </summary>
public sealed class ParallaxControl : Control
{ {
/// <summary>
/// Renders the parallax background as a UI control.
/// </summary>
public sealed class ParallaxControl : Control
{
[Dependency] private readonly IParallaxManager _parallaxManager = default!; [Dependency] private readonly IParallaxManager _parallaxManager = default!;
[Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly IRobustRandom _random = default!;
@@ -28,20 +28,38 @@ namespace Content.Client.Parallax
protected override void Draw(DrawingHandleScreen handle) protected override void Draw(DrawingHandleScreen handle)
{ {
var tex = _parallaxManager.ParallaxTexture; foreach (var layer in _parallaxManager.ParallaxLayers)
if (tex == null) {
return; var tex = layer.Texture;
var texSize = tex.Size * layer.Config.Scale.Floored();
var size = tex.Size;
var ourSize = PixelSize; var ourSize = PixelSize;
for (var x = -size.X + Offset.X; x < ourSize.X; x += size.X) if (layer.Config.Tiled)
{ {
for (var y = -size.Y + Offset.Y; y < ourSize.Y; y += size.Y) // Multiply offset by slowness to match normal parallax
var scaledOffset = (Offset * layer.Config.Slowness).Floored();
// Then modulo the scaled offset by the size to prevent drawing a bunch of offscreen tiles for really small images.
scaledOffset.X %= texSize.X;
scaledOffset.Y %= texSize.Y;
// Note: scaledOffset must never be below 0 or there will be visual issues.
// It could be allowed to be >= texSize on a given axis but that would be wasteful.
for (var x = -scaledOffset.X; x < ourSize.X; x += texSize.X)
{ {
handle.DrawTexture(tex, (x, y)); for (var y = -scaledOffset.Y; y < ourSize.Y; y += texSize.Y)
{
handle.DrawTextureRect(tex, UIBox2.FromDimensions((x, y), texSize));
} }
} }
} }
else
{
var origin = ((ourSize - texSize) / 2) + layer.Config.ControlHomePosition;
handle.DrawTextureRect(tex, UIBox2.FromDimensions(origin, texSize));
}
}
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
@@ -18,7 +19,7 @@ namespace Content.Client.Parallax
{ {
private readonly List<Layer> Layers = new(); private readonly List<Layer> Layers = new();
public static Image<Rgba32> GenerateParallax(TomlTable config, Size size, ISawmill sawmill, List<Image<Rgba32>>? debugLayerDump) public static Image<Rgba32> GenerateParallax(TomlTable config, Size size, ISawmill sawmill, List<Image<Rgba32>>? debugLayerDump, CancellationToken cancel = default)
{ {
sawmill.Debug("Generating parallax!"); sawmill.Debug("Generating parallax!");
var generator = new ParallaxGenerator(); var generator = new ParallaxGenerator();
@@ -27,10 +28,11 @@ namespace Content.Client.Parallax
sawmill.Debug("Timing start!"); sawmill.Debug("Timing start!");
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
var image = new Image<Rgba32>(Configuration.Default, size.Width, size.Height, new Rgba32(0, 0, 0, 255)); var image = new Image<Rgba32>(Configuration.Default, size.Width, size.Height, new Rgba32(0, 0, 0, 0));
var count = 0; var count = 0;
foreach (var layer in generator.Layers) foreach (var layer in generator.Layers)
{ {
cancel.ThrowIfCancellationRequested();
layer.Apply(image); layer.Apply(image);
debugLayerDump?.Add(image.Clone()); debugLayerDump?.Add(image.Clone());
sawmill.Debug("Layer {0} done!", count++); sawmill.Debug("Layer {0} done!", count++);
@@ -48,6 +50,16 @@ namespace Content.Client.Parallax
{ {
switch (((TomlValue<string>) layerArray.Get("type")).Value) switch (((TomlValue<string>) layerArray.Get("type")).Value)
{ {
case "clear":
var layerClear = new LayerClear(layerArray);
Layers.Add(layerClear);
break;
case "toalpha":
var layerToAlpha = new LayerToAlpha(layerArray);
Layers.Add(layerToAlpha);
break;
case "noise": case "noise":
var layerNoise = new LayerNoise(layerArray); var layerNoise = new LayerNoise(layerArray);
Layers.Add(layerNoise); Layers.Add(layerNoise);
@@ -69,6 +81,52 @@ namespace Content.Client.Parallax
public abstract void Apply(Image<Rgba32> bitmap); public abstract void Apply(Image<Rgba32> bitmap);
} }
private abstract class LayerConversion : Layer
{
public abstract Color ConvertColor(Color input);
public override void Apply(Image<Rgba32> bitmap)
{
var span = bitmap.GetPixelSpan();
for (var y = 0; y < bitmap.Height; y++)
{
for (var x = 0; x < bitmap.Width; x++)
{
var i = y * bitmap.Width + x;
span[i] = ConvertColor(span[i].ConvertImgSharp()).ConvertImgSharp();
}
}
}
}
private sealed class LayerClear : LayerConversion
{
private readonly Color Color = Color.Black;
public LayerClear(TomlTable table)
{
if (table.TryGetValue("color", out var tomlObject))
{
Color = Color.FromHex(((TomlValue<string>) tomlObject).Value);
}
}
public override Color ConvertColor(Color input) => Color;
}
private sealed class LayerToAlpha : LayerConversion
{
public LayerToAlpha(TomlTable table)
{
}
public override Color ConvertColor(Color input)
{
return new Color(input.R, input.G, input.B, MathF.Min(input.R + input.G + input.B, 1.0f));
}
}
private sealed class LayerNoise : Layer private sealed class LayerNoise : Layer
{ {
private readonly Color InnerColor = Color.White; private readonly Color InnerColor = Color.White;

View File

@@ -0,0 +1,22 @@
using System;
using Robust.Client.Graphics;
using Content.Client.Parallax.Data;
namespace Content.Client.Parallax;
/// <summary>
/// A 'prepared' (i.e. texture loaded and ready to use) parallax layer.
/// </summary>
public struct ParallaxLayerPrepared
{
/// <summary>
/// The loaded texture for this layer.
/// </summary>
public Texture Texture { get; set; }
/// <summary>
/// The configuration for this layer.
/// </summary>
public ParallaxLayerConfig Config { get; set; }
}

View File

@@ -1,21 +1,20 @@
using System; using System;
using Content.Client.Parallax.Managers; using Content.Client.Parallax.Managers;
using Content.Shared.CCVar;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Shared.Configuration;
using Robust.Shared.Enums; using Robust.Shared.Enums;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Client.Parallax namespace Content.Client.Parallax;
public sealed class ParallaxOverlay : Overlay
{ {
public sealed class ParallaxOverlay : Overlay
{
[Dependency] private readonly IParallaxManager _parallaxManager = default!; [Dependency] private readonly IParallaxManager _parallaxManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
private const float Slowness = 0.5f;
private Texture? _parallaxTexture;
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowWorld; public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowWorld;
private readonly ShaderInstance _shader; private readonly ShaderInstance _shader;
@@ -24,20 +23,16 @@ namespace Content.Client.Parallax
{ {
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
_shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance(); _shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
if (_parallaxManager.ParallaxTexture == null)
{
_parallaxManager.OnTextureLoaded += texture => _parallaxTexture = texture;
}
else
{
_parallaxTexture = _parallaxManager.ParallaxTexture;
}
} }
protected override void Draw(in OverlayDrawArgs args) protected override void Draw(in OverlayDrawArgs args)
{ {
if (_parallaxTexture == null || args.Viewport.Eye == null) if (args.Viewport.Eye == null)
{
return;
}
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
{ {
return; return;
} }
@@ -45,28 +40,56 @@ namespace Content.Client.Parallax
var screenHandle = args.WorldHandle; var screenHandle = args.WorldHandle;
screenHandle.UseShader(_shader); screenHandle.UseShader(_shader);
var (sizeX, sizeY) = _parallaxTexture.Size / (float) EyeManager.PixelsPerMeter; foreach (var layer in _parallaxManager.ParallaxLayers)
var (posX, posY) = args.Viewport.Eye.Position; {
var o = new Vector2(posX * Slowness, posY * Slowness); var tex = layer.Texture;
// Size of the texture in world units.
var size = (tex.Size / (float) EyeManager.PixelsPerMeter) * layer.Config.Scale;
// The "home" position is the effective origin of this layer.
// Parallax shifting is relative to the home, and shifts away from the home and towards the Eye centre.
// The effects of this are such that a slowness of 1 anchors the layer to the centre of the screen, while a slowness of 0 anchors the layer to the world.
// (For values 0.0 to 1.0 this is in effect a lerp, but it's deliberately unclamped.)
// The ParallaxAnchor adapts the parallax for station positioning and possibly map-specific tweaks.
var home = layer.Config.WorldHomePosition + _parallaxManager.ParallaxAnchor;
// Origin - start with the parallax shift itself.
var originBL = (args.Viewport.Eye.Position.Position - home) * layer.Config.Slowness;
// Place at the home.
originBL += home;
// Adjust.
originBL += layer.Config.WorldAdjustPosition;
// Centre the image.
originBL -= size / 2;
if (layer.Config.Tiled)
{
// Remove offset so we can floor. // Remove offset so we can floor.
var (l, b) = args.WorldAABB.BottomLeft - o; var flooredBL = args.WorldAABB.BottomLeft - originBL;
// Floor to background size. // Floor to background size.
l = sizeX * MathF.Floor(l / sizeX); flooredBL = (flooredBL / size).Floored() * size;
b = sizeY * MathF.Floor(b / sizeY);
// Re-offset. // Re-offset.
l += o.X; flooredBL += originBL;
b += o.Y;
for (var x = l; x < args.WorldAABB.Right; x += sizeX) for (var x = flooredBL.X; x < args.WorldAABB.Right; x += size.X)
{ {
for (var y = b; y < args.WorldAABB.Top; y += sizeY) for (var y = flooredBL.Y; y < args.WorldAABB.Top; y += size.Y)
{ {
screenHandle.DrawTexture(_parallaxTexture, (x, y)); screenHandle.DrawTextureRect(tex, Box2.FromDimensions((x, y), size));
} }
} }
} }
else
{
screenHandle.DrawTextureRect(tex, Box2.FromDimensions(originBL, size));
}
}
} }
} }

View File

@@ -1,25 +1,20 @@
using System; using System;
using Content.Client.Parallax.Managers; using Content.Client.Parallax.Managers;
using Content.Client.Parallax;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Shared.Maths;
namespace Content.IntegrationTests namespace Content.IntegrationTests
{ {
public sealed class DummyParallaxManager : IParallaxManager public sealed class DummyParallaxManager : IParallaxManager
{ {
public event Action<Texture> OnTextureLoaded public string ParallaxName { get; set; } = "";
{ public Vector2 ParallaxAnchor { get; set; }
add public ParallaxLayerPrepared[] ParallaxLayers { get; } = {};
{
}
remove
{
}
}
public Texture ParallaxTexture => null;
public void LoadParallax() public void LoadParallax()
{ {
ParallaxName = "default";
} }
} }
} }

View File

@@ -32,6 +32,7 @@ using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
@@ -51,6 +52,7 @@ namespace Content.Server.Entry
new[] { "Content.Client", "Content.Shared", "Content.Shared.Database" }); new[] { "Content.Client", "Content.Shared", "Content.Shared.Database" });
var factory = IoCManager.Resolve<IComponentFactory>(); var factory = IoCManager.Resolve<IComponentFactory>();
var prototypes = IoCManager.Resolve<IPrototypeManager>();
factory.DoAutoRegistrations(); factory.DoAutoRegistrations();
@@ -59,6 +61,8 @@ namespace Content.Server.Entry
factory.RegisterIgnore(ignoreName); factory.RegisterIgnore(ignoreName);
} }
prototypes.RegisterIgnore("parallax");
ServerContentIoC.Register(); ServerContentIoC.Register();
foreach (var callback in TestingCallbacks) foreach (var callback in TestingCallbacks)

View File

@@ -328,6 +328,9 @@ namespace Content.Shared.CCVar
public static readonly CVarDef<bool> ParallaxDebug = public static readonly CVarDef<bool> ParallaxDebug =
CVarDef.Create("parallax.debug", false, CVar.CLIENTONLY); CVarDef.Create("parallax.debug", false, CVar.CLIENTONLY);
public static readonly CVarDef<bool> ParallaxLowQuality =
CVarDef.Create("parallax.low_quality", false, CVar.ARCHIVE | CVar.CLIENTONLY);
/* /*
* Physics * Physics
*/ */

View File

@@ -49,6 +49,7 @@ ui-options-vp-integer-scaling-tooltip = If this option is enabled, the viewport
means that black bars appear at the top/bottom of the screen or that part means that black bars appear at the top/bottom of the screen or that part
of the viewport is not visible. of the viewport is not visible.
ui-options-vp-low-res = Low-resolution viewport ui-options-vp-low-res = Low-resolution viewport
ui-options-parallax-low-quality = Low-quality Parallax (background)
ui-options-fps-counter = Show FPS counter ui-options-fps-counter = Show FPS counter
## Controls menu ## Controls menu

View File

@@ -0,0 +1,26 @@
- type: parallax
id: default
layers:
- texture:
!type:GeneratedParallaxTextureSource
id: "hq_wizard_clouds"
configPath: "/Prototypes/Parallaxes/parallax_config_clouds.toml"
slowness: 0.5
- texture:
!type:GeneratedParallaxTextureSource
id: "hq_wizard_stars_dim"
configPath: "/Prototypes/Parallaxes/parallax_config_stars_dim.toml"
slowness: 0.375
- texture:
!type:GeneratedParallaxTextureSource
id: "hq_wizard_stars"
configPath: "/Prototypes/Parallaxes/parallax_config_stars.toml"
slowness: 0.25
layersLQ:
- texture:
!type:GeneratedParallaxTextureSource
id: ""
configPath: "/Prototypes/Parallaxes/parallax_config.toml"
slowness: 0.5
layersLQUseHQ: false

View File

@@ -1,3 +1,8 @@
# Clear to black.
[[layers]]
type = "clear"
color = "#000000"
# Background nebula detail. # Background nebula detail.
[[layers]] [[layers]]
type = "noise" type = "noise"

View File

@@ -0,0 +1,31 @@
# Clear to black.
[[layers]]
type = "clear"
color = "#000000"
# Background nebula detail.
[[layers]]
type = "noise"
seed = 7832
innercolor = "#5d1fe1"
outercolor = "#230070"
noise_type = "ridged"
frequency = "4"
octaves = 8
power = "0.25"
threshold = "0.40"
# Mask background nebula.
[[layers]]
type = "noise"
noise_type = "fbm"
innercolor = "#000000"
outercolor = "#000000"
destfactor = "SrcAlpha"
seed = 3551
octaves = 4
power = "0.35"
lacunarity = "1.5"
frequency = "3"
threshold = "0.0"

View File

@@ -0,0 +1,57 @@
# Clear to black.
[[layers]]
type = "clear"
color = "#000000"
# Bright background nebula stars.
[[layers]]
type = "points"
closecolor = "#7E86BF"
count = 1000
seed = 3472
mask = true
masknoise_type = "fbm"
maskoctaves = 4
maskpersistence = "0.5"
maskpower = "0.35"
masklacunarity = "1.5"
maskfrequency = "3"
maskthreshold = "0.37"
maskseed = 3551
# Bright background nebula stars, dim edge.
[[layers]]
type = "points"
closecolor = "#3D415C"
pointsize = 2
count = 1000
seed = 3472
mask = true
masknoise_type = "fbm"
maskoctaves = 4
maskpersistence = "0.5"
maskpower = "0.35"
masklacunarity = "1.5"
maskfrequency = "3"
maskthreshold = "0.37"
maskseed = 3551
# Couple of odd bright yellow-ish stars.
[[layers]]
type = "points"
closecolor = "#FFD363"
count = 30
seed = 6454
# And their dim edge.
[[layers]]
type = "points"
closecolor = "#43371A"
pointsize = 2
count = 30
seed = 6454
# Colour-to-alpha.
[[layers]]
type = "toalpha"

View File

@@ -0,0 +1,25 @@
# Clear to black.
[[layers]]
type = "clear"
color = "#000000"
# Dim background nebula stars.
[[layers]]
type = "points"
seed = 3909
closecolor = "#4B5072"
count = 1500
mask = true
masknoise_type = "fbm"
maskoctaves = 4
maskpersistence = "0.5"
maskpower = "0.35"
masklacunarity = "1.5"
maskfrequency = "3"
maskthreshold = "0.0"
maskseed = 3551
# Colour-to-alpha.
[[layers]]
type = "toalpha"

View File

@@ -0,0 +1,54 @@
# looks like the AME became the universe again.
# someone call that guy who went and became the universe last time.
# using Content.Client.Parallax.Managers; IoCManager.Resolve<IParallaxManager>().ParallaxName = "test";
- type: parallax
id: test
layers:
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Decals/dirty.rsi/damaged.png"
slowness: 0.5
scale: "2, 2"
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Decals/dirty.rsi/rust.png"
slowness: 3.0
scale: "3, 3"
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Objects/Power/AME/ame_part.rsi/box.png"
slowness: 0.995
tiled: false
controlHomePosition: "-128, -128"
worldHomePosition: "0, 0"
worldAdjustPosition: "4, 4"
scale: "8, 8"
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Objects/Power/AME/ame_jar.rsi/jar.png"
slowness: 0.0
tiled: false
scale: "2, 2"
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Objects/Power/AME/ame_jar.rsi/jar.png"
slowness: 0.125
tiled: false
scale: "1, 1"
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Objects/Power/AME/ame_part.rsi/box.png"
slowness: 0.0
tiled: false
controlHomePosition: "0, 32"
worldHomePosition: "0, 1"
scale: "2, 2"
layersLQ:
- texture:
!type:ImageParallaxTextureSource
path: "/Textures/Decals/dirty.rsi/rust.png"
slowness: 3.0
scale: "3, 3"
layersLQUseHQ: false