CatStation/Content.Client/Parallax/ParallaxGenerator.cs
Dmitry 25c5e59248
Upstream (#3172)
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com>
Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
Co-authored-by: Partmedia <kevinz5000@gmail.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: Stomf <5dorkydorks@gmail.com>
Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com>
Co-authored-by: PraxisMapper <praxismapper@gmail.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com>
Co-authored-by: abadaba695 <spacestation13thingy@gmail.com>
Co-authored-by: kosticia <kosticia46@gmail.com>
Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com>
Co-authored-by: pathetic meowmeow <uhhadd@gmail.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com>
Co-authored-by: Myra <vasilis@pikachu.systems>
Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: BIGZi0348 <svalker0348@gmail.com>
Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com>
Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com>
Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com>
Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com>
Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: eoineoineoin <helloworld@eoinrul.es>
Co-authored-by: Tiniest Shark <head.rebel@yahoo.com>
Co-authored-by: nikitosych <boriszyn@gmail.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com>
Co-authored-by: YoungThug <ramialanbagy@gmail.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com>
Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com>
Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com>
Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: J <billsmith116@gmail.com>
Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com>
Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: Minemoder5000 <minemoder50000@gmail.com>
Co-authored-by: Spanky <scott@wearejacob.com>
Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com>
Co-authored-by: Deerstop <edainturner@gmail.com>
Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com>
Co-authored-by: archee1 <archee3@hotmail.co.uk>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com>
Co-authored-by: poklj <compgeek223@gmail.com>
Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com>
Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com>
Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: ScarKy0 <scarky0@onet.eu>
Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
2025-05-24 11:00:17 +03:00

492 lines
18 KiB
C#

using System;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Nett;
using Robust.Client.Utility;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Noise;
using Robust.Shared.Random;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Color = Robust.Shared.Maths.Color;
namespace Content.Client.Parallax
{
public sealed class ParallaxGenerator
{
private readonly List<Layer> Layers = new();
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();
generator._loadConfig(config);
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, 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++);
}
sw.Stop();
sawmill.Debug("Total time: {0}", sw.Elapsed.TotalSeconds);
return image;
}
private void _loadConfig(TomlTable config)
{
foreach (var layerArray in ((TomlTableArray) config.Get("layers")).Items)
{
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);
break;
case "points":
var layerPoint = new LayerPoints(layerArray);
Layers.Add(layerPoint);
break;
default:
throw new NotSupportedException();
}
}
}
private abstract class Layer
{
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;
private readonly Color OuterColor = Color.Black;
private readonly FastNoiseLite.FractalType NoiseType = FastNoiseLite.FractalType.FBm;
private readonly uint Seed = 1234;
private readonly float Persistence = 0.5f;
private readonly float Lacunarity = (float) (Math.PI / 3);
private readonly float Frequency = 1;
private readonly uint Octaves = 3;
private readonly float Threshold;
private readonly float Power = 1;
private readonly Color.BlendFactor SrcFactor = Color.BlendFactor.One;
private readonly Color.BlendFactor DstFactor = Color.BlendFactor.One;
public LayerNoise(TomlTable table)
{
if (table.TryGetValue("innercolor", out var tomlObject))
{
InnerColor = Color.FromHex(((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("outercolor", out tomlObject))
{
OuterColor = Color.FromHex(((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("seed", out tomlObject))
{
Seed = (uint) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("persistence", out tomlObject))
{
Persistence = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("lacunarity", out tomlObject))
{
Lacunarity = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("frequency", out tomlObject))
{
Frequency = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("octaves", out tomlObject))
{
Octaves = (uint) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("threshold", out tomlObject))
{
Threshold = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("sourcefactor", out tomlObject))
{
SrcFactor = (Color.BlendFactor) Enum.Parse(typeof(Color.BlendFactor), ((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("destfactor", out tomlObject))
{
DstFactor = (Color.BlendFactor) Enum.Parse(typeof(Color.BlendFactor), ((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("power", out tomlObject))
{
Power = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("noise_type", out tomlObject))
{
switch (((TomlValue<string>) tomlObject).Value)
{
case "fbm":
NoiseType = FastNoiseLite.FractalType.FBm;
break;
case "ridged":
NoiseType = FastNoiseLite.FractalType.Ridged;
break;
default:
throw new InvalidOperationException();
}
}
}
public override void Apply(Image<Rgba32> bitmap)
{
var noise = new FastNoiseLite((int)Seed);
noise.SetFractalType(NoiseType);
noise.SetFrequency(Frequency);
noise.SetFractalLacunarity(Lacunarity);
noise.SetFractalOctaves((int)Octaves);
var threshVal = 1 / (1 - Threshold);
var powFactor = 1 / Power;
var span = bitmap.GetPixelSpan();
for (var y = 0; y < bitmap.Height; y++)
{
for (var x = 0; x < bitmap.Width; x++)
{
// Do noise calculations.
var noiseVal = MathF.Min(1, MathF.Max(0, (noise.GetNoise(x, y) + 1) / 2));
// Threshold
noiseVal = MathF.Max(0, noiseVal - Threshold);
noiseVal *= threshVal;
noiseVal = MathF.Pow(noiseVal, powFactor);
// Get colors based on noise values.
var srcColor = Color.InterpolateBetween(OuterColor, InnerColor, noiseVal)
.WithAlpha(noiseVal);
// Apply blending factors & write back.
var i = y * bitmap.Width + x;
var dstColor = span[i].ConvertImgSharp();
span[i] = Color.Blend(dstColor, srcColor, DstFactor, SrcFactor).ConvertImgSharp();
}
}
}
}
private sealed class LayerPoints : Layer
{
private readonly int Seed = 1234;
private readonly int PointCount = 100;
private readonly Color CloseColor = Color.White;
private readonly Color FarColor = Color.Black;
private readonly Color.BlendFactor SrcFactor = Color.BlendFactor.One;
private readonly Color.BlendFactor DstFactor = Color.BlendFactor.One;
// Noise mask stuff.
private readonly bool Masked;
private readonly FastNoiseLite.FractalType MaskNoiseType = FastNoiseLite.FractalType.FBm;
private readonly uint MaskSeed = 1234;
private readonly float MaskPersistence = 0.5f;
private readonly float MaskLacunarity = (float) (Math.PI * 2 / 3);
private readonly float MaskFrequency = 1;
private readonly uint MaskOctaves = 3;
private readonly float MaskThreshold;
private readonly int PointSize = 1;
private readonly float MaskPower = 1;
public LayerPoints(TomlTable table)
{
if (table.TryGetValue("seed", out var tomlObject))
{
Seed = (int) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("count", out tomlObject))
{
PointCount = (int) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("sourcefactor", out tomlObject))
{
SrcFactor = (Color.BlendFactor) Enum.Parse(typeof(Color.BlendFactor), ((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("destfactor", out tomlObject))
{
DstFactor = (Color.BlendFactor) Enum.Parse(typeof(Color.BlendFactor), ((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("farcolor", out tomlObject))
{
FarColor = Color.FromHex(((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("closecolor", out tomlObject))
{
CloseColor = Color.FromHex(((TomlValue<string>) tomlObject).Value);
}
if (table.TryGetValue("pointsize", out tomlObject))
{
PointSize = (int) ((TomlValue<long>) tomlObject).Value;
}
// Noise mask stuff.
if (table.TryGetValue("mask", out tomlObject))
{
Masked = ((TomlValue<bool>) tomlObject).Value;
}
if (table.TryGetValue("maskseed", out tomlObject))
{
MaskSeed = (uint) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("maskpersistence", out tomlObject))
{
MaskPersistence = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("masklacunarity", out tomlObject))
{
MaskLacunarity = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("maskfrequency", out tomlObject))
{
MaskFrequency = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("maskoctaves", out tomlObject))
{
MaskOctaves = (uint) ((TomlValue<long>) tomlObject).Value;
}
if (table.TryGetValue("maskthreshold", out tomlObject))
{
MaskThreshold = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
if (table.TryGetValue("masknoise_type", out tomlObject))
{
switch (((TomlValue<string>) tomlObject).Value)
{
case "fbm":
MaskNoiseType = FastNoiseLite.FractalType.FBm;
break;
case "ridged":
MaskNoiseType = FastNoiseLite.FractalType.Ridged;
break;
default:
throw new InvalidOperationException();
}
}
if (table.TryGetValue("maskpower", out tomlObject))
{
MaskPower = float.Parse(((TomlValue<string>) tomlObject).Value, CultureInfo.InvariantCulture);
}
}
public override void Apply(Image<Rgba32> bitmap)
{
// Temporary buffer so we don't mess up blending.
using (var buffer = new Image<Rgba32>(Configuration.Default, bitmap.Width, bitmap.Height, new Rgba32(0, 0, 0, 0)))
{
if (Masked)
{
GenPointsMasked(buffer);
}
else
{
GenPoints(buffer);
}
var srcSpan = buffer.GetPixelSpan();
var dstSpan = bitmap.GetPixelSpan();
var width = bitmap.Width;
var height = bitmap.Height;
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var i = y * width + x;
var dstColor = dstSpan[i].ConvertImgSharp();
var srcColor = srcSpan[i].ConvertImgSharp();
dstSpan[i] = Color.Blend(dstColor, srcColor, DstFactor, SrcFactor).ConvertImgSharp();
}
}
}
}
private void GenPoints(Image<Rgba32> buffer)
{
var o = PointSize - 1;
var random = new Random(Seed);
var span = buffer.GetPixelSpan();
for (var i = 0; i < PointCount; i++)
{
var x = random.Next(0, buffer.Width);
var y = random.Next(0, buffer.Height);
var dist = random.NextFloat();
for (var oy = y - o; oy <= y + o; oy++)
{
for (var ox = x - o; ox <= x + o; ox++)
{
var ix = MathHelper.Mod(ox, buffer.Width);
var iy = MathHelper.Mod(oy, buffer.Height);
var color = Color.InterpolateBetween(FarColor, CloseColor, dist).ConvertImgSharp();
span[iy * buffer.Width + ix] = color;
}
}
}
}
private void GenPointsMasked(Image<Rgba32> buffer)
{
var o = PointSize - 1;
var random = new Random(Seed);
var noise = new FastNoiseLite((int)MaskSeed);
noise.SetFractalType(MaskNoiseType);
noise.SetFractalLacunarity(MaskLacunarity);
noise.SetFractalOctaves((int)MaskOctaves);
var threshVal = 1 / (1 - MaskThreshold);
var powFactor = 1 / MaskPower;
const int maxPointAttemptCount = 9999;
var pointAttemptCount = 0;
var span = buffer.GetPixelSpan();
for (var i = 0; i < PointCount; i++)
{
var x = random.Next(0, buffer.Width);
var y = random.Next(0, buffer.Height);
// Grab noise at this point.
var noiseVal = MathF.Min(1, MathF.Max(0, (noise.GetNoise(x, y) + 1) / 2));
// Threshold
noiseVal = MathF.Max(0, noiseVal - MaskThreshold);
noiseVal *= threshVal;
noiseVal = MathF.Pow(noiseVal, powFactor);
var randomThresh = random.NextFloat();
if (randomThresh > noiseVal)
{
if (++pointAttemptCount <= maxPointAttemptCount)
{
i--;
}
continue;
}
var dist = random.NextFloat();
for (var oy = y - o; oy <= y + o; oy++)
{
for (var ox = x - o; ox <= x + o; ox++)
{
var ix = MathHelper.Mod(ox, buffer.Width);
var iy = MathHelper.Mod(oy, buffer.Height);
var color = Color.InterpolateBetween(FarColor, CloseColor, dist).ConvertImgSharp();
span[iy * buffer.Width + ix] = color;
}
}
}
}
}
}
}