Files
CalamityModPublic/Graphics/Metaballs/MetaballManager.cs
T
fryzahh b33a70fd08 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.
2025-11-15 16:41:52 -04:00

115 lines
4.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
using CalamityMod.Enums;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod.Graphics.Metaballs
{
public class MetaballManager : ModSystem
{
internal static readonly List<Metaball> metaballs = new();
public override void OnModLoad()
{
// Prepare event subscribers.
RenderTargetManager.RenderTargetUpdateLoopEvent += PrepareMetaballTargets;
}
public override void OnModUnload()
{
// Clear all unmanaged metaball target resources on the GPU on mod unload.
Main.QueueMainThreadAction(() =>
{
foreach (Metaball metaball in metaballs)
metaball?.Dispose();
});
}
public override void OnWorldUnload()
{
foreach (Metaball metaball in metaballs)
metaball.ClearInstances();
}
public override void PostUpdateEverything()
{
var activeMetaballs = metaballs.Where(m => m.AnythingToDraw);
foreach (Metaball metaball in activeMetaballs)
{
if (metaball.IgnoreFPS)
metaball.Update();
}
}
private void PrepareMetaballTargets()
{
// Get a list of all metaballs currently in use.
var activeMetaballs = metaballs.Where(m => m.AnythingToDraw);
// Don't bother wasting resources if metaballs are not in use at the moment.
if (!activeMetaballs.Any())
return;
// Prepare the sprite batch for drawing. Metaballs may restart the sprite batch via PrepareSpriteBatch if their implementation requires it.
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, Main.Rasterizer, null, Matrix.Identity);
var gd = Main.instance.GraphicsDevice;
foreach (Metaball metaball in activeMetaballs)
{
// Update the metaball collection.
if (!Main.gamePaused && !metaball.IgnoreFPS)
metaball.Update();
// Prepare the sprite batch in accordance to the needs of the metaball instance. By default this does nothing,
metaball.PrepareSpriteBatch(Main.spriteBatch);
// Draw the raw contents of the metaball to each of its render targets.
foreach (ManagedRenderTarget target in metaball.LayerTargets)
{
gd.SetRenderTarget(target);
gd.Clear(Color.Transparent);
metaball.DrawInstances();
// Flush metaball contents to its render target and reset the sprite batch for the next iteration.
Main.spriteBatch.End();
Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, Main.Rasterizer, null, Matrix.Identity);
}
}
// Return to the backbuffer and end the sprite batch.
gd.SetRenderTarget(null);
Main.spriteBatch.End();
}
/// <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(GeneralDrawLayer layerType) =>
metaballs.Any(m => m.AnythingToDraw && m.DrawLayer == layerType);
/// <summary>
/// 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)
{
foreach (Metaball metaball in metaballs.Where(m => m.DrawLayer == layerType && m.AnythingToDraw))
{
for (int i = 0; i < metaball.LayerTargets.Count; i++)
{
// Prepare shaders for the given layer target.
metaball.PrepareShaderForTarget(i);
// Draw the metaball's raw contents with the shader.
Main.spriteBatch.Draw(metaball.LayerTargets[i], Main.screenLastPosition - Main.screenPosition, Color.White);
}
}
}
}
}