Unravel Engine C++ Reference
Loading...
Searching...
No Matches
particle_system.h
Go to the documentation of this file.
1/*
2 * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef PARTICLE_SYSTEM_H_HEADER_GUARD
7#define PARTICLE_SYSTEM_H_HEADER_GUARD
8
9#include <bx/allocator.h>
10#include <bx/bounds.h>
11#include <bx/easing.h>
12#include <bx/rng.h>
13#include <bgfx/bgfx.h>
14#include <math/gradient.h>
15#include <math/math.h>
16#include <math/bbox.h>
18
19struct EmitterHandle { uint16_t idx; };
20
21template<typename Ty>
22inline bool isValid(Ty _handle)
23{
24 return _handle.idx != UINT16_MAX;
25}
26
40
42{
43 enum Enum
44 {
47
48 Count
49 };
50};
51
53{
54 enum Enum
55 {
56 World, // Particles are simulated in world space (current behavior)
57 Local, // Particles are simulated in local space and transformed during rendering
58
59 Count
60 };
61};
62
64{
65 void reset();
66
67 // Simulation space determines how particles are transformed
69
70 // Transform for both local and world simulation
71 // Using math::transform for better performance - keeps components separate and combines into matrix when needed
73 math::transform m_prevTransform; // Previous transform for motion interpolation (set internally)
74
75 // Emission shape scale (separate from transform scale for flexibility)
76 math::vec3 m_emissionShapeScale; // 3D scale for the emission shape (x, y, z)
77
78 math::gradient<frange_t> m_velocityGradient; // Velocity gradient over particle lifetime
79 math::gradient<frange_t> m_blendGradient; // Blend/opacity gradient over particle lifetime
80 math::gradient<frange_t> m_scaleGradient; // Scale gradient over particle lifetime
83 float m_particlesPerSecond; // Emission rate in particles per second
84 float m_temporalMotion; // Temporal motion interpolation factor (0.0 = no interpolation, 1.0 = full interpolation)
85 float m_velocityDamping; // Velocity damping factor (0.0 = no damping, 1.0 = full damping)
86 math::vec3 m_forceOverLifetime; // Additional force applied over particle lifetime
87 frange_t m_sizeBySpeedRange; // Size multiplier range [min_multiplier, max_multiplier]
88 frange_t m_sizeBySpeedVelocityRange; // Velocity range for size mapping [min_speed, max_speed]
89 math::gradient<math::color> m_colorBySpeedGradient; // Color gradient based on speed
90 frange_t m_colorBySpeedVelocityRange; // Velocity range for color mapping [min_speed, max_speed]
91
92 math::gradient<float> m_lifetimeByEmitterSpeedGradient; // Lifetime multiplier gradient based on emitter speed
93 frange_t m_lifetimeByEmitterSpeedRange; // Emitter speed range for lifetime mapping [min_speed, max_speed]
94
95 math::gradient<math::color> m_colorGradient; // Color gradient over particle lifetime
96 float m_emissionLifetime; // Duration of one emission cycle
97 float m_blendMultiplier; // Global blend multiplier for all particles (0.0 = fully transparent, 1.0 = no change)
98
99 // Playback control states
100 bool m_playing; // Whether the emitter is currently playing/active
101 bool m_paused; // Whether the emitter is paused (playing but with dt = 0)
102 bool m_loop; // Whether the emitter loops continuously (true) or emits only once (false)
103
104 bx::Easing::Enum m_easePos; // Only position easing remains - others handled by gradients
105
106 bgfx::TextureHandle m_texture;
107};
108
110void psInit(uint16_t _maxEmitters = 64, bx::AllocatorI* _allocator = nullptr);
111
113void psShutdown();
114
115// Note: Sprite system removed - use bgfx::TextureHandle directly in EmitterUniforms
116
118EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles);
119
121void psUpdateEmitter(EmitterHandle _handle, float _dt, EmitterUniforms* _uniforms = nullptr);
122
123bool psHasUpdated(EmitterHandle _handle);
124
126void psResetEmitter(EmitterHandle _handle);
127
129void psGetAabb(EmitterHandle _handle, math::bbox& _outAabb);
130
131uint32_t psGetNumParticles(EmitterHandle _handle);
132
134void psDestroyEmitter(EmitterHandle _handle);
135
137void psRenderEmitter(EmitterHandle _handle, uint8_t _view, bgfx::ProgramHandle _program, const float* _mtxView, const math::vec3& _eye, bgfx::TextureHandle _texture);
138
149uint32_t psRenderEmitterBatch(const EmitterHandle* _handles, uint32_t _count, uint8_t _view, bgfx::ProgramHandle _program, const float* _mtxView, const math::vec3& _eye, bgfx::TextureHandle _texture);
150
151#endif // PARTICLE_SYSTEM_H_HEADER_GUARD
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
void psResetEmitter(EmitterHandle _handle)
bool psHasUpdated(EmitterHandle _handle)
void psInit(uint16_t _maxEmitters=64, bx::AllocatorI *_allocator=nullptr)
void psUpdateEmitter(EmitterHandle _handle, float _dt, EmitterUniforms *_uniforms=nullptr)
EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
void psShutdown()
uint32_t psRenderEmitterBatch(const EmitterHandle *_handles, uint32_t _count, uint8_t _view, bgfx::ProgramHandle _program, const float *_mtxView, const math::vec3 &_eye, bgfx::TextureHandle _texture)
uint32_t psGetNumParticles(EmitterHandle _handle)
void psGetAabb(EmitterHandle _handle, math::bbox &_outAabb)
bool isValid(Ty _handle)
void psRenderEmitter(EmitterHandle _handle, uint8_t _view, bgfx::ProgramHandle _program, const float *_mtxView, const math::vec3 &_eye, bgfx::TextureHandle _texture)
void psDestroyEmitter(EmitterHandle _handle)
math::gradient< math::color > m_colorGradient
math::transform m_transform
frange_t m_sizeBySpeedRange
math::transform m_prevTransform
bx::Easing::Enum m_easePos
math::gradient< frange_t > m_blendGradient
bgfx::TextureHandle m_texture
frange_t m_lifetimeByEmitterSpeedRange
math::gradient< frange_t > m_scaleGradient
math::gradient< frange_t > m_velocityGradient
math::gradient< float > m_lifetimeByEmitterSpeedGradient
frange_t m_sizeBySpeedVelocityRange
SimulationSpace::Enum m_simulationSpace
math::gradient< math::color > m_colorBySpeedGradient
math::vec3 m_forceOverLifetime
math::vec3 m_emissionShapeScale
frange_t m_colorBySpeedVelocityRange
Storage for box vector values and wraps up common functionality.
Definition bbox.h:21