OpacityFastFadeModifier.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using MonoGame.Extended.Particles.Data;
  5. namespace MonoGame.Extended.Particles.Modifiers;
  6. /// <summary>
  7. /// A modifier that rapidly decreases particle opacity based on their age.
  8. /// </summary>
  9. /// <remarks>
  10. /// The <see cref="OpacityFastFadeModifier"/> creates a linear fade-out effect where particles
  11. /// become more transparent as they age.
  12. ///
  13. /// Important notes:
  14. /// <list type="bullet">
  15. /// <item>
  16. /// This modifier assumes particles have a standard lifespan of 1.0 second. For particles
  17. /// with different lifespans, the fade effect may not complete before the particle is removed,
  18. /// or may become fully transparent before the particle's actual end of life.
  19. /// </item>
  20. /// <item>
  21. /// Unlike other modifiers that accumulate changes over time, this modifier directly sets
  22. /// the opacity value each frame based solely on the particle's age.
  23. /// </item>
  24. /// </list>
  25. /// </remarks>
  26. public sealed class OpacityFastFadeModifier : Modifier
  27. {
  28. /// <summary>
  29. /// Updates all particles by setting their opacity based on their age.
  30. /// </summary>
  31. /// <inheritdoc/>
  32. public override unsafe void Update(float elapsedSeconds, ParticleIterator iterator)
  33. {
  34. if (!Enabled) { return; }
  35. while (iterator.HasNext)
  36. {
  37. Particle* particle = iterator.Next();
  38. particle->Opacity = 1.0f - particle->Age;
  39. }
  40. }
  41. }