GeneralDrawLayer generalization + TempParticle start

* Split up GeneralDrawLayer.BeforeTiles into BeforeAllTiles and BeforeSolidTiles.
* Moved screen renderers, metaballs and pixelated primitive drawing to use GeneralDrawLayer and subsequentially removed their own draw layer enums.
* Moved screen renderers, metaballs, particles, pixelated primitive drawing and pixelated drawers draw calls to MechanicalILChanges.
* Added TempParticle and TempParticleManager, used for defining temporary particle handlers in any file for short-term, fully-modifiable particle effects
* Removed ParticleAutoDrawingOverride and added a "CustomShader" property and "PrepareCustomShader" method to Particle.cs. These should be used in conjunction when wanting to apply shader effects to particles. If anything more precise is needed, use the new TempParticleManager class.
This commit is contained in:
fryzahh committed 2025-11-15 16:41:52 -04:00
1 parent 8b10aadab8
commit b33a70fd08
55 files changed
+636 -535

No files matched your search

+13 -12
View File
@@ -1,20 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalamityMod.Enums
{
/// <summary>
/// A general collection of points in Terraria's overall draw order that graphical systems in the mod draw to.
/// </summary>
[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,
}
}
+2 -1
View File
@@ -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<Texture2D>($"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;
+2 -1
View File
@@ -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 };
+2 -1
View File
@@ -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 };
+2 -1
View File
@@ -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<Texture2D>($"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);
+2 -2
View File
@@ -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<Texture2D>($"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);
+2 -2
View File
@@ -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<Texture2D>($"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);
+2 -2
View File
@@ -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<Texture2D>($"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);
+2 -1
View File
@@ -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<DistortionParticle> 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<DoGRiftCrack>());
+2 -1
View File
@@ -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()
{
+2 -1
View File
@@ -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()
{
+2 -1
View File
@@ -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);
+2 -1
View File
@@ -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);
+2 -1
View File
@@ -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
/// <summary>
/// The draw layer in which metaballs should be drawn.
/// </summary>
public abstract MetaballDrawLayer DrawContext
public abstract GeneralDrawLayer DrawLayer
{
get;
}
-9
View File
@@ -1,9 +0,0 @@
namespace CalamityMod.Graphics.Metaballs
{
public enum MetaballDrawLayer
{
BeforeNPCs,
BeforeProjectiles,
AfterProjectiles,
}
}
+6 -38
View File
@@ -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);
}
/// <summary>
/// Checks if a metaball of a given layering type is currently in use. This is used to minimize needless <see cref="SpriteBatch"/> restarts when there is nothing to draw.
/// </summary>
/// <param name="layerType">The metaball layer type to check against.</param>
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);
/// <summary>
/// Draws all metaballs of a given <see cref="MetaballDrawLayer"/>. Used for layer ordering reasons.
/// Draws all metaballs of a given <see cref="GeneralDrawLayer"/>. Used for layer ordering reasons.
/// </summary>
/// <param name="layerType">The layer type to draw with.</param>
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++)
{
+2 -1
View File
@@ -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<Texture2D> Layers
{
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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;
+2 -1
View File
@@ -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<Texture2D> Layers
{
+2 -7
View File
@@ -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<Texture2D>($"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;
+2 -2
View File
@@ -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<Texture2D>($"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);
+2 -2
View File
@@ -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)
+2 -3
View File
@@ -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)
+2 -2
View File
@@ -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);
+2 -1
View File
@@ -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);
@@ -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
/// <summary>
/// The layer to render the primitive(s) to.
/// </summary>
PixelationPrimitiveLayer LayerToRenderTo => PixelationPrimitiveLayer.BeforeProjectiles;
GeneralDrawLayer LayerToRenderTo => GeneralDrawLayer.BeforeProjectiles;
/// <summary>
/// Render primitives that use pixelation here.
/// </summary>
void RenderPixelatedPrimitives(SpriteBatch spriteBatch, PixelationPrimitiveLayer layer);
void RenderPixelatedPrimitives(SpriteBatch spriteBatch, GeneralDrawLayer layer);
}
}
@@ -1,17 +0,0 @@
using System;
namespace CalamityMod.Graphics.Primitives
{
/// <summary>
/// Controls what layer the <see cref="IPixelatedPrimitiveRenderer.RenderPixelatedPrimitives"/> renders to.
/// </summary>
[Flags]
public enum PixelationPrimitiveLayer
{
BeforeNPCs = 1,
AfterNPCs = 2,
BeforeProjectiles = 4,
AfterProjectiles = 8,
AfterPlayers = 16
}
}
@@ -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);
/// <summary>
@@ -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<IPixelatedPrimitiveRenderer>();
var beforeSolidTiles = new List<IPixelatedPrimitiveRenderer>();
var beforeNPCs = new List<IPixelatedPrimitiveRenderer>();
var afterNPCs = new List<IPixelatedPrimitiveRenderer>();
var beforeProjectiles = new List<IPixelatedPrimitiveRenderer>();
var afterProjectiles = new List<IPixelatedPrimitiveRenderer>();
var afterPlayers = new List<IPixelatedPrimitiveRenderer>();
var afterDusts = new List<IPixelatedPrimitiveRenderer>();
var afterEverything = new List<IPixelatedPrimitiveRenderer>();
// 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<IPixelatedPrimitiveRenderer> pixelPrimitives)
private static void DrawPrimsToRenderTarget(RenderTarget2D renderTarget, GeneralDrawLayer layer, List<IPixelatedPrimitiveRenderer> 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
}
+1 -1
View File
@@ -18,7 +18,7 @@ namespace CalamityMod.Graphics.Primitives
/// <item>The third controls how many points are created for the trail, and the fourth determines whether the primitive is subsequently rendered.</item>
/// </list>
/// If you wish to use pixelation, you <b>MUST</b> make the NPC/Projectile inherit <see cref="IPixelatedPrimitiveRenderer"/> and use <see cref="IPixelatedPrimitiveRenderer.RenderPixelatedPrimitives"/> instead of predraw..<br/>
/// You can also optionally specify a render layer with <see cref="IPixelatedPrimitiveRenderer.LayerToRenderTo"/>. It is <see cref="PixelationPrimitiveLayer.BeforeNPCs"/> by default.
/// You can also optionally specify a render layer with <see cref="IPixelatedPrimitiveRenderer.LayerToRenderTo"/>. It is <see cref="Enums.GeneralDrawLayer.BeforeNPCs"/> by default.
/// </summary>
[Autoload(Side = ModSide.Client)]
public sealed class PrimitiveRenderer : ModSystem
+2 -1
View File
@@ -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
/// <summary>
/// The layer that the target should be drawn at.
/// </summary>
public abstract DrawLayer Layer { get; }
public abstract GeneralDrawLayer Layer { get; }
/// <summary>
/// Whether the target should draw and be drawn to this frame.
@@ -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];
@@ -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;
@@ -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<IDyeableShaderRenderer> 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;
@@ -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
{
@@ -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;
+16 -99
View File
@@ -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
}
}
+9
View File
@@ -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;
+80
View File
@@ -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)
{
+38 -114
View File
@@ -57,15 +57,16 @@ namespace CalamityMod.Particles
/// </summary>
internal static Dictionary<int, Texture2D> particleTexturesByIDs;
private static Dictionary<Type, ParticleAutoDrawingOverride> particleAutoDrawingOverrides;
// Collections for tracking particles active in the world.
private static List<Particle> activeParticles;
private static List<Particle> particlesToKill;
private static Dictionary<GeneralDrawLayer, Queue<Particle>> particlesToSpawnNextFrame;
private static Dictionary<GeneralDrawLayer, Queue<Particle>> particlesToSpawnNextFrame_Pixelated;
// Collections for correctly organizing active prticles for drawing.
private static Dictionary<BlendState, List<Particle>> particlesToDraw;
private static Dictionary<BlendState, List<Particle>> particlesToDraw_Pixelated;
private static Dictionary<Effect, List<Particle>> particlesToDraw_CustomShader;
#endregion
#region Loading and Unloading
@@ -93,22 +94,6 @@ namespace CalamityMod.Particles
texturePath = instance.Texture;
particleTexturesByIDs[ID] = ModContent.Request<Texture2D>(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);
}
+15 -10
View File
@@ -75,9 +75,14 @@ namespace CalamityMod.Particles
/// <summary>
/// The "layer" or point at which you'd like your particle to draw in Terraria's internal draw order.
/// <br>Defaults to <see cref="GeneralDrawLayer.AfterEverything"/>.</br>
/// <br>Defaults to <see cref="GeneralDrawLayer.AfterDusts"/>.</br>
/// </summary>
public GeneralDrawLayer DrawLayer = GeneralDrawLayer.AfterEverything;
public GeneralDrawLayer DrawLayer = GeneralDrawLayer.AfterDusts;
/// <summary>
/// An 0-1 interpolant representing how close this particle is from its <see cref="Lifetime"/>.
/// </summary>
public float LifetimeCompletion => Lifetime != 0 ? Time / (float)Lifetime : 0;
/// <summary>
/// The path to this particle's autoloaded texture.
@@ -92,11 +97,6 @@ namespace CalamityMod.Particles
/// </summary>
public virtual int FrameVariants => 1;
/// <summary>
/// An 0-1 interpolant representing how close this particle is from its <see cref="Lifetime"/>.
/// </summary>
public float LifetimeCompletion => Lifetime != 0 ? Time / (float)Lifetime : 0;
/// <summary>
/// Set this to true if you NEED the particle to render even if the particle cap is reached.
/// </summary>
@@ -123,10 +123,10 @@ namespace CalamityMod.Particles
public virtual bool UseCustomDraw => false;
/// <summary>
/// Override and set this to true to disable automatic drawing for all instances of this particle entirely.
/// <br>Drawing of this particle must be done from a <see cref="ParticleAutoDrawingOverride"/> class.</br>
/// An optional shader parameter for this paricle to be drawn with.
/// Use <see cref="PrepareCustomShader(Effect)"/> to set shader parameters accordingly.
/// </summary>
public virtual bool OverrideAutomaticDrawing => false;
public virtual Effect CustomShader => null;
/// <summary>
/// Use this method if you want to handle the particle drawing yourself. Only called if <b><see cref="UseCustomDraw"/></b> is set to true.
@@ -139,6 +139,11 @@ namespace CalamityMod.Particles
/// <param name="basePosition">The base position of the particle set.</param>
public virtual void CustomDraw(SpriteBatch spriteBatch, Vector2 basePosition) { }
/// <summary>
/// Use this method if you're using a custom shader effect with this particle type to set shader parameters accordingly.
/// </summary>
public virtual void PrepareCustomShader(Effect shader) { }
/// <summary>
/// Called every frame in <see cref="GeneralParticleHandler.Update"/>.
/// The particle's velocity gets automatically added to its position, and its time automatically increases.
-57
View File
@@ -1,57 +0,0 @@
using System.Collections.Generic;
using CalamityMod.Enums;
using Microsoft.Xna.Framework.Graphics;
using Terraria.ModLoader;
namespace CalamityMod.Particles
{
/// <summary>
/// Allows one to directly control how all active instances of a specified particle type in the world are drawn.
/// <br>Useful for cases such as applying shaders to particles without resetting the SpriteBatch for every individual particle instance.</br>
/// </summary>
/// <remarks>
/// Note that particles using an instance of this class <b>MUST</b> have <b><see cref="Particle.OverrideAutomaticDrawing"/></b> set to true.
/// </remarks>
public abstract class ParticleAutoDrawingOverride : ModType
{
/// <summary>
/// All active particle instances for this drawer, identified by a particle's draw layer.
/// </summary>
public Dictionary<GeneralDrawLayer, List<Particle>> ActiveParticleInstances { get; private set; } = [];
/// <summary>
/// The internal name of the particle type that should be manually drawn.
/// <br>Particle names should be prefixed with the internal name of the mod they are from.</br>
/// <br>e.g. <b>"CalamityMod.PulseRing"</b> is valid, while just <b>"PulseRing"</b> is not.</br>
/// </summary>
/// <remarks>
/// This is only checked once when the mod is loading. Changing this during runtime will not affect anything.
/// </remarks>
public abstract string TargetParticleTypeName { get; }
/// <summary>
/// Whether or not this drawer should draw its particles this frame.
/// <br>Return false to prevent currently stored particles from being drawn and new ones from being added to <b><see cref="ActiveParticleInstances"/></b>.</br>
/// </summary>
/// <remarks>
/// Returns true by default.
/// </remarks>
public virtual bool ShouldDrawParticles => true;
/// <summary>
/// An optional property which will override the draw layer of all particles for this drawer entirely, using the specified one here instead.
/// </summary>
public virtual GeneralDrawLayer? DrawLayerOverride => null;
protected sealed override void Register() => ModTypeLookup<ParticleAutoDrawingOverride>.Register(this);
public sealed override void SetupContent() => SetStaticDefaults();
/// <summary>
/// Write all relevenat draw code for your particles here.
/// </summary>
/// <param name="particlesToDraw">Stores particles based on their default draw layer.
/// <br>Iterate through this collection to run individual particle drawing logic.</br></param>
public abstract void DrawAllParticles(SpriteBatch spriteBatch, List<Particle> particlesToDraw);
}
}
+3 -1
View File
@@ -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<Texture2D>("CalamityMod/ExtraTextures/Trails/ScarletDevilStreak"));
+3 -2
View File
@@ -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<Texture2D>("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);
+3 -2
View File
@@ -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<Texture2D>("CalamityMod/ExtraTextures/Trails/SylvestaffStreak"));
+2 -1
View File
@@ -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<Texture2D>("CalamityMod/ExtraTextures/Trails/ScarletDevilStreak"));
+4 -3
View File
@@ -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<Sylvestaff>();
@@ -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);
}
@@ -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);
+3 -2
View File
@@ -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<Texture2D>("CalamityMod/ExtraTextures/Trails/SylvestaffStreak"));
PrimitiveRenderer.RenderTrail(Projectile.oldPos, new(PrimitiveWidthFunction, PrimitiveColorFunction, (_) => Projectile.Size * 0.5f, pixelate: true, shader: GameShaders.Misc["CalamityMod:TrailStreak"]));
+3 -2
View File
@@ -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<Texture2D>("CalamityMod/ExtraTextures/Trails/SylvestaffStreak"));
PrimitiveRenderer.RenderTrail(Projectile.oldPos, new(PrimitiveWidthFunction, PrimitiveColorFunction, (_) => Projectile.Size * 0.5f, pixelate: true, shader: GameShaders.Misc["CalamityMod:TrailStreak"]));
@@ -12,7 +12,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
{
public class PixelationManager : ModSystem
{
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_BeforeTiles;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_BeforeAllTiles;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_BeforeSolidTiles;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_BeforeNPCs;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_AfterNPCs;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_BeforeProjectiles;
@@ -21,7 +22,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_AfterDusts;
private static Dictionary<BlendState, ManagedRenderTarget> PixelTargets_AfterEverything;
private static List<PixelatedDrawer> ActivePixelatedDrawers_BeforeTiles;
private static List<PixelatedDrawer> ActivePixelatedDrawers_BeforeAllTiles;
private static List<PixelatedDrawer> ActivePixelatedDrawers_BeforeSolidTiles;
private static List<PixelatedDrawer> ActivePixelatedDrawers_BeforeNPCs;
private static List<PixelatedDrawer> ActivePixelatedDrawers_AfterNPCs;
private static List<PixelatedDrawer> ActivePixelatedDrawers_BeforeProjectiles;
@@ -54,12 +56,13 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
/// </summary>
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<BlendState, ManagedRenderTarget> targetCollection, Matrix pixelationMatrix, List<PixelatedDrawer> drawerCollection)
{
foreach (var blendStateTargetPair in targetCollection)
@@ -229,8 +189,9 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
drawerCollection.Clear();
}
private static void DrawPixelatedTargets(Dictionary<BlendState, ManagedRenderTarget> 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<PixelatedDrawer> 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<BlendState, ManagedRenderTarget> ReturnAssociatedTargetCollection(GeneralDrawLayer drawLayer)
{
Dictionary<BlendState, ManagedRenderTarget> 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:
@@ -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<Texture2D> 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;
/// <summary>
/// An 0-1 interpolant representing how close this particle is from its <see cref="Lifetime"/>.
/// </summary>
public float LifetimeCompletionRatio => MathHelper.Clamp(Time / (float)Lifetime, 0f, 1f);
public void SetBasicParticleData(Asset<Texture2D> 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];
}
}
}
@@ -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<TempParticle> ActiveParticles;
public Queue<TempParticle> ParticlePool;
public int MaxParticles;
public Asset<Texture2D> BaseParticleTexture;
public ParticleUpdateFunctionDelegate ParticleUpdateFunction;
public ParticleDrawFunctionDelegate ParticleDrawingOverride;
/// <summary>
/// A temporary particle handler made for the typically short-term creation and manipulation of particle effects done by instances of a generic particle object <see cref="TempParticle"/>.
/// <br>Should typically be used in areas where <see cref="Particles.GeneralParticleHandler"/> is less practical or cannot work at all.</br>
/// </summary>
/// <param name="maxParticles">The maximum amount of particles that can exist at a time in this manager.</param>
/// <param name="baseParticleTexturePath">The base texture that particles spawned by this manager will use when drawn.</param>
/// <param name="particleUpdateFunction">An additional update function to control how particles spawned by this manager are updated.</param>
/// <param name="particleDrawingOverride">Set this parameter to override how particles spawned by this manager are drawn.</param>
public TempParticleManager(int maxParticles, string baseParticleTexturePath, ParticleUpdateFunctionDelegate particleUpdateFunction = null, ParticleDrawFunctionDelegate particleDrawingOverride = null)
{
MaxParticles = maxParticles;
BaseParticleTexture = ModContent.Request<Texture2D>(baseParticleTexturePath);
ParticleUpdateFunction = particleUpdateFunction;
ParticleDrawingOverride = particleDrawingOverride;
ActiveParticles = [];
if (ParticlePool is null)
{
ParticlePool = [];
for (int i = 0; i < maxParticles; i++)
ParticlePool.Enqueue(new TempParticle());
}
}
/// <summary>
/// Spawns a <see cref="TempParticle"/> instance attached to a <see cref="TempParticleManager"/>.
/// </summary>
/// <param name="position">The initial spawn position of this particle.</param>
/// <param name="velocity">The initial velocity of this particle.</param>
/// <param name="scale">The initial scale of this particle.</param>
/// <param name="drawColor">The base color this particle is drawn in.</param>
/// <param name="lifetime">The maximum amount of time this particle can be alive for.</param>
/// <param name="rotation">The initial rotation of this particle.</param>
/// <param name="opacity">The initial opacity of this particle.</param>
/// <param name="depth">The depth at which this particle appears. Should only be used in cases where particles need to be drawn in the foreground or background.</param>
/// <param name="frameX">The initial X-frame of this particle on its spritesheet.</param>
/// <param name="frameY">The initial Y-frame of this particle on its spritesheet.</param>
/// <param name="maxHorizontalFrames">The maximum amount of horizontal frames this particle has on its spritesheet.</param>
/// <param name="maxVerticalFrames">The maximum amount of horizontal frames this particle has on its spritesheet.</param>
/// <param name="storedPosition">An optional position which can be stored for this particle to attach to.</param>
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);
}
}
/// <summary>
/// Updates all active particles attached to this <see cref="TempParticleManager"/>
/// </summary>
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;
});
}
/// <summary>
/// Draws all active particles attached to this <see cref="TempParticleManager"/>
/// </summary>
/// <remarks>
/// <b>Note you will need to start the spritebatch manually before calling this.</b>
/// </remarks>
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);
}
}
}
}
}