Added GeneralDrawLayerSystem

This commit is contained in:
Flowaria authored and Cameron Ewell committed 2025-12-16 17:48:41 -05:00
1 parent c5cb314499
commit 104ced20da
9 files changed
+182 -123

No files matched your search

+13 -10
View File
@@ -1,11 +1,12 @@
using Terraria;
using Terraria.ModLoader;
using CalamityMod.Graphics;
using CalamityMod.Systems.Graphic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using Terraria.ID;
using CalamityMod.Graphics;
using Terraria;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Backgrounds
{
@@ -22,12 +23,17 @@ namespace CalamityMod.Backgrounds
return;
}
On_Main.CheckMonoliths += DrawToTarget;
GeneralDrawLayerSystem.OnPrepareDraw += DrawToTarget;
On_Main.DrawBackgroundBlackFill += On_Main_DrawBackgroundBlackFill;
Main.QueueMainThreadAction(() => WaterDistortionTarget = new(true, ManagedRenderTarget.CreateScreenSizedTarget));
}
public override void Unload()
{
GeneralDrawLayerSystem.OnPrepareDraw -= DrawToTarget;
}
/// <summary>
/// This code runs because there is a system in vanilla that causes backgrounds to not draw if lighting is too low.
/// That would be fine, the problem arises because sloped tiles still block light the same way normal tiles do.
@@ -119,17 +125,16 @@ namespace CalamityMod.Backgrounds
if (!Main.tile[pos.X, pos.Y].HasTile &&
Main.tile[pos.X, pos.Y].WallType == WallID.None)
Lighting.AddLight(pos.X, pos.Y, TorchID.Red, 0.6f);
}
}
}
}
}
}
private void DrawToTarget(On_Main.orig_CheckMonoliths orig)
private void DrawToTarget()
{
if (Main.gameMenu)
{
orig();
return;
}
@@ -147,8 +152,6 @@ namespace CalamityMod.Backgrounds
Main.graphics.GraphicsDevice.SetRenderTarget(null);
CurrentlyRendering = false;
orig();
}
private void On_Main_DrawBackgroundBlackFill(On_Main.orig_DrawBackgroundBlackFill orig, Main self)
+8 -2
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using CalamityMod.Enums;
using CalamityMod.Systems.Graphic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
@@ -12,9 +13,10 @@ namespace CalamityMod.Graphics.Metaballs
{
internal static readonly List<Metaball> metaballs = new();
public override void OnModLoad()
public override void Load()
{
// Prepare event subscribers.
GeneralDrawLayerSystem.OnDrawLayer += DrawMetaballs;
RenderTargetManager.RenderTargetUpdateLoopEvent += PrepareMetaballTargets;
}
@@ -96,8 +98,10 @@ namespace CalamityMod.Graphics.Metaballs
/// 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>
internal static void DrawMetaballs(GeneralDrawLayer layerType)
private static void DrawMetaballs(GeneralDrawLayer layerType)
{
Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
foreach (Metaball metaball in metaballs.Where(m => m.DrawLayer == layerType && m.AnythingToDraw))
{
for (int i = 0; i < metaball.LayerTargets.Count; i++)
@@ -109,6 +113,8 @@ namespace CalamityMod.Graphics.Metaballs
Main.spriteBatch.Draw(metaball.LayerTargets[i], Main.screenLastPosition - Main.screenPosition, Color.White);
}
}
Main.spriteBatch.End();
}
}
}
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using CalamityMod.Enums;
using CalamityMod.Systems.Graphic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
@@ -60,7 +61,8 @@ namespace CalamityMod.Graphics.Primitives
PixelationTarget_AfterEverything = new(true, CreatePixelTarget);
});
On_Main.CheckMonoliths += DrawToTargets;
GeneralDrawLayerSystem.OnDrawLayer += DrawTargetScaled;
GeneralDrawLayerSystem.OnPrepareDraw += DrawToTargets;
//On_Main.DrawBackgroundBlackFill += DrawTarget_BeforeAllTiles;
//On_Main.DoDraw_Tiles_Solid += DrawTarget_BeforeSolidTiles;
//On_Main.DoDraw_DrawNPCsOverTiles += DrawTarget_NPCs;
@@ -75,16 +77,16 @@ namespace CalamityMod.Graphics.Primitives
if (Main.dedServ)
return;
On_Main.CheckMonoliths -= DrawToTargets;
GeneralDrawLayerSystem.OnDrawLayer -= DrawTargetScaled;
GeneralDrawLayerSystem.OnPrepareDraw -= DrawToTargets;
}
#endregion
#region Drawing To Targets
private void DrawToTargets(On_Main.orig_CheckMonoliths orig)
private void DrawToTargets()
{
if (Main.gameMenu)
{
orig();
return;
}
@@ -167,7 +169,6 @@ namespace CalamityMod.Graphics.Primitives
Main.instance.GraphicsDevice.SetRenderTarget(null);
CurrentlyRendering = false;
orig();
}
private static void DrawPrimsToRenderTarget(RenderTarget2D renderTarget, GeneralDrawLayer layer, List<IPixelatedPrimitiveRenderer> pixelPrimitives)
@@ -188,15 +189,11 @@ namespace CalamityMod.Graphics.Primitives
#endregion
#region Target Drawing
internal static void DrawTargetScaled(GeneralDrawLayer drawLayer, bool startSpritebatch = true)
private static void DrawTargetScaled(GeneralDrawLayer drawLayer)
{
if (startSpritebatch)
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
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();
Main.spriteBatch.End();
}
private static RenderTarget2D ReturnAssociatedRenderTarget(GeneralDrawLayer drawLayer)
+5 -5
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using CalamityMod.Enums;
using CalamityMod.Systems.Graphic;
using CalamityMod.Utilities.Daybreak;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@@ -26,7 +27,8 @@ namespace CalamityMod.Graphics.Renderers
return;
// This hooks here, because doing it any sooner causes the screen position to be a frame behind.
On_Main.CheckMonoliths += DrawToTargets;
GeneralDrawLayerSystem.OnDrawLayer += DrawRendererAtLayer;
GeneralDrawLayerSystem.OnPrepareDraw += DrawToTargets;
}
public override void Unload()
@@ -60,7 +62,7 @@ namespace CalamityMod.Graphics.Renderers
#endregion
#region Drawing
internal static void DrawRendererAtLayer(GeneralDrawLayer drawLayer)
private static void DrawRendererAtLayer(GeneralDrawLayer drawLayer)
{
var renderers = Renderers.Where(renderer => renderer.ShouldDraw && renderer.Layer == drawLayer);
if (renderers.Any())
@@ -74,10 +76,8 @@ namespace CalamityMod.Graphics.Renderers
}
}
private void DrawToTargets(On_Main.orig_CheckMonoliths orig)
private void DrawToTargets()
{
orig();
if (Main.gameMenu || Main.dedServ)
return;
-9
View File
@@ -55,15 +55,6 @@ 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;
-70
View File
@@ -7,9 +7,6 @@ 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.Armor.Wulfrum;
using CalamityMod.Items.Critters;
@@ -27,7 +24,6 @@ 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.Utilities.Daybreak;
@@ -863,72 +859,6 @@ namespace CalamityMod.ILEditing
}
#endregion
#region GeneralDrawLayer Systems Drawing
private static void GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer drawLayer)
{
GeneralParticleHandler.DrawParticleCollectionsAtSpecificLayer(drawLayer);
Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
MetaballManager.DrawMetaballs(drawLayer);
Main.spriteBatch.End();
PrimitivePixelationSystem.DrawTargetScaled(drawLayer);
PixelationManager.DrawPixelatedTargets(drawLayer);
RendererManager.DrawRendererAtLayer(drawLayer);
}
private static void GeneralDrawLayer_DrawToLayer_BeforeAllTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self)
{
Main.spriteBatch.End(out var ss);
GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.BeforeAllTiles);
Main.spriteBatch.Begin(ss);
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);
Main.spriteBatch.End(out var ss);
GeneralDrawLayer_DrawForSupportedSystems(GeneralDrawLayer.AfterEverything);
Main.spriteBatch.Begin(ss);
}
#endregion
#region General Particle Rendering
private static void DrawFusableParticles(On_Main.orig_SortDrawCacheWorms orig, Main self)
{
+4 -1
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using CalamityMod.Enums;
using CalamityMod.Systems.Graphic;
using CalamityMod.Systems.Graphic.PixelationSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@@ -100,6 +101,8 @@ namespace CalamityMod.Particles
particlesToDraw = [];
particlesToDraw_Pixelated = [];
particlesToDraw_CustomShader = [];
GeneralDrawLayerSystem.OnDrawLayer += DrawParticleCollectionsAtSpecificLayer;
}
public override void Unload()
@@ -289,7 +292,7 @@ namespace CalamityMod.Particles
particlesToKill.Clear();
}
internal static void DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer drawLayer)
private static void DrawParticleCollectionsAtSpecificLayer(GeneralDrawLayer drawLayer)
{
if (Main.dedServ)
return;
+131
View File
@@ -0,0 +1,131 @@
using System;
using CalamityMod.Enums;
using CalamityMod.Utilities.Daybreak;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod.Systems.Graphic
{
internal sealed class GeneralDrawLayerSystem : ModSystem
{
public static event Action OnPrepareDraw;
public static event Action<GeneralDrawLayer> OnDrawLayer;
public static event Action<GeneralDrawLayer> OnDrawLayerLate;
public static event Action OnBeforeAllTiles;
public static event Action OnBeforeSolidTiles;
public static event Action OnBeforeNPCs;
public static event Action OnAfterNPCs;
public static event Action OnBeforeProjectiles;
public static event Action OnAfterProjectiles;
public static event Action OnAfterPlayers;
public static event Action OnAfterDusts;
public static event Action OnAfterEverything;
public override void OnModLoad()
{
On_Main.CheckMonoliths += CheckMonoliths;
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;
}
public override void Unload()
{
OnPrepareDraw = null;
OnDrawLayer = null;
OnDrawLayerLate = null;
OnBeforeAllTiles = null;
OnBeforeSolidTiles = null;
OnBeforeNPCs = null;
OnAfterNPCs = null;
OnBeforeProjectiles = null;
OnAfterProjectiles = null;
OnAfterPlayers = null;
OnAfterDusts = null;
OnAfterEverything = null;
}
private static void CheckMonoliths(On_Main.orig_CheckMonoliths orig)
{
orig();
OnPrepareDraw?.Invoke();
}
#region GeneralDrawLayer Systems Drawing
private static void GeneralDrawLayer_DrawForLayer(GeneralDrawLayer drawLayer)
{
OnDrawLayer?.Invoke(drawLayer);
OnDrawLayerLate?.Invoke(drawLayer);
var callback = drawLayer switch
{
GeneralDrawLayer.BeforeAllTiles => OnBeforeAllTiles,
GeneralDrawLayer.BeforeSolidTiles => OnBeforeSolidTiles,
GeneralDrawLayer.BeforeNPCs => OnBeforeNPCs,
GeneralDrawLayer.AfterNPCs => OnAfterNPCs,
GeneralDrawLayer.BeforeProjectiles => OnBeforeProjectiles,
GeneralDrawLayer.AfterProjectiles => OnAfterProjectiles,
GeneralDrawLayer.AfterPlayers => OnAfterPlayers,
GeneralDrawLayer.AfterDusts => OnAfterDusts,
GeneralDrawLayer.AfterEverything => OnAfterEverything,
_ => null
};
callback?.Invoke();
}
private static void GeneralDrawLayer_DrawToLayer_BeforeAllTiles(On_Main.orig_DrawBackgroundBlackFill orig, Main self)
{
Main.spriteBatch.End(out var ss);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.BeforeAllTiles);
Main.spriteBatch.Begin(ss);
orig(self);
}
private static void GeneralDrawLayer_DrawToLayer_BeforeSolidTiles(On_Main.orig_DoDraw_Tiles_Solid orig, Main self)
{
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.BeforeSolidTiles);
orig(self);
}
private static void GeneralDrawLayer_DrawToLayer_NPCs(On_Main.orig_DoDraw_DrawNPCsOverTiles orig, Main self)
{
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.BeforeNPCs);
orig(self);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.AfterNPCs);
}
private static void GeneralDrawLayer_DrawToLayer_Projectiles(On_Main.orig_DrawProjectiles orig, Main self)
{
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.BeforeProjectiles);
orig(self);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.AfterProjectiles);
}
private static void GeneralDrawLayer_DrawToLayer_AfterPlayers(On_Main.orig_DrawPlayers_AfterProjectiles orig, Main self)
{
orig(self);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.AfterPlayers);
}
private static void GeneralDrawLayer_DrawToLayer_AfterDusts(On_Main.orig_DrawDust orig, Main self)
{
orig(self);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.AfterDusts);
}
private static void GeneralDrawLayer_DrawToLayer_AfterEverything(On_Main.orig_DrawInfernoRings orig, Main self)
{
orig(self);
Main.spriteBatch.End(out var ss);
GeneralDrawLayer_DrawForLayer(GeneralDrawLayer.AfterEverything);
Main.spriteBatch.Begin(ss);
}
#endregion
}
}
@@ -81,7 +81,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
ActivePixelatedDrawers_AfterDusts = [];
ActivePixelatedDrawers_AfterEverything = [];
On_Main.CheckMonoliths += PrepareTargets;
GeneralDrawLayerSystem.OnDrawLayerLate += DrawPixelatedTargets;
GeneralDrawLayerSystem.OnPrepareDraw += PrepareTargets;
}
public override void Unload()
@@ -148,7 +149,7 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
ReturnAssociatedDrawerCollection(drawLayer).Add(drawer);
}
private static void PrepareTargets(On_Main.orig_CheckMonoliths orig)
private static void PrepareTargets()
{
if (!Main.gameMenu && !Main.dedServ)
{
@@ -165,11 +166,8 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
Main.graphics.GraphicsDevice.SetRenderTarget(null);
}
orig();
}
private static void DrawCollectionsToTarget(Dictionary<BlendState, ManagedRenderTarget> targetCollection, Matrix pixelationMatrix, List<PixelatedDrawer> drawerCollection)
{
foreach (var blendStateTargetPair in targetCollection)
@@ -192,17 +190,17 @@ namespace CalamityMod.Systems.Graphic.PixelationSystem
drawerCollection.Clear();
}
internal static void DrawPixelatedTargets(GeneralDrawLayer drawLayer)
private static void DrawPixelatedTargets(GeneralDrawLayer drawLayer)
{
var targetCollection = ReturnAssociatedTargetCollection(drawLayer);
foreach (var keyValuePair in targetCollection)
{
Main.spriteBatch.End();
Main.spriteBatch.Begin(default, keyValuePair.Key, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
foreach (var keyValuePair in targetCollection)
{
Main.spriteBatch.Begin(default, keyValuePair.Key, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.GameViewMatrix.TransformationMatrix);
float targetScale = 1f / PixelationResolution;
Main.spriteBatch.Draw(keyValuePair.Value, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, targetScale, SpriteEffects.None, 0f);
}
float targetScale = 1f / PixelationResolution;
Main.spriteBatch.Draw(keyValuePair.Value, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, targetScale, SpriteEffects.None, 0f);
Main.spriteBatch.End();
}
}
private static List<PixelatedDrawer> ReturnAssociatedDrawerCollection(GeneralDrawLayer drawLayer)