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

@@ -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 Robust.Client.Graphics;
using Content.Client.Parallax;
namespace Content.Client.Parallax.Managers
namespace Content.Client.Parallax.Managers;
public interface IParallaxManager
{
public interface IParallaxManager
{
event Action<Texture>? OnTextureLoaded;
Texture? ParallaxTexture { get; }
void LoadParallax();
}
/// <summary>
/// The current parallax.
/// Changing this causes a new parallax to be loaded (eventually).
/// 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();
}

View File

@@ -1,12 +1,16 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Content.Client.Parallax.Data;
using Content.Shared;
using Content.Shared.CCVar;
using Nett;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Prototypes;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
@@ -15,91 +19,115 @@ using Robust.Shared.Utility;
using SixLabors.ImageSharp;
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 IConfigurationManager _configurationManager = default!;
private string _parallaxName = "";
public string ParallaxName
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml");
// Both of these below are in the user directory.
private static readonly ResourcePath ParallaxCachedImagePath = new("/parallax_cache.png");
private static readonly ResourcePath PreviousParallaxConfigPath = new("/parallax_config_old");
public event Action<Texture>? OnTextureLoaded;
public Texture? ParallaxTexture { get; private set; }
public async void LoadParallax()
get => _parallaxName;
set
{
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
return;
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
return;
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
if (debugParallax
|| !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{
var table = Toml.ReadString(parallaxConfig);
await UpdateCachedTexture(table, debugParallax);
//Update the previous config
using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath);
writer.Write(parallaxConfig);
}
ParallaxTexture = GetCachedTexture();
OnTextureLoaded?.Invoke(ParallaxTexture);
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers)
{
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
var sawmill = _logManager.GetSawmill("parallax");
// 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 debugImage = debugImages[i];
using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png"));
debugImage.SaveAsPng(debugImageStream);
}
}
}
private Texture GetCachedTexture()
{
using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
}
private string? GetParallaxConfig()
{
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
{
Logger.ErrorS("parallax", "Parallax config not found.");
return null;
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
LoadParallaxByName(value);
}
}
public Vector2 ParallaxAnchor { get; set; }
private CancellationTokenSource? _presentParallaxLoadCancel;
private ParallaxLayerPrepared[] _parallaxLayersHQ = {};
private ParallaxLayerPrepared[] _parallaxLayersLQ = {};
public ParallaxLayerPrepared[] ParallaxLayers => _configurationManager.GetCVar(CCVars.ParallaxLowQuality) ? _parallaxLayersLQ : _parallaxLayersHQ;
public async void LoadParallax()
{
await LoadParallaxByName("default");
}
private async Task LoadParallaxByName(string name)
{
// Update _parallaxName
if (_parallaxName == name)
{
return;
}
_parallaxName = name;
// Cancel any existing load and setup the new cancellation token
_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;
}
// Begin (for real)
Logger.InfoS("parallax", $"Loading parallax {name}");
try
{
var parallaxPrototype = _prototypeManager.Index<ParallaxPrototype>(name);
ParallaxLayerPrepared[] hq;
ParallaxLayerPrepared[] lq;
if (parallaxPrototype.LayersLQUseHQ)
{
lq = hq = await LoadParallaxLayers(parallaxPrototype.Layers, cancel);
}
else
{
var results = await Task.WhenAll(
LoadParallaxLayers(parallaxPrototype.Layers, cancel),
LoadParallaxLayers(parallaxPrototype.LayersLQ, cancel)
);
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 async Task<ParallaxLayerPrepared[]> LoadParallaxLayers(List<ParallaxLayerConfig> layersIn, CancellationToken cancel = default)
{
// Because this is async, make sure it doesn't change (prototype reloads could muck this up)
// 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 async Task<ParallaxLayerPrepared> LoadParallaxLayer(ParallaxLayerConfig config, CancellationToken cancel = default)
{
return new ParallaxLayerPrepared()
{
Texture = await config.Texture.GenerateTexture(cancel),
Config = config
};
}
}

View File

@@ -6,42 +6,60 @@ using Robust.Shared.Maths;
using Robust.Shared.Random;
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 IRobustRandom _random = default!;
[ViewVariables(VVAccess.ReadWrite)] public Vector2i Offset { get; set; }
public ParallaxControl()
{
[Dependency] private readonly IParallaxManager _parallaxManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
IoCManager.InjectDependencies(this);
[ViewVariables(VVAccess.ReadWrite)] public Vector2i Offset { get; set; }
Offset = (_random.Next(0, 1000), _random.Next(0, 1000));
RectClipContent = true;
}
public ParallaxControl()
protected override void Draw(DrawingHandleScreen handle)
{
foreach (var layer in _parallaxManager.ParallaxLayers)
{
IoCManager.InjectDependencies(this);
Offset = (_random.Next(0, 1000), _random.Next(0, 1000));
RectClipContent = true;
}
protected override void Draw(DrawingHandleScreen handle)
{
var tex = _parallaxManager.ParallaxTexture;
if (tex == null)
return;
var size = tex.Size;
var tex = layer.Texture;
var texSize = tex.Size * layer.Config.Scale.Floored();
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.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
@@ -18,7 +19,7 @@ namespace Content.Client.Parallax
{
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!");
var generator = new ParallaxGenerator();
@@ -27,10 +28,11 @@ namespace Content.Client.Parallax
sawmill.Debug("Timing start!");
var sw = new Stopwatch();
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;
foreach (var layer in generator.Layers)
{
cancel.ThrowIfCancellationRequested();
layer.Apply(image);
debugLayerDump?.Add(image.Clone());
sawmill.Debug("Layer {0} done!", count++);
@@ -48,6 +50,16 @@ namespace Content.Client.Parallax
{
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":
var layerNoise = new LayerNoise(layerArray);
Layers.Add(layerNoise);
@@ -69,6 +81,52 @@ namespace Content.Client.Parallax
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 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,72 +1,95 @@
using System;
using Content.Client.Parallax.Managers;
using Content.Shared.CCVar;
using Robust.Client.Graphics;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
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 IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowWorld;
private readonly ShaderInstance _shader;
public ParallaxOverlay()
{
[Dependency] private readonly IParallaxManager _parallaxManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
IoCManager.InjectDependencies(this);
_shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
}
private const float Slowness = 0.5f;
private Texture? _parallaxTexture;
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowWorld;
private readonly ShaderInstance _shader;
public ParallaxOverlay()
protected override void Draw(in OverlayDrawArgs args)
{
if (args.Viewport.Eye == null)
{
IoCManager.InjectDependencies(this);
_shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
return;
}
if (_parallaxManager.ParallaxTexture == null)
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
{
return;
}
var screenHandle = args.WorldHandle;
screenHandle.UseShader(_shader);
foreach (var layer in _parallaxManager.ParallaxLayers)
{
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)
{
_parallaxManager.OnTextureLoaded += texture => _parallaxTexture = texture;
// Remove offset so we can floor.
var flooredBL = args.WorldAABB.BottomLeft - originBL;
// Floor to background size.
flooredBL = (flooredBL / size).Floored() * size;
// Re-offset.
flooredBL += originBL;
for (var x = flooredBL.X; x < args.WorldAABB.Right; x += size.X)
{
for (var y = flooredBL.Y; y < args.WorldAABB.Top; y += size.Y)
{
screenHandle.DrawTextureRect(tex, Box2.FromDimensions((x, y), size));
}
}
}
else
{
_parallaxTexture = _parallaxManager.ParallaxTexture;
}
}
protected override void Draw(in OverlayDrawArgs args)
{
if (_parallaxTexture == null || args.Viewport.Eye == null)
{
return;
}
var screenHandle = args.WorldHandle;
screenHandle.UseShader(_shader);
var (sizeX, sizeY) = _parallaxTexture.Size / (float) EyeManager.PixelsPerMeter;
var (posX, posY) = args.Viewport.Eye.Position;
var o = new Vector2(posX * Slowness, posY * Slowness);
// Remove offset so we can floor.
var (l, b) = args.WorldAABB.BottomLeft - o;
// Floor to background size.
l = sizeX * MathF.Floor(l / sizeX);
b = sizeY * MathF.Floor(b / sizeY);
// Re-offset.
l += o.X;
b += o.Y;
for (var x = l; x < args.WorldAABB.Right; x += sizeX)
{
for (var y = b; y < args.WorldAABB.Top; y += sizeY)
{
screenHandle.DrawTexture(_parallaxTexture, (x, y));
}
screenHandle.DrawTextureRect(tex, Box2.FromDimensions(originBL, size));
}
}
}
}