using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace GladiatorIdle.Gfx { /// /// 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. /// 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, RenderTargetUsage use) => new(gd, Math.Max(1, w), Math.Max(1, h), false, SurfaceFormat.Color, depth, msaa, use); /// 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(); // die Szene behält ihren Inhalt (Zielwechsel während des Zeichnens), die Stufen nicht _scene = Make(gd, w, h, msaa, DepthFormat.Depth24Stencil8, RenderTargetUsage.PreserveContents); _a = Make(gd, w / 4, h / 4, 0, DepthFormat.None, RenderTargetUsage.DiscardContents); _b = Make(gd, w / 8, h / 8, 0, DepthFormat.None, RenderTargetUsage.DiscardContents); _c = Make(gd, w / 16, h / 16, 0, DepthFormat.None, RenderTargetUsage.DiscardContents); _msaa = msaa; } gd.SetRenderTarget(_scene); } /// Fertiges Bild nach (null = Bildschirm), mit Nachleuchten der Stärke . 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) // ¼ und Schwelle in einem Durchgang (ein Zielwechsel) gd.SetRenderTarget(_a); sb.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.LinearClamp); sb.Draw(_scene, new Rectangle(0, 0, _a!.Width, _a.Height), Color.White); sb.End(); sb.Begin(SpriteSortMode.Deferred, 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.Deferred, 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); sb.Begin(SpriteSortMode.Deferred, blend, SamplerState.LinearClamp); sb.Draw(from, new Rectangle(0, 0, to.Width, to.Height), c); sb.End(); } } }