-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentParticleSystem.h
More file actions
177 lines (140 loc) · 3.5 KB
/
ComponentParticleSystem.h
File metadata and controls
177 lines (140 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef __COMPONENTPARTICLESYSTEM_H__
#define __COMPONENTPARTICLESYSTEM_H__
#include "Component.h"
#include <vector>
#include <stack>
#include "MathGeoLib\include\MathGeoLib.h"
class ResourceFileTexture;
struct Mesh;
class ComponentCamera;
struct Particle
{
math::float3 position;
math::float3 speed;
math::float3 origin;
math::float3 dst_origin;
float life = -1.0f;
float cam_distance = -1.0f;
int next_c_id = 0;
math::float4 color;
bool operator<(Particle& b);
};
struct ColorTimeItem
{
float alpha;
float position; //In %
math::float3 color;
ColorTimeItem(float alpha, float position, const math::float3& color);
};
enum ParticleShapeType
{
SHAPE_BOX,
SHAPE_SPHERE
};
enum PSState
{
PS_PLAYING,
PS_PAUSE,
PS_STOP
};
struct Burst
{
float time = 0.0f;
int min_particles = 10;
int max_particles = 10;
bool completed = false;
};
class ComponentParticleSystem : public Component
{
public:
ComponentParticleSystem(ComponentType type, GameObject* game_object);
~ComponentParticleSystem();
void OnInspector(bool debug);
void Save(Data& file) const;
void Load(Data& conf);
void Update();
void PostUpdate();
void OnTransformModified();
void OnPlay();
void OnPause();
void OnStop();
unsigned int GetTextureId()const;
void SortParticles(ComponentCamera* cam);
void StopAll(); //Stops the particle system and removes the alive particles
void Play();
void Pause();
bool operator<(ComponentParticleSystem& b);
private:
void InspectorDelete();
void InspectorChangeTexture();
void InspectorSimulation(); //Play/Stop simulation
void InspectorColorOverTime();
void InspectorTextureAnimation();
void InspectorShape();
void InspectorBoundingBox();
void InspectorEmission();
void InspectorSimulationSpace();
void SpawnParticle(int delay);
int FindUnusedParticle();
private:
int cycles = 0; //Life cycles of the system
//Properties
int max_particles = 1000;
float speed = 1.0f;
bool play_on_awake = true;
float duration = 5.0f;
bool looping = true;
bool simulation_space_local = true; //False -> simulation space world
//Color over time
public:
bool color_over_time_active = false;
private:
std::vector<ColorTimeItem*> color_time;
ColorTimeItem cti_entry;
//Shape
ParticleShapeType shape_type = SHAPE_BOX;
math::float3 box_shape = math::float3(1);
math::OBB box_shape_obb;
math::Sphere sphere_shape;
bool sphere_emit_from_shell = false;
//Animated texture
public:
bool texture_anim = false;
math::float3 tex_anim_data; //x-rows y-columns z-cycles
private:
ResourceFileTexture* texture = nullptr;
//Emission
float emission_rate = 10.0f;
float spawn_time = 0.1f; // 1 / emission rate
float spawn_timer = 0.0;
std::vector<Burst> bursts;
const int top_max_particles = 1000; //Maximum number of particles (not editable for the user)
std::vector<Particle> particles_container;
//Simulation in editor
float system_life = 0.0f;
PSState state;
math::LCG rnd;
//Bounding box
public:
math::AABB bounding_box;
private:
math::float3 bb_size;
math::float3 bb_pos_offset;
public:
std::vector<math::float3> alive_particles_position;
std::vector<math::float4> alive_particles_color;
std::vector<float> alive_particles_life;
//Buffers
unsigned int position_buffer = 0;
unsigned int color_buffer = 0;
unsigned int life_buffer = 0;
int num_alive_particles = 0; //Number of particles alive
//Properites
float size = 1.0f;
math::float2 img_size;
math::float3 color;
float life_time = 5.0f;
//To sort the entire system
float cam_distance = 0.0f;
};
#endif // !__COMPONENTPARTICLESYTEM_H__