Heavy smoke fx & Particle handler optimizations
This commit is contained in:
1 parent
63aa210555
commit
2d723ee26c
4 files changed
+121
-11
No files matched your search
@@ -22,8 +22,10 @@ namespace CalamityMod.Particles
|
||||
private static List<Particle> particleInstances;
|
||||
//Lists used when drawing particles batched
|
||||
private static List<Particle> batchedAlphaBlendParticles;
|
||||
private static List<Particle> batchedNonPremultipliedParticles;
|
||||
private static List<Particle> batchedAdditiveBlendParticles;
|
||||
|
||||
|
||||
internal static void Load()
|
||||
{
|
||||
particles = new List<Particle>();
|
||||
@@ -33,6 +35,7 @@ namespace CalamityMod.Particles
|
||||
particleInstances = new List<Particle>();
|
||||
|
||||
batchedAlphaBlendParticles = new List<Particle>();
|
||||
batchedNonPremultipliedParticles = new List<Particle>();
|
||||
batchedAdditiveBlendParticles = new List<Particle>();
|
||||
|
||||
Type baseParticleType = typeof(Particle);
|
||||
@@ -64,6 +67,7 @@ namespace CalamityMod.Particles
|
||||
particleTextures = null;
|
||||
particleInstances = null;
|
||||
batchedAlphaBlendParticles = null;
|
||||
batchedNonPremultipliedParticles = null;
|
||||
batchedAdditiveBlendParticles = null;
|
||||
}
|
||||
|
||||
@@ -109,6 +113,8 @@ namespace CalamityMod.Particles
|
||||
|
||||
if (particle.UseAdditiveBlend)
|
||||
batchedAdditiveBlendParticles.Add(particle);
|
||||
else if (particle.UseHalfTransparency)
|
||||
batchedNonPremultipliedParticles.Add(particle);
|
||||
else
|
||||
batchedAlphaBlendParticles.Add(particle);
|
||||
}
|
||||
@@ -124,24 +130,49 @@ namespace CalamityMod.Particles
|
||||
}
|
||||
}
|
||||
|
||||
sb.End();
|
||||
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, null, null, Main.GameViewMatrix.ZoomMatrix);
|
||||
|
||||
foreach (Particle particle in batchedAdditiveBlendParticles)
|
||||
if (batchedNonPremultipliedParticles.Count > 0)
|
||||
{
|
||||
if (particle.UseCustomDraw)
|
||||
particle.CustomDraw(sb);
|
||||
else
|
||||
sb.End();
|
||||
sb.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, null, null, Main.GameViewMatrix.ZoomMatrix);
|
||||
|
||||
foreach (Particle particle in batchedNonPremultipliedParticles)
|
||||
{
|
||||
Rectangle frame = particleTextures[particle.Type].Frame(1, particle.FrameVariants, 0, particle.Variant);
|
||||
sb.Draw(particleTextures[particle.Type], particle.Position - Main.screenPosition, frame, particle.Color, particle.Rotation, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f);
|
||||
if (particle.UseCustomDraw)
|
||||
particle.CustomDraw(sb);
|
||||
else
|
||||
{
|
||||
Rectangle frame = particleTextures[particle.Type].Frame(1, particle.FrameVariants, 0, particle.Variant);
|
||||
sb.Draw(particleTextures[particle.Type], particle.Position - Main.screenPosition, frame, particle.Color, particle.Rotation, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sb.End();
|
||||
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
||||
if (batchedAdditiveBlendParticles.Count > 0)
|
||||
{
|
||||
sb.End();
|
||||
sb.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, null, null, Main.GameViewMatrix.ZoomMatrix);
|
||||
|
||||
foreach (Particle particle in batchedAdditiveBlendParticles)
|
||||
{
|
||||
if (particle.UseCustomDraw)
|
||||
particle.CustomDraw(sb);
|
||||
else
|
||||
{
|
||||
Rectangle frame = particleTextures[particle.Type].Frame(1, particle.FrameVariants, 0, particle.Variant);
|
||||
sb.Draw(particleTextures[particle.Type], particle.Position - Main.screenPosition, frame, particle.Color, particle.Rotation, frame.Size() * 0.5f, particle.Scale, SpriteEffects.None, 0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Return to normal if we swapped the blend state before
|
||||
if (batchedAdditiveBlendParticles.Count > 0 || batchedNonPremultipliedParticles.Count > 0)
|
||||
{
|
||||
sb.End();
|
||||
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
||||
}
|
||||
|
||||
batchedAlphaBlendParticles.Clear();
|
||||
batchedNonPremultipliedParticles.Clear();
|
||||
batchedAdditiveBlendParticles.Clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
using Terraria.ModLoader;
|
||||
|
||||
namespace CalamityMod.Particles
|
||||
{
|
||||
public class HeavySmokeParticle : Particle
|
||||
{
|
||||
public override bool SetLifetime => true;
|
||||
public override int FrameVariants => 7;
|
||||
public override bool UseCustomDraw => true;
|
||||
public override bool Important => StrongVisual;
|
||||
public override bool UseAdditiveBlend => Glowing;
|
||||
public override bool UseHalfTransparency => !Glowing;
|
||||
|
||||
public override string Texture => "CalamityMod/Particles/HeavySmoke";
|
||||
|
||||
private float Opacity;
|
||||
private float Spin;
|
||||
private bool StrongVisual;
|
||||
private bool Glowing;
|
||||
private float HueShift;
|
||||
static int FrameAmount = 6;
|
||||
|
||||
public HeavySmokeParticle(Vector2 position, Vector2 velocity, Color color, int lifetime, float scale, float opacity, float rotationSpeed = 0f, bool glowing = false, float hueshift = 0f, bool required = false)
|
||||
{
|
||||
Position = position;
|
||||
Velocity = velocity;
|
||||
Color = color;
|
||||
Scale = scale;
|
||||
Variant = Main.rand.Next(7);
|
||||
Lifetime = lifetime;
|
||||
Opacity = opacity;
|
||||
Spin = rotationSpeed;
|
||||
StrongVisual = required;
|
||||
Glowing = glowing;
|
||||
HueShift = hueshift;
|
||||
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (Time / (float)Lifetime < 0.2f)
|
||||
Scale += 0.01f;
|
||||
else
|
||||
Scale *= 0.975f;
|
||||
|
||||
Color = Main.hslToRgb(Main.rgbToHsl(Color).X + HueShift, Main.rgbToHsl(Color).Y, Main.rgbToHsl(Color).Z);
|
||||
Opacity *= 0.98f;
|
||||
Rotation += Spin * ((Velocity.X > 0) ? 1f : -1f);
|
||||
Velocity *= 0.85f;
|
||||
|
||||
float opacity = Utils.InverseLerp(1f, 0.85f, LifetimeCompletion, true);
|
||||
Color *= opacity;
|
||||
}
|
||||
|
||||
public override void CustomDraw(SpriteBatch spriteBatch)
|
||||
{
|
||||
Texture2D tex = GeneralParticleHandler.GetTexture(Type);
|
||||
int animationFrame = (int)Math.Floor(Time / ((float)(Lifetime / (float)FrameAmount)));
|
||||
Rectangle frame = new Rectangle(80 * Variant, 80 * animationFrame, 80, 80);
|
||||
|
||||
|
||||
spriteBatch.Draw(tex, Position - Main.screenPosition, frame, Color * Opacity, Rotation, frame.Size() / 2f, Scale, SpriteEffects.None, 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
@@ -71,6 +71,11 @@ namespace CalamityMod.Particles
|
||||
/// </summary>
|
||||
public virtual bool UseAdditiveBlend => false;
|
||||
|
||||
/// <summary>
|
||||
/// Set this to true to make your particles work with semi transparent pixels. Is overriden if UseAdditiveBlend is set to true.
|
||||
/// </summary>
|
||||
public virtual bool UseHalfTransparency => false;
|
||||
|
||||
/// <summary>
|
||||
/// Removes the particle from the handler
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user