Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::particle::ParticleSystem Class Reference

#include <ParticleSystem.hpp>

Public Member Functions

 ParticleSystem ()
void reseedFrom (const esengine::RandomSource &source)
void update (ecs::Registry &registry, f32 dt)
void setGpuAvailable (bool available)
bool gpuAvailable () const
bool peekGpuStep (Entity entity, GpuStep &out) const
void commitGpuStep (Entity entity, const GpuStep &step)
 The kernel ran this step: the time and births it covered are spent.
const std::vector< ForceFieldInstance > & forceFields () const
const std::vector< SdfColliderInstance > & sdfColliders () const
void addGpuCollisions (Entity entity, u32 total, const CollisionEvent *events, u32 count)
 Collisions a kernel reported, a frame or two late since they come back from the GPU; they reach the game as any others do.
const std::vector< CollisionQuery > & collisionQueries () const
const std::vector< CollisionEvent > & collisionEvents () const
const std::vector< CollisionStats > & collisionStats () const
void setCollisionBudget (u32 queries, u32 events, bool debug=false)
void resolveCollisions (ecs::Registry &registry, const CollisionReply *replies, u32 count)
void play (Entity entity)
void stop (Entity entity)
void reset (Entity entity)
u32 aliveCount (Entity entity) const
u32 emitAt (ecs::Registry &registry, Entity entity, u32 count, const glm::vec3 &position, const glm::vec3 &normal, const glm::vec3 &velocity)
 Spawn count of entity's particles at a world point, now: an impact's sparks, a muzzle's flash, without moving the emitter or replaying it.
u32 totalAliveParticles () const
void setParticleLimit (u32 limit)
template<typename Fn>
void forEachParticle (Entity entity, Fn &&fn) const
EmitterState * getState (Entity entity)
const EmitterState * getState (Entity entity) const
void setColorLut (Entity entity, const f32 *rgba, i32 count)
void setSizeLut (Entity entity, const f32 *values, i32 count)
void setSpeedLut (Entity entity, const f32 *values, i32 count)
void setBursts (Entity entity, const f32 *values, i32 count)
const ColorLut * colorLut (Entity entity) const
 The over-life curves an entity was given, for the GPU kernel to read; null where none was set.
const SizeLut * sizeLut (Entity entity) const
const SizeLut * speedLut (Entity entity) const

Static Public Attributes

static constexpr const char * kRandomStream = "particles"
static constexpr u32 kGpuMaxCapacity = 2'000'000
static constexpr u32 kGpuFallbackCapacity = 20'000

Detailed Description

Definition at line 138 of file ParticleSystem.hpp.

Constructor & Destructor Documentation

◆ ParticleSystem()

esengine::particle::ParticleSystem::ParticleSystem ( )

Definition at line 16 of file ParticleSystem.cpp.

References kRandomStream.

Member Function Documentation

◆ addGpuCollisions()

void esengine::particle::ParticleSystem::addGpuCollisions ( Entity entity,
u32 total,
const CollisionEvent * events,
u32 count )

Collisions a kernel reported, a frame or two late since they come back from the GPU; they reach the game as any others do.

Definition at line 908 of file ParticleSystem.cpp.

References addGpuCollisions().

Referenced by addGpuCollisions().

◆ aliveCount()

u32 esengine::particle::ParticleSystem::aliveCount ( Entity entity) const

Definition at line 372 of file ParticleSystem.cpp.

◆ collisionEvents()

const std::vector< CollisionEvent > & esengine::particle::ParticleSystem::collisionEvents ( ) const
inline

Definition at line 173 of file ParticleSystem.hpp.

◆ collisionQueries()

const std::vector< CollisionQuery > & esengine::particle::ParticleSystem::collisionQueries ( ) const
inline

Definition at line 172 of file ParticleSystem.hpp.

◆ collisionStats()

const std::vector< CollisionStats > & esengine::particle::ParticleSystem::collisionStats ( ) const
inline

Definition at line 174 of file ParticleSystem.hpp.

◆ colorLut()

const ColorLut * esengine::particle::ParticleSystem::colorLut ( Entity entity) const
inline

The over-life curves an entity was given, for the GPU kernel to read; null where none was set.

Definition at line 226 of file ParticleSystem.hpp.

◆ commitGpuStep()

void esengine::particle::ParticleSystem::commitGpuStep ( Entity entity,
const GpuStep & step )

◆ emitAt()

