Files
GladiatorIdle/Mono/Gfx/PostFx.cs

85 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GladiatorIdle.Gfx
{
/// <summary>
/// Nachbearbeitung ohne eigene Shader: das Bild entsteht in einer Zwischenfläche (mit
/// Kantenglättung/MSAA), danach Nachleuchten (Bloom) aus SpriteBatch und Mischmodi:
/// verkleinern, dunkle Anteile abziehen (helle bleiben übrig), in drei Stufen weich
/// verkleinern und additiv über das Bild legen.
/// </summary>
internal sealed class PostFx
{
RenderTarget2D? _scene, _a, _b, _c;
int _msaa = -1;
// dunkle Anteile abziehen: Ziel = Ziel − Quelle
static readonly BlendState Subtract = new()
{
ColorBlendFunction = BlendFunction.ReverseSubtract, ColorSourceBlend = Blend.One, ColorDestinationBlend = Blend.One,
AlphaBlendFunction = BlendFunction.Add, AlphaSourceBlend = Blend.Zero, AlphaDestinationBlend = Blend.One,
};
static RenderTarget2D Make(GraphicsDevice gd, int w, int h, int msaa, DepthFormat depth) =>
new(gd, Math.Max(1, w), Math.Max(1, h), false, SurfaceFormat.Color, depth, msaa, RenderTargetUsage.PreserveContents);
/// Zeichnen in die Zwischenfläche beginnen (Größe wie der Bildschirm).
public void Begin(GraphicsDevice gd, int w, int h, bool antiAlias)
{
int msaa = antiAlias ? 4 : 0;
if (_scene == null || _scene.Width != w || _scene.Height != h || _msaa != msaa)
{
_scene?.Dispose(); _a?.Dispose(); _b?.Dispose(); _c?.Dispose();
_scene = Make(gd, w, h, msaa, DepthFormat.Depth24Stencil8);
_a = Make(gd, w / 4, h / 4, 0, DepthFormat.None);
_b = Make(gd, w / 8, h / 8, 0, DepthFormat.None);
_c = Make(gd, w / 16, h / 16, 0, DepthFormat.None);
_msaa = msaa;
}
gd.SetRenderTarget(_scene);
}
/// Fertiges Bild nach <paramref name="output"/> (null = Bildschirm), mit Nachleuchten der Stärke <paramref name="bloom"/>.
public void End(GraphicsDevice gd, SpriteBatch sb, RenderTarget2D? output, float bloom, float threshold = 0.76f)
{
if (_scene == null) return;
if (bloom > 0)
{
// ¼: verkleinern, dann Schwelle abziehen (nur Helles bleibt)
Blit(gd, sb, _scene, _a!, BlendState.Opaque, Color.White);
sb.Begin(SpriteSortMode.Immediate, Subtract);
sb.Draw(Assets.Pixel, new Rectangle(0, 0, _a!.Width, _a.Height), new Color(threshold, threshold, threshold, 0f));
sb.End();
// ⅛ und 1/16: weiter verkleinern = weicher
Blit(gd, sb, _a, _b!, BlendState.Opaque, Color.White);
Blit(gd, sb, _b!, _c!, BlendState.Opaque, Color.White);
}
gd.SetRenderTarget(output);
int w = output?.Width ?? gd.PresentationParameters.BackBufferWidth, h = output?.Height ?? gd.PresentationParameters.BackBufferHeight;
sb.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp);
sb.Draw(_scene, new Rectangle(0, 0, w, h), Color.White);
sb.End();
if (bloom > 0)
{
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.LinearClamp);
var full = new Rectangle(0, 0, w, h);
sb.Draw(_a, full, Color.White * (0.30f * bloom));
sb.Draw(_b, full, Color.White * (0.45f * bloom));
sb.Draw(_c, full, Color.White * (0.60f * bloom));
sb.End();
}
}
static void Blit(GraphicsDevice gd, SpriteBatch sb, Texture2D from, RenderTarget2D to, BlendState blend, Color c)
{
gd.SetRenderTarget(to);
gd.Clear(Color.Black);
sb.Begin(SpriteSortMode.Immediate, blend, SamplerState.LinearClamp);
sb.Draw(from, new Rectangle(0, 0, to.Width, to.Height), c);
sb.End();
}
}
}