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

View File

@@ -9,21 +9,17 @@
*/ */
#endregion #endregion
using System.Collections.Generic; using System;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
using GUtil = OpenRA.Graphics.Util;
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)] [TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Used for bursted one-colored whole screen effects. Add this to the world actor.")] [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.")] [Desc("Measured in ticks.")]
public readonly int Length = 20; public readonly int Length = 20;
@@ -32,20 +28,21 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Set this when using multiple independent flash effects.")] [Desc("Set this when using multiple independent flash effects.")]
public readonly string Type = null; 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; Info = info;
} }
int remainingFrames;
public void Enable(int ticks) public void Enable(int ticks)
{ {
if (ticks == -1) if (ticks == -1)
@@ -57,27 +54,14 @@ namespace OpenRA.Mods.Common.Traits
void ITick.Tick(Actor self) void ITick.Tick(Actor self)
{ {
if (remainingFrames > 0) 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) shader.SetVec("Blend", blend);
return; shader.SetVec("Color", (float)Info.Color.B / 255, (float)Info.Color.G / 255, (float)Info.Color.R / 255);
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);
}
}
} }
} }
} }

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 Name => "Replace palette modifiers with post-processing shaders.";
public override string Description => public override string Description =>
"MenuPaletteEffect is renamed to MenuPostProcessEffect.\n" + "MenuPaletteEffect is renamed to MenuPostProcessEffect\n" +
"ChronoshiftPaletteEffect is renamed to ChronoshiftPostProcessEffect."; "ChronoshiftPaletteEffect is renamed to ChronoshiftPostProcessEffect\n" +
"FlashPaletteEffect is renamed to FlashPostProcessEffect";
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode) public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNodeBuilder actorNode)
{ {
actorNode.RenameChildrenMatching("MenuPaletteEffect", "MenuPostProcessEffect"); actorNode.RenameChildrenMatching("MenuPaletteEffect", "MenuPostProcessEffect");
actorNode.RenameChildrenMatching("ChronoshiftPaletteEffect", "ChronoshiftPostProcessEffect"); 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; yield break;
} }

View File

@@ -15,19 +15,19 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads namespace OpenRA.Mods.Common.Warheads
{ {
[Desc("Used to trigger a FlashPaletteEffect trait on the world actor.")] [Desc("Used to trigger a FlashPostProcessEffect trait on the world actor.")]
public class FlashPaletteEffectWarhead : Warhead 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; public readonly string FlashType = null;
[FieldLoader.Require] [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 readonly int Duration = 0;
public override void DoImpact(in Target target, WarheadArgs args) 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) if (flash.Info.Type == FlashType)
flash.Enable(Duration); flash.Enable(Duration);
} }

View File

@@ -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
}

View File

@@ -100,7 +100,7 @@
MenuPostProcessEffect: MenuPostProcessEffect:
MenuEffect: Desaturated MenuEffect: Desaturated
CloakPaletteEffect: CloakPaletteEffect:
FlashPaletteEffect: FlashPostProcessEffect:
RotationPaletteEffect@water: RotationPaletteEffect@water:
ExcludePalettes: effect, chrome ExcludePalettes: effect, chrome
RotationBase: 32 RotationBase: 32

View File

@@ -92,7 +92,7 @@ Atomic:
Duration: 20 Duration: 20
Intensity: 5 Intensity: 5
Multiplier: 1,1 Multiplier: 1,1
Warhead@14FlashEffect: FlashPaletteEffect Warhead@14FlashEffect: FlashEffect
Duration: 20 Duration: 20
IonCannon: IonCannon:

View File

@@ -65,7 +65,7 @@
Alpha: 0.68 Alpha: 0.68
Premultiply: false Premultiply: false
MenuPostProcessEffect: MenuPostProcessEffect:
FlashPaletteEffect: FlashPostProcessEffect:
PaletteFromPlayerPaletteWithAlpha@cloak: PaletteFromPlayerPaletteWithAlpha@cloak:
BaseName: cloak BaseName: cloak
BasePalette: player BasePalette: player

View File

@@ -57,6 +57,6 @@ ParaBomb:
ValidTargets: Ground, Infantry ValidTargets: Ground, Infantry
Size: 3 Size: 3
Delay: 10 Delay: 10
Warhead@13FlashEffect: FlashPaletteEffect Warhead@13FlashEffect: FlashEffect
Duration: 20 Duration: 20
FlashType: Nuke FlashType: Nuke

View File

@@ -24,7 +24,7 @@ World:
Ambient: 0.45 Ambient: 0.45
MusicPlaylist: MusicPlaylist:
BackgroundMusic: rain BackgroundMusic: rain
FlashPaletteEffect@LIGHTNINGSTRIKE: FlashPostProcessEffect@LIGHTNINGSTRIKE:
Type: LightningStrike Type: LightningStrike
LuaScript: LuaScript:
Scripts: campaign.lua, fort-lonestar.lua, fort-lonestar-AI.lua Scripts: campaign.lua, fort-lonestar.lua, fort-lonestar-AI.lua

View File

@@ -75,6 +75,6 @@ ParaBomb:
ValidTargets: Ground, Infantry ValidTargets: Ground, Infantry
Size: 3 Size: 3
Delay: 10 Delay: 10
Warhead@13FlashEffect: FlashPaletteEffect Warhead@13FlashEffect: FlashEffect
Duration: 20 Duration: 20
FlashType: Nuke FlashType: Nuke

View File

@@ -100,7 +100,7 @@
LightPaletteRotator: LightPaletteRotator:
ExcludePalettes: terrain, effect, desert ExcludePalettes: terrain, effect, desert
ChronoshiftPostProcessEffect: ChronoshiftPostProcessEffect:
FlashPaletteEffect@NUKE: FlashPostProcessEffect@NUKE:
Type: Nuke Type: Nuke
IndexedPalette@CIV2: IndexedPalette@CIV2:
Name: civilian2 Name: civilian2

View File

@@ -271,7 +271,7 @@ CrateNuke:
ValidTargets: Ground, Infantry ValidTargets: Ground, Infantry
Size: 4 Size: 4
Delay: 5 Delay: 5
Warhead@7FlashEffect: FlashPaletteEffect Warhead@7FlashEffect: FlashEffect
Duration: 20 Duration: 20
FlashType: Nuke FlashType: Nuke
@@ -345,6 +345,6 @@ MiniNuke:
ValidTargets: Ground, Infantry ValidTargets: Ground, Infantry
Size: 4 Size: 4
Delay: 15 Delay: 15
Warhead@14FlashEffect: FlashPaletteEffect Warhead@14FlashEffect: FlashEffect
Duration: 20 Duration: 20
FlashType: Nuke FlashType: Nuke

View File

@@ -330,4 +330,3 @@ Colt45:
Range: 7c0 Range: 7c0
Warhead@1Dam: SpreadDamage Warhead@1Dam: SpreadDamage
Damage: 10000 Damage: 10000

View File

@@ -134,6 +134,6 @@ Atomic:
Duration: 20 Duration: 20
Intensity: 5 Intensity: 5
Multiplier: 1,1 Multiplier: 1,1
Warhead@22FlashEffect: FlashPaletteEffect Warhead@22FlashEffect: FlashEffect
Duration: 20 Duration: 20
FlashType: Nuke FlashType: Nuke