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 {
48
49 Count
50 };
51};
52
54{
55 enum Enum
56 {
57 Inside, // Particles spawn inside the shape (current behavior)
58 Surface, // Particles spawn on the surface of the shape
59
60 Count
61 };
62};
63
65{
66 enum Enum
67 {
68 World, // Particles are simulated in world space (current behavior)
69 Local, // Particles are simulated in local space and transformed during rendering
70
71 Count
72 };
73};
74
76{
77 enum Enum
78 {
79 MultiChannel, // Standard RGBA texture (default)
80 Mask, // Black/white mask texture (black = transparent, white = opaque)
81
82 Count
83 };
84};
85
87{
88 enum Enum
89 {
90 Billboard, // Always faces camera (default)
91 HorizontalBillboard, // Rotates around Y axis only, stays horizontal (parallel to ground)
92 VerticalBillboard, // Rotates around X/Z axis, stays vertical (perpendicular to ground)
93
94 Count
95 };
96};
97
99{
106};
107
109{
110 void reset();
111
112 // Simulation space determines how particles are transformed
114
115 // Transform for both local and world simulation
116 // Using math::transform for better performance - keeps components separate and combines into matrix when needed
118 math::transform m_prevTransform; // Previous transform for motion interpolation (set internally)
119
120 // Emission shape properties (separate from transform for flexibility)
121 math::vec3 m_emissionShapePosition; // Position offset for the emission shape (relative to transform)
122 math::vec3 m_emissionShapeScale; // 3D scale for the emission shape (x, y, z)
123
124 // Spawn location determines where particles spawn within the emission shape
126
127 math::gradient<frange_t> m_velocityGradient; // Velocity gradient over particle lifetime
128 math::gradient<frange_t> m_scaleGradient; // Scale gradient over particle lifetime
129 math::vec3 m_initialScale3D; // 3D particle scale (allows rectangular particles, default: 1,1,1)
132 float m_particlesPerSecond; // Emission rate in particles per second
133 float m_temporalMotion; // Temporal motion interpolation factor (0.0 = no interpolation, 1.0 = full interpolation)
134 float m_velocityDamping; // Velocity damping factor (0.0 = no damping, 1.0 = full damping)
135 math::vec3 m_forceOverLifetime; // Additional force applied over particle lifetime
136 frange_t m_sizeBySpeedRange; // Size multiplier range [min_multiplier, max_multiplier]
137 frange_t m_sizeBySpeedVelocityRange; // Velocity range for size mapping [min_speed, max_speed]
138 math::gradient<math::color> m_colorBySpeedGradient; // Color gradient based on speed
139 frange_t m_colorBySpeedVelocityRange; // Velocity range for color mapping [min_speed, max_speed]
140
141 math::gradient<float> m_lifetimeByEmitterSpeedGradient; // Lifetime multiplier gradient based on emitter speed
142 frange_t m_lifetimeByEmitterSpeedRange; // Emitter speed range for lifetime mapping [min_speed, max_speed]
143
144 math::gradient<math::color> m_colorGradient; // Color gradient over particle lifetime
145 float m_emissionLifetime; // Duration of one emission cycle
146 float m_opacity; // Global opacity for all particles (0.0 = fully transparent, 1.0 = no change)
147 float m_colorIntensity; // HDR multiplier for particle color RGB (1.0 = no change, >1.0 = glow)
148
149 // Playback control states
150 bool m_playing; // Whether the emitter is currently playing/active
151 bool m_paused; // Whether the emitter is paused (playing but with dt = 0)
152 bool m_loop; // Whether the emitter loops continuously (true) or emits only once (false)
153 float m_startDelay; // Delay before particle emission starts (in seconds, similar to Unity's start delay)
154
155 bx::Easing::Enum m_easePos; // Only position easing remains - others handled by gradients
156
157 TextureMode::Enum m_textureMode; // Texture mode (MultiChannel or Mask)
158 RenderMode::Enum m_renderMode; // Render mode (Billboard, Horizontal, or Vertical)
159 BlendMode::Enum m_blendMode; // Blend mode (Normal, Additive, Multiply)
160
161 // Billboard vectors (calculated from render mode and camera)
162 math::vec3 m_billboardRight; // Right vector for billboarding
163 math::vec3 m_billboardUp; // Up vector for billboarding
164
165 // Texture sheet animation parameters
166 math::vec2 m_texSheetTiles; // Number of tiles in the texture sheet grid (X columns, Y rows)
167 float m_texSheetCycles; // Number of times the animation loops over particle lifetime (0 = disabled)
168 bool m_texSheetRandomize; // Start each particle at a random frame in the animation
169
170 // Rotation control
171 bool m_alignToDirection; // If true, particles rotate to align with their velocity direction
172
173 // Pivot control (0,0 = bottom-left, 0.5,0.5 = center, 1,1 = top-right)
174 math::vec2 m_pivot; // Pivot point for particle rotation and positioning (default: 0.5, 0.5 = center)
175};
176
178void psInit(uint16_t _maxEmitters = 64, bx::AllocatorI* _allocator = nullptr);
179
181void psShutdown();
182
183// Note: Sprite system removed - use bgfx::TextureHandle directly in EmitterUniforms
184
186EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles);
187
189void psUpdateEmitter(EmitterHandle _handle, float _dt, EmitterUniforms* _uniforms = nullptr);
190
191bool psHasUpdated(EmitterHandle _handle);
192
194void psResetEmitter(EmitterHandle _handle);
195
197void psGetAabb(EmitterHandle _handle, math::bbox& _outAabb);
198
199uint32_t psGetNumParticles(EmitterHandle _handle);
200
202void psDestroyEmitter(EmitterHandle _handle);
203
204
213uint32_t psRenderEmitterBatch(const EmitterHandle* _handles,
214 uint32_t _count,
215 uint8_t _view,
216 bgfx::ProgramHandle _program,
217 const float* _mtxView,
218 const math::vec3& _eye,
219 bgfx::TextureHandle _texture,
220 uint64_t _blend_state,
221 bool _sort_by_depth = true);
222
223#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 psGetNumParticles(EmitterHandle _handle)
void psGetAabb(EmitterHandle _handle, math::bbox &_outAabb)
bool isValid(Ty _handle)
void psDestroyEmitter(EmitterHandle _handle)
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, uint64_t _blend_state, bool _sort_by_depth=true)
math::vec2 m_texSheetTiles
math::gradient< math::color > m_colorGradient
math::transform m_transform
frange_t m_sizeBySpeedRange
math::transform m_prevTransform
math::vec3 m_initialScale3D
math::vec3 m_billboardUp
BlendMode::Enum m_blendMode
bx::Easing::Enum m_easePos
math::vec3 m_billboardRight
RenderMode::Enum m_renderMode
frange_t m_lifetimeByEmitterSpeedRange
math::vec3 m_emissionShapePosition
math::gradient< frange_t > m_scaleGradient
math::gradient< frange_t > m_velocityGradient
TextureMode::Enum m_textureMode
math::gradient< float > m_lifetimeByEmitterSpeedGradient
frange_t m_sizeBySpeedVelocityRange
EmitterSpawnLocation::Enum m_spawnLocation
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