Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/cpu_particles_2d.h
9896 views
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
#pragma once
32
33
#include "scene/2d/node_2d.h"
34
35
class RandomNumberGenerator;
36
37
class CPUParticles2D : public Node2D {
38
private:
39
GDCLASS(CPUParticles2D, Node2D);
40
41
public:
42
enum DrawOrder {
43
DRAW_ORDER_INDEX,
44
DRAW_ORDER_LIFETIME,
45
};
46
47
enum Parameter {
48
PARAM_INITIAL_LINEAR_VELOCITY,
49
PARAM_ANGULAR_VELOCITY,
50
PARAM_ORBIT_VELOCITY,
51
PARAM_LINEAR_ACCEL,
52
PARAM_RADIAL_ACCEL,
53
PARAM_TANGENTIAL_ACCEL,
54
PARAM_DAMPING,
55
PARAM_ANGLE,
56
PARAM_SCALE,
57
PARAM_HUE_VARIATION,
58
PARAM_ANIM_SPEED,
59
PARAM_ANIM_OFFSET,
60
PARAM_MAX
61
};
62
63
enum ParticleFlags {
64
PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
65
PARTICLE_FLAG_ROTATE_Y, // Unused, but exposed for consistency with 3D.
66
PARTICLE_FLAG_DISABLE_Z, // Unused, but exposed for consistency with 3D.
67
PARTICLE_FLAG_MAX
68
};
69
70
enum EmissionShape {
71
EMISSION_SHAPE_POINT,
72
EMISSION_SHAPE_SPHERE,
73
EMISSION_SHAPE_SPHERE_SURFACE,
74
EMISSION_SHAPE_RECTANGLE,
75
EMISSION_SHAPE_POINTS,
76
EMISSION_SHAPE_DIRECTED_POINTS,
77
EMISSION_SHAPE_MAX
78
};
79
80
private:
81
bool emitting = false;
82
bool active = false;
83
84
struct Particle {
85
Transform2D transform;
86
Color color;
87
real_t custom[4] = {};
88
real_t rotation = 0.0;
89
Vector2 velocity;
90
bool active = false;
91
real_t angle_rand = 0.0;
92
real_t scale_rand = 0.0;
93
real_t hue_rot_rand = 0.0;
94
real_t anim_offset_rand = 0.0;
95
Color start_color_rand;
96
double time = 0.0;
97
double lifetime = 0.0;
98
Color base_color;
99
100
uint32_t seed = 0;
101
};
102
103
double time = 0.0;
104
double frame_remainder = 0.0;
105
int cycle = 0;
106
bool do_redraw = false;
107
108
RID mesh;
109
RID multimesh;
110
111
Vector<Particle> particles;
112
Vector<float> particle_data;
113
Vector<int> particle_order;
114
115
struct SortLifetime {
116
const Particle *particles = nullptr;
117
118
bool operator()(int p_a, int p_b) const {
119
return particles[p_a].time > particles[p_b].time;
120
}
121
};
122
123
struct SortAxis {
124
const Particle *particles = nullptr;
125
Vector2 axis;
126
bool operator()(int p_a, int p_b) const {
127
return axis.dot(particles[p_a].transform[2]) < axis.dot(particles[p_b].transform[2]);
128
}
129
};
130
131
//
132
133
bool one_shot = false;
134
135
double lifetime = 1.0;
136
double pre_process_time = 0.0;
137
double _requested_process_time = 0.0;
138
real_t explosiveness_ratio = 0.0;
139
real_t randomness_ratio = 0.0;
140
double lifetime_randomness = 0.0;
141
double speed_scale = 1.0;
142
bool local_coords = false;
143
int fixed_fps = 0;
144
bool fractional_delta = true;
145
uint32_t seed = 0;
146
bool use_fixed_seed = false;
147
148
Transform2D inv_emission_transform;
149
150
#ifdef TOOLS_ENABLED
151
bool show_gizmos = false;
152
#endif
153
154
DrawOrder draw_order = DRAW_ORDER_INDEX;
155
156
Ref<Texture2D> texture;
157
158
////////
159
160
Vector2 direction = Vector2(1, 0);
161
real_t spread = 45.0;
162
163
real_t parameters_min[PARAM_MAX] = {};
164
real_t parameters_max[PARAM_MAX] = {};
165
166
Ref<Curve> curve_parameters[PARAM_MAX];
167
Color color;
168
Ref<Gradient> color_ramp;
169
Ref<Gradient> color_initial_ramp;
170
171
bool particle_flags[PARTICLE_FLAG_MAX];
172
173
EmissionShape emission_shape = EMISSION_SHAPE_POINT;
174
real_t emission_sphere_radius = 1.0;
175
Vector2 emission_rect_extents = Vector2(1, 1);
176
Vector<Vector2> emission_points;
177
Vector<Vector2> emission_normals;
178
Vector<Color> emission_colors;
179
int emission_point_count = 0;
180
181
Ref<Curve> scale_curve_x;
182
Ref<Curve> scale_curve_y;
183
bool split_scale = false;
184
185
Vector2 gravity = Vector2(0, 980);
186
187
Ref<RandomNumberGenerator> rng;
188
189
void _update_internal();
190
void _particles_process(double p_delta);
191
void _update_particle_data_buffer();
192
void _set_emitting();
193
194
Mutex update_mutex;
195
196
struct InterpolationData {
197
// Whether this particle is non-interpolated, but following an interpolated parent.
198
bool interpolated_follow = false;
199
200
// If doing interpolated follow, we need to keep these updated per tick.
201
Transform2D global_xform_curr;
202
Transform2D global_xform_prev;
203
} _interpolation_data;
204
205
void _update_render_thread();
206
207
void _update_mesh_texture();
208
209
void _set_do_redraw(bool p_do_redraw);
210
211
void _texture_changed();
212
213
void _refresh_interpolation_state();
214
215
protected:
216
static void _bind_methods();
217
void _notification(int p_what);
218
#ifdef TOOLS_ENABLED
219
void _draw_emission_gizmo();
220
#endif
221
void _validate_property(PropertyInfo &p_property) const;
222
223
#ifndef DISABLE_DEPRECATED
224
void _restart_bind_compat_92089();
225
static void _bind_compatibility_methods();
226
#endif
227
228
public:
229
void set_emitting(bool p_emitting);
230
void set_amount(int p_amount);
231
void set_lifetime(double p_lifetime);
232
void set_one_shot(bool p_one_shot);
233
void set_pre_process_time(double p_time);
234
void set_explosiveness_ratio(real_t p_ratio);
235
void set_randomness_ratio(real_t p_ratio);
236
void set_lifetime_randomness(double p_random);
237
void set_use_local_coordinates(bool p_enable);
238
void set_speed_scale(double p_scale);
239
240
bool is_emitting() const;
241
int get_amount() const;
242
double get_lifetime() const;
243
bool get_one_shot() const;
244
double get_pre_process_time() const;
245
real_t get_explosiveness_ratio() const;
246
real_t get_randomness_ratio() const;
247
double get_lifetime_randomness() const;
248
bool get_use_local_coordinates() const;
249
double get_speed_scale() const;
250
251
void set_fixed_fps(int p_count);
252
int get_fixed_fps() const;
253
254
void set_fractional_delta(bool p_enable);
255
bool get_fractional_delta() const;
256
257
void set_draw_order(DrawOrder p_order);
258
DrawOrder get_draw_order() const;
259
260
void set_texture(const Ref<Texture2D> &p_texture);
261
Ref<Texture2D> get_texture() const;
262
263
void set_use_fixed_seed(bool p_use_fixed_seed);
264
bool get_use_fixed_seed() const;
265
266
void set_seed(uint32_t p_seed);
267
#ifdef TOOLS_ENABLED
268
void set_show_gizmos(bool p_show_gizmos);
269
#endif
270
uint32_t get_seed() const;
271
272
void request_particles_process(real_t p_requested_process_time);
273
274
///////////////////
275
276
void set_direction(Vector2 p_direction);
277
Vector2 get_direction() const;
278
279
void set_spread(real_t p_spread);
280
real_t get_spread() const;
281
282
void set_param_min(Parameter p_param, real_t p_value);
283
real_t get_param_min(Parameter p_param) const;
284
285
void set_param_max(Parameter p_param, real_t p_value);
286
real_t get_param_max(Parameter p_param) const;
287
288
void set_param_curve(Parameter p_param, const Ref<Curve> &p_curve);
289
Ref<Curve> get_param_curve(Parameter p_param) const;
290
291
void set_color(const Color &p_color);
292
Color get_color() const;
293
294
void set_color_ramp(const Ref<Gradient> &p_ramp);
295
Ref<Gradient> get_color_ramp() const;
296
297
void set_color_initial_ramp(const Ref<Gradient> &p_ramp);
298
Ref<Gradient> get_color_initial_ramp() const;
299
300
void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
301
bool get_particle_flag(ParticleFlags p_particle_flag) const;
302
303
void set_emission_shape(EmissionShape p_shape);
304
void set_emission_sphere_radius(real_t p_radius);
305
void set_emission_rect_extents(Vector2 p_extents);
306
void set_emission_points(const Vector<Vector2> &p_points);
307
void set_emission_normals(const Vector<Vector2> &p_normals);
308
void set_emission_colors(const Vector<Color> &p_colors);
309
void set_scale_curve_x(Ref<Curve> p_scale_curve);
310
void set_scale_curve_y(Ref<Curve> p_scale_curve);
311
void set_split_scale(bool p_split_scale);
312
313
EmissionShape get_emission_shape() const;
314
real_t get_emission_sphere_radius() const;
315
Vector2 get_emission_rect_extents() const;
316
Vector<Vector2> get_emission_points() const;
317
Vector<Vector2> get_emission_normals() const;
318
Vector<Color> get_emission_colors() const;
319
Ref<Curve> get_scale_curve_x() const;
320
Ref<Curve> get_scale_curve_y() const;
321
bool get_split_scale();
322
323
void set_gravity(const Vector2 &p_gravity);
324
Vector2 get_gravity() const;
325
326
PackedStringArray get_configuration_warnings() const override;
327
328
void restart(bool p_keep_seed = false);
329
330
void convert_from_particles(Node *p_particles);
331
332
CPUParticles2D();
333
~CPUParticles2D();
334
};
335
336
VARIANT_ENUM_CAST(CPUParticles2D::DrawOrder)
337
VARIANT_ENUM_CAST(CPUParticles2D::Parameter)
338
VARIANT_ENUM_CAST(CPUParticles2D::ParticleFlags)
339
VARIANT_ENUM_CAST(CPUParticles2D::EmissionShape)
340
341