Skip to content

Commit

Permalink
Particle brush - Added lifetime color application mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBeekman committed Jun 15, 2021
1 parent e57de37 commit 3edd738
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public class ParticlesPropertyGroup : LayerPropertyGroup
[PropertyDescription]
public ColorGradientLayerProperty Colors { get; set; }

[PropertyDescription(Description = "Choose the way the colors picked above behave")]
public EnumLayerProperty<ParticleColorMode> ColorMode { get; set; }

[PropertyDescription(Description = "The velocity at which particles are emitted, they can later be slowed down by gravity")]
public FloatRangeLayerProperty InitialVelocity { get; set; }

Expand Down Expand Up @@ -120,6 +123,12 @@ protected override void DisableProperties()
}
}

public enum ParticleColorMode
{
Random,
Lifetime
}

public enum EmitterPosition
{
Top,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using Artemis.Core;
using Artemis.Plugins.LayerBrushes.Particle.Models;
using Artemis.Plugins.LayerBrushes.Particle.Particle.Shapes;
using SkiaSharp;
Expand Down Expand Up @@ -85,7 +87,7 @@ public float Height
public float RotationY { get; set; }
public float RotationZ { get; set; }

public SKColorF Color { get; set; }
public SKColor Color { get; set; }
public ParticleShape Shape { get; set; }
public SKPoint Velocity { get; set; }
public SKPoint MaximumVelocity { get; set; }
Expand All @@ -94,6 +96,7 @@ public float Height
public float RotationVelocityZ { get; set; }
public bool FadeOut { get; set; }
public double Lifetime { get; set; }
public double TotalLifetime { get; set; }
public SKRect Bounds { get; private set; }
public bool IsComplete { get; private set; }
public bool HasBeenDrawn { get; set; }
Expand All @@ -120,7 +123,7 @@ public void Draw(SKCanvas canvas, SKPaint paint)
matrix = matrix.PreConcat(matrix44.Matrix);
canvas.SetMatrix(canvas.TotalMatrix.PreConcat(matrix));

paint.ColorF = Color;
paint.Color = Color;
Shape.Draw(canvas, paint, Width, Height);
canvas.Restore();
}
Expand Down Expand Up @@ -159,23 +162,29 @@ public void ApplyForce(SKPoint force, TimeSpan deltaTime)

Lifetime -= deltaTime.TotalSeconds;
if (Lifetime <= 0)
{
if (FadeOut)
{
SKColorF c = Color;
float alpha = c.Alpha - secs;
Color = c.WithAlpha(alpha);
IsComplete = alpha <= 0;
}
else
{
IsComplete = true;
}
}
IsComplete = true;

RotationX = (RotationX + RotationVelocityX * secs) % 360;
RotationY = (RotationY + RotationVelocityY * secs) % 360;
RotationZ = (RotationZ + RotationVelocityZ * secs) % 360;
}

public void ApplyLifetimeColor(ColorGradient colors, bool applyGradient)
{
if (IsComplete)
return;

float alphaMultiplier = 1;
if (FadeOut)
alphaMultiplier = (float) (Lifetime / TotalLifetime);

if (applyGradient)
{
Color = colors.GetColor(1f - (float) (Lifetime / TotalLifetime));
Color = Color.WithAlpha((byte) Math.Clamp(Color.Alpha * alphaMultiplier, 0, 255));
}
else
Color = Color.WithAlpha((byte) Math.Clamp(255 * alphaMultiplier, 0, 255));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Artemis.Core;
using Artemis.Plugins.LayerBrushes.Particle.LayerProperties;
using Artemis.Plugins.LayerBrushes.Particle.Models;
using SkiaSharp;

Expand Down Expand Up @@ -36,7 +38,7 @@ public ParticleEmitter Emitter

public ParticleEmitterBounds EmitterBounds { get; set; } = new(SKConfettiEmitterSide.Center);
public SKRect Bounds { get; set; }
public List<SKColor> Colors { get; set; } = new();
public ColorGradient Colors { get; set; } = new();
public List<float> Masses { get; set; } = CreateDefaultMasses();
public List<ParticleConfiguration> Configurations { get; set; } = new();
public float StartAngle { get; set; }
Expand All @@ -48,6 +50,7 @@ public ParticleEmitter Emitter
public bool FadeOut { get; set; } = true;
public SKPoint Gravity { get; set; } = new(0, 9.81f);
public bool IsComplete { get; set; }
public ParticleColorMode ParticleColorMode { get; set; }

public void Update(TimeSpan deltaTime)
{
Expand All @@ -62,6 +65,8 @@ public void Update(TimeSpan deltaTime)
Particle particle = _particles[i];
particle.ApplyForce(g, deltaTime);

particle.ApplyLifetimeColor(Colors, ParticleColorMode == ParticleColorMode.Lifetime);

if (particle.IsComplete || particle.HasBeenDrawn && !Bounds.IntersectsWithInclusive(particle.Bounds))
{
_particles.RemoveAt(i);
Expand Down Expand Up @@ -102,9 +107,10 @@ private void OnCreateParticle(int count)
Configurations == null || Configurations.Count == 0)
return;

SKColor[] colors = Colors.GetColorsArray();
for (int i = 0; i < count; i++)
{
SKColor c = Colors[_random.Next(Colors.Count)];
SKColor c = colors[_random.Next(Colors.Count)];
float mass = Masses[_random.Next(Masses.Count)];
ParticleConfiguration conf = Configurations[_random.Next(Configurations.Count)];
Particle particle = new(conf)
Expand All @@ -115,7 +121,8 @@ private void OnCreateParticle(int count)
Mass = mass,
MaximumVelocity = new SKPoint(MaximumVelocity, MaximumVelocity),
FadeOut = FadeOut,
Lifetime = Lifetime
Lifetime = Lifetime,
TotalLifetime = Lifetime
};

_particles.Add(particle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public override void Update(double deltaTime)
_particleSystem.Lifetime = Properties.Particles.Lifetime;
_particleSystem.FadeOut = Properties.Particles.FadeOut;
_particleSystem.Gravity = Properties.Gravity;
_particleSystem.Colors = Properties.Particles.Colors.CurrentValue.GetColorsArray().ToList();
_particleSystem.Colors = Properties.Particles.Colors.CurrentValue;
_particleSystem.ParticleColorMode = Properties.Particles.ColorMode.CurrentValue;
_particleSystem.Configurations = Properties.ParticleConfigurations.CurrentValue;
_particleSystem.Emitter.ParticleRate = Properties.Emitter.EmitParticles ? Properties.Emitter.ParticleRate : 0;

Expand Down

0 comments on commit 3edd738

Please sign in to comment.