Plasma Engine  2.0
Loading...
Searching...
No Matches
ParticleEffectInstance.h
1#pragma once
2
3#include <Foundation/Math/Transform.h>
4#include <Foundation/SimdMath/SimdVec4i.h>
5#include <Foundation/Threading/TaskSystem.h>
6#include <Foundation/Types/SharedPtr.h>
7#include <ParticlePlugin/System/ParticleSystemInstance.h>
8
10
12{
13public:
15
16 plTime m_UpdateDiff;
17
18private:
19 virtual void Execute() override;
20
21 plParticleEffectInstance* m_pEffect;
22};
23
24class PL_PARTICLEPLUGIN_DLL plParticleEffectInstance
25{
26 friend class plParticleWorldModule;
27 friend class plParticleEffectUpdateTask;
28
29public:
32
33 void Construct(plParticleEffectHandle hEffectHandle, const plParticleEffectResourceHandle& hResource, plWorld* pWorld, plParticleWorldModule* pOwnerModule, plUInt64 uiRandomSeed, bool bIsShared, plArrayPtr<plParticleEffectFloatParam> floatParams, plArrayPtr<plParticleEffectColorParam> colorParams);
34 void Destruct();
35
36 void Interrupt();
37
38 const plParticleEffectHandle& GetHandle() const { return m_hEffectHandle; }
39
40 void SetEmitterEnabled(bool bEnable);
41 bool GetEmitterEnabled() const { return m_bEmitterEnabled; }
42
43 bool HasActiveParticles() const;
44
45 void ClearParticleSystems();
46 void ClearEventReactions();
47
48 bool IsContinuous() const;
49
50 plWorld* GetWorld() const { return m_pWorld; }
51 plParticleWorldModule* GetOwnerWorldModule() const { return m_pOwnerModule; }
52
53 const plParticleEffectResourceHandle& GetResource() const { return m_hResource; }
54
55 const plHybridArray<plParticleSystemInstance*, 4>& GetParticleSystems() const { return m_ParticleSystems; }
56
57 void AddParticleEvent(const plParticleEvent& pe);
58
59 plRandom& GetRNG() { return m_Random; }
60
61 plUInt64 GetRandomSeed() const { return m_uiRandomSeed; }
62
63 void RequestWindSamples();
64 void UpdateWindSamples(plTime diff);
65
67 plUInt64 GetNumActiveParticles() const;
68
71public:
73 bool IsSimulatedInLocalSpace() const { return m_bSimulateInLocalSpace; }
74
76 void SetTransform(const plTransform& transform, const plVec3& vParticleStartVelocity);
77
80 void SetTransformForNextFrame(const plTransform& transform, const plVec3& vParticleStartVelocity);
81
83 const plTransform& GetTransform() const { return m_Transform; }
84
86 bool NeedsToApplyTransform() const { return m_bSimulateInLocalSpace || m_bIsSharedEffect; }
87
91 plSimdVec4f GetWindAt(const plSimdVec4f& vPosition) const;
92
93private:
94 void PassTransformToSystems();
95
96 plTransform m_Transform;
97 plTransform m_TransformForNextFrame;
98
99 plVec3 m_vVelocity;
100 plVec3 m_vVelocityForNextFrame;
101
102 struct WindSampleGrid
103 {
104 plSimdVec4f m_vMinPos;
105 plSimdVec4f m_vMaxPos;
106 plSimdVec4f m_vInvCellSize;
108 };
109
110 plVec4I32 m_vNumWindSamples; // not unsigned so it can be loaded directly to a SIMD register, w contains total num samples
111 mutable plUniquePtr<WindSampleGrid> m_WindSampleGrids[2];
112
116
117public:
119 bool Update(const plTime& diff);
120
125 plTime GetTotalEffectLifeTime() const { return m_TotalEffectLifeTime; }
126
127private: // friend plParticleWorldModule
129 bool ShouldBeUpdated() const;
130
132 const plSharedPtr<plTask>& GetUpdateTask() { return m_pTask; }
133
134private: // friend plParticleEffectUpdateTask
135 friend class plParticleEffectController;
137 void PreSimulate();
138
140 bool StepSimulation(const plTime& tDiff);
141
142private:
143 plTime m_TotalEffectLifeTime = plTime::MakeZero();
144 plTime m_ElapsedTimeSinceUpdate = plTime::MakeZero();
145
146
150public:
152 bool IsSharedEffect() const { return m_bIsSharedEffect; }
153
154private: // friend plParticleWorldModule
155 void AddSharedInstance(const void* pSharedInstanceOwner);
156 void RemoveSharedInstance(const void* pSharedInstanceOwner);
157
158private:
159 bool m_bIsSharedEffect = false;
160
164public:
167 void SetIsVisible() const;
168
169 void SetVisibleIf(plParticleEffectInstance* pOtherVisible);
170
172 bool IsVisible() const;
173
176 void GetBoundingVolume(plBoundingBoxSphere& ref_volume) const;
177
178private:
179 void CombineSystemBoundingVolumes();
180
181 plBoundingBoxSphere m_BoundingVolume;
182 mutable plTime m_EffectIsVisible;
183 plParticleEffectInstance* m_pVisibleIf = nullptr;
184 plEnum<plEffectInvisibleUpdateRate> m_InvisibleUpdateRate;
185 plUInt64 m_uiRandomSeed = 0;
186
190public:
191 void SetParameter(const plTempHashedString& sName, float value);
192 void SetParameter(const plTempHashedString& sName, const plColor& value);
193
194 plInt32 FindFloatParameter(const plTempHashedString& sName) const;
195 float GetFloatParameter(const plTempHashedString& sName, float fDefaultValue) const;
196 float GetFloatParameter(plUInt32 uiIdx) const { return m_FloatParameters[uiIdx].m_fValue; }
197
198 plInt32 FindColorParameter(const plTempHashedString& sName) const;
199 const plColor& GetColorParameter(const plTempHashedString& sName, const plColor& defaultValue) const;
200 const plColor& GetColorParameter(plUInt32 uiIdx) const { return m_ColorParameters[uiIdx].m_Value; }
201
202
203private:
204 struct FloatParameter
205 {
206 PL_DECLARE_POD_TYPE();
207 plUInt64 m_uiNameHash;
208 float m_fValue;
209 };
210
211 struct ColorParameter
212 {
213 PL_DECLARE_POD_TYPE();
214 plUInt64 m_uiNameHash;
215 plColor m_Value;
216 };
217
218 plHybridArray<FloatParameter, 2> m_FloatParameters;
219 plHybridArray<ColorParameter, 2> m_ColorParameters;
220
222
223
224private:
225 void Reconfigure(bool bFirstTime, plArrayPtr<plParticleEffectFloatParam> floatParams, plArrayPtr<plParticleEffectColorParam> colorParams);
226 void ClearParticleSystem(plUInt32 index);
227 void ProcessEventQueues();
228
229 // for deterministic randomness
230 plRandom m_Random;
231
232 plHashSet<const void*> m_SharedInstances;
233 plParticleEffectHandle m_hEffectHandle;
234 bool m_bEmitterEnabled = true;
235 bool m_bSimulateInLocalSpace = false;
236 bool m_bIsFinishing = false;
237 plUInt8 m_uiReviveTimeout = 3;
238 plInt8 m_iMinSimStepsToDo = 0;
239 float m_fApplyInstanceVelocity = 0;
240 plTime m_PreSimulateDuration;
242
243 plParticleWorldModule* m_pOwnerModule = nullptr;
244 plWorld* m_pWorld = nullptr;
247
248 plSharedPtr<plTask> m_pTask;
249
251};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
Definition HashSet.h:191
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition ParticleEffectController.h:7
A handle to a particle effect.
Definition Declarations.h:39
Definition ParticleEffectInstance.h:25
bool IsSharedEffect() const
Returns true, if this effect is configured to be simulated once per frame, but rendered by multiple i...
Definition ParticleEffectInstance.h:152
bool IsSimulatedInLocalSpace() const
Whether the effect is simulated around the origin and thus not affected by instance position and rota...
Definition ParticleEffectInstance.h:73
plTime GetTotalEffectLifeTime() const
Returns the total (game) time that the effect is alive and has been updated.
Definition ParticleEffectInstance.h:125
bool NeedsToApplyTransform() const
For the renderer to know whether the instance transform has to be applied to each particle position.
Definition ParticleEffectInstance.h:86
const plTransform & GetTransform() const
Returns the transform of the main or shared instance.
Definition ParticleEffectInstance.h:83
Definition ParticleEffectInstance.h:12
virtual void Execute() override
Override this to implement the task's supposed functionality.
Definition ParticleEffectInstance.cpp:851
This world module stores all particle effect data that is active in a given plWorld instance.
Definition ParticleWorldModule.h:24
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
A 4-component SIMD vector class.
Definition SimdVec4f.h:8
Definition SmallArray.h:219
Wraps a C-style array, which has a fixed size at compile-time, with a more convenient interface.
Definition StaticArray.h:13
Base class for custom tasks.
Definition Task.h:10
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition ParticleEvent.h:9
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42