From a51a9700cfd43fa2c67c0c6d589d8b173997070b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 22 Oct 2023 17:06:28 +0100 Subject: [PATCH] Replace FlashPaletteEffect with a post-processing shader. --- .../Scripting/Global/LightingGlobal.cs | 8 ++-- ...tteEffect.cs => FlashPostProcessEffect.cs} | 44 ++++++------------- .../Rules/20230801/ReplacePaletteModifiers.cs | 15 ++++++- ...EffectWarhead.cs => FlashEffectWarhead.cs} | 10 ++--- glsl/postprocess_flash.frag | 31 +++++++++++++ mods/cnc/rules/palettes.yaml | 2 +- mods/cnc/weapons/superweapons.yaml | 2 +- mods/d2k/rules/palettes.yaml | 2 +- .../weapons.yaml | 2 +- mods/ra/maps/fort-lonestar/rules.yaml | 2 +- mods/ra/maps/situation-critical/weapons.yaml | 2 +- mods/ra/rules/palettes.yaml | 2 +- mods/ra/weapons/explosions.yaml | 4 +- mods/ra/weapons/smallcaliber.yaml | 1 - mods/ra/weapons/superweapons.yaml | 2 +- 15 files changed, 77 insertions(+), 52 deletions(-) rename OpenRA.Mods.Common/Traits/PaletteEffects/{FlashPaletteEffect.cs => FlashPostProcessEffect.cs} (53%) rename OpenRA.Mods.Common/Warheads/{FlashPaletteEffectWarhead.cs => FlashEffectWarhead.cs} (74%) create mode 100644 glsl/postprocess_flash.frag diff --git a/OpenRA.Mods.Common/Scripting/Global/LightingGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/LightingGlobal.cs index 140b62fa3a..29dafb1916 100644 --- a/OpenRA.Mods.Common/Scripting/Global/LightingGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/LightingGlobal.cs @@ -18,22 +18,22 @@ namespace OpenRA.Mods.Common.Scripting [ScriptGlobal("Lighting")] public class LightingGlobal : ScriptGlobal { - readonly IEnumerable flashPaletteEffects; + readonly IEnumerable flashEffects; readonly GlobalLightingPaletteEffect lighting; readonly bool hasLighting; public LightingGlobal(ScriptContext context) : base(context) { - flashPaletteEffects = context.World.WorldActor.TraitsImplementing(); + flashEffects = context.World.WorldActor.TraitsImplementing(); lighting = context.World.WorldActor.TraitOrDefault(); 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); } diff --git a/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs b/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPostProcessEffect.cs similarity index 53% rename from OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs rename to OpenRA.Mods.Common/Traits/PaletteEffects/FlashPostProcessEffect.cs index 489031d0df..9e16edce94 100644 --- a/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs +++ b/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPostProcessEffect.cs @@ -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 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 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); } } } diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/20230801/ReplacePaletteModifiers.cs b/OpenRA.Mods.Common/UpdateRules/Rules/20230801/ReplacePaletteModifiers.cs index cf05ddb40d..f23c878e79 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/20230801/ReplacePaletteModifiers.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/20230801/ReplacePaletteModifiers.cs @@ -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 UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode) { actorNode.RenameChildrenMatching("MenuPaletteEffect", "MenuPostProcessEffect"); actorNode.RenameChildrenMatching("ChronoshiftPaletteEffect", "ChronoshiftPostProcessEffect"); + actorNode.RenameChildrenMatching("FlashPaletteEffect", "FlashPostProcessEffect"); + + yield break; + } + + public override IEnumerable UpdateWeaponNode(ModData modData, MiniYamlNodeBuilder weaponNode) + { + foreach (var warheadNode in weaponNode.ChildrenMatching("Warhead")) + if (warheadNode.Value.Value == "FlashPaletteEffect") + warheadNode.Value.Value = "FlashEffect"; yield break; } diff --git a/OpenRA.Mods.Common/Warheads/FlashPaletteEffectWarhead.cs b/OpenRA.Mods.Common/Warheads/FlashEffectWarhead.cs similarity index 74% rename from OpenRA.Mods.Common/Warheads/FlashPaletteEffectWarhead.cs rename to OpenRA.Mods.Common/Warheads/FlashEffectWarhead.cs index da96b5677e..2ef36b9df7 100644 --- a/OpenRA.Mods.Common/Warheads/FlashPaletteEffectWarhead.cs +++ b/OpenRA.Mods.Common/Warheads/FlashEffectWarhead.cs @@ -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()) + foreach (var flash in args.SourceActor.World.WorldActor.TraitsImplementing()) if (flash.Info.Type == FlashType) flash.Enable(Duration); } diff --git a/glsl/postprocess_flash.frag b/glsl/postprocess_flash.frag new file mode 100644 index 0000000000..bf8653505f --- /dev/null +++ b/glsl/postprocess_flash.frag @@ -0,0 +1,31 @@ +#version {VERSION} +#ifdef GL_ES +precision mediump float; +#endif + +uniform float Blend; +uniform vec3 Color; +uniform sampler2D WorldTexture; + +#if __VERSION__ == 120 +uniform vec2 WorldTextureSize; +#else +out vec4 fragColor; +#endif + +void main() +{ +#if __VERSION__ == 120 + vec4 c = texture2D(WorldTexture, gl_FragCoord.xy / WorldTextureSize); +#else + vec4 c = texture(WorldTexture, gl_FragCoord.xy / textureSize(WorldTexture, 0)); +#endif + + c = vec4(Color, c.a) * Blend + c * (1.0 - Blend); + + #if __VERSION__ == 120 + gl_FragColor = c; + #else + fragColor = c; + #endif +} diff --git a/mods/cnc/rules/palettes.yaml b/mods/cnc/rules/palettes.yaml index c9ff698aab..5677d92599 100644 --- a/mods/cnc/rules/palettes.yaml +++ b/mods/cnc/rules/palettes.yaml @@ -100,7 +100,7 @@ MenuPostProcessEffect: MenuEffect: Desaturated CloakPaletteEffect: - FlashPaletteEffect: + FlashPostProcessEffect: RotationPaletteEffect@water: ExcludePalettes: effect, chrome RotationBase: 32 diff --git a/mods/cnc/weapons/superweapons.yaml b/mods/cnc/weapons/superweapons.yaml index a957d72861..e6a5457657 100644 --- a/mods/cnc/weapons/superweapons.yaml +++ b/mods/cnc/weapons/superweapons.yaml @@ -92,7 +92,7 @@ Atomic: Duration: 20 Intensity: 5 Multiplier: 1,1 - Warhead@14FlashEffect: FlashPaletteEffect + Warhead@14FlashEffect: FlashEffect Duration: 20 IonCannon: diff --git a/mods/d2k/rules/palettes.yaml b/mods/d2k/rules/palettes.yaml index 5fbd4bfa30..07d0182bb2 100644 --- a/mods/d2k/rules/palettes.yaml +++ b/mods/d2k/rules/palettes.yaml @@ -65,7 +65,7 @@ Alpha: 0.68 Premultiply: false MenuPostProcessEffect: - FlashPaletteEffect: + FlashPostProcessEffect: PaletteFromPlayerPaletteWithAlpha@cloak: BaseName: cloak BasePalette: player diff --git a/mods/ra/maps/fall-of-greece-1-personal-war/weapons.yaml b/mods/ra/maps/fall-of-greece-1-personal-war/weapons.yaml index 96b3463b0e..aeded73fdc 100644 --- a/mods/ra/maps/fall-of-greece-1-personal-war/weapons.yaml +++ b/mods/ra/maps/fall-of-greece-1-personal-war/weapons.yaml @@ -57,6 +57,6 @@ ParaBomb: ValidTargets: Ground, Infantry Size: 3 Delay: 10 - Warhead@13FlashEffect: FlashPaletteEffect + Warhead@13FlashEffect: FlashEffect Duration: 20 FlashType: Nuke diff --git a/mods/ra/maps/fort-lonestar/rules.yaml b/mods/ra/maps/fort-lonestar/rules.yaml index aab4eb2056..304e04bc80 100644 --- a/mods/ra/maps/fort-lonestar/rules.yaml +++ b/mods/ra/maps/fort-lonestar/rules.yaml @@ -24,7 +24,7 @@ World: Ambient: 0.45 MusicPlaylist: BackgroundMusic: rain - FlashPaletteEffect@LIGHTNINGSTRIKE: + FlashPostProcessEffect@LIGHTNINGSTRIKE: Type: LightningStrike LuaScript: Scripts: campaign.lua, fort-lonestar.lua, fort-lonestar-AI.lua diff --git a/mods/ra/maps/situation-critical/weapons.yaml b/mods/ra/maps/situation-critical/weapons.yaml index 53a60036fd..0c51bf4402 100644 --- a/mods/ra/maps/situation-critical/weapons.yaml +++ b/mods/ra/maps/situation-critical/weapons.yaml @@ -75,6 +75,6 @@ ParaBomb: ValidTargets: Ground, Infantry Size: 3 Delay: 10 - Warhead@13FlashEffect: FlashPaletteEffect + Warhead@13FlashEffect: FlashEffect Duration: 20 FlashType: Nuke diff --git a/mods/ra/rules/palettes.yaml b/mods/ra/rules/palettes.yaml index b870083bf6..9bf7a8fe84 100644 --- a/mods/ra/rules/palettes.yaml +++ b/mods/ra/rules/palettes.yaml @@ -100,7 +100,7 @@ LightPaletteRotator: ExcludePalettes: terrain, effect, desert ChronoshiftPostProcessEffect: - FlashPaletteEffect@NUKE: + FlashPostProcessEffect@NUKE: Type: Nuke IndexedPalette@CIV2: Name: civilian2 diff --git a/mods/ra/weapons/explosions.yaml b/mods/ra/weapons/explosions.yaml index a5fd4d809a..7c8696d473 100644 --- a/mods/ra/weapons/explosions.yaml +++ b/mods/ra/weapons/explosions.yaml @@ -271,7 +271,7 @@ CrateNuke: ValidTargets: Ground, Infantry Size: 4 Delay: 5 - Warhead@7FlashEffect: FlashPaletteEffect + Warhead@7FlashEffect: FlashEffect Duration: 20 FlashType: Nuke @@ -345,6 +345,6 @@ MiniNuke: ValidTargets: Ground, Infantry Size: 4 Delay: 15 - Warhead@14FlashEffect: FlashPaletteEffect + Warhead@14FlashEffect: FlashEffect Duration: 20 FlashType: Nuke diff --git a/mods/ra/weapons/smallcaliber.yaml b/mods/ra/weapons/smallcaliber.yaml index 22192fb1d3..e8dbfeeaec 100644 --- a/mods/ra/weapons/smallcaliber.yaml +++ b/mods/ra/weapons/smallcaliber.yaml @@ -330,4 +330,3 @@ Colt45: Range: 7c0 Warhead@1Dam: SpreadDamage Damage: 10000 - diff --git a/mods/ra/weapons/superweapons.yaml b/mods/ra/weapons/superweapons.yaml index 40853bc752..a5ebea2a50 100644 --- a/mods/ra/weapons/superweapons.yaml +++ b/mods/ra/weapons/superweapons.yaml @@ -134,6 +134,6 @@ Atomic: Duration: 20 Intensity: 5 Multiplier: 1,1 - Warhead@22FlashEffect: FlashPaletteEffect + Warhead@22FlashEffect: FlashEffect Duration: 20 FlashType: Nuke