Replace MenuPaletteEffect with a post-processing shader.

This commit is contained in:
Paul Chote
2023-10-22 17:09:32 +01:00
committed by Gustas
parent 47af7a9023
commit fe6de396f2
8 changed files with 107 additions and 72 deletions

View File

@@ -9,9 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Widgets;
@@ -19,97 +17,56 @@ namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Fades the world from/to black at the start/end of the game, and can (optionally) desaturate the world")]
public class MenuPaletteEffectInfo : TraitInfo
public class MenuPostProcessEffectInfo : TraitInfo
{
[Desc("Time (in ticks) to fade between states")]
public readonly int FadeLength = 10;
[Desc("Effect style to fade to during gameplay. Accepts values of None or Desaturated.")]
public readonly MenuPaletteEffect.EffectType Effect = MenuPaletteEffect.EffectType.None;
public readonly MenuPostProcessEffect.EffectType Effect = MenuPostProcessEffect.EffectType.None;
[Desc("Effect style to fade to when opening the in-game menu. Accepts values of None, Black or Desaturated.")]
public readonly MenuPaletteEffect.EffectType MenuEffect = MenuPaletteEffect.EffectType.None;
public readonly MenuPostProcessEffect.EffectType MenuEffect = MenuPostProcessEffect.EffectType.None;
public override object Create(ActorInitializer init) { return new MenuPaletteEffect(this); }
public override object Create(ActorInitializer init) { return new MenuPostProcessEffect(this); }
}
public class MenuPaletteEffect : IPaletteModifier, IRender, IWorldLoaded, INotifyGameLoaded
public class MenuPostProcessEffect : RenderPostProcessPassBase, IWorldLoaded, INotifyGameLoaded
{
public enum EffectType { None, Black, Desaturated }
public readonly MenuPaletteEffectInfo Info;
public readonly MenuPostProcessEffectInfo Info;
EffectType from = EffectType.Black;
EffectType to = EffectType.Black;
float frac = 0;
long startTime;
long endTime;
public MenuPaletteEffect(MenuPaletteEffectInfo info) { Info = info; }
public MenuPostProcessEffect(MenuPostProcessEffectInfo info)
: base("menufade", PostProcessPassType.AfterShroud)
{
Info = info;
}
public void Fade(EffectType type)
{
startTime = Game.RunTime;
endTime = startTime + Ui.Timestep * Info.FadeLength;
frac = 1;
from = to;
to = type;
}
IEnumerable<IRenderable> IRender.Render(Actor self, WorldRenderer wr)
protected override bool Enabled => to != EffectType.None || endTime != 0;
protected override void PrepareRender(WorldRenderer wr, IShader shader)
{
if (endTime == 0)
yield break;
var blend = (endTime - Game.RunTime) * 1f / (endTime - startTime);
if (blend < 0)
blend = startTime = endTime = 0;
frac = (endTime - Game.RunTime) * 1f / (endTime - startTime);
if (frac < 0)
frac = startTime = endTime = 0;
yield break;
}
IEnumerable<Rectangle> IRender.ScreenBounds(Actor self, WorldRenderer wr)
{
yield break;
}
static Color ColorForEffect(EffectType t, Color orig)
{
switch (t)
{
case EffectType.Black:
return Color.FromArgb(orig.A, Color.Black);
case EffectType.Desaturated:
var lum = (int)(255 * orig.GetBrightness());
return Color.FromArgb(orig.A, lum, lum, lum);
default:
case EffectType.None:
return orig;
}
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
if (to == EffectType.None && endTime == 0)
return;
foreach (var pal in palettes.Values)
{
for (var x = 0; x < Palette.Size; x++)
{
var orig = pal.GetColor(x);
var t = ColorForEffect(to, orig);
if (endTime == 0)
pal.SetColor(x, t);
else
{
var f = ColorForEffect(from, orig);
pal.SetColor(x, Exts.ColorLerp(frac, t, f));
}
}
}
shader.SetVec("From", (int)from);
shader.SetVec("To", (int)to);
shader.SetVec("Blend", blend);
}
void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr)