Debian Codegraph @

scene/2d/cpu_particles_2d.h

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