Skip to content

Commit

Permalink
Improve shader handling
Browse files Browse the repository at this point in the history
  • Loading branch information
flustix committed Jul 14, 2024
1 parent 063d0ad commit 64477ac
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 209 deletions.
4 changes: 2 additions & 2 deletions fluXis.Game/Graphics/Shaders/Bloom/BloomContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;

Expand All @@ -6,8 +7,7 @@ namespace fluXis.Game.Graphics.Shaders.Bloom;
public partial class BloomContainer : ShaderContainer
{
protected override string FragmentShader => "Blur";

public float Strength { get; set; } = 0;
public override ShaderType Type => ShaderType.Bloom;

public BloomContainer()
{
Expand Down
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Chromatic/ChromaticContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Chromatic;

public partial class ChromaticContainer : ShaderContainer
{
protected override string FragmentShader => "ChromaticAberration";
public override ShaderType Type => ShaderType.Chromatic;
protected override DrawNode CreateShaderDrawNode() => new ChromaticContainerDrawNode(this, SharedData);

/// <summary>
/// The strength of the chromatic aberration effect. In pixels.
/// </summary>
public float Strength { get; set; }
}
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Greyscale/GreyscaleContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Greyscale;

public partial class GreyscaleContainer : ShaderContainer
{
protected override string FragmentShader => "Greyscale";
public override ShaderType Type => ShaderType.Greyscale;
protected override DrawNode CreateShaderDrawNode() => new GreyscaleDrawNode(this, SharedData);

/// <summary>
/// The strength of the greyscale effect. From 0 to 1.
/// </summary>
public float Strength { get; set; }
}
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Invert/InvertContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Invert;

public partial class InvertContainer : ShaderContainer
{
protected override string FragmentShader => "Invert";
public override ShaderType Type => ShaderType.Invert;
protected override DrawNode CreateShaderDrawNode() => new InvertContainerDrawNode(this, SharedData);

/// <summary>
/// The strength of the invert effect. From 0 to 1.
/// </summary>
public float Strength { get; set; }
}
22 changes: 2 additions & 20 deletions fluXis.Game/Graphics/Shaders/Mosaic/MosaicContainer.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Mosaic;

public partial class MosaicContainer : ShaderContainer
{
protected override string FragmentShader => "Mosaic";
public override ShaderType Type => ShaderType.Mosaic;
protected override DrawNode CreateShaderDrawNode() => new MosaicDrawNode(this, SharedData);

private float strength;

/// <summary>
/// The strength of the mosaic effect. From 0 to 1.
/// <br/>
/// 0 means its full resolution, 1 means its 1x1 pixel.
/// </summary>
public float Strength
{
get => strength;
set
{
if (value == strength)
return;

strength = value;
Invalidate(Invalidation.DrawNode);
}
}
}
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Noise/NoiseContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Noise;

public partial class NoiseContainer : ShaderContainer
{
protected override string FragmentShader => "Noise";
public override ShaderType Type => ShaderType.Noise;
protected override DrawNode CreateShaderDrawNode() => new NoiseContainerDrawNode(this, SharedData);

/// <summary>
/// The strength of the invert effect. From 0 to 1.
/// </summary>
public float Strength { get; set; }
}
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Retro/RetroContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Retro;

public partial class RetroContainer : ShaderContainer
{
protected override string FragmentShader => "Retro";
public override ShaderType Type => ShaderType.Retro;
protected override DrawNode CreateShaderDrawNode() => new RetroContainerDrawNode(this, SharedData);

/// <summary>
/// The strength of the invert effect. From 0 to 1.
/// </summary>
public float Strength { get; set; }
}
22 changes: 22 additions & 0 deletions fluXis.Game/Graphics/Shaders/ShaderContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using fluXis.Game.Map.Events;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
Expand All @@ -14,8 +15,29 @@ public abstract partial class ShaderContainer : Container, IBufferedDrawable
{
protected virtual string VertexShader => VertexShaderDescriptor.TEXTURE_2;
protected abstract string FragmentShader { get; }
public abstract ShaderType Type { get; }
protected abstract DrawNode CreateShaderDrawNode();

private float strength;

/// <summary>
/// The strength of the mosaic effect. From 0 to 1.
/// <br/>
/// 0 means its full resolution, 1 means its 1x1 pixel.
/// </summary>
public float Strength
{
get => strength;
set
{
if (value == strength)
return;

strength = value;
Invalidate(Invalidation.DrawNode);
}
}

public bool DrawOriginal { get; set; }
public ColourInfo EffectColour { get; set; } = Color4.White;
public BlendingParameters EffectBlending { get; set; } = BlendingParameters.Inherit;
Expand Down
4 changes: 4 additions & 0 deletions fluXis.Game/Graphics/Shaders/ShaderStackContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
Expand Down Expand Up @@ -40,4 +41,7 @@ public ShaderStackContainer AddContent(Drawable[] content)

public T GetShader<T>() where T : ShaderContainer
=> shaders.FirstOrDefault(s => s.GetType() == typeof(T)) as T;

public ShaderContainer GetShader(ShaderType type)
=> shaders.FirstOrDefault(s => s.Type == type);
}
7 changes: 2 additions & 5 deletions fluXis.Game/Graphics/Shaders/Vignette/VignetteContainer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using fluXis.Game.Map.Events;
using osu.Framework.Graphics;

namespace fluXis.Game.Graphics.Shaders.Vignette;

public partial class VignetteContainer : ShaderContainer
{
protected override string FragmentShader => "Vignette";
public override ShaderType Type => ShaderType.Vignette;
protected override DrawNode CreateShaderDrawNode() => new VignetteContainerDrawNode(this, SharedData);

/// <summary>
/// The strength of the invert effect. From 0 to 1.
/// </summary>
public float Strength { get; set; }
}
41 changes: 28 additions & 13 deletions fluXis.Game/Map/Events/ShaderEvent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using fluXis.Game.Map.Structures;
using Newtonsoft.Json;
using osu.Framework.Logging;

namespace fluXis.Game.Map.Events;

Expand All @@ -9,29 +11,42 @@ public class ShaderEvent : ITimedObject, IHasDuration
public double Time { get; set; }

[JsonProperty("shader")]
public string ShaderName { get; set; } = string.Empty;
public string ShaderName
{
get => Type.ToString();
set
{
if (Enum.TryParse<ShaderType>(value, out var type))
Type = type;
else
Logger.Log($"Failed to parse {value} as {nameof(ShaderType)}!", LoggingTarget.Runtime, LogLevel.Error);
}
}

[JsonIgnore]
public ShaderType Type { get; set; } = ShaderType.Bloom;

[JsonProperty("duration")]
public double Duration { get; set; }

[JsonProperty("params")]
public ShaderParameters Parameters { get; set; } = new();

public static string[] ShaderNames =
{
"Bloom",
"Greyscale",
"Invert",
"Chromatic",
"Mosaic",
"Noise",
"Vignette",
"Retro"
};

public class ShaderParameters
{
[JsonProperty("strength")]
public float Strength { get; set; }
}
}

public enum ShaderType
{
Bloom,
Greyscale,
Invert,
Chromatic,
Mosaic,
Noise,
Vignette,
Retro
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using fluXis.Game.Graphics.Sprites;
Expand All @@ -22,7 +23,7 @@ private float maxStrength
{
get
{
if (shader.ShaderName == "Chromatic")
if (shader.Type == ShaderType.Chromatic)
return 20f;

return 1f;
Expand All @@ -33,7 +34,7 @@ private float step
{
get
{
if (shader.ShaderName == "Chromatic")
if (shader.Type == ShaderType.Chromatic)
return 1f;

return .01f;
Expand All @@ -51,7 +52,7 @@ public override ITimedObject CreateClone()
{
Time = Object.Time,
Duration = shader.Duration,
ShaderName = shader.ShaderName,
Type = shader.Type,
Parameters = new ShaderEvent.ShaderParameters
{
Strength = shader.Parameters.Strength
Expand All @@ -76,15 +77,15 @@ protected override IEnumerable<Drawable> CreateSettings()
return base.CreateSettings().Concat(new Drawable[]
{
new PointSettingsLength<ShaderEvent>(Map, shader, BeatLength),
new PointSettingsDropdown<string>
new PointSettingsDropdown<ShaderType>
{
Text = "Shader",
TooltipText = "The shader to apply to the playfield.",
CurrentValue = shader.ShaderName,
Items = ShaderEvent.ShaderNames.ToList(),
CurrentValue = shader.Type,
Items = Enum.GetValues<ShaderType>().ToList(),
OnValueChanged = value =>
{
shader.ShaderName = value;
shader.Type = value;
Map.Update(shader);
}
},
Expand Down
19 changes: 10 additions & 9 deletions fluXis.Game/Screens/Gameplay/GameplayScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using fluXis.Game.Graphics.Shaders.Vignette;
using fluXis.Game.Input;
using fluXis.Game.Map;
using fluXis.Game.Map.Events;
using fluXis.Game.Mods;
using fluXis.Game.Online.Activity;
using fluXis.Game.Online.API.Requests.Scores;
Expand Down Expand Up @@ -217,20 +218,20 @@ private void load()
dependencies.Cache(Playfield = new Playfield());

var shaders = new ShaderStackContainer();
var shaderTypes = MapEvents.ShaderEvents.Select(e => e.ShaderName).Distinct().ToList();
var shaderTypes = MapEvents.ShaderEvents.Select(e => e.Type).Distinct().ToList();

foreach (var shaderType in shaderTypes)
{
ShaderContainer shader = shaderType switch
{
"Chromatic" => new ChromaticContainer(),
"Greyscale" => new GreyscaleContainer(),
"Invert" => new InvertContainer(),
"Bloom" => new BloomContainer(),
"Mosaic" => new MosaicContainer(),
"Noise" => new NoiseContainer(),
"Vignette" => new VignetteContainer(),
"Retro" => new RetroContainer(),
ShaderType.Chromatic => new ChromaticContainer(),
ShaderType.Greyscale => new GreyscaleContainer(),
ShaderType.Invert => new InvertContainer(),
ShaderType.Bloom => new BloomContainer(),
ShaderType.Mosaic => new MosaicContainer(),
ShaderType.Noise => new NoiseContainer(),
ShaderType.Vignette => new VignetteContainer(),
ShaderType.Retro => new RetroContainer(),
_ => null
};

Expand Down
Loading

0 comments on commit 64477ac

Please sign in to comment.