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