summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Particles/Modifiers/VelocityColorModifier.cs
blob: ae9dc7b7219d63e6460cc4d5e2db6bab6b6cbcfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;

namespace MonoGame.Extended.Particles.Modifiers
{
    public class VelocityColorModifier : Modifier
    {
        public HslColor StationaryColor { get; set; }
        public HslColor VelocityColor { get; set; }
        public float VelocityThreshold { get; set; }

        public override unsafe void Update(float elapsedSeconds, ParticleBuffer.ParticleIterator iterator)
        {
            var velocityThreshold2 = VelocityThreshold*VelocityThreshold;

            while (iterator.HasNext)
            {
                var particle = iterator.Next();
                var velocity2 = particle->Velocity.X*particle->Velocity.X +
                                particle->Velocity.Y*particle->Velocity.Y;
                var deltaColor = VelocityColor - StationaryColor;

                if (velocity2 >= velocityThreshold2)
                    VelocityColor.CopyTo(out particle->Color);
                else
                {
                    var t = (float) Math.Sqrt(velocity2)/VelocityThreshold;

                    particle->Color = new HslColor(
                        deltaColor.H*t + StationaryColor.H,
                        deltaColor.S*t + StationaryColor.S,
                        deltaColor.L*t + StationaryColor.L);
                }
            }
        }
    }
}