Skylicht Engine
Loading...
Searching...
No Matches
CGroup.h
1/*
2!@
3MIT License
4
5Copyright (c) 2020 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "CParticle.h"
28#include "Entity/CEntityPrefab.h"
29
30#include "Emitters/CEmitter.h"
31#include "Zones/CZone.h"
32#include "Renderers/IRenderer.h"
33#include "Systems/ISystem.h"
34
35#include "Systems/CParticleSystem.h"
36#include "Systems/CParticleInstancingSystem.h"
37#include "Systems/CParticleCPUBufferSystem.h"
38
39#include "CParticleInstancing.h"
40#include "CParticleCPUBuffer.h"
41
42#include "CModel.h"
43
44#include "CParticleSerializable.h"
45
46namespace Skylicht
47{
48 namespace Particle
49 {
55 struct SLaunchParticle
56 {
57 CEmitter* Emitter;
58 u32 Number;
59 s32 Parent;
60
61 SLaunchParticle()
62 {
63 Emitter = NULL;
64 Number = 0;
65 Parent = -1;
66 }
67 };
68
74 class COMPONENT_API IParticleCallback
75 {
76 public:
77 IParticleCallback()
78 {
79
80 }
81
82 virtual ~IParticleCallback()
83 {
84
85 }
86
87 virtual void OnParticleUpdate(CParticle* particles, int num, CGroup* group, float dt)
88 {
89
90 }
91
92 virtual void OnParticleBorn(CParticle& p)
93 {
94
95 }
96
97 virtual void OnParticleDead(CParticle& p)
98 {
99
100 }
101
102 virtual void OnSwapParticleData(CParticle& p1, CParticle& p2)
103 {
104
105 }
106
107 virtual void OnGroupDestroy()
108 {
109
110 }
111 };
112
131 class COMPONENT_API CGroup : public CParticleSerializable
132 {
133 protected:
138
140 std::vector<CEmitter*> m_emitters;
142 std::vector<ISystem*> m_systems;
144 std::vector<CModel*> m_models;
146 std::vector<CInterpolator*> m_interpolators;
147
150
155
158
163
165 std::vector<IParticleCallback*> m_callback;
166
167 public:
169 float Friction;
171 float LifeMin;
173 float LifeMax;
176
183
192
194 std::wstring Name;
197
200
201 protected:
202 core::matrix4 m_parentWorld;
203 core::matrix4 m_world;
204 core::aabbox3df m_bbox;
205
206 int m_frameUpdate;
207 public:
208 CGroup();
209
210 virtual ~CGroup();
211
214
217
219 void update();
220
223
226
229
232 {
233 m_parentWorld = m;
234 }
235
237 inline void setWorldMatrix(const core::matrix4& m)
238 {
239 m_world = m;
240 }
241
243 inline const core::matrix4& getWorld()
244 {
245 return m_world;
246 }
247
250
253
255 inline const core::aabbox3df& getBBox()
256 {
257 return m_bbox;
258 }
259
262 {
263 return m_particles.size();
264 }
265
268 {
269 return m_particles.pointer();
270 }
271
274 {
275 m_emitters.push_back(e);
276 return e;
277 }
278
280 inline std::vector<CEmitter*>& getEmitters()
281 {
282 return m_emitters;
283 }
284
287
290 {
291 std::vector<IParticleCallback*>::iterator i = std::find(m_callback.begin(), m_callback.end(), cb);
292 if (i == m_callback.end())
293 m_callback.push_back(cb);
294 }
295
298 {
299 std::vector<IParticleCallback*>::iterator i = std::find(m_callback.begin(), m_callback.end(), cb);
300 if (i != m_callback.end())
301 m_callback.erase(i);
302 }
303
305 inline std::vector<IParticleCallback*>& getCallback()
306 {
307 return m_callback;
308 }
309
311 inline void addSystem(ISystem* s)
312 {
313 m_systems.push_back(s);
314 }
315
317 inline std::vector<ISystem*>& getSystems()
318 {
319 return m_systems;
320 }
321
324 {
325 std::vector<ISystem*>::iterator i = std::find(m_systems.begin(), m_systems.end(), s);
326 if (i != m_systems.end())
327 m_systems.erase(i);
328 }
329
331 int addParticle(u32 emiterID, const core::vector3df& position, const core::vector3df& subEmitterDirection)
332 {
333 if (emiterID < m_emitters.size())
334 {
335 return addParticleByEmitter(m_emitters[emiterID], position, subEmitterDirection);
336 }
337
338 return -1;
339 }
340
342 int addParticleVelocity(u32 emiterID, const core::vector3df& position, const core::vector3df& velocity)
343 {
344 if (emiterID < m_emitters.size())
345 {
346 return addParticleVelocityByEmitter(m_emitters[emiterID], position, velocity);
347 }
348
349 return -1;
350 }
351
353 int addParticleByEmitter(CEmitter* emitter, const core::vector3df& position, const core::vector3df& subEmitterDirection);
354
356 int addParticleVelocityByEmitter(CEmitter* emitter, const core::vector3df& position, const core::vector3df& velocity);
357
360
363 {
364 return m_renderer;
365 }
366
372
378
381 {
382 return m_particles.size();
383 }
384
386 CModel* createModel(const std::wstring& attributeName);
387
390
393
396
398 std::vector<CModel*>& getModels()
399 {
400 return m_models;
401 }
402
405
408
410 std::vector<CInterpolator*>& getInterpolators()
411 {
412 return m_interpolators;
413 }
414
420
422 inline int getFrameUpdate()
423 {
424 return m_frameUpdate;
425 }
426
428 inline void updateFrame(int frame)
429 {
430 // sync frame from CParticleComponent
431 m_frameUpdate = frame;
432 }
433
434 DECLARE_GETTYPENAME(CGroup)
435
437
439
440 protected:
441
442 virtual void updateLaunchEmitter();
443
444 virtual void bornParticle();
445
446 virtual bool launchParticle(CParticle& p, SLaunchParticle& launch);
447
448 void initParticleModel(CParticle& p);
449
450 inline void initParticleLifeTime(CParticle& p)
451 {
452 p.Age = 0.0f;
453 p.Life = random(LifeMin, LifeMax);
454 p.Immortal = Immortal;
455 p.LifeTime = p.Life;
456 p.HaveRotate = false;
457 }
458
459 CParticle* create(u32 num);
460
461 void remove(u32 i);
462 };
463 }
464}
Keyframe interpolator for scalar, vector, and color values.
Definition CInterpolator.h:146
Definition CObjectSerializable.h:36
Base class for particle emitters.
Definition CEmitter.h:88
Represents a group of particles with shared settings, emitters, and a renderer.
Definition CGroup.h:132
void clearParticles()
Clears all active particles.
CParticleCPUBufferSystem * m_cpuBufferSystem
Internal system for CPU buffer updates.
Definition CGroup.h:154
int addParticleByEmitter(CEmitter *emitter, const core::vector3df &position, const core::vector3df &subEmitterDirection)
Manually spawns a particle using a pointer to an emitter.
core::array< CParticle > m_particles
Active particles in this group.
Definition CGroup.h:135
u32 getCurrentParticleCount()
Gets current particle count.
Definition CGroup.h:380
bool Immortal
If true, particles do not die from age.
Definition CGroup.h:175
IRenderer * setRenderer(IRenderer *r)
Assigns a renderer to this group.
void removeSystem(ISystem *s)
Removes a system.
Definition CGroup.h:323
IRenderer * m_renderer
The renderer used to draw this group.
Definition CGroup.h:157
core::vector3df OrientationUp
Custom orientation up vector for fixed billboarding.
Definition CGroup.h:189
std::vector< CModel * > m_models
Models defining parameter animation (e.g. ColorA over life).
Definition CGroup.h:144
bool Visible
Visibility flag.
Definition CGroup.h:196
int addParticleVelocityByEmitter(CEmitter *emitter, const core::vector3df &position, const core::vector3df &velocity)
Manually spawns a particle with initial velocity using a pointer to an emitter.
float Friction
Global friction for all particles in this group.
Definition CGroup.h:169
std::vector< CModel * > & getModels()
Gets all models.
Definition CGroup.h:398
CParticleCPUBuffer * m_cpuBuffer
Buffer for CPU rendering data.
Definition CGroup.h:162
float LifeMin
Minimum life time for newly born particles.
Definition CGroup.h:171
std::vector< CEmitter * > & getEmitters()
Gets all emitters.
Definition CGroup.h:280
virtual CObjectSerializable * createSerializable()
Creates a serializable object for property editing or saving.
const core::aabbox3df & getBBox()
Gets the bounding box of all active particles.
Definition CGroup.h:255
std::vector< CInterpolator * > m_interpolators
Interpolators shared by models.
Definition CGroup.h:146
void update()
Main update loop for particle physics and lifecycle.
CModel * getModel(EParticleParams param)
Finds a model for a parameter type.
int addParticle(u32 emiterID, const core::vector3df &position, const core::vector3df &subEmitterDirection)
Manually spawns a particle using an emitter index.
Definition CGroup.h:331
CParticleInstancing * m_instancing
Buffer for GPU instancing data.
Definition CGroup.h:160
std::vector< CEmitter * > m_emitters
List of emitters assigned to this group.
Definition CGroup.h:140
void updateForRenderer()
Updates buffers for rendering.
std::vector< IParticleCallback * > & getCallback()
Gets all callbacks.
Definition CGroup.h:305
float GravityValue
Gravity magnitude.
Definition CGroup.h:178
CModel * createModel(EParticleParams param)
Creates or retrieves a model for a specific parameter type.
virtual core::vector3df getTransformVector(const core::vector3df &vec)
Transforms a vector (rotation only) from local group space to world space.
void removeCallback(IParticleCallback *cb)
Removes a lifecycle callback.
Definition CGroup.h:297
void clearImmortalParticles()
Clears only immortal particles.
bool Optimized
If true, uses optimized particle swap on deletion (may affect sorting).
Definition CGroup.h:199
CEmitter * addEmitter(CEmitter *e)
Adds an emitter to the group.
Definition CGroup.h:273
void deleteInterpolator(CInterpolator *interpolator)
Deletes an interpolator.
core::vector3df Gravity
Calculated gravity vector.
Definition CGroup.h:185
std::vector< IParticleCallback * > m_callback
Callbacks for particle lifecycle events.
Definition CGroup.h:165
int addParticleVelocity(u32 emiterID, const core::vector3df &position, const core::vector3df &velocity)
Manually spawns a particle with initial velocity using an emitter index.
Definition CGroup.h:342
void removeEmitter(CEmitter *e)
Removes an emitter.
IRenderer * getRenderer()
Gets the current renderer.
Definition CGroup.h:362
void addSystem(ISystem *s)
Adds a custom system to process particles.
Definition CGroup.h:311
core::array< CParticle > & getParticles()
Gets raw access to particle list.
Definition CGroup.h:416
float LifeMax
Maximum life time for newly born particles.
Definition CGroup.h:173
virtual void loadSerializable(CObjectSerializable *object)
Loads properties from a serializable object.
core::vector3df GravityRotation
Gravity rotation (euler angles).
Definition CGroup.h:180
core::vector3df OrientationNormal
Custom orientation normal for fixed billboarding.
Definition CGroup.h:187
void setParticleRotation(const core::vector3df &euler)
Sets particle orientation using euler rotation.
void setGravityRotation(const core::vector3df &euler)
Sets gravity direction using euler rotation.
std::wstring Name
Friendly name of the group.
Definition CGroup.h:194
void updateFrame(int frame)
Syncs frame count.
Definition CGroup.h:428
CModel * createModel(const std::wstring &attributeName)
Creates or retrieves a model for a specific parameter name.
const core::matrix4 & getWorld()
Gets the world matrix of this group.
Definition CGroup.h:243
CParticleInstancingSystem * m_instancingSystem
Internal system for GPU instancing updates.
Definition CGroup.h:152
CParticleInstancing * getIntancing()
Internal: gets instancing buffer.
Definition CGroup.h:368
void setWorldMatrix(const core::matrix4 &m)
Sets the local-to-world transform of this group.
Definition CGroup.h:237
CInterpolator * createInterpolator()
Creates a new interpolator.
void deleteModel(EParticleParams param)
Deletes a model.
u32 getNumParticles()
Gets current particle count.
Definition CGroup.h:261
bool UseOrientationAsBillboard
Whether to use custom orientation instead of camera billboard.
Definition CGroup.h:191
core::array< SLaunchParticle > m_launch
Buffer of particles to be launched in the current frame.
Definition CGroup.h:137
virtual core::vector3df getTransformPosition(const core::vector3df &pos)
Transforms a position from local group space to world space.
void addCallback(IParticleCallback *cb)
Adds a lifecycle callback.
Definition CGroup.h:289
std::vector< ISystem * > & getSystems()
Gets all systems.
Definition CGroup.h:317
void setParentWorldMatrix(const core::matrix4 &m)
Sets the parent entity's world matrix.
Definition CGroup.h:231
int getFrameUpdate()
Gets last frame index update.
Definition CGroup.h:422
core::vector3df ParticleRotation
Global rotation for all particles.
Definition CGroup.h:182
CParticleSystem * m_particleSystem
Core particle movement and lifecycle system.
Definition CGroup.h:149
std::vector< CInterpolator * > & getInterpolators()
Gets all interpolators.
Definition CGroup.h:410
std::vector< ISystem * > m_systems
Custom update systems affecting this group's particles.
Definition CGroup.h:142
CParticleCPUBuffer * getParticleBuffer()
Internal: gets CPU mesh buffer.
Definition CGroup.h:374
CParticle * getParticlePointer()
Gets raw pointer to particle array.
Definition CGroup.h:267
Data model for animating a particle parameter (EParticleParams) over its lifetime.
Definition CModel.h:44
Internal class for managing particle mesh buffers on the CPU.
Definition CParticleCPUBuffer.h:37
Internal system that updates the CPU mesh buffer with current particle data for non-instanced renderi...
Definition CParticleCPUBufferSystem.h:39
Individual particle data structure.
Definition CParticle.h:65
Internal class for managing GPU instancing buffers for particles.
Definition CParticleInstancing.h:72
Internal system that updates the GPU instancing buffer with current particle data.
Definition CParticleInstancingSystem.h:39
Core particle update system.
Definition CParticleSystem.h:40
Interface for receiving particle lifecycle events.
Definition CGroup.h:75
Base interface for particle renderers.
Definition IRenderer.h:54
Base interface for particle systems that update particle data.
Definition ISystem.h:41
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
EParticleParams
IDs for particle parameters that can be animated over time.
Definition CParticle.h:39
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
aabbox3d< f32 > aabbox3df
Typedef for a f32 3d bounding box.
Definition aabbox3d.h:351
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
Information about particles being launched by an emitter.
Definition CGroup.h:56