1/**************************************************************************/
2/* cpu_particles_2d.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef CPU_PARTICLES_2D_H
32#define CPU_PARTICLES_2D_H
33
34#include "scene/2d/node_2d.h"
35
36class CPUParticles2D : public Node2D {
37private:
38 GDCLASS(CPUParticles2D, Node2D);
39
40public:
41 enum DrawOrder {
42 DRAW_ORDER_INDEX,
43 DRAW_ORDER_LIFETIME,
44 };
45
46 enum Parameter {
47 PARAM_INITIAL_LINEAR_VELOCITY,
48 PARAM_ANGULAR_VELOCITY,
49 PARAM_ORBIT_VELOCITY,
50 PARAM_LINEAR_ACCEL,
51 PARAM_RADIAL_ACCEL,
52 PARAM_TANGENTIAL_ACCEL,
53 PARAM_DAMPING,
54 PARAM_ANGLE,
55 PARAM_SCALE,
56 PARAM_HUE_VARIATION,
57 PARAM_ANIM_SPEED,
58 PARAM_ANIM_OFFSET,
59 PARAM_MAX
60 };
61
62 enum ParticleFlags {
63 PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
64 PARTICLE_FLAG_ROTATE_Y, // Unused, but exposed for consistency with 3D.
65 PARTICLE_FLAG_DISABLE_Z, // Unused, but exposed for consistency with 3D.
66 PARTICLE_FLAG_MAX
67 };
68
69 enum EmissionShape {
70 EMISSION_SHAPE_POINT,
71 EMISSION_SHAPE_SPHERE,
72 EMISSION_SHAPE_SPHERE_SURFACE,
73 EMISSION_SHAPE_RECTANGLE,
74 EMISSION_SHAPE_POINTS,
75 EMISSION_SHAPE_DIRECTED_POINTS,
76 EMISSION_SHAPE_MAX
77 };
78
79private:
80 bool emitting = false;
81 bool active = false;
82
83 struct Particle {
84 Transform2D transform;
85 Color color;
86 real_t custom[4] = {};
87 real_t rotation = 0.0;
88 Vector2 velocity;
89 bool active = false;
90 real_t angle_rand = 0.0;
91 real_t scale_rand = 0.0;
92 real_t hue_rot_rand = 0.0;
93 real_t anim_offset_rand = 0.0;
94 Color start_color_rand;
95 double time = 0.0;
96 double lifetime = 0.0;
97 Color base_color;
98
99 uint32_t seed = 0;
100 };
101
102 double time = 0.0;
103 double frame_remainder = 0.0;
104 int cycle = 0;
105 bool do_redraw = false;
106
107 RID mesh;
108 RID multimesh;
109
110 Vector<Particle> particles;
111 Vector<float> particle_data;
112 Vector<int> particle_order;
113
114 struct SortLifetime {
115 const Particle *particles = nullptr;
116
117 bool operator()(int p_a, int p_b) const {
118 return particles[p_a].time > particles[p_b].time;
119 }
120 };
121
122 struct SortAxis {
123 const Particle *particles = nullptr;
124 Vector2 axis;
125 bool operator()(int p_a, int p_b) const {
126 return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
127 }
128 };
129
130 //
131
132 bool one_shot = false;
133
134 double lifetime = 1.0;
135 double pre_process_time = 0.0;
136 real_t explosiveness_ratio = 0.0;
137 real_t randomness_ratio = 0.0;
138 double lifetime_randomness = 0.0;
139 double speed_scale = 1.0;
140 bool local_coords = false;
141 int fixed_fps = 0;
142 bool fractional_delta = true;
143
144 Transform2D inv_emission_transform;
145
146 DrawOrder draw_order = DRAW_ORDER_INDEX;
147
148 Ref<Texture2D> texture;
149
150 ////////
151
152 Vector2 direction = Vector2(1, 0);
153 real_t spread = 45.0;
154
155 real_t parameters_min[PARAM_MAX] = {};
156 real_t parameters_max[PARAM_MAX] = {};
157
158 Ref<Curve> curve_parameters[PARAM_MAX];
159 Color color;
160 Ref<Gradient> color_ramp;
161 Ref<Gradient> color_initial_ramp;
162
163 bool particle_flags[PARTICLE_FLAG_MAX];
164
165 EmissionShape emission_shape = EMISSION_SHAPE_POINT;
166 real_t emission_sphere_radius = 1.0;
167 Vector2 emission_rect_extents = Vector2(1, 1);
168 Vector<Vector2> emission_points;
169 Vector<Vector2> emission_normals;
170 Vector<Color> emission_colors;
171 int emission_point_count = 0;
172
173 Ref<Curve> scale_curve_x;
174 Ref<Curve> scale_curve_y;
175 bool split_scale = false;
176
177 Vector2 gravity = Vector2(0, 980);
178
179 void _update_internal();
180 void _particles_process(double p_delta);
181 void _update_particle_data_buffer();
182
183 Mutex update_mutex;
184
185 void _update_render_thread();
186
187 void _update_mesh_texture();
188
189 void _set_do_redraw(bool p_do_redraw);
190
191 void _texture_changed();
192
193protected:
194 static void _bind_methods();
195 void _notification(int p_what);
196 void _validate_property(PropertyInfo &p_property) const;
197
198public:
199 void set_emitting(bool p_emitting);
200 void set_amount(int p_amount);
201 void set_lifetime(double p_lifetime);
202 void set_one_shot(bool p_one_shot);
203 void set_pre_process_time(double p_time);
204 void set_explosiveness_ratio(real_t p_ratio);
205 void set_randomness_ratio(real_t p_ratio);
206 void set_lifetime_randomness(double p_random);
207 void set_use_local_coordinates(bool p_enable);
208 void set_speed_scale(double p_scale);
209
210 bool is_emitting() const;
211 int get_amount() const;
212 double get_lifetime() const;
213 bool get_one_shot() const;
214 double get_pre_process_time() const;
215 real_t get_explosiveness_ratio() const;
216 real_t get_randomness_ratio() const;
217 double get_lifetime_randomness() const;
218 bool get_use_local_coordinates() const;
219 double get_speed_scale() const;
220
221 void set_fixed_fps(int p_count);
222 int get_fixed_fps() const;
223
224 void set_fractional_delta(bool p_enable);
225 bool get_fractional_delta() const;
226
227 void set_draw_order(DrawOrder p_order);
228 DrawOrder get_draw_order() const;
229
230 void set_texture(const Ref<Texture2D> &p_texture);
231 Ref<Texture2D> get_texture() const;
232
233 ///////////////////
234
235 void set_direction(Vector2 p_direction);
236 Vector2 get_direction() const;
237
238 void set_spread(real_t p_spread);
239 real_t get_spread() const;
240
241 void set_param_min(Parameter p_param, real_t p_value);
242 real_t get_param_min(Parameter p_param) const;
243
244 void set_param_max(Parameter p_param, real_t p_value);
245 real_t get_param_max(Parameter p_param) const;
246
247 void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
248 Ref<Curve> get_param_curve(Parameter p_param) const;
249
250 void set_color(const Color &p_color);
251 Color get_color() const;
252
253 void set_color_ramp(const Ref<Gradient> &p_ramp);
254 Ref<Gradient> get_color_ramp() const;
255
256 void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
257 Ref<Gradient> get_color_initial_ramp() const;
258
259 void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
260 bool get_particle_flag(ParticleFlags p_particle_flag) const;
261
262 void set_emission_shape(EmissionShape p_shape);
263 void set_emission_sphere_radius(real_t p_radius);
264 void set_emission_rect_extents(Vector2 p_extents);
265 void set_emission_points(const Vector<Vector2> &p_points);
266 void set_emission_normals(const Vector<Vector2> &p_normals);
267 void set_emission_colors(const Vector<Color> &p_colors);
268 void set_scale_curve_x(Ref<Curve> p_scale_curve);
269 void set_scale_curve_y(Ref<Curve> p_scale_curve);
270 void set_split_scale(bool p_split_scale);
271
272 EmissionShape get_emission_shape() const;
273 real_t get_emission_sphere_radius() const;
274 Vector2 get_emission_rect_extents() const;
275 Vector<Vector2> get_emission_points() const;
276 Vector<Vector2> get_emission_normals() const;
277 Vector<Color> get_emission_colors() const;
278 Ref<Curve> get_scale_curve_x() const;
279 Ref<Curve> get_scale_curve_y() const;
280 bool get_split_scale();
281
282 void set_gravity(const Vector2 &p_gravity);
283 Vector2 get_gravity() const;
284
285 PackedStringArray get_configuration_warnings() const override;
286
287 void restart();
288
289 void convert_from_particles(Node *p_particles);
290
291 CPUParticles2D();
292 ~CPUParticles2D();
293};
294
295VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
296VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
297VARIANT_ENUM_CAST(CPUParticles2D::ParticleFlags)
298VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)
299
300#endif // CPU_PARTICLES_2D_H
301