u32 esengine::particle::ParticleSystem::emitAt ( ecs::Registry & registry,
Entity entity,
u32 count,
const glm::vec3 & position,
const glm::vec3 & normal,
const glm::vec3 & velocity )

Spawn count of entity's particles at a world point, now: an impact's sparks, a muzzle's flash, without moving the emitter or replaying it.

The emitter's own settings make them (shape, speed, size, colour, life). Its shape stands at position, turned so its local +Y — a cone's or a hemisphere's axis — points along normal when one is given, and as the entity is turned otherwise; velocity is added to every particle. A Local-space emitter keeps them in its own frame, as a sub-emitter's burst is kept. CPU-simulated emitters only: a GPU one answers 0.

Returns
How many were spawned (fewer than asked when the pool or the particle budget is full).

Definition at line 517 of file ParticleSystem.cpp.

References esengine::particle::ParticlePool::aliveCount(), esengine::ecs::Registry::get(), esengine::particle::EmitterState::gpu, esengine::ecs::Registry::has(), esengine::ecs::Local, esengine::particle::EmitterState::pool, esengine::ecs::Registry::valid(), and esengine::ecs::Transform::worldRotation.

◆ forceFields()

const std::vector< ForceFieldInstance > & esengine::particle::ParticleSystem::forceFields ( ) const
inline

Definition at line 164 of file ParticleSystem.hpp.

◆ forEachParticle()

template<typename Fn>
void esengine::particle::ParticleSystem::forEachParticle ( Entity entity,
Fn && fn ) const
inline

Definition at line 202 of file ParticleSystem.hpp.

◆ getState() [1/2]

EmitterState * esengine::particle::ParticleSystem::getState ( Entity entity)

Definition at line 420 of file ParticleSystem.cpp.

◆ getState() [2/2]

const EmitterState * esengine::particle::ParticleSystem::getState ( Entity entity) const

Definition at line 425 of file ParticleSystem.cpp.

◆ gpuAvailable()

bool esengine::particle::ParticleSystem::gpuAvailable ( ) const
inline

Definition at line 154 of file ParticleSystem.hpp.

◆ peekGpuStep()

bool esengine::particle::ParticleSystem::peekGpuStep ( Entity entity,
GpuStep & out ) const

◆ play()

void esengine::particle::ParticleSystem::play ( Entity entity)

Definition at line 293 of file ParticleSystem.cpp.

◆ reseedFrom()

void esengine::particle::ParticleSystem::reseedFrom ( const esengine::RandomSource & source)

Re-derive this system's generator from the engine's seed. Spawns after this point reproduce; particles already alive keep the values they got.

Definition at line 19 of file ParticleSystem.cpp.

References kRandomStream, and esengine::RandomSource::stream().

◆ reset()

void esengine::particle::ParticleSystem::reset ( Entity entity)

Definition at line 313 of file ParticleSystem.cpp.

◆ resolveCollisions()

◆ sdfColliders()

const std::vector< SdfColliderInstance > & esengine::particle::ParticleSystem::sdfColliders ( ) const
inline

Definition at line 165 of file ParticleSystem.hpp.

◆ setBursts()

void esengine::particle::ParticleSystem::setBursts ( Entity entity,
const f32 * values,
i32 count )

Set/clear an entity's burst schedule: count pairs of (seconds, particles) in values. Each fires once per pass of the emitter's clock over its time, and a looping emitter passes every duration.

Definition at line 360 of file ParticleSystem.cpp.

◆ setCollisionBudget()

void esengine::particle::ParticleSystem::setCollisionBudget ( u32 queries,
u32 events,
bool debug = false )
inline

Definition at line 175 of file ParticleSystem.hpp.

◆ setColorLut()

void esengine::particle::ParticleSystem::setColorLut ( Entity entity,
const f32 * rgba,
i32 count )

Set (count == kColorLutSize) or clear (count == 0) an entity's color-over-life LUT. When set, particle color is sampled from it instead of start/end+easing.

Definition at line 331 of file ParticleSystem.cpp.

References esengine::particle::kColorLutSize.

◆ setGpuAvailable()

void esengine::particle::ParticleSystem::setGpuAvailable ( bool available)
inline

Whether emitters that ask for GPU simulation get it. Set by the renderer, which knows the device; false means they run on the CPU, capped at kGpuFallbackCapacity.

Definition at line 153 of file ParticleSystem.hpp.

◆ setParticleLimit()

