diff --git a/Enums/GeneralDrawLayer.cs b/Enums/GeneralDrawLayer.cs index 23aa7c20d0..319e734f49 100644 --- a/Enums/GeneralDrawLayer.cs +++ b/Enums/GeneralDrawLayer.cs @@ -1,20 +1,21 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace CalamityMod.Enums { + /// + /// A general collection of points in Terraria's overall draw order that graphical systems in the mod draw to. + /// + [Flags] public enum GeneralDrawLayer { - BeforeTiles, - BeforeNPCs, - AfterNPCs, - BeforeProjectiles, - AfterProjectiles, - AfterPlayers, - AfterDusts, - AfterEverything, + BeforeAllTiles = 0, + BeforeSolidTiles = 1, + BeforeNPCs = 2, + AfterNPCs = 4, + BeforeProjectiles = 8, + AfterProjectiles = 16, + AfterPlayers = 32, + AfterDusts = 64, + AfterEverything = 128, } } diff --git a/Graphics/Metaballs/BigRipMetaball.cs b/Graphics/Metaballs/BigRipMetaball.cs index c78acfa9c5..faee855da9 100644 --- a/Graphics/Metaballs/BigRipMetaball.cs +++ b/Graphics/Metaballs/BigRipMetaball.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; @@ -77,7 +78,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/BigRipLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => new Color(239, 111, 85) * 0.5f; diff --git a/Graphics/Metaballs/BloodBoilerMetaball.cs b/Graphics/Metaballs/BloodBoilerMetaball.cs index 86bf43c440..e3ea116062 100644 --- a/Graphics/Metaballs/BloodBoilerMetaball.cs +++ b/Graphics/Metaballs/BloodBoilerMetaball.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -48,7 +49,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override Color EdgeColor => Color.Red with { A = 0 }; diff --git a/Graphics/Metaballs/BloodBoilerMetaball2.cs b/Graphics/Metaballs/BloodBoilerMetaball2.cs index a6034d5219..6f3829158d 100644 --- a/Graphics/Metaballs/BloodBoilerMetaball2.cs +++ b/Graphics/Metaballs/BloodBoilerMetaball2.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -48,7 +49,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override Color EdgeColor => Color.Red with { A = 0 }; diff --git a/Graphics/Metaballs/BloodMetaball.cs b/Graphics/Metaballs/BloodMetaball.cs index 28457126f8..952950e6ce 100644 --- a/Graphics/Metaballs/BloodMetaball.cs +++ b/Graphics/Metaballs/BloodMetaball.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; @@ -79,7 +80,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/BloodLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => new Color(67, 17, 17); diff --git a/Graphics/Metaballs/CalamitasMetaball.cs b/Graphics/Metaballs/CalamitasMetaball.cs index 86bcd3fa23..52e9eeb066 100644 --- a/Graphics/Metaballs/CalamitasMetaball.cs +++ b/Graphics/Metaballs/CalamitasMetaball.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; -using static CalamityMod.Graphics.Metaballs.StreamGougeMetaball; namespace CalamityMod.Graphics.Metaballs { @@ -71,7 +71,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/CalamitasLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => Color.Lerp(Color.Crimson, Color.Black, 0f); diff --git a/Graphics/Metaballs/CataclysmMetaball.cs b/Graphics/Metaballs/CataclysmMetaball.cs index 62131a6df4..59221f3d51 100644 --- a/Graphics/Metaballs/CataclysmMetaball.cs +++ b/Graphics/Metaballs/CataclysmMetaball.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; -using static CalamityMod.Graphics.Metaballs.StreamGougeMetaball; namespace CalamityMod.Graphics.Metaballs { @@ -71,7 +71,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/CataclysmLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => new Color(255,0,229); diff --git a/Graphics/Metaballs/CatastropheMetaball.cs b/Graphics/Metaballs/CatastropheMetaball.cs index 24f54493a0..399af76ad4 100644 --- a/Graphics/Metaballs/CatastropheMetaball.cs +++ b/Graphics/Metaballs/CatastropheMetaball.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; -using static CalamityMod.Graphics.Metaballs.StreamGougeMetaball; namespace CalamityMod.Graphics.Metaballs { @@ -73,7 +73,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/CatastropheLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => new Color(0,255,255); diff --git a/Graphics/Metaballs/DoGDistortionMetaball.cs b/Graphics/Metaballs/DoGDistortionMetaball.cs index 173b2430ea..c2c8992591 100644 --- a/Graphics/Metaballs/DoGDistortionMetaball.cs +++ b/Graphics/Metaballs/DoGDistortionMetaball.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using CalamityMod.Effects; +using CalamityMod.Enums; using CalamityMod.Projectiles.Boss; using CalamityMod.Systems.Graphic; using Microsoft.Xna.Framework; @@ -33,7 +34,7 @@ namespace CalamityMod.Graphics.Metaballs public static List Particles { get; private set; } = []; - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override bool AnythingToDraw => Particles.Count > 0 || CalamityUtils.AnyProjectiles(ModContent.ProjectileType()); diff --git a/Graphics/Metaballs/DragonsBreathMetaball.cs b/Graphics/Metaballs/DragonsBreathMetaball.cs index e041506bb2..0f39f10fe5 100644 --- a/Graphics/Metaballs/DragonsBreathMetaball.cs +++ b/Graphics/Metaballs/DragonsBreathMetaball.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -68,7 +69,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override void Update() { diff --git a/Graphics/Metaballs/DragonsBreathMetaball2.cs b/Graphics/Metaballs/DragonsBreathMetaball2.cs index c07f13e894..1af9bd38ca 100644 --- a/Graphics/Metaballs/DragonsBreathMetaball2.cs +++ b/Graphics/Metaballs/DragonsBreathMetaball2.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -68,7 +69,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override void Update() { diff --git a/Graphics/Metaballs/GalaxyMetaball.cs b/Graphics/Metaballs/GalaxyMetaball.cs index 4d93ed1020..feec8791a1 100644 --- a/Graphics/Metaballs/GalaxyMetaball.cs +++ b/Graphics/Metaballs/GalaxyMetaball.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; @@ -56,7 +57,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeNPCs; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeNPCs; public override Color EdgeColor => Color.Lerp(Color.DarkBlue, Color.Black, 0.75f); diff --git a/Graphics/Metaballs/GruesomeMetaball.cs b/Graphics/Metaballs/GruesomeMetaball.cs index 0c281d7e5f..348099bcd6 100644 --- a/Graphics/Metaballs/GruesomeMetaball.cs +++ b/Graphics/Metaballs/GruesomeMetaball.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using CalamityMod.Projectiles.Magic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -56,7 +57,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override Color EdgeColor => new(61, 6, 2); diff --git a/Graphics/Metaballs/Metaball.cs b/Graphics/Metaballs/Metaball.cs index adff926565..f4d6f366bf 100644 --- a/Graphics/Metaballs/Metaball.cs +++ b/Graphics/Metaballs/Metaball.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -33,7 +34,7 @@ namespace CalamityMod.Graphics.Metaballs /// /// The draw layer in which metaballs should be drawn. /// - public abstract MetaballDrawLayer DrawContext + public abstract GeneralDrawLayer DrawLayer { get; } diff --git a/Graphics/Metaballs/MetaballDrawLayer.cs b/Graphics/Metaballs/MetaballDrawLayer.cs deleted file mode 100644 index d061aabe61..0000000000 --- a/Graphics/Metaballs/MetaballDrawLayer.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace CalamityMod.Graphics.Metaballs -{ - public enum MetaballDrawLayer - { - BeforeNPCs, - BeforeProjectiles, - AfterProjectiles, - } -} diff --git a/Graphics/Metaballs/MetaballManager.cs b/Graphics/Metaballs/MetaballManager.cs index 2e7721341b..123e007766 100644 --- a/Graphics/Metaballs/MetaballManager.cs +++ b/Graphics/Metaballs/MetaballManager.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -15,8 +16,6 @@ namespace CalamityMod.Graphics.Metaballs { // Prepare event subscribers. RenderTargetManager.RenderTargetUpdateLoopEvent += PrepareMetaballTargets; - On_Main.DrawProjectiles += DrawMetaballsAfterProjectiles; - On_Main.DrawNPCs += DrawMetaballsBeforeNPCs; } public override void OnModUnload() @@ -86,51 +85,20 @@ namespace CalamityMod.Graphics.Metaballs Main.spriteBatch.End(); } - private void DrawMetaballsAfterProjectiles(On_Main.orig_DrawProjectiles orig, Main self) - { - if (AnyActiveMetaballsAtLayer(MetaballDrawLayer.BeforeProjectiles)) - { - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - DrawMetaballs(MetaballDrawLayer.BeforeProjectiles); - Main.spriteBatch.End(); - } - - orig(self); - - if (AnyActiveMetaballsAtLayer(MetaballDrawLayer.AfterProjectiles)) - { - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - DrawMetaballs(MetaballDrawLayer.AfterProjectiles); - Main.spriteBatch.End(); - } - } - - private void DrawMetaballsBeforeNPCs(On_Main.orig_DrawNPCs orig, Main self, bool behindTiles) - { - if (!behindTiles && AnyActiveMetaballsAtLayer(MetaballDrawLayer.BeforeNPCs)) - { - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - DrawMetaballs(MetaballDrawLayer.BeforeNPCs); - Main.spriteBatch.ExitShaderRegion(); - } - orig(self, behindTiles); - } - /// /// Checks if a metaball of a given layering type is currently in use. This is used to minimize needless restarts when there is nothing to draw. /// /// The metaball layer type to check against. - internal static bool AnyActiveMetaballsAtLayer(MetaballDrawLayer layerType) => - metaballs.Any(m => m.AnythingToDraw && m.DrawContext == layerType); + internal static bool AnyActiveMetaballsAtLayer(GeneralDrawLayer layerType) => + metaballs.Any(m => m.AnythingToDraw && m.DrawLayer == layerType); /// - /// Draws all metaballs of a given . Used for layer ordering reasons. + /// Draws all metaballs of a given . Used for layer ordering reasons. /// /// The layer type to draw with. - public static void DrawMetaballs(MetaballDrawLayer layerType) + internal static void DrawMetaballs(GeneralDrawLayer layerType) { - foreach (Metaball metaball in metaballs.Where(m => m.DrawContext == layerType && m.AnythingToDraw)) + foreach (Metaball metaball in metaballs.Where(m => m.DrawLayer == layerType && m.AnythingToDraw)) { for (int i = 0; i < metaball.LayerTargets.Count; i++) { diff --git a/Graphics/Metaballs/PaintMetaballs.cs b/Graphics/Metaballs/PaintMetaballs.cs index 0501af90ff..8242812c96 100644 --- a/Graphics/Metaballs/PaintMetaballs.cs +++ b/Graphics/Metaballs/PaintMetaballs.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -64,7 +65,7 @@ namespace CalamityMod.Graphics.Metaballs public override bool AnythingToDraw => Particles.Any(); - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override IEnumerable Layers { diff --git a/Graphics/Metaballs/PhotoMetaball.cs b/Graphics/Metaballs/PhotoMetaball.cs index 6d3447dd90..e8d6a3bdca 100644 --- a/Graphics/Metaballs/PhotoMetaball.cs +++ b/Graphics/Metaballs/PhotoMetaball.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; -using CalamityMod.Rarities; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -54,7 +54,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public Color sparkColor; public int Time = 0; diff --git a/Graphics/Metaballs/PhotoMetaball2.cs b/Graphics/Metaballs/PhotoMetaball2.cs index f1cdc7737f..0178d70457 100644 --- a/Graphics/Metaballs/PhotoMetaball2.cs +++ b/Graphics/Metaballs/PhotoMetaball2.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; -using CalamityMod.Rarities; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -54,7 +54,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public Color sparkColor; public int Time; diff --git a/Graphics/Metaballs/PhotoMetaball3.cs b/Graphics/Metaballs/PhotoMetaball3.cs index cfb21ea60d..71c5d56f7a 100644 --- a/Graphics/Metaballs/PhotoMetaball3.cs +++ b/Graphics/Metaballs/PhotoMetaball3.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; -using CalamityMod.Rarities; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -47,7 +47,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public Color sparkColor; public int Time = 0; diff --git a/Graphics/Metaballs/PhotoMetaball4.cs b/Graphics/Metaballs/PhotoMetaball4.cs index 2df1a25d8b..ad720df0c6 100644 --- a/Graphics/Metaballs/PhotoMetaball4.cs +++ b/Graphics/Metaballs/PhotoMetaball4.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; -using CalamityMod.Rarities; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -47,7 +47,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public Color sparkColor; public int Time; diff --git a/Graphics/Metaballs/RancorLavaMetaball.cs b/Graphics/Metaballs/RancorLavaMetaball.cs index ef0d3c6124..33047c5d9e 100644 --- a/Graphics/Metaballs/RancorLavaMetaball.cs +++ b/Graphics/Metaballs/RancorLavaMetaball.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using CalamityMod.Effects; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -44,7 +45,7 @@ namespace CalamityMod.Graphics.Metaballs public override bool AnythingToDraw => Particles.Any(); - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override IEnumerable Layers { diff --git a/Graphics/Metaballs/ScalArenaMetaball.cs b/Graphics/Metaballs/ScalArenaMetaball.cs index a57d474009..c7a1039e74 100644 --- a/Graphics/Metaballs/ScalArenaMetaball.cs +++ b/Graphics/Metaballs/ScalArenaMetaball.cs @@ -1,18 +1,13 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using CalamityMod.NPCs; +using CalamityMod.Enums; using CalamityMod.NPCs.SupremeCalamitas; -using CalamityMod.Systems.Mechanic; -using CalamityMod.World; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; -using static CalamityMod.Graphics.Metaballs.StreamGougeMetaball; -using static CalamityMod.Systems.LavaRenderingSystem; namespace CalamityMod.Graphics.Metaballs { @@ -82,7 +77,7 @@ namespace CalamityMod.Graphics.Metaballs WavyLineLayer = ModContent.Request($"CalamityMod/Graphics/Metaballs/ScalArenaLayerWave", AssetRequestMode.ImmediateLoad).Value; } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeNPCs; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeNPCs; public override Color EdgeColor => SupremeCalamitas.CurrentColor; diff --git a/Graphics/Metaballs/SeekersMetaball.cs b/Graphics/Metaballs/SeekersMetaball.cs index d389e569e5..a0658bb66d 100644 --- a/Graphics/Metaballs/SeekersMetaball.cs +++ b/Graphics/Metaballs/SeekersMetaball.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.ModLoader; -using static CalamityMod.Graphics.Metaballs.StreamGougeMetaball; namespace CalamityMod.Graphics.Metaballs { @@ -73,7 +73,7 @@ namespace CalamityMod.Graphics.Metaballs // Load the layer asset wrapper. LayerAsset = ModContent.Request($"CalamityMod/Graphics/Metaballs/SeekersLayer", AssetRequestMode.ImmediateLoad); } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => new Color(255,106,0); diff --git a/Graphics/Metaballs/SignusMetaball.cs b/Graphics/Metaballs/SignusMetaball.cs index 7c90c5fda0..c0179a8ebc 100644 --- a/Graphics/Metaballs/SignusMetaball.cs +++ b/Graphics/Metaballs/SignusMetaball.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; -using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Graphics.Metaballs @@ -77,7 +77,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeNPCs; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeNPCs; public override void Load() { if (Main.dedServ) diff --git a/Graphics/Metaballs/StarsmokeMetaball.cs b/Graphics/Metaballs/StarsmokeMetaball.cs index e5f2565257..3bdfa5e22d 100644 --- a/Graphics/Metaballs/StarsmokeMetaball.cs +++ b/Graphics/Metaballs/StarsmokeMetaball.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; -using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; -using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Graphics.Metaballs @@ -80,7 +79,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeNPCs; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeNPCs; public override void Load() { if (Main.dedServ) diff --git a/Graphics/Metaballs/StreamGougeMetaball.cs b/Graphics/Metaballs/StreamGougeMetaball.cs index 0f31626192..d13ebc9af9 100644 --- a/Graphics/Metaballs/StreamGougeMetaball.cs +++ b/Graphics/Metaballs/StreamGougeMetaball.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; -using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Graphics.Metaballs @@ -56,7 +56,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.AfterProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.AfterProjectiles; public override Color EdgeColor => Color.Lerp(Color.DarkBlue, Color.Black, 0.75f); diff --git a/Graphics/Metaballs/VoidGeneratorMetaball.cs b/Graphics/Metaballs/VoidGeneratorMetaball.cs index 69029411f3..a21754f651 100644 --- a/Graphics/Metaballs/VoidGeneratorMetaball.cs +++ b/Graphics/Metaballs/VoidGeneratorMetaball.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -26,7 +27,7 @@ namespace CalamityMod.Graphics.Metaballs } } - public override MetaballDrawLayer DrawContext => MetaballDrawLayer.BeforeProjectiles; + public override GeneralDrawLayer DrawLayer => GeneralDrawLayer.BeforeProjectiles; public override Color EdgeColor => Color.Lerp(Color.Purple, Color.Black, 0.75f); diff --git a/Graphics/Primitives/IPixelatedPrimitiveRenderer.cs b/Graphics/Primitives/IPixelatedPrimitiveRenderer.cs index 14edbcb506..f40c983f56 100644 --- a/Graphics/Primitives/IPixelatedPrimitiveRenderer.cs +++ b/Graphics/Primitives/IPixelatedPrimitiveRenderer.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework.Graphics; +using CalamityMod.Enums; +using Microsoft.Xna.Framework.Graphics; namespace CalamityMod.Graphics.Primitives { @@ -10,11 +11,11 @@ namespace CalamityMod.Graphics.Primitives /// /// The layer to render the primitive(s) to. /// - PixelationPrimitiveLayer LayerToRenderTo => PixelationPrimitiveLayer.BeforeProjectiles; + GeneralDrawLayer LayerToRenderTo => GeneralDrawLayer.BeforeProjectiles; /// /// Render primitives that use pixelation here. /// - void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer); + void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer); } } diff --git a/Graphics/Primitives/PixelationPrimitiveLayer.cs b/Graphics/Primitives/PixelationPrimitiveLayer.cs deleted file mode 100644 index 4f05c51ef2..0000000000 --- a/Graphics/Primitives/PixelationPrimitiveLayer.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace CalamityMod.Graphics.Primitives -{ - /// - /// Controls what layer the renders to. - /// - [Flags] - public enum PixelationPrimitiveLayer - { - BeforeNPCs = 1, - AfterNPCs = 2, - BeforeProjectiles = 4, - AfterProjectiles = 8, - AfterPlayers = 16 - } -} diff --git a/Graphics/Primitives/PrimitivePixelationSystem.cs b/Graphics/Primitives/PrimitivePixelationSystem.cs index 4d2bc635de..36a0e186b8 100644 --- a/Graphics/Primitives/PrimitivePixelationSystem.cs +++ b/Graphics/Primitives/PrimitivePixelationSystem.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -10,6 +11,10 @@ namespace CalamityMod.Graphics.Primitives public class PrimitivePixelationSystem : ModSystem { #region Fields/Properties + private static ManagedRenderTarget PixelationTarget_BeforeAllTiles; + + private static ManagedRenderTarget PixelationTarget_BeforeSolidTiles; + private static ManagedRenderTarget PixelationTarget_BeforeNPCs; private static ManagedRenderTarget PixelationTarget_AfterNPCs; @@ -20,6 +25,10 @@ namespace CalamityMod.Graphics.Primitives private static ManagedRenderTarget PixelationTarget_AfterPlayers; + private static ManagedRenderTarget PixelationTarget_AfterDusts; + + private static ManagedRenderTarget PixelationTarget_AfterEverything; + private static RenderTarget2D CreatePixelTarget(int width, int height) => new(Main.instance.GraphicsDevice, width / 2, height / 2); /// @@ -35,22 +44,38 @@ namespace CalamityMod.Graphics.Primitives #region Loading public override void Load() { - On_Main.CheckMonoliths += DrawToTargets; - On_Main.DoDraw_DrawNPCsOverTiles += DrawTarget_NPCs; - On_Main.DrawProjectiles += DrawTarget_Projectiles; - On_Main.DrawPlayers_AfterProjectiles += DrawTarget_Players; + if (Main.dedServ) + return; - PixelationTarget_BeforeNPCs = new(true, CreatePixelTarget); - PixelationTarget_AfterNPCs = new(true, CreatePixelTarget); - PixelationTarget_BeforeProjectiles = new(true, CreatePixelTarget); - PixelationTarget_AfterProjectiles = new(true, CreatePixelTarget); - PixelationTarget_AfterPlayers = new(true, CreatePixelTarget); + Main.QueueMainThreadAction(() => + { + PixelationTarget_BeforeAllTiles = new(true, CreatePixelTarget); + PixelationTarget_BeforeSolidTiles = new(true, CreatePixelTarget); + PixelationTarget_BeforeNPCs = new(true, CreatePixelTarget); + PixelationTarget_AfterNPCs = new(true, CreatePixelTarget); + PixelationTarget_BeforeProjectiles = new(true, CreatePixelTarget); + PixelationTarget_AfterProjectiles = new(true, CreatePixelTarget); + PixelationTarget_AfterPlayers = new(true, CreatePixelTarget); + PixelationTarget_AfterDusts = new(true, CreatePixelTarget); + PixelationTarget_AfterEverything = new(true, CreatePixelTarget); + }); + + On_Main.CheckMonoliths += DrawToTargets; + //On_Main.DrawBackgroundBlackFill += DrawTarget_BeforeAllTiles; + //On_Main.DoDraw_Tiles_Solid += DrawTarget_BeforeSolidTiles; + //On_Main.DoDraw_DrawNPCsOverTiles += DrawTarget_NPCs; + //On_Main.DrawProjectiles += DrawTarget_Projectiles; + //On_Main.DrawPlayers_AfterProjectiles += DrawTarget_Players; + //On_Main.DrawDust += DrawTarget_AfterDusts; + //On_Main.DrawInfernoRings += DrawTarget_AfterEverything; } public override void Unload() { + if (Main.dedServ) + return; + On_Main.CheckMonoliths -= DrawToTargets; - On_Main.DoDraw_DrawNPCsOverTiles -= DrawTarget_NPCs; } #endregion @@ -63,11 +88,15 @@ namespace CalamityMod.Graphics.Primitives return; } + var beforeAllTiles = new List(); + var beforeSolidTiles = new List(); var beforeNPCs = new List(); var afterNPCs = new List(); var beforeProjectiles = new List(); var afterProjectiles = new List(); var afterPlayers = new List(); + var afterDusts = new List(); + var afterEverything = new List(); // Check every active projectile. foreach (Projectile projectile in Main.ActiveProjectiles) @@ -75,14 +104,24 @@ namespace CalamityMod.Graphics.Primitives // If the projectile is active, a mod projectile, and uses the interface, add it to the list of primitives to draw this frame. if (projectile.ModProjectile != null && projectile.ModProjectile is IPixelatedPrimitiveRenderer pixelPrimitiveProjectile) { - if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.BeforeNPCs)) + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeAllTiles)) + beforeAllTiles.Add(pixelPrimitiveProjectile); + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeSolidTiles)) + beforeSolidTiles.Add(pixelPrimitiveProjectile); + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeNPCs)) beforeNPCs.Add(pixelPrimitiveProjectile); - if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.AfterNPCs)) + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterNPCs)) afterNPCs.Add(pixelPrimitiveProjectile); - if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.BeforeProjectiles)) + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeProjectiles)) beforeProjectiles.Add(pixelPrimitiveProjectile); - if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.AfterPlayers)) + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterProjectiles)) + afterProjectiles.Add(pixelPrimitiveProjectile); + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterPlayers)) afterPlayers.Add(pixelPrimitiveProjectile); + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterDusts)) + afterDusts.Add(pixelPrimitiveProjectile); + if (pixelPrimitiveProjectile.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterEverything)) + afterEverything.Add(pixelPrimitiveProjectile); } } @@ -92,24 +131,38 @@ namespace CalamityMod.Graphics.Primitives // If the NPC is active, a mod NPC, and uses the interface, add it to the list of primitives to draw this frame. if (npc.ModNPC != null && npc.ModNPC is IPixelatedPrimitiveRenderer pixelPrimitiveNPC) { - if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.BeforeNPCs)) + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeAllTiles)) + beforeAllTiles.Add(pixelPrimitiveNPC); + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeSolidTiles)) + beforeSolidTiles.Add(pixelPrimitiveNPC); + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeNPCs)) beforeNPCs.Add(pixelPrimitiveNPC); - if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.AfterNPCs)) + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterNPCs)) afterNPCs.Add(pixelPrimitiveNPC); - if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.BeforeProjectiles)) + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.BeforeProjectiles)) beforeProjectiles.Add(pixelPrimitiveNPC); - if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(PixelationPrimitiveLayer.AfterPlayers)) + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterProjectiles)) + afterProjectiles.Add(pixelPrimitiveNPC); + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterPlayers)) afterPlayers.Add(pixelPrimitiveNPC); + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterDusts)) + afterDusts.Add(pixelPrimitiveNPC); + if (pixelPrimitiveNPC.LayerToRenderTo.HasFlag(GeneralDrawLayer.AfterEverything)) + afterEverything.Add(pixelPrimitiveNPC); } } CurrentlyRendering = true; - DrawPrimsToRenderTarget(PixelationTarget_BeforeNPCs, PixelationPrimitiveLayer.BeforeNPCs, beforeNPCs); - DrawPrimsToRenderTarget(PixelationTarget_AfterNPCs, PixelationPrimitiveLayer.AfterNPCs, afterNPCs); - DrawPrimsToRenderTarget(PixelationTarget_BeforeProjectiles, PixelationPrimitiveLayer.BeforeProjectiles, beforeProjectiles); - DrawPrimsToRenderTarget(PixelationTarget_AfterProjectiles, PixelationPrimitiveLayer.AfterProjectiles, afterProjectiles); - DrawPrimsToRenderTarget(PixelationTarget_AfterPlayers, PixelationPrimitiveLayer.AfterPlayers, afterPlayers); + DrawPrimsToRenderTarget(PixelationTarget_BeforeAllTiles, GeneralDrawLayer.BeforeAllTiles, beforeAllTiles); + DrawPrimsToRenderTarget(PixelationTarget_BeforeSolidTiles, GeneralDrawLayer.BeforeSolidTiles, beforeSolidTiles); + DrawPrimsToRenderTarget(PixelationTarget_BeforeNPCs, GeneralDrawLayer.BeforeNPCs, beforeNPCs); + DrawPrimsToRenderTarget(PixelationTarget_AfterNPCs, GeneralDrawLayer.AfterNPCs, afterNPCs); + DrawPrimsToRenderTarget(PixelationTarget_BeforeProjectiles, GeneralDrawLayer.BeforeProjectiles, beforeProjectiles); + DrawPrimsToRenderTarget(PixelationTarget_AfterProjectiles, GeneralDrawLayer.AfterProjectiles, afterProjectiles); + DrawPrimsToRenderTarget(PixelationTarget_AfterPlayers, GeneralDrawLayer.AfterPlayers, afterPlayers); + DrawPrimsToRenderTarget(PixelationTarget_AfterDusts, GeneralDrawLayer.AfterDusts, afterDusts); + DrawPrimsToRenderTarget(PixelationTarget_AfterEverything, GeneralDrawLayer.AfterEverything, afterEverything); Main.instance.GraphicsDevice.SetRenderTarget(null); @@ -117,7 +170,7 @@ namespace CalamityMod.Graphics.Primitives orig(); } - private static void DrawPrimsToRenderTarget(RenderTarget2D renderTarget, PixelationPrimitiveLayer layer, List pixelPrimitives) + private static void DrawPrimsToRenderTarget(RenderTarget2D renderTarget, GeneralDrawLayer layer, List pixelPrimitives) { // Swap to the target regardless, in order to clear any leftover content from last frame. Not doing this results in the final frame lingering once it stops rendering. renderTarget.SwapTo(); @@ -135,31 +188,31 @@ namespace CalamityMod.Graphics.Primitives #endregion #region Target Drawing - private void DrawTarget_NPCs(On_Main.orig_DoDraw_DrawNPCsOverTiles orig, Main self) + internal static void DrawTargetScaled(GeneralDrawLayer drawLayer, bool startSpritebatch = true) { - DrawTargetScaled(PixelationTarget_BeforeNPCs); - orig(self); - DrawTargetScaled(PixelationTarget_AfterNPCs); + if (startSpritebatch) + Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); + + Main.spriteBatch.Draw(ReturnAssociatedRenderTarget(drawLayer), Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f); + + if (startSpritebatch) + Main.spriteBatch.End(); } - private void DrawTarget_Projectiles(On_Main.orig_DrawProjectiles orig, Main self) + private static RenderTarget2D ReturnAssociatedRenderTarget(GeneralDrawLayer drawLayer) { - DrawTargetScaled(PixelationTarget_BeforeProjectiles); - orig(self); - DrawTargetScaled(PixelationTarget_AfterProjectiles); - } - - private void DrawTarget_Players(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self) - { - orig(self); - DrawTargetScaled(PixelationTarget_AfterPlayers); - } - - private static void DrawTargetScaled(ManagedRenderTarget target) - { - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - Main.spriteBatch.Draw(target, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f); - Main.spriteBatch.End(); + return drawLayer switch + { + GeneralDrawLayer.BeforeAllTiles => PixelationTarget_BeforeAllTiles, + GeneralDrawLayer.BeforeSolidTiles => PixelationTarget_BeforeSolidTiles, + GeneralDrawLayer.BeforeNPCs => PixelationTarget_BeforeNPCs, + GeneralDrawLayer.AfterNPCs => PixelationTarget_AfterNPCs, + GeneralDrawLayer.BeforeProjectiles => PixelationTarget_BeforeProjectiles, + GeneralDrawLayer.AfterProjectiles => PixelationTarget_AfterProjectiles, + GeneralDrawLayer.AfterPlayers => PixelationTarget_AfterPlayers, + GeneralDrawLayer.AfterDusts => PixelationTarget_AfterDusts, + _ => PixelationTarget_AfterEverything + }; } #endregion } diff --git a/Graphics/Primitives/PrimitiveRenderer.cs b/Graphics/Primitives/PrimitiveRenderer.cs index 2fcc1b9acf..84e3d9ca65 100644 --- a/Graphics/Primitives/PrimitiveRenderer.cs +++ b/Graphics/Primitives/PrimitiveRenderer.cs @@ -18,7 +18,7 @@ namespace CalamityMod.Graphics.Primitives /// The third controls how many points are created for the trail, and the fourth determines whether the primitive is subsequently rendered. /// /// If you wish to use pixelation, you MUST make the NPC/Projectile inherit and use instead of predraw..
- /// You can also optionally specify a render layer with . It is by default. + /// You can also optionally specify a render layer with . It is by default. ///
[Autoload(Side = ModSide.Client)] public sealed class PrimitiveRenderer : ModSystem diff --git a/Graphics/Renderers/BaseRenderer.cs b/Graphics/Renderers/BaseRenderer.cs index 8cdfa0ae6d..cb1e3dc5a8 100644 --- a/Graphics/Renderers/BaseRenderer.cs +++ b/Graphics/Renderers/BaseRenderer.cs @@ -1,4 +1,5 @@ using System; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria.ModLoader; @@ -14,7 +15,7 @@ namespace CalamityMod.Graphics.Renderers /// /// The layer that the target should be drawn at. /// - public abstract DrawLayer Layer { get; } + public abstract GeneralDrawLayer Layer { get; } /// /// Whether the target should draw and be drawn to this frame. diff --git a/Graphics/Renderers/CalamityRenderers/DeerclopsShadowRenderer.cs b/Graphics/Renderers/CalamityRenderers/DeerclopsShadowRenderer.cs index ac4203ca1b..8c2f927e08 100644 --- a/Graphics/Renderers/CalamityRenderers/DeerclopsShadowRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/DeerclopsShadowRenderer.cs @@ -1,4 +1,5 @@ -using CalamityMod.NPCs.VanillaNPCAIOverrides.Bosses; +using CalamityMod.Enums; +using CalamityMod.NPCs.VanillaNPCAIOverrides.Bosses; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -11,7 +12,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers { #region Fields/Properties - public override DrawLayer Layer => DrawLayer.AfterEverything; + public override GeneralDrawLayer Layer => GeneralDrawLayer.AfterEverything; public static NPC Deerclops => Main.npc[NPC.deerclopsBoss]; diff --git a/Graphics/Renderers/CalamityRenderers/DoGDeathAnimationRenderer.cs b/Graphics/Renderers/CalamityRenderers/DoGDeathAnimationRenderer.cs index a00c083af6..fab7fc3d6f 100644 --- a/Graphics/Renderers/CalamityRenderers/DoGDeathAnimationRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/DoGDeathAnimationRenderer.cs @@ -1,4 +1,5 @@ -using CalamityMod.NPCs; +using CalamityMod.Enums; +using CalamityMod.NPCs; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -18,7 +19,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers private set; } - public override DrawLayer Layer => DrawLayer.NPC; + public override GeneralDrawLayer Layer => GeneralDrawLayer.AfterNPCs; public override bool ShouldDraw => !Main.gameMenu && CalamityDrawParameterNPC.DoGDeathAnimationTimer > 0; diff --git a/Graphics/Renderers/CalamityRenderers/DyeableShadersRenderer.cs b/Graphics/Renderers/CalamityRenderers/DyeableShadersRenderer.cs index 3b4b977c61..1e4c5f474e 100644 --- a/Graphics/Renderers/CalamityRenderers/DyeableShadersRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/DyeableShadersRenderer.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using CalamityMod.DataStructures; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -28,7 +29,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers private static List RenderersToDrawThisFrame; - public override DrawLayer Layer => DrawLayer.Player; + public override GeneralDrawLayer Layer => GeneralDrawLayer.AfterPlayers; // This ignores MainTarget, and handles its own targets, so always call the draw methods. public override bool ShouldDraw => true; diff --git a/Graphics/Renderers/CalamityRenderers/HolyInfernoRenderer.cs b/Graphics/Renderers/CalamityRenderers/HolyInfernoRenderer.cs index 409ff05a3e..6599c20f89 100644 --- a/Graphics/Renderers/CalamityRenderers/HolyInfernoRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/HolyInfernoRenderer.cs @@ -1,4 +1,5 @@ -using CalamityMod.NPCs; +using CalamityMod.Enums; +using CalamityMod.NPCs; using CalamityMod.NPCs.Providence; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -13,7 +14,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers { #region Fields/Properties - public override DrawLayer Layer => DrawLayer.BeforeTiles; + public override GeneralDrawLayer Layer => GeneralDrawLayer.BeforeAllTiles; public static Providence Provi { diff --git a/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs b/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs index 98871deb15..95ef112087 100644 --- a/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs +++ b/Graphics/Renderers/CalamityRenderers/MiracleBlightRenderer.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using CalamityMod.Enums; using CalamityMod.NPCs; using CalamityMod.NPCs.ExoMechs.Ares; using Microsoft.Xna.Framework; @@ -42,7 +43,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers private set; } - public override DrawLayer Layer => DrawLayer.NPC; + public override GeneralDrawLayer Layer => GeneralDrawLayer.AfterNPCs; // Unsure whether its more performant to always draw, or to check if any NPC actually has the debuff. public override bool ShouldDraw => true; diff --git a/Graphics/Renderers/RendererManager.cs b/Graphics/Renderers/RendererManager.cs index a4b0132ce1..338feab7b0 100644 --- a/Graphics/Renderers/RendererManager.cs +++ b/Graphics/Renderers/RendererManager.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -24,16 +25,8 @@ namespace CalamityMod.Graphics.Renderers if (Main.dedServ) return; - Main.QueueMainThreadAction(() => - { - // This hooks here, because doing it any sooner causes the screen position to be a frame behind. - On_Main.CheckMonoliths += DrawToTargets; - On_Main.DrawNPCs += DrawNPCRenderers; - On_Main.DrawProjectiles += DrawProjectileRenderers; - On_Main.DrawPlayers_AfterProjectiles += DrawPlayerRenderers; - On_Main.DrawBackgroundBlackFill += DrawBeforeTileRenderers; - On_Main.DrawInfernoRings += DrawAfterEverythingRenderers; - }); + // This hooks here, because doing it any sooner causes the screen position to be a frame behind. + Main.QueueMainThreadAction(() => On_Main.CheckMonoliths += DrawToTargets); } public override void Unload() @@ -43,24 +36,13 @@ namespace CalamityMod.Graphics.Renderers // Clear the cached renderers, so they can be readded on mod loading when initialized. Renderers.Clear(); - - Main.QueueMainThreadAction(() => - { - // Supposed to be auto-unloaded, but considering TMods poor track record with actually doing this, manually unload them. - On_Main.CheckMonoliths -= DrawToTargets; - On_Main.DrawNPCs -= DrawNPCRenderers; - On_Main.DrawProjectiles -= DrawProjectileRenderers; - On_Main.DrawPlayers_AfterProjectiles -= DrawPlayerRenderers; - On_Main.DrawBackgroundBlackFill -= DrawBeforeTileRenderers; - On_Main.DrawInfernoRings -= DrawAfterEverythingRenderers; - }); } #endregion #region Updating public override void PreUpdateEntities() { - if (Main.netMode is NetmodeID.Server) + if (Main.dedServ) return; foreach (var renderer in Renderers) @@ -69,7 +51,7 @@ namespace CalamityMod.Graphics.Renderers public override void PostUpdateEverything() { - if (Main.netMode is NetmodeID.Server) + if (Main.dedServ) return; foreach (var renderer in Renderers) @@ -78,6 +60,17 @@ namespace CalamityMod.Graphics.Renderers #endregion #region Drawing + internal static void DrawRendererAtLayer(GeneralDrawLayer drawLayer) + { + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); + + var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer == drawLayer); + foreach (var renderer in renderers) + renderer.DrawTarget(Main.spriteBatch); + + Main.spriteBatch.End(); + } + private void DrawToTargets(On_Main.orig_CheckMonoliths orig) { orig(); @@ -106,82 +99,6 @@ namespace CalamityMod.Graphics.Renderers Main.instance.GraphicsDevice.SetRenderTarget(null); } - - private void DrawNPCRenderers(On_Main.orig_DrawNPCs orig, Main self, bool behindTiles) - { - orig(self, behindTiles); - - if (behindTiles) - return; - - var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer is DrawLayer.NPC); - - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); - - foreach (var renderer in renderers) - renderer.DrawTarget(Main.spriteBatch); - - Main.spriteBatch.ExitShaderRegion(); - } - - private void DrawProjectileRenderers(On_Main.orig_DrawProjectiles orig, Main self) - { - orig(self); - - var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer is DrawLayer.Projectile); - - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); - - foreach (var renderer in renderers) - renderer.DrawTarget(Main.spriteBatch); - - Main.spriteBatch.End(); - } - - private void DrawPlayerRenderers(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self) - { - orig(self); - - var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer is DrawLayer.Player); - - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); - - foreach (var renderer in renderers) - renderer.DrawTarget(Main.spriteBatch); - - Main.spriteBatch.End(); - } - - private void DrawBeforeTileRenderers(On_Main.orig_DrawBackgroundBlackFill orig, Main self) - { - var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer is DrawLayer.BeforeTiles); - - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); - - foreach (var renderer in renderers) - renderer.DrawTarget(Main.spriteBatch); - - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Main.Transform); - - orig(self); - } - - private void DrawAfterEverythingRenderers(On_Main.orig_DrawInfernoRings orig, Main self) - { - var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer is DrawLayer.AfterEverything); - - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise); - foreach (var renderer in renderers) - renderer.DrawTarget(Main.spriteBatch); - - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Main.Transform); - orig(self); - } #endregion } } diff --git a/ILEditing/ILChangesLoading.cs b/ILEditing/ILChangesLoading.cs index 33d7ff2632..3ae73efc26 100644 --- a/ILEditing/ILChangesLoading.cs +++ b/ILEditing/ILChangesLoading.cs @@ -57,6 +57,15 @@ namespace CalamityMod.ILEditing IL_TileDrawing.DrawTrees += DrawTreeGlowMask; On_TileDrawing.DrawBasicTile += DrawTreeTrunkAndCactusGlowMask; + // Graphics (GeneralDrawLayer detours) + On_Main.DrawBackgroundBlackFill += GeneralDrawLayer_DrawToLayer_BeforeAllTiles; + On_Main.DoDraw_Tiles_Solid += GeneralDrawLayer_DrawToLayer_BeforeSolidTiles; + On_Main.DoDraw_DrawNPCsOverTiles += GeneralDrawLayer_DrawToLayer_NPCs; + On_Main.DrawProjectiles += GeneralDrawLayer_DrawToLayer_Projectiles; + On_Main.DrawPlayers_AfterProjectiles += GeneralDrawLayer_DrawToLayer_AfterPlayers; + On_Main.DrawDust += GeneralDrawLayer_DrawToLayer_AfterDusts; + On_Main.DrawInfernoRings += GeneralDrawLayer_DrawToLayer_AfterEverything; + // NPC behavior IL_Main.UpdateTime += PermitNighttimeTownNPCSpawning; On_Main.UpdateTime_SpawnTownNPCs += AlterTownNPCSpawnRate; diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index ecc6161446..4c3b68fb83 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -9,6 +9,9 @@ using CalamityMod.DataStructures; using CalamityMod.Enums; using CalamityMod.Events; using CalamityMod.FluidSimulation; +using CalamityMod.Graphics.Metaballs; +using CalamityMod.Graphics.Primitives; +using CalamityMod.Graphics.Renderers; using CalamityMod.Items.Accessories; using CalamityMod.Items.Accessories.Vanity; using CalamityMod.Items.Armor.Wulfrum; @@ -31,6 +34,7 @@ using CalamityMod.Projectiles; using CalamityMod.Projectiles.Ranged; using CalamityMod.Projectiles.Typeless; using CalamityMod.Systems; +using CalamityMod.Systems.Graphic.PixelationSystem; using CalamityMod.Systems.Mechanic; using CalamityMod.Tiles; using CalamityMod.Walls; @@ -914,6 +918,82 @@ namespace CalamityMod.ILEditing } #endregion + #region GeneralDrawLayer Systems Drawing + private static void GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer drawLayer) + { + Main.spriteBatch.SafeAction(() => + { + Main.spriteBatch.TryEnd(); + GeneralParticleHandler.DrawParticleCollectionsAtSpecificLayer(drawLayer); + }); + + Main.spriteBatch.SafeAction(() => + { + Main.spriteBatch.TryEnd(); + Main.spriteBatch.TryBegin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); + MetaballManager.DrawMetaballs(drawLayer); + }); + + Main.spriteBatch.SafeAction(() => + { + Main.spriteBatch.TryEnd(); + PrimitivePixelationSystem.DrawTargetScaled(drawLayer); + }); + + PixelationManager.DrawPixelatedTargets(drawLayer); + + Main.spriteBatch.SafeAction(() => + { + Main.spriteBatch.TryEnd(); + RendererManager.DrawRendererAtLayer(drawLayer); + }); + } + + private static void GeneralDrawLayer_DrawToLayer_BeforeAllTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self) + { + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeAllTiles); + orig(self); + } + + private static void GeneralDrawLayer_DrawToLayer_BeforeSolidTiles(On_Main.orig_DoDraw_Tiles_Solid orig, Main self) + { + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeSolidTiles); + orig(self); + } + + private static void GeneralDrawLayer_DrawToLayer_NPCs(On_Main.orig_DoDraw_DrawNPCsOverTiles orig, Main self) + { + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeNPCs); + orig(self); + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterNPCs); + } + + private static void GeneralDrawLayer_DrawToLayer_Projectiles(On_Main.orig_DrawProjectiles orig, Main self) + { + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeProjectiles); + orig(self); + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterProjectiles); + } + + private static void GeneralDrawLayer_DrawToLayer_AfterPlayers(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self) + { + orig(self); + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterPlayers); + } + + private static void GeneralDrawLayer_DrawToLayer_AfterDusts(On_Main.orig_DrawDust orig, Main self) + { + orig(self); + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterDusts); + } + + private static void GeneralDrawLayer_DrawToLayer_AfterEverything(On_Main.orig_DrawInfernoRings orig, Main self) + { + orig(self); + GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterEverything); + } + #endregion + #region General Particle Rendering private static void DrawFusableParticles(On_Main.orig_SortDrawCacheWorms orig, Main self) { diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index c50b6d413e..0e87641d04 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -57,15 +57,16 @@ namespace CalamityMod.Particles /// internal static Dictionary particleTexturesByIDs; - private static Dictionary particleAutoDrawingOverrides; - + // Collections for tracking particles active in the world. private static List activeParticles; private static List particlesToKill; private static Dictionary> particlesToSpawnNextFrame; private static Dictionary> particlesToSpawnNextFrame_Pixelated; + // Collections for correctly organizing active prticles for drawing. private static Dictionary> particlesToDraw; private static Dictionary> particlesToDraw_Pixelated; + private static Dictionary> particlesToDraw_CustomShader; #endregion #region Loading and Unloading @@ -93,22 +94,6 @@ namespace CalamityMod.Particles texturePath = instance.Texture; particleTexturesByIDs[ID] = ModContent.Request(texturePath, AssetRequestMode.ImmediateLoad).Value; }); - - foreach (Mod mod in ModLoader.Mods) - { - var drawers = mod.GetContent().Where(c => c is ParticleAutoDrawingOverride).Select(c => c as ParticleAutoDrawingOverride); - foreach (var drawer in drawers) - { - if (drawer.TargetParticleTypeName != "") - { - if (particleTypesByNames.TryGetValue(drawer.TargetParticleTypeName, out Type particleType)) - particleAutoDrawingOverrides.Add(particleType, drawer); - else - throw new Exception($"The Particle of name \"{drawer.TargetParticleTypeName}\" used in {drawer.GetType().Name} could not be found! " + - $"Please ensure the Particle's internal name is spelt correctly and is prefixed by its home mod's internal name."); - } - } - } } public override void Load() @@ -117,21 +102,14 @@ namespace CalamityMod.Particles particleIDsByTypes = []; particleTexturesByIDs = []; - particleAutoDrawingOverrides = []; - activeParticles = []; particlesToKill = []; particlesToSpawnNextFrame = []; particlesToSpawnNextFrame_Pixelated = []; + particlesToDraw = []; particlesToDraw_Pixelated = []; - - On_Main.DrawBackgroundBlackFill += DrawParticles_BeforeTiles; - On_Main.DrawNPCs += DrawParticles_NPCs; - On_Main.DrawProjectiles += DrawParticles_Projectiles; - On_Main.DrawPlayers_AfterProjectiles += DrawParticles_AfterPlayers; - On_Main.DrawDust += DrawParticles_AfterDusts; - On_Main.DrawInfernoRings += DrawParticles_AfterEverything; + particlesToDraw_CustomShader = []; } public override void Unload() @@ -140,14 +118,14 @@ namespace CalamityMod.Particles particleIDsByTypes = null; particleTexturesByIDs = null; - particleAutoDrawingOverrides = null; - activeParticles = null; particlesToKill = null; particlesToSpawnNextFrame = null; particlesToSpawnNextFrame_Pixelated = null; + particlesToDraw = null; particlesToDraw_Pixelated = null; + particlesToDraw_CustomShader = null; } public override void OnWorldUnload() @@ -156,8 +134,10 @@ namespace CalamityMod.Particles particlesToKill.Clear(); particlesToSpawnNextFrame.Clear(); particlesToSpawnNextFrame_Pixelated.Clear(); + particlesToDraw.Clear(); particlesToDraw_Pixelated.Clear(); + particlesToDraw_CustomShader.Clear(); } #endregion @@ -312,62 +292,14 @@ namespace CalamityMod.Particles particlesToKill.Clear(); } - private static void DrawParticles_BeforeTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self) + internal static void DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer drawLayer) { - Main.spriteBatch.End(); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.BeforeTiles); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); + if (Main.dedServ) + return; - orig(self); - } - - private static void DrawParticles_NPCs(On_Main.orig_DrawNPCs orig, Main self, bool behindTiles) - { - if (!behindTiles) - { - Main.spriteBatch.End(); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.BeforeNPCs); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - } - - orig(self, behindTiles); - - if (!behindTiles) - { - Main.spriteBatch.End(); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.AfterNPCs); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix); - } - } - - private static void DrawParticles_Projectiles(On_Main.orig_DrawProjectiles orig, Main self) - { - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.BeforeProjectiles); - - orig(self); - - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.AfterProjectiles); - } - - private static void DrawParticles_AfterPlayers(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self) - { - orig(self); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.AfterPlayers); - } - - private static void DrawParticles_AfterDusts(On_Main.orig_DrawDust orig, Main self) - { - orig(self); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.AfterDusts); - } - - private static void DrawParticles_AfterEverything(On_Main.orig_DrawInfernoRings orig, Main self) - { - orig(self); - - Main.spriteBatch.End(); - DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer.AfterEverything); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Main.Transform); + DrawParticleCollection(particlesToDraw, drawLayer); + DrawParticleCollection(particlesToDraw_Pixelated, drawLayer, true); + DrawParticlesWithShaders(drawLayer); } private static void DrawParticleInstance(Particle particle) @@ -454,35 +386,27 @@ namespace CalamityMod.Particles } } - private static void DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer drawLayer) + private static void DrawParticlesWithShaders(GeneralDrawLayer drawLayer) { - if (Main.dedServ) - return; - - DrawParticleCollection(particlesToDraw, drawLayer); - DrawParticleCollection(particlesToDraw_Pixelated, drawLayer, true); - - foreach (ParticleAutoDrawingOverride drawer in particleAutoDrawingOverrides.Values) + foreach (var keyValuePair in particlesToDraw_CustomShader) { - if (!drawer.ShouldDrawParticles) - return; - - foreach (var keyValuePair in drawer.ActiveParticleInstances) + // If there's nothing to be drawn, remove the effect from the dictionary and return. + if (keyValuePair.Value.Count == 0) { - if (keyValuePair.Value.Count == 0) - return; - - if (drawer.DrawLayerOverride.HasValue) - { - if (drawer.DrawLayerOverride.Value == drawLayer) - drawer.DrawAllParticles(Main.spriteBatch, keyValuePair.Value); - } - else - { - if (keyValuePair.Key == drawLayer) - drawer.DrawAllParticles(Main.spriteBatch, keyValuePair.Value); - } + particlesToDraw_CustomShader.Remove(keyValuePair.Key); + return; } + + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullNone, keyValuePair.Key, Main.GameViewMatrix.TransformationMatrix); + + var particlesAtDrawLayer = keyValuePair.Value.Where(p => p.DrawLayer == drawLayer); + foreach (Particle particle in particlesAtDrawLayer) + { + particle.PrepareCustomShader(keyValuePair.Key); + DrawParticleInstance(particle); + } + + Main.spriteBatch.End(); } } @@ -536,11 +460,11 @@ namespace CalamityMod.Particles private static void AddToAssociatedDrawCollection(Particle particle) { - if (particle.OverrideAutomaticDrawing && particleAutoDrawingOverrides[particle.GetType()].ShouldDrawParticles) + if (particle.CustomShader != null) { - if (!particleAutoDrawingOverrides[particle.GetType()].ActiveParticleInstances.ContainsKey(particle.DrawLayer)) - particleAutoDrawingOverrides[particle.GetType()].ActiveParticleInstances[particle.DrawLayer] = []; - particleAutoDrawingOverrides[particle.GetType()].ActiveParticleInstances[particle.DrawLayer].Add(particle); + if (!particlesToDraw_CustomShader.ContainsKey(particle.CustomShader)) + particlesToDraw_CustomShader[particle.CustomShader] = []; + particlesToDraw_CustomShader[particle.CustomShader].Add(particle); } else { @@ -550,8 +474,8 @@ namespace CalamityMod.Particles private static void RemoveFromAssociatedDrawCollection(Particle particle) { - if (particle.OverrideAutomaticDrawing) - particleAutoDrawingOverrides[particle.GetType()].ActiveParticleInstances[particle.DrawLayer].Remove(particle); + if (particle.CustomShader != null) + particlesToDraw_CustomShader[particle.CustomShader].Remove(particle); else ReturnAssociatedDrawCollection(particle).Remove(particle); } diff --git a/Particles/Particle.cs b/Particles/Particle.cs index ddc3f94bcf..2de8391621 100644 --- a/Particles/Particle.cs +++ b/Particles/Particle.cs @@ -75,9 +75,14 @@ namespace CalamityMod.Particles /// /// The "layer" or point at which you'd like your particle to draw in Terraria's internal draw order. - ///
Defaults to .
+ ///
Defaults to .
///
- public GeneralDrawLayer DrawLayer = GeneralDrawLayer.AfterEverything; + public GeneralDrawLayer DrawLayer = GeneralDrawLayer.AfterDusts; + + /// + /// An 0-1 interpolant representing how close this particle is from its . + /// + public float LifetimeCompletion => Lifetime != 0 ? Time / (float)Lifetime : 0; /// /// The path to this particle's autoloaded texture. @@ -92,11 +97,6 @@ namespace CalamityMod.Particles /// public virtual int FrameVariants => 1; - /// - /// An 0-1 interpolant representing how close this particle is from its . - /// - public float LifetimeCompletion => Lifetime != 0 ? Time / (float)Lifetime : 0; - /// /// Set this to true if you NEED the particle to render even if the particle cap is reached. /// @@ -123,10 +123,10 @@ namespace CalamityMod.Particles public virtual bool UseCustomDraw => false; /// - /// Override and set this to true to disable automatic drawing for all instances of this particle entirely. - ///
Drawing of this particle must be done from a class.
+ /// An optional shader parameter for this paricle to be drawn with. + /// Use to set shader parameters accordingly. ///
- public virtual bool OverrideAutomaticDrawing => false; + public virtual Effect CustomShader => null; /// /// Use this method if you want to handle the particle drawing yourself. Only called if is set to true. @@ -139,6 +139,11 @@ namespace CalamityMod.Particles /// The base position of the particle set. public virtual void CustomDraw(SpriteBatch spriteBatch, Vector2 basePosition) { } + /// + /// Use this method if you're using a custom shader effect with this particle type to set shader parameters accordingly. + /// + public virtual void PrepareCustomShader(Effect shader) { } + /// /// Called every frame in . /// The particle's velocity gets automatically added to its position, and its time automatically increases. diff --git a/Particles/ParticleAutoDrawingOverride.cs b/Particles/ParticleAutoDrawingOverride.cs deleted file mode 100644 index 5a5ec3a257..0000000000 --- a/Particles/ParticleAutoDrawingOverride.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.Collections.Generic; -using CalamityMod.Enums; -using Microsoft.Xna.Framework.Graphics; -using Terraria.ModLoader; - -namespace CalamityMod.Particles -{ - /// - /// Allows one to directly control how all active instances of a specified particle type in the world are drawn. - ///
Useful for cases such as applying shaders to particles without resetting the SpriteBatch for every individual particle instance.
- ///
- /// - /// Note that particles using an instance of this class MUST have set to true. - /// - public abstract class ParticleAutoDrawingOverride : ModType - { - /// - /// All active particle instances for this drawer, identified by a particle's draw layer. - /// - public Dictionary> ActiveParticleInstances { get; private set; } = []; - - /// - /// The internal name of the particle type that should be manually drawn. - ///
Particle names should be prefixed with the internal name of the mod they are from.
- ///
e.g. "CalamityMod.PulseRing" is valid, while just "PulseRing" is not.
- ///
- /// - /// This is only checked once when the mod is loading. Changing this during runtime will not affect anything. - /// - public abstract string TargetParticleTypeName { get; } - - /// - /// Whether or not this drawer should draw its particles this frame. - ///
Return false to prevent currently stored particles from being drawn and new ones from being added to .
- ///
- /// - /// Returns true by default. - /// - public virtual bool ShouldDrawParticles => true; - - /// - /// An optional property which will override the draw layer of all particles for this drawer entirely, using the specified one here instead. - /// - public virtual GeneralDrawLayer? DrawLayerOverride => null; - - protected sealed override void Register() => ModTypeLookup.Register(this); - - public sealed override void SetupContent() => SetStaticDefaults(); - - /// - /// Write all relevenat draw code for your particles here. - /// - /// Stores particles based on their default draw layer. - ///
Iterate through this collection to run individual particle drawing logic.
- public abstract void DrawAllParticles(SpriteBatch spriteBatch, List particlesToDraw); - } -} diff --git a/Projectiles/Boss/DoGFire.cs b/Projectiles/Boss/DoGFire.cs index e036754e9f..dcb6e68f66 100644 --- a/Projectiles/Boss/DoGFire.cs +++ b/Projectiles/Boss/DoGFire.cs @@ -4,6 +4,7 @@ using CalamityMod.Buffs.DamageOverTime; using CalamityMod.DataStructures; using CalamityMod.Graphics.Primitives; using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria.DataStructures; @@ -12,6 +13,7 @@ using Terraria.Audio; using Terraria.Graphics.Shaders; using Terraria.ID; using Terraria.ModLoader; + namespace CalamityMod.Projectiles.Boss { public class DoGFire : ModProjectile, ILocalizedModType, IPixelatedPrimitiveRenderer @@ -159,7 +161,7 @@ namespace CalamityMod.Projectiles.Boss return Color.Lerp(fullBodyColor, Color.White, 0.175f); } - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { // Render the main trail for the body for the flame. GameShaders.Misc["CalamityMod:ImpFlameTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/ScarletDevilStreak")); diff --git a/Projectiles/Magic/SHPB.cs b/Projectiles/Magic/SHPB.cs index 8e37b4b884..9b2448527d 100644 --- a/Projectiles/Magic/SHPB.cs +++ b/Projectiles/Magic/SHPB.cs @@ -2,6 +2,7 @@ using CalamityMod.Graphics.Primitives; using CalamityMod.Items.Weapons.Magic; using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -13,7 +14,7 @@ namespace CalamityMod.Projectiles.Magic { public class SHPB : ModProjectile, ILocalizedModType, IPixelatedPrimitiveRenderer { - public PixelationPrimitiveLayer LayerToRenderTo => PixelationPrimitiveLayer.BeforeProjectiles; + public GeneralDrawLayer LayerToRenderTo => GeneralDrawLayer.BeforeProjectiles; public ref float ExplodeTimer => ref Projectile.ai[2]; @@ -377,7 +378,7 @@ namespace CalamityMod.Projectiles.Magic return Color.Lerp(endColor, Color.White, deathInterpolant); } - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { GameShaders.Misc["CalamityMod:ImpFlameTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/SylvestaffStreak")); PrimitiveRenderer.RenderTrail(Projectile.oldPos, new(PlasmaBallWidthFunction, PlasmaBallColorFunction, (_) => Projectile.Size * 0.5f, true, true, GameShaders.Misc["CalamityMod:ImpFlameTrail"]), Projectile.oldPos.Length * 2); diff --git a/Projectiles/Magic/SHPL.cs b/Projectiles/Magic/SHPL.cs index 055311f127..2d704a38a0 100644 --- a/Projectiles/Magic/SHPL.cs +++ b/Projectiles/Magic/SHPL.cs @@ -2,6 +2,7 @@ using System.Linq; using CalamityMod.Graphics.Primitives; using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -22,7 +23,7 @@ namespace CalamityMod.Projectiles.Magic public override string Texture => "CalamityMod/Projectiles/InvisibleProj"; - public PixelationPrimitiveLayer LayerToRenderTo => PixelationPrimitiveLayer.BeforeProjectiles; + public GeneralDrawLayer LayerToRenderTo => GeneralDrawLayer.BeforeProjectiles; public override void SetStaticDefaults() { @@ -137,7 +138,7 @@ namespace CalamityMod.Projectiles.Magic return Color.Lerp(startColor, bodyColor, completion); } - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { // Render the main trail for the body for the laser. GameShaders.Misc["CalamityMod:ImpFlameTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/SylvestaffStreak")); diff --git a/Projectiles/Magic/SHPS.cs b/Projectiles/Magic/SHPS.cs index bd02542b8b..d4d256b49c 100644 --- a/Projectiles/Magic/SHPS.cs +++ b/Projectiles/Magic/SHPS.cs @@ -3,6 +3,7 @@ using System.Linq; using CalamityMod.DataStructures; using CalamityMod.Graphics.Primitives; using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -234,7 +235,7 @@ namespace CalamityMod.Projectiles.Magic return Color.Lerp(Color.White, tipColor, completion); } - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { // Render the main trail for the body for the soul. GameShaders.Misc["CalamityMod:ImpFlameTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/ScarletDevilStreak")); diff --git a/Projectiles/Magic/SylvestaffHoldout.cs b/Projectiles/Magic/SylvestaffHoldout.cs index 3109ae5bff..095581baa7 100644 --- a/Projectiles/Magic/SylvestaffHoldout.cs +++ b/Projectiles/Magic/SylvestaffHoldout.cs @@ -2,6 +2,7 @@ using CalamityMod.Graphics.Primitives; using CalamityMod.Items.Weapons.Magic; using CalamityMod.Physics; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -14,7 +15,7 @@ namespace CalamityMod.Projectiles.Magic { public class SylvestaffHoldout : ModProjectile, IPixelatedPrimitiveRenderer { - public PixelationPrimitiveLayer LayerToRenderTo => PixelationPrimitiveLayer.BeforeProjectiles | PixelationPrimitiveLayer.AfterPlayers; + public GeneralDrawLayer LayerToRenderTo => GeneralDrawLayer.BeforeProjectiles | GeneralDrawLayer.AfterPlayers; public override LocalizedText DisplayName => CalamityUtils.GetItemName(); @@ -186,9 +187,9 @@ namespace CalamityMod.Projectiles.Magic } } - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { - bool backLayer = layer == PixelationPrimitiveLayer.BeforeProjectiles; + bool backLayer = layer == GeneralDrawLayer.BeforeProjectiles; RenderRibbon(LeftRibbon, -1, backLayer); RenderRibbon(RightRibbon, 1, backLayer); } diff --git a/Projectiles/Melee/Galaxia_PhoenixsPrideFirewall.cs b/Projectiles/Melee/Galaxia_PhoenixsPrideFirewall.cs index 90f305fa35..9bc3a59757 100644 --- a/Projectiles/Melee/Galaxia_PhoenixsPrideFirewall.cs +++ b/Projectiles/Melee/Galaxia_PhoenixsPrideFirewall.cs @@ -2,6 +2,7 @@ using CalamityMod.Graphics.Primitives; using CalamityMod.Items.Weapons.Melee; using CalamityMod.Particles; +using CalamityMod.Enums; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; @@ -81,7 +82,7 @@ namespace CalamityMod.Projectiles.Melee public float WidthFunction(float completionRatio) => MathF.Max(50f * Scale * (MathF.Pow(1f - completionRatio, 2) * 2.75f), 0.35f); public Color ColorFunction(float completionRatio) => Color.Orange * (completionRatio > 0.1f ? 1f : completionRatio * 10f); - public void RenderPixelatedPrimitives(SpriteBatch spritebatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spritebatch, GeneralDrawLayer layer) { GameShaders.Misc["CalamityMod:Bordernado"].UseSaturation(-0.2f); GameShaders.Misc["CalamityMod:Bordernado"].UseOpacity(fadeOut); diff --git a/Projectiles/Summon/LiliesOfEternityBullet.cs b/Projectiles/Summon/LiliesOfEternityBullet.cs index d87e226da0..d60b3bb10a 100644 --- a/Projectiles/Summon/LiliesOfEternityBullet.cs +++ b/Projectiles/Summon/LiliesOfEternityBullet.cs @@ -1,4 +1,5 @@ -using CalamityMod.Graphics.Primitives; +using CalamityMod.Enums; +using CalamityMod.Graphics.Primitives; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -60,7 +61,7 @@ namespace CalamityMod.Projectiles.Summon private Color PrimitiveColorFunction(float completionRatio) => Color.Lerp(Color.DarkGoldenrod, Color.LightGoldenrodYellow, completionRatio); - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { GameShaders.Misc["CalamityMod:TrailStreak"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/SylvestaffStreak")); PrimitiveRenderer.RenderTrail(Projectile.oldPos, new(PrimitiveWidthFunction, PrimitiveColorFunction, (_) => Projectile.Size * 0.5f, pixelate: true, shader: GameShaders.Misc["CalamityMod:TrailStreak"])); diff --git a/Projectiles/Summon/LiliesOfFinalityBullet.cs b/Projectiles/Summon/LiliesOfFinalityBullet.cs index 19a527e5d2..b969ecd814 100644 --- a/Projectiles/Summon/LiliesOfFinalityBullet.cs +++ b/Projectiles/Summon/LiliesOfFinalityBullet.cs @@ -1,4 +1,5 @@ -using CalamityMod.Graphics.Primitives; +using CalamityMod.Enums; +using CalamityMod.Graphics.Primitives; using CalamityMod.Items.Weapons.Summon; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -64,7 +65,7 @@ namespace CalamityMod.Projectiles.Summon private Color PrimitiveColorFunction(float completionRatio) => Color.Lerp(Color.DarkGoldenrod, Color.LightGoldenrodYellow, completionRatio); - public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer) + public void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer) { GameShaders.Misc["CalamityMod:TrailStreak"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/SylvestaffStreak")); PrimitiveRenderer.RenderTrail(Projectile.oldPos, new(PrimitiveWidthFunction, PrimitiveColorFunction, (_) => Projectile.Size * 0.5f, pixelate: true, shader: GameShaders.Misc["CalamityMod:TrailStreak"])); diff --git a/Systems/Graphic/PixelationSystem/PixelationManager.cs b/Systems/Graphic/PixelationSystem/PixelationManager.cs index 00628ffda8..8758c40444 100644 --- a/Systems/Graphic/PixelationSystem/PixelationManager.cs +++ b/Systems/Graphic/PixelationSystem/PixelationManager.cs @@ -12,7 +12,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem { public class PixelationManager : ModSystem { - private static Dictionary PixelTargets_BeforeTiles; + private static Dictionary PixelTargets_BeforeAllTiles; + private static Dictionary PixelTargets_BeforeSolidTiles; private static Dictionary PixelTargets_BeforeNPCs; private static Dictionary PixelTargets_AfterNPCs; private static Dictionary PixelTargets_BeforeProjectiles; @@ -21,7 +22,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem private static Dictionary PixelTargets_AfterDusts; private static Dictionary PixelTargets_AfterEverything; - private static List ActivePixelatedDrawers_BeforeTiles; + private static List ActivePixelatedDrawers_BeforeAllTiles; + private static List ActivePixelatedDrawers_BeforeSolidTiles; private static List ActivePixelatedDrawers_BeforeNPCs; private static List ActivePixelatedDrawers_AfterNPCs; private static List ActivePixelatedDrawers_BeforeProjectiles; @@ -54,12 +56,13 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem ///
internal static RenderTarget2D CreatePixelTarget(int width, int height) => new(Main.graphics.GraphicsDevice, (int)(width * PixelationResolution), (int)(height * PixelationResolution)); - public override void OnModLoad() + public override void Load() { if (Main.dedServ) return; - PixelTargets_BeforeTiles = []; + PixelTargets_BeforeAllTiles = []; + PixelTargets_BeforeSolidTiles = []; PixelTargets_BeforeNPCs = []; PixelTargets_AfterNPCs = []; PixelTargets_BeforeProjectiles = []; @@ -68,7 +71,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem PixelTargets_AfterDusts = []; PixelTargets_AfterEverything = []; - ActivePixelatedDrawers_BeforeTiles = []; + ActivePixelatedDrawers_BeforeAllTiles = []; + ActivePixelatedDrawers_BeforeSolidTiles = []; ActivePixelatedDrawers_BeforeNPCs = []; ActivePixelatedDrawers_AfterNPCs = []; ActivePixelatedDrawers_BeforeProjectiles = []; @@ -78,20 +82,15 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem ActivePixelatedDrawers_AfterEverything = []; RenderTargetManager.RenderTargetUpdateLoopEvent += PrepareTargets; - On_Main.DrawBackgroundBlackFill += DrawPixelated_BeforeTiles; - On_Main.DrawNPCs += DrawPixelated_NPCs; - On_Main.DrawProjectiles += DrawPixelated_Projectiles; - On_Main.DrawPlayers_AfterProjectiles += DrawPixelated_AfterPlayers; - On_Main.DrawDust += DrawPixelated_AfterDusts; - On_Main.DrawInfernoRings += DrawPixelated_AfterEverything; } - public override void OnModUnload() + public override void Unload() { if (Main.dedServ) return; - PixelTargets_BeforeTiles = null; + PixelTargets_BeforeAllTiles = null; + PixelTargets_BeforeSolidTiles = null; PixelTargets_BeforeNPCs = null; PixelTargets_AfterNPCs = null; PixelTargets_BeforeProjectiles = null; @@ -100,7 +99,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem PixelTargets_AfterDusts = null; PixelTargets_AfterEverything = null; - ActivePixelatedDrawers_BeforeTiles = null; + ActivePixelatedDrawers_BeforeAllTiles = null; + ActivePixelatedDrawers_BeforeSolidTiles = null; ActivePixelatedDrawers_BeforeNPCs = null; ActivePixelatedDrawers_AfterNPCs = null; ActivePixelatedDrawers_BeforeProjectiles = null; @@ -115,7 +115,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem if (Main.dedServ) return; - ActivePixelatedDrawers_BeforeTiles.Clear(); + ActivePixelatedDrawers_BeforeAllTiles.Clear(); + ActivePixelatedDrawers_BeforeSolidTiles.Clear(); ActivePixelatedDrawers_BeforeNPCs.Clear(); ActivePixelatedDrawers_AfterNPCs.Clear(); ActivePixelatedDrawers_BeforeProjectiles.Clear(); @@ -152,7 +153,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem if (!Main.gameMenu && !Main.dedServ) { // Draw all collections to their respctive render targets. - DrawCollectionsToTarget(PixelTargets_BeforeTiles, PixelationMatrix, ActivePixelatedDrawers_BeforeTiles); + DrawCollectionsToTarget(PixelTargets_BeforeAllTiles, PixelationMatrix, ActivePixelatedDrawers_BeforeAllTiles); + DrawCollectionsToTarget(PixelTargets_BeforeSolidTiles, PixelationMatrix, ActivePixelatedDrawers_BeforeSolidTiles); DrawCollectionsToTarget(PixelTargets_BeforeNPCs, PixelationMatrix, ActivePixelatedDrawers_BeforeNPCs); DrawCollectionsToTarget(PixelTargets_AfterNPCs, PixelationMatrix, ActivePixelatedDrawers_AfterNPCs); DrawCollectionsToTarget(PixelTargets_BeforeProjectiles, PixelationMatrix, ActivePixelatedDrawers_BeforeProjectiles); @@ -164,49 +166,7 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem Main.graphics.GraphicsDevice.SetRenderTarget(null); } } - - private static void DrawPixelated_BeforeTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self) - { - DrawPixelatedTargets(PixelTargets_BeforeTiles); - orig(self); - } - - private static void DrawPixelated_NPCs(On_Main.orig_DrawNPCs orig, Main self, bool behindTiles) - { - if (!behindTiles) - DrawPixelatedTargets(PixelTargets_BeforeNPCs); - - orig(self, behindTiles); - - if (!behindTiles) - DrawPixelatedTargets(PixelTargets_AfterNPCs); - } - - private static void DrawPixelated_Projectiles(On_Main.orig_DrawProjectiles orig, Main self) - { - DrawPixelatedTargets(PixelTargets_BeforeProjectiles); - orig(self); - DrawPixelatedTargets(PixelTargets_AfterProjectiles); - } - - private static void DrawPixelated_AfterPlayers(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self) - { - orig(self); - DrawPixelatedTargets(PixelTargets_AfterPlayers); - } - - private static void DrawPixelated_AfterDusts(On_Main.orig_DrawDust orig, Main self) - { - orig(self); - DrawPixelatedTargets(PixelTargets_AfterDusts); - } - - private static void DrawPixelated_AfterEverything(On_Main.orig_DrawInfernoRings orig, Main self) - { - orig(self); - DrawPixelatedTargets(PixelTargets_AfterEverything); - } - + private static void DrawCollectionsToTarget(Dictionary targetCollection, Matrix pixelationMatrix, List drawerCollection) { foreach (var blendStateTargetPair in targetCollection) @@ -229,8 +189,9 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem drawerCollection.Clear(); } - private static void DrawPixelatedTargets(Dictionary targetCollection) + internal static void DrawPixelatedTargets(GeneralDrawLayer drawLayer) { + var targetCollection = ReturnAssociatedTargetCollection(drawLayer); Main.spriteBatch.SafeAction(() => { foreach (var keyValuePair in targetCollection) @@ -248,7 +209,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem { List drawerCollection = drawLayer switch { - GeneralDrawLayer.BeforeTiles => ActivePixelatedDrawers_BeforeTiles, + GeneralDrawLayer.BeforeAllTiles => ActivePixelatedDrawers_BeforeAllTiles, + GeneralDrawLayer.BeforeSolidTiles => ActivePixelatedDrawers_BeforeSolidTiles, GeneralDrawLayer.BeforeNPCs => ActivePixelatedDrawers_BeforeNPCs, GeneralDrawLayer.AfterNPCs => ActivePixelatedDrawers_AfterNPCs, GeneralDrawLayer.BeforeProjectiles => ActivePixelatedDrawers_BeforeProjectiles, @@ -261,13 +223,36 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem return drawerCollection; } + private static Dictionary ReturnAssociatedTargetCollection(GeneralDrawLayer drawLayer) + { + Dictionary targetCollection = drawLayer switch + { + GeneralDrawLayer.BeforeAllTiles => PixelTargets_BeforeAllTiles, + GeneralDrawLayer.BeforeSolidTiles => PixelTargets_BeforeSolidTiles, + GeneralDrawLayer.BeforeNPCs => PixelTargets_BeforeNPCs, + GeneralDrawLayer.AfterNPCs => PixelTargets_AfterNPCs, + GeneralDrawLayer.BeforeProjectiles => PixelTargets_BeforeProjectiles, + GeneralDrawLayer.AfterProjectiles => PixelTargets_AfterProjectiles, + GeneralDrawLayer.AfterPlayers => PixelTargets_AfterPlayers, + GeneralDrawLayer.AfterDusts => PixelTargets_AfterDusts, + _ => PixelTargets_AfterEverything, + }; + + return targetCollection; + } + private static void VerifyTargetExistence(GeneralDrawLayer drawLayer, BlendState blendState) { switch (drawLayer) { - case GeneralDrawLayer.BeforeTiles: - if (!PixelTargets_BeforeTiles.ContainsKey(blendState)) - Main.QueueMainThreadAction(() => PixelTargets_BeforeTiles[blendState] = new(true, CreatePixelTarget)); + case GeneralDrawLayer.BeforeAllTiles: + if (!PixelTargets_BeforeAllTiles.ContainsKey(blendState)) + Main.QueueMainThreadAction(() => PixelTargets_BeforeAllTiles[blendState] = new(true, CreatePixelTarget)); + break; + + case GeneralDrawLayer.BeforeSolidTiles: + if (!PixelTargets_BeforeSolidTiles.ContainsKey(blendState)) + Main.QueueMainThreadAction(() => PixelTargets_BeforeSolidTiles[blendState] = new(true, CreatePixelTarget)); break; case GeneralDrawLayer.BeforeNPCs: diff --git a/Systems/Graphic/TempParticleSystem/TempParticle.cs b/Systems/Graphic/TempParticleSystem/TempParticle.cs new file mode 100644 index 0000000000..19b747cbe7 --- /dev/null +++ b/Systems/Graphic/TempParticleSystem/TempParticle.cs @@ -0,0 +1,67 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ReLogic.Content; + +namespace CalamityMod.Systems.Graphic.TempParticleSystem +{ + public class TempParticle + { + public Asset StoredTexture; + + public Vector2 Position; + + public Vector2 StoredPosition; + + public Vector2 Velocity; + + public Vector2 Scale; + + public Color DrawColor; + + public float Rotation; + + public float Opacity; + + public float Depth; + + public int Lifetime; + + public int Time; + + public int FrameX; + + public int FrameY; + + public int MaxVerticalFrames; + + public int MaxHorizontalFrames; + + public float[] ExtraData; + + /// + /// An 0-1 interpolant representing how close this particle is from its . + /// + public float LifetimeCompletionRatio => MathHelper.Clamp(Time / (float)Lifetime, 0f, 1f); + + public void SetBasicParticleData(Asset storedTexture, Vector2 position, Vector2 storedPosition, Vector2 velocity, Vector2 scale, Color drawColor, float rotation, float opacity, float depth, int lifetime, int frameX, int frameY, int maxHorizontalFrames, int maxVerticalFrames) + { + StoredTexture = storedTexture; + Position = position; + StoredPosition = storedPosition; + Velocity = velocity; + Scale = scale; + DrawColor = drawColor; + Rotation = rotation; + Opacity = opacity; + Depth = depth; + Lifetime = lifetime; + FrameX = frameX; + FrameY = frameY; + MaxHorizontalFrames = maxHorizontalFrames; + MaxVerticalFrames = maxVerticalFrames; + + Time = 0; + ExtraData = new float[8]; + } + } +} diff --git a/Systems/Graphic/TempParticleSystem/TempParticleManager.cs b/Systems/Graphic/TempParticleSystem/TempParticleManager.cs new file mode 100644 index 0000000000..3a63fabf46 --- /dev/null +++ b/Systems/Graphic/TempParticleSystem/TempParticleManager.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ReLogic.Content; +using ReLogic.Threading; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Systems.Graphic.TempParticleSystem +{ + public class TempParticleManager + { + public delegate void ParticleUpdateFunctionDelegate(TempParticle tempParticle); + + public delegate void ParticleDrawFunctionDelegate(TempParticle tempParticle, SpriteBatch spriteBatch, Vector2 drawOffset); + + public List ActiveParticles; + + public Queue ParticlePool; + + public int MaxParticles; + + public Asset BaseParticleTexture; + + public ParticleUpdateFunctionDelegate ParticleUpdateFunction; + + public ParticleDrawFunctionDelegate ParticleDrawingOverride; + + /// + /// A temporary particle handler made for the typically short-term creation and manipulation of particle effects done by instances of a generic particle object . + ///
Should typically be used in areas where is less practical or cannot work at all.
+ ///
+ /// The maximum amount of particles that can exist at a time in this manager. + /// The base texture that particles spawned by this manager will use when drawn. + /// An additional update function to control how particles spawned by this manager are updated. + /// Set this parameter to override how particles spawned by this manager are drawn. + public TempParticleManager(int maxParticles, string baseParticleTexturePath, ParticleUpdateFunctionDelegate particleUpdateFunction = null, ParticleDrawFunctionDelegate particleDrawingOverride = null) + { + MaxParticles = maxParticles; + BaseParticleTexture = ModContent.Request(baseParticleTexturePath); + ParticleUpdateFunction = particleUpdateFunction; + ParticleDrawingOverride = particleDrawingOverride; + + ActiveParticles = []; + if (ParticlePool is null) + { + ParticlePool = []; + for (int i = 0; i < maxParticles; i++) + ParticlePool.Enqueue(new TempParticle()); + } + } + + /// + /// Spawns a instance attached to a . + /// + /// The initial spawn position of this particle. + /// The initial velocity of this particle. + /// The initial scale of this particle. + /// The base color this particle is drawn in. + /// The maximum amount of time this particle can be alive for. + /// The initial rotation of this particle. + /// The initial opacity of this particle. + /// The depth at which this particle appears. Should only be used in cases where particles need to be drawn in the foreground or background. + /// The initial X-frame of this particle on its spritesheet. + /// The initial Y-frame of this particle on its spritesheet. + /// The maximum amount of horizontal frames this particle has on its spritesheet. + /// The maximum amount of horizontal frames this particle has on its spritesheet. + /// An optional position which can be stored for this particle to attach to. + public void SpawnParticle(Vector2 position, Vector2 velocity, Vector2 scale, Color drawColor, int lifetime, float rotation = 0f, float opacity = 1f, float depth = 0f, int frameX = 0, int frameY = 0, int maxHorizontalFrames = 0, int maxVerticalFrames = 0, Vector2? storedPosition = null) + { + if (ActiveParticles.Count >= MaxParticles || ActiveParticles.Count >= CalamityClientConfig.Instance.ParticleLimit) + return; + + if (ParticlePool.Count > 0) + { + TempParticle pooledParticle = ParticlePool.Dequeue(); + pooledParticle.SetBasicParticleData(BaseParticleTexture, position, storedPosition ?? Vector2.Zero, velocity, scale, drawColor, rotation, opacity, depth, lifetime, frameX, frameY, maxHorizontalFrames, maxVerticalFrames); + ActiveParticles.Add(pooledParticle); + } + } + + public void SpawnParticle(Vector2 position, Vector2 velocity, float scale, Color drawColor, int lifetime, float rotation = 0f, float opacity = 1f, float depth = 0f, int frameX = 0, int frameY = 0, int maxHorizontalFrames = 0, int maxVerticalFrames = 0, Vector2? storedPosition = null) + { + if (ActiveParticles.Count >= MaxParticles || ActiveParticles.Count >= CalamityClientConfig.Instance.ParticleLimit) + return; + + if (ParticlePool.Count > 0) + { + TempParticle pooledParticle = ParticlePool.Dequeue(); + pooledParticle.SetBasicParticleData(BaseParticleTexture, position, storedPosition ?? Vector2.Zero, velocity, new(scale), drawColor, rotation, opacity, depth, lifetime, frameX, frameY, maxHorizontalFrames, maxVerticalFrames); + ActiveParticles.Add(pooledParticle); + } + } + + /// + /// Updates all active particles attached to this + /// + public void Update() + { + FastParallel.For(0, MaxParticles, (start, end, context) => + { + for (int i = start; i < end; i++) + { + ActiveParticles[i].Time++; + ActiveParticles[i].Position += ActiveParticles[i].Velocity; + ParticleUpdateFunction.Invoke(ActiveParticles[i]); + } + }); + + // Return the particle to the pool upon death. + ActiveParticles.RemoveAll(p => + { + if (p.Time >= p.Lifetime) + { + ParticlePool.Enqueue(p); + return true; + } + return false; + }); + } + + /// + /// Draws all active particles attached to this + /// + /// + /// Note you will need to start the spritebatch manually before calling this. + /// + public void DrawAllParticles(SpriteBatch spriteBatch, Vector2? baseOffset = null) + { + Vector2 drawOffset = baseOffset ?? -Main.screenPosition; + + if (ParticleDrawingOverride != null) + { + foreach (TempParticle tempParticle in ActiveParticles) + ParticleDrawingOverride.Invoke(tempParticle, spriteBatch, drawOffset); + } + else + { + foreach (TempParticle tempParticle in ActiveParticles) + { + Vector2 drawPosition = tempParticle.Position + drawOffset + tempParticle.StoredPosition; + Rectangle frame = BaseParticleTexture.Frame(tempParticle.MaxHorizontalFrames, tempParticle.MaxVerticalFrames, tempParticle.FrameX, tempParticle.FrameY); + Vector2 origin = frame.Size() * 0.5f; + + spriteBatch.Draw(BaseParticleTexture.Value, drawPosition, frame, tempParticle.DrawColor * tempParticle.Opacity, tempParticle.Rotation, origin, tempParticle.Scale, SpriteEffects.None, 0f); + } + } + } + } +}