Replace FlashPaletteEffect with a post-processing shader.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
31
glsl/postprocess_flash.frag
Normal file
31
glsl/postprocess_flash.frag
Normal 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
|
||||
}
|
||||
@@ -100,7 +100,7 @@
|
||||
MenuPostProcessEffect:
|
||||
MenuEffect: Desaturated
|
||||
CloakPaletteEffect:
|
||||
FlashPaletteEffect:
|
||||
FlashPostProcessEffect:
|
||||
RotationPaletteEffect@water:
|
||||
ExcludePalettes: effect, chrome
|
||||
RotationBase: 32
|
||||
|
||||
@@ -92,7 +92,7 @@ Atomic:
|
||||
Duration: 20
|
||||
Intensity: 5
|
||||
Multiplier: 1,1
|
||||
Warhead@14FlashEffect: FlashPaletteEffect
|
||||
Warhead@14FlashEffect: FlashEffect
|
||||
Duration: 20
|
||||
|
||||
IonCannon:
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
Alpha: 0.68
|
||||
Premultiply: false
|
||||
MenuPostProcessEffect:
|
||||
FlashPaletteEffect:
|
||||
FlashPostProcessEffect:
|
||||
PaletteFromPlayerPaletteWithAlpha@cloak:
|
||||
BaseName: cloak
|
||||
BasePalette: player
|
||||
|
||||
@@ -57,6 +57,6 @@ ParaBomb:
|
||||
ValidTargets: Ground, Infantry
|
||||
Size: 3
|
||||
Delay: 10
|
||||
Warhead@13FlashEffect: FlashPaletteEffect
|
||||
Warhead@13FlashEffect: FlashEffect
|
||||
Duration: 20
|
||||
FlashType: Nuke
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -75,6 +75,6 @@ ParaBomb:
|
||||
ValidTargets: Ground, Infantry
|
||||
Size: 3
|
||||
Delay: 10
|
||||
Warhead@13FlashEffect: FlashPaletteEffect
|
||||
Warhead@13FlashEffect: FlashEffect
|
||||
Duration: 20
|
||||
FlashType: Nuke
|
||||
|
||||
@@ -100,7 +100,7 @@
|
||||
LightPaletteRotator:
|
||||
ExcludePalettes: terrain, effect, desert
|
||||
ChronoshiftPostProcessEffect:
|
||||
FlashPaletteEffect@NUKE:
|
||||
FlashPostProcessEffect@NUKE:
|
||||
Type: Nuke
|
||||
IndexedPalette@CIV2:
|
||||
Name: civilian2
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -330,4 +330,3 @@ Colt45:
|
||||
Range: 7c0
|
||||
Warhead@1Dam: SpreadDamage
|
||||
Damage: 10000
|
||||
|
||||
|
||||
@@ -134,6 +134,6 @@ Atomic:
|
||||
Duration: 20
|
||||
Intensity: 5
|
||||
Multiplier: 1,1
|
||||
Warhead@22FlashEffect: FlashPaletteEffect
|
||||
Warhead@22FlashEffect: FlashEffect
|
||||
Duration: 20
|
||||
FlashType: Nuke
|
||||
|
||||
Reference in New Issue
Block a user