Replace FlashPaletteEffect with a post-processing shader.

This commit is contained in:
Paul Chote
2023-10-22 17:06:28 +01:00
committed by Gustas
parent 59d40c8b4e
commit a51a9700cf
15 changed files with 77 additions and 52 deletions

View File

@@ -18,22 +18,22 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptGlobal("Lighting")]
public class LightingGlobal : ScriptGlobal
{
readonly IEnumerable<FlashPaletteEffect> flashPaletteEffects;
readonly IEnumerable<FlashPostProcessEffect> flashEffects;
readonly GlobalLightingPaletteEffect lighting;
readonly bool hasLighting;
public LightingGlobal(ScriptContext context)
: base(context)
{
flashPaletteEffects = context.World.WorldActor.TraitsImplementing<FlashPaletteEffect>();
flashEffects = context.World.WorldActor.TraitsImplementing<FlashPostProcessEffect>();
lighting = context.World.WorldActor.TraitOrDefault<GlobalLightingPaletteEffect>();
hasLighting = lighting != null;
}
[Desc("Controls the `" + nameof(FlashPaletteEffect) + "` trait.")]
[Desc("Controls the `" + nameof(FlashPostProcessEffect) + "` trait.")]
public void Flash(string type = null, int ticks = -1)
{
foreach (var effect in flashPaletteEffects)
foreach (var effect in flashEffects)
if (effect.Info.Type == type)
effect.Enable(ticks);
}

View File

@@ -9,21 +9,17 @@
*/
#endregion
using System.Collections.Generic;
using System;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
using GUtil = OpenRA.Graphics.Util;
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Used for bursted one-colored whole screen effects. Add this to the world actor.")]
public class FlashPaletteEffectInfo : TraitInfo
public class FlashPostProcessEffectInfo : TraitInfo
{
public readonly HashSet<string> ExcludePalettes = new() { "cursor", "chrome", "colorpicker", "fog", "shroud" };
[Desc("Measured in ticks.")]
public readonly int Length = 20;
@@ -32,20 +28,21 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Set this when using multiple independent flash effects.")]
public readonly string Type = null;
public override object Create(ActorInitializer init) { return new FlashPaletteEffect(this); }
public override object Create(ActorInitializer init) { return new FlashPostProcessEffect(this); }
}
public class FlashPaletteEffect : IPaletteModifier, ITick
public class FlashPostProcessEffect : RenderPostProcessPassBase, ITick
{
public readonly FlashPaletteEffectInfo Info;
public readonly FlashPostProcessEffectInfo Info;
int remainingFrames;
float blend;
public FlashPaletteEffect(FlashPaletteEffectInfo info)
public FlashPostProcessEffect(FlashPostProcessEffectInfo info)
: base("flash", PostProcessPassType.AfterWorld)
{
Info = info;
}
int remainingFrames;
public void Enable(int ticks)
{
if (ticks == -1)
@@ -57,27 +54,14 @@ namespace OpenRA.Mods.Common.Traits
void ITick.Tick(Actor self)
{
if (remainingFrames > 0)
remainingFrames--;
blend = Math.Min((float)--remainingFrames / Info.Length, 1);
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
protected override bool Enabled => remainingFrames > 0;
protected override void PrepareRender(WorldRenderer wr, IShader shader)
{
if (remainingFrames == 0)
return;
var frac = (float)remainingFrames / Info.Length;
foreach (var pal in palettes)
{
for (var x = 0; x < Palette.Size; x++)
{
var orig = pal.Value.GetColor(x);
var c = Info.Color;
var color = Color.FromArgb(orig.A, ((int)c.R).Clamp(0, 255), ((int)c.G).Clamp(0, 255), ((int)c.B).Clamp(0, 255));
var final = GUtil.PremultipliedColorLerp(frac, orig, GUtil.PremultiplyAlpha(Color.FromArgb(orig.A, color)));
pal.Value.SetColor(x, final);
}
}
shader.SetVec("Blend", blend);
shader.SetVec("Color", (float)Info.Color.B / 255, (float)Info.Color.G / 255, (float)Info.Color.R / 255);
}
}
}

View File

@@ -18,13 +18,24 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
public override string Name => "Replace palette modifiers with post-processing shaders.";
public override string Description =>
"MenuPaletteEffect is renamed to MenuPostProcessEffect.\n" +
"ChronoshiftPaletteEffect is renamed to ChronoshiftPostProcessEffect.";
"MenuPaletteEffect is renamed to MenuPostProcessEffect\n" +
"ChronoshiftPaletteEffect is renamed to ChronoshiftPostProcessEffect\n" +
"FlashPaletteEffect is renamed to FlashPostProcessEffect";
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode)
{
actorNode.RenameChildrenMatching("MenuPaletteEffect", "MenuPostProcessEffect");
actorNode.RenameChildrenMatching("ChronoshiftPaletteEffect", "ChronoshiftPostProcessEffect");
actorNode.RenameChildrenMatching("FlashPaletteEffect", "FlashPostProcessEffect");
yield break;
}
public override IEnumerable<string> UpdateWeaponNode(ModData modData, MiniYamlNodeBuilder weaponNode)
{
foreach (var warheadNode in weaponNode.ChildrenMatching("Warhead"))
if (warheadNode.Value.Value == "FlashPaletteEffect")
warheadNode.Value.Value = "FlashEffect";
yield break;
}

View File

@@ -15,19 +15,19 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
[Desc("Used to trigger a FlashPaletteEffect trait on the world actor.")]
public class FlashPaletteEffectWarhead : Warhead
[Desc("Used to trigger a FlashPostProcessEffect trait on the world actor.")]
public class FlashEffectWarhead : Warhead
{
[Desc("Corresponds to `Type` from `FlashPaletteEffect` on the world actor.")]
[Desc("Corresponds to `Type` from `FlashPostProcessEffect` on the world actor.")]
public readonly string FlashType = null;
[FieldLoader.Require]
[Desc("Duration of the flashing, measured in ticks. Set to -1 to default to the `Length` of the `FlashPaletteEffect`.")]
[Desc("Duration of the flashing, measured in ticks. Set to -1 to default to the `Length` of the `FlashPostProcessEffect`.")]
public readonly int Duration = 0;
public override void DoImpact(in Target target, WarheadArgs args)
{
foreach (var flash in args.SourceActor.World.WorldActor.TraitsImplementing<FlashPaletteEffect>())
foreach (var flash in args.SourceActor.World.WorldActor.TraitsImplementing<FlashPostProcessEffect>())
if (flash.Info.Type == FlashType)
flash.Enable(Duration);
}