3using System.Runtime.CompilerServices;
77 private readonly
Particle[] _particles;
78 private readonly Random _rng =
new();
79 private readonly SKPaint _paint =
new() { IsAntialias =
true };
81 private long _particlesLastTick = 0;
84 private readonly SKBitmap? _particleSprite;
90 public readonly List<ParticleEmitter>
Emitters =
new();
192 string? nickname =
null,
193 int maxParticles = 2000,
194 SKBitmap? particleSprite =
null)
204 _particles = ArrayPool<Particle>.Shared.Rent(maxParticles);
205 _particleSprite = particleSprite;
222 string? nickname =
null,
223 int maxParticles = 2000,
224 SKBitmap? particleSprite =
null)
252 string? nickname =
null,
253 int maxParticles = 2000,
254 SKBitmap? particleSprite =
null)
293 protected override void Dispose(
bool disposing)
297 ArrayPool<Particle>.Shared.Return(_particles, clearArray:
true);
300 _particlesLastTick = 0;
303 base.Dispose(disposing);
319 if (_particlesLastTick > 0)
320 dt = HighResTimer.GetDuration(_particlesLastTick, tick);
322 _particlesLastTick = tick;
325 for (
int i = 0; i <
Emitters.Count; i++)
329 for (
int i = 0; i <
Emitters.Count; i++)
333 em._accumulator += rate * dt;
335 int toEmit = (int)em._accumulator;
339 em._accumulator -= toEmit;
340 EmitFrom(em, toEmit);
347 for (
int i = 0; i < _alive; i++)
349 ref var p = ref _particles[i];
356 if (p.MaxVelocity > 0f)
359 float vx = p.VX, vy = p.VY;
360 float velocity2 = (vx * vx) + (vy * vy);
361 float max2 = p.MaxVelocity * p.MaxVelocity;
363 if (velocity2 > max2)
365 float inv = p.MaxVelocity / MathF.Sqrt(velocity2);
376 p.Rotation += p.AngularVel * dt;
389 if (p.Life > 0 && isInView)
390 _particles[write++] = p;
431 var canvas = backbuffer.
Canvas;
439 float sx = (srcBounds.Width > 0) ? (destRectScreen.Width / srcBounds.Width) : 1f;
440 float sy = (srcBounds.Height > 0) ? (destRectScreen.Height / srcBounds.Height) : 1f;
443 float sSize = (sx + sy) * 0.5f;
445 for (
int i = 0; i < _alive; i++)
447 ref var p = ref _particles[i];
450 _paint.BlendMode = p.BlendMode;
453 float t = 1f - (p.Life / p.MaxLife);
454 byte a = (byte)(255 * (1f - t));
460 _paint.Color = ApplyTint(p.Color, a, tint);
463 float x = destRectScreen.Left + (p.X - srcBounds.Left) * sx;
464 float y = destRectScreen.Top + (p.Y - srcBounds.Top) * sy;
466 if (p.ParticleSprite is
null)
468 float size = (p.Size * sSize) * (1f + 0.5f * t);
478 float s = p.Size * sSize;
480 var dst =
new SKRect(
487 canvas.RotateDegrees(p.Rotation, x, y);
488 canvas.DrawBitmap(p.ParticleSprite, dst, _paint);
496 for (
int i = 0; i < count && _alive < _particles.Length; i++)
498 ref var p = ref _particles[_alive++];
508 (
float dx,
float dy) = SampleSpawnOffset(em);
509 p.X = em.Position.X + dx;
510 p.Y = em.Position.Y + dy;
518 p.AX = em.GravityX ?? this.GravityX;
519 p.AY = em.GravityY ?? this.GravityY;
527 p.Rotation = NextRange(0f, 360f);
528 p.AngularVel = NextRange(-180f, 180f);
531 p.ParticleSprite = em.ParticleSprite ?? this._particleSprite;
537 p.MaxVelocity = em.MaxVelocity ?? 0f;
547 private float NextRange(
float min,
float max) => (float)(_rng.NextDouble() * (max - min) + min);
549 private (
float dx,
float dy) SampleSpawnOffset(ParticleEmitter em)
551 float jx = MathF.Abs(em.JitterX);
552 float jy = MathF.Abs(em.JitterY);
556 return (0f, NextRange(-jy, jy));
559 return (NextRange(-jx, jx), 0f);
561 switch (em.SpawnDistribution)
565 return (NextRange(-jx, jx), NextRange(-jy, jy));
570 float a = NextRange(0f, MathF.Tau);
571 float r = MathF.Sqrt(NextRange(0f, 1f));
572 return (MathF.Cos(a) * r * jx, MathF.Sin(a) * r * jy);
578 float inner = Math.Clamp(em.RingInnerRadius01, 0f, 1f);
579 float a = NextRange(0f, MathF.Tau);
582 float u = NextRange(0f, 1f);
583 float r = MathF.Sqrt(inner * inner + u * (1f - inner * inner));
585 return (MathF.Cos(a) * r * jx, MathF.Sin(a) * r * jy);
591 float sd = Math.Clamp(em.GaussianStdDev01, 1e-4f, 10f);
593 float dx = NextGaussian() * (jx * sd);
594 float dy = NextGaussian() * (jy * sd);
599 float q = nx * nx + ny * ny;
603 float s = 1f / MathF.Sqrt(q);
613 private float NextGaussian()
617 double u1 = 1.0 - _rng.NextDouble();
618 double u2 = 1.0 - _rng.NextDouble();
619 double mag = Math.Sqrt(-2.0 * Math.Log(u1));
620 double z0 = mag * Math.Cos(Math.Tau * u2);
626 [MethodImpl(MethodImplOptions.AggressiveInlining)]
627 private SKColor ApplyTint(SKColor c,
byte lifeAlpha, SKColor tint)
629 int r = (c.Red * tint.Red) / 255;
630 int g = (c.Green * tint.Green) / 255;
631 int b = (c.Blue * tint.Blue) / 255;
632 int a = (c.Alpha * lifeAlpha * tint.Alpha) / (255 * 255);
View? View
Gets the view to which this direct drawing is attached, or null if the drawing is in scene-layer mode...
long _lastTick
The last engine tick value used for delta-time calculations in Update.
SceneLayer? SceneLayer
Gets the scene layer to which this direct drawing is attached, or null if the drawing is in view mode...
Rectangle WorldBounds
Gets or sets the world-space bounds of this direct drawing in pixels.
DirectDrawingMode Mode
Gets the drawing mode that determines how this direct drawing is positioned and transformed.
Rectangle ScreenBounds
Gets or sets the screen-space bounds of this direct drawing in pixels.
DirectDrawingMovableBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, SceneLayer? sceneLayer, View? view, Rectangle? screenBounds, Rectangle? worldBounds, string? name=null)
Initializes a new instance of the DirectDrawingMovableBase class with integrated movement capabilitie...
Configures how particles are spawned over time at a given location.
SKColor? Tint
Optional per-emitter tint. If set, it overrides the particle system's GlobalColorTint for particles s...
float float Max VelocityRangeY
SKColor Color
Base color. Alpha will be modulated by lifetime fade. You can randomize in OnSpawn if desired.
float JitterX
Gets or sets the horizontal jitter value for spawning particles, which represents a random offset app...
SKBlendMode BlendMode
Blend mode used when rendering particles from this emitter. Defaults to SKBlendMode....
PointF Position
RenderHost / adapter pixel position where particles originate.
float float Max LifeRange
float float Max SizeRange
ParticleSpawnHandler? OnSpawn
Optional per-particle customization hook (e.g., perlin drift, hue jitter).
float float Max VelocityRangeX
float JitterY
Gets or sets the vertical jitter value for spawning particles, which represents a random offset appli...
A flexible particle system for effects like smoke, sparks, fire, or snow, implemented as a DirectDraw...
override void Update(long tick)
Tick-driven update override. Computes delta internally and advances simulation.
float GravityY
Gets or sets the gravitational acceleration along the Y-axis, measured in pixels per second squared....
ParticleSurface(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle? screenBounds, string? nickname=null, int maxParticles=2000, SKBitmap? particleSprite=null)
Initializes a new instance of the ParticleSurface class for view-based rendering with screen-space co...
override void Dispose(bool disposing)
Releases the particle array back to the pool and disposes managed resources.
readonly List< ParticleEmitter > Emitters
Collection of particle emitters this system updates and renders. Add multiple emitters (e....
ParticleSurface(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle? worldBounds, string? nickname=null, int maxParticles=2000, SKBitmap? particleSprite=null)
Initializes a new instance of the ParticleSurface class for scene layer rendering with world-space co...
void Burst(ParticleEmitter emitter, int count)
Immediately spawns a fixed number of particles from the given emitter. Useful for explosions,...
int ActiveParticleCount
Gets the current number of active (alive) particles being simulated.
SKColor GlobalColorTint
Global tint color multiplied against every particle's own color during rendering.
float GlobalEmitScale
Global multiplier applied to all emitter EmitRate values in this particle system. Acts like a master ...
float GravityX
Gets or sets the horizontal component of gravity, measured in pixels per second squared....
float CullingMarginY
Gets or sets the vertical culling margin, in pixels, beyond the bounds of the viewport to keep elemen...
float CullingMarginX
Gets or sets the horizontal culling margin, in pixels, beyond the bounds of the viewport to keep elem...
override void OnDraw(BackbufferBase backbuffer, RectangleF destRectScreen)
Renders all live particles to the current backbuffer.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
ParticleSpawnDistribution
Defines the spatial distribution pattern used when spawning particles within an emitter.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.
Represents a single particle instance in the particle system.