void esengine::particle::ParticleSystem::setParticleLimit ( u32 limit)
inline

Definition at line 199 of file ParticleSystem.hpp.

◆ setSizeLut()

void esengine::particle::ParticleSystem::setSizeLut ( Entity entity,
const f32 * values,
i32 count )

Set/clear an entity's size-over-life multiplier LUT (× the particle's start size).

Definition at line 342 of file ParticleSystem.cpp.

References esengine::particle::kColorLutSize.

◆ setSpeedLut()

void esengine::particle::ParticleSystem::setSpeedLut ( Entity entity,
const f32 * values,
i32 count )

Set/clear an entity's speed-over-life multiplier LUT. It scales how fast a particle travels along the trajectory it already has — it is not a force, and gravity, damping and noise still accumulate into velocity as before.

Definition at line 351 of file ParticleSystem.cpp.

References esengine::particle::kColorLutSize.

◆ sizeLut()

const SizeLut * esengine::particle::ParticleSystem::sizeLut ( Entity entity) const
inline

Definition at line 227 of file ParticleSystem.hpp.

◆ speedLut()

const SizeLut * esengine::particle::ParticleSystem::speedLut ( Entity entity) const
inline

Definition at line 228 of file ParticleSystem.hpp.

◆ stop()

void esengine::particle::ParticleSystem::stop ( Entity entity)

Definition at line 305 of file ParticleSystem.cpp.

◆ totalAliveParticles()

u32 esengine::particle::ParticleSystem::totalAliveParticles ( ) const

Definition at line 412 of file ParticleSystem.cpp.

◆ update()

void esengine::particle::ParticleSystem::update ( ecs::Registry & registry,
f32 dt )

Definition at line 23 of file ParticleSystem.cpp.

References esengine::ecs::Registry::activeView(), esengine::particle::ParticlePool::aliveCount(), esengine::ecs::Box, esengine::particle::EmitterState::burst_clock, esengine::particle::EmitterState::burst_timer, esengine::particle::burstsDue(), esengine::particle::ForceFieldInstance::direction, esengine::particle::EmitterState::elapsed_time, esengine::particle::EmitterState::emission_accumulator, esengine::particle::CollisionStats::emitter, esengine::ecs::Transform::ensureDecomposed(), esengine::particle::CollisionStats::eventDropped, esengine::particle::CollisionStats::events, esengine::particle::SdfColliderInstance::ext, esengine::particle::ForceFieldInstance::falloff, esengine::particle::EmitterState::first_update, esengine::Entity::fromRaw(), esengine::particle::EmitterState::gpu, esengine::particle::EmitterState::gpuBurst, esengine::particle::EmitterState::gpuCapacity, esengine::particle::CollisionStats::hits, esengine::particle::SdfColliderInstance::inverted, esengine::ecs::Camera::isActive, kGpuFallbackCapacity, kGpuMaxCapacity, esengine::particle::kMaxTrailPoints, esengine::particle::SdfColliderInstance::mask, esengine::particle::CollisionStats::mode, esengine::particle::EmitterState::noise_time, esengine::ecs::Registry::onDestroy(), esengine::particle::EmitterState::playing, esengine::particle::EmitterState::pool, esengine::particle::ForceFieldInstance::position, esengine::particle::SdfColliderInstance::position, esengine::particle::ForceFieldInstance::radius, esengine::particle::ForceFieldInstance::radiusSq, esengine::particle::SdfColliderInstance::rotation, esengine::particle::SdfColliderInstance::shape, esengine::particle::ForceFieldInstance::strength, esengine::particle::ForceFieldInstance::type, and esengine::ecs::World.

Member Data Documentation

◆ kGpuFallbackCapacity

u32 esengine::particle::ParticleSystem::kGpuFallbackCapacity = 20'000
staticconstexpr

Definition at line 156 of file ParticleSystem.hpp.

Referenced by update().

◆ kGpuMaxCapacity

u32 esengine::particle::ParticleSystem::kGpuMaxCapacity = 2'000'000
staticconstexpr

Definition at line 155 of file ParticleSystem.hpp.

Referenced by update().

◆ kRandomStream

const char* esengine::particle::ParticleSystem::kRandomStream = "particles"
staticconstexpr

The engine stream this system draws from — see core/RandomSource.hpp.

Definition at line 141 of file ParticleSystem.hpp.

Referenced by ParticleSystem(), and reseedFrom().


The documentation for this class was generated from the following files: