Class ParticlePool
The ParticlePool is a class which manages an unmanaged memory block used for the particles. The maximum required size calculated on the number of particles and their fields' sizes is calculated every time the sizes or the count change
public class ParticlePool : IDisposable, IEnumerable
- Inheritance
-
ParticlePool
- Implements
- Extension Methods
Constructors
ParticlePool(int, int, ListPolicy)
ParticlePool constructor
public ParticlePool(int size, int capacity, ParticlePool.ListPolicy listPolicy = ListPolicy.Stack)
Parameters
size
intInitial size in bytes of a single particle
capacity
intInitial capacity (maximum number of particles) of the pool
listPolicy
ParticlePool.ListPolicyList policy - stack (living particles are in the front) or ring
Fields
DefaultMaxFielsPerPool
public const int DefaultMaxFielsPerPool = 16
Field Value
Properties
AvailableParticles
Gets how many more particles can be spawned
public int AvailableParticles { get; }
Property Value
LivingParticles
Get the number of living (active) particles
public int LivingParticles { get; }
Property Value
NextFreeIndex
NextFreeIndex points to the next index ready for allocation, between 0 and ParticleCapacity - 1. In case of stack list the NextFreeIndex equals the number of living particles in the pool.
public int NextFreeIndex { get; }
Property Value
ParticleCapacity
The maximum allowed number of particles in this ParticlePool. Use SetCapacity(int) if you need to change it.
public int ParticleCapacity { get; }
Property Value
ParticleData
ParticleData is where the memory block (particle pool) actually resides. Its size equals ParticleSize * ParticleCapacity
public nint ParticleData { get; }
Property Value
ParticleSize
Returns the size of a single Particle. If PARTICLES_SOA is defined, the size of the Particle is the sum of all field strides. Otherwise, the size of the Particle is the pool's stride.
public int ParticleSize { get; }
Property Value
Methods
AddParticle()
Add a new particle to the pool. Doesn't worry about initialization.
public Particle AddParticle()
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
DisposeParticleData()
protected void DisposeParticleData()
FieldExists(ParticleFieldDescription, bool)
Polls if a filed with this description exists in the pool and optionally forces creation of a new field
public bool FieldExists(ParticleFieldDescription fieldDesc, bool forceCreate = false)
Parameters
fieldDesc
ParticleFieldDescriptionDescription of the field
forceCreate
boolForce the creation of non-existing fields if
true
Returns
~ParticlePool()
protected ~ParticlePool()
FromIndex(int)
Get a particle from its index in the pool
public Particle FromIndex(int idx)
Parameters
idx
int
Returns
GetEnumerator()
Returns an ParticlePool.Enumerator to the particles in this ParticlePool In case of Ring dead particles are returned too, so the calling entity should handle such cases.
public ParticlePool.Enumerator GetEnumerator()
Returns
GetField<T>(ParticleFieldDescription<T>)
Unsafe method for getting a ParticleFieldAccessor. If the field doesn't exist an invalid accessor is returned to the user.
public ParticleFieldAccessor<T> GetField<T>(ParticleFieldDescription<T> fieldDesc) where T : struct
Parameters
fieldDesc
ParticleFieldDescription<T>
Returns
Type Parameters
T
RemoveField(ParticleFieldDescription)
Removes a particle field from this pool with the specified description, or gets an existing one
public bool RemoveField(ParticleFieldDescription fieldDesc)
Parameters
fieldDesc
ParticleFieldDescriptionDescription of the field
Returns
- bool
true
if the field was successfully removed,false
otherwise
Reset()
Clears all particle fields, but keeps the particle capacity the same.
public void Reset()
SetCapacity(int)
Set a different capacity (maximum Particle count for this pool) Whenever possible, existing particles will be copied and continue simulation
public void SetCapacity(int newCapacity)
Parameters
newCapacity
intNew maximum capacity
TryGetField<T>(ParticleFieldDescription<T>, out ParticleFieldAccessor<T>)
Gets the particle field with the specified description if the field exists in this pool
public bool TryGetField<T>(ParticleFieldDescription<T> fieldDesc, out ParticleFieldAccessor<T> accessor) where T : struct
Parameters
fieldDesc
ParticleFieldDescription<T>Field's decription
accessor
ParticleFieldAccessor<T>Accessor for the field
Returns
Type Parameters
T
Type data for the field