Debian Codegraph @

scene/2d/cpu_particles_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* cpu_particles_2d.cpp */
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 #include "cpu_particles_2d.h"
32 #include "cpu_particles_2d.compat.inc"
33
34 #include "core/math/random_number_generator.h"
35 #include "core/math/transform_interpolator.h"
36 #include "scene/2d/gpu_particles_2d.h"
37 #include "scene/resources/atlas_texture.h"
38 #include "scene/resources/canvas_item_material.h"
39 #include "scene/resources/curve_texture.h"
40 #include "scene/resources/gradient_texture.h"
41 #include "scene/resources/particle_process_material.h"
42
43 void CPUParticles2D::set_emitting(bool p_emitting) {
44 if (emitting == p_emitting) {
45 return;
46 }
47
48 if (p_emitting && !use_fixed_seed && one_shot) {
49 set_seed(Math::rand());
50 }
51
52 emitting = p_emitting;
53 if (emitting) {
54 _set_emitting();
55 }
56 }
57
58 void CPUParticles2D::_set_emitting() {
59 active = true;
60 set_process_internal(true);
61 // first update before rendering to avoid one frame delay after emitting starts
62 if (time == 0) {
63 _update_internal();
64 }
65 }
66
67 void CPUParticles2D::set_amount(int p_amount) {
68 ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
69
70 particles.resize(p_amount);
71 {
72 Particle *w = particles.ptrw();
73
74 for (int i = 0; i < p_amount; i++) {
75 w[i].active = false;
76 }
77 }
78
79 particle_data.resize((8 + 4 + 4) * p_amount);
80 RS::get_singleton()->multimesh_allocate_data(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_2D, true, true);
81
82 particle_order.resize(p_amount);
83 }
84
85 void CPUParticles2D::set_lifetime(double p_lifetime) {
86 ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
87 lifetime = p_lifetime;
88 }
89
90 void CPUParticles2D::set_one_shot(bool p_one_shot) {
91 one_shot = p_one_shot;
92 }
93
94 void CPUParticles2D::set_pre_process_time(double p_time) {
95 pre_process_time = p_time;
96 }
97
98 void CPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
99 explosiveness_ratio = p_ratio;
100 }
101
102 void CPUParticles2D::set_randomness_ratio(real_t p_ratio) {
103 randomness_ratio = p_ratio;
104 }
105
106 void CPUParticles2D::set_lifetime_randomness(double p_random) {
107 lifetime_randomness = p_random;
108 }
109
110 void CPUParticles2D::set_use_local_coordinates(bool p_enable) {
111 local_coords = p_enable;
112
113 // Prevent sending item transforms when using global coords,
114 // and inform the RenderingServer to use identity mode.
115 set_canvas_item_use_identity_transform(!local_coords);
116
117 // We only need NOTIFICATION_TRANSFORM_CHANGED
118 // when following an interpolated target.
119
120 #ifdef TOOLS_ENABLED
121 set_notify_transform(_interpolation_data.interpolated_follow || (Engine::get_singleton()->is_editor_hint() && !local_coords));
122 #else
123 set_notify_transform(_interpolation_data.interpolated_follow);
124 #endif
125
126 queue_redraw();
127 }
128
129 void CPUParticles2D::set_speed_scale(double p_scale) {
130 speed_scale = p_scale;
131 }
132
133 bool CPUParticles2D::is_emitting() const {
134 return emitting;
135 }
136
137 int CPUParticles2D::get_amount() const {
138 return particles.size();
139 }
140
141 double CPUParticles2D::get_lifetime() const {
142 return lifetime;
143 }
144
145 bool CPUParticles2D::get_one_shot() const {
146 return one_shot;
147 }
148
149 double CPUParticles2D::get_pre_process_time() const {
150 return pre_process_time;
151 }
152
153 real_t CPUParticles2D::get_explosiveness_ratio() const {
154 return explosiveness_ratio;
155 }
156
157 real_t CPUParticles2D::get_randomness_ratio() const {
158 return randomness_ratio;
159 }
160
161 double CPUParticles2D::get_lifetime_randomness() const {
162 return lifetime_randomness;
163 }
164
165 bool CPUParticles2D::get_use_local_coordinates() const {
166 return local_coords;
167 }
168
169 double CPUParticles2D::get_speed_scale() const {
170 return speed_scale;
171 }
172
173 void CPUParticles2D::set_draw_order(DrawOrder p_order) {
174 draw_order = p_order;
175 }
176
177 CPUParticles2D::DrawOrder CPUParticles2D::get_draw_order() const {
178 return draw_order;
179 }
180
181 void CPUParticles2D::_update_mesh_texture() {
182 Size2 tex_size;
183 if (texture.is_valid()) {
184 tex_size = texture->get_size();
185 } else {
186 tex_size = Size2(1, 1);
187 }
188
189 Vector<Vector2> vertices = {
190 -tex_size * 0.5,
191 -tex_size * 0.5 + Vector2(tex_size.x, 0),
192 -tex_size * 0.5 + tex_size,
193 -tex_size * 0.5 + Vector2(0, tex_size.y)
194 };
195
196 Vector<Vector2> uvs;
197 AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
198 if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
199 Rect2 region_rect = atlas_texture->get_region();
200 Size2 atlas_size = atlas_texture->get_atlas()->get_size();
201 uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
202 uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
203 uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
204 uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
205 } else {
206 uvs.push_back(Vector2(0, 0));
207 uvs.push_back(Vector2(1, 0));
208 uvs.push_back(Vector2(1, 1));
209 uvs.push_back(Vector2(0, 1));
210 }
211
212 Vector<Color> colors = {
213 Color(1, 1, 1, 1),
214 Color(1, 1, 1, 1),
215 Color(1, 1, 1, 1),
216 Color(1, 1, 1, 1)
217 };
218
219 Vector<int> indices = { 0, 1, 2, 2, 3, 0 };
220
221 Array arr;
222 arr.resize(RS::ARRAY_MAX);
223 arr[RS::ARRAY_VERTEX] = vertices;
224 arr[RS::ARRAY_TEX_UV] = uvs;
225 arr[RS::ARRAY_COLOR] = colors;
226 arr[RS::ARRAY_INDEX] = indices;
227
228 RS::get_singleton()->mesh_clear(mesh);
229 RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr);
230 }
231
232 void CPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
233 if (p_texture == texture) {
234 return;
235 }
236
237 if (texture.is_valid()) {
238 texture->disconnect_changed(callable_mp(this, &CPUParticles2D::_texture_changed));
239 }
240
241 texture = p_texture;
242
243 if (texture.is_valid()) {
244 texture->connect_changed(callable_mp(this, &CPUParticles2D::_texture_changed));
245 }
246
247 queue_redraw();
248 _update_mesh_texture();
249 }
250
251 void CPUParticles2D::_texture_changed() {
252 if (texture.is_valid()) {
253 queue_redraw();
254 _update_mesh_texture();
255 }
256 }
257
258 void CPUParticles2D::_refresh_interpolation_state() {
259 if (!is_inside_tree()) {
260 return;
261 }
262
263 // The logic for whether to do an interpolated follow.
264 // This is rather complex, but basically:
265 // If project setting interpolation is ON and this particle system is in global mode,
266 // we will follow the INTERPOLATED position rather than the actual position.
267 // This is so that particles aren't generated AHEAD of the interpolated parent.
268 bool follow = !local_coords && get_tree()->is_physics_interpolation_enabled();
269
270 if (follow == _interpolation_data.interpolated_follow) {
271 return;
272 }
273
274 _interpolation_data.interpolated_follow = follow;
275
276 set_physics_process_internal(_interpolation_data.interpolated_follow);
277 }
278
279 Ref<Texture2D> CPUParticles2D::get_texture() const {
280 return texture;
281 }
282
283 void CPUParticles2D::set_fixed_fps(int p_count) {
284 fixed_fps = p_count;
285 }
286
287 int CPUParticles2D::get_fixed_fps() const {
288 return fixed_fps;
289 }
290
291 void CPUParticles2D::set_fractional_delta(bool p_enable) {
292 fractional_delta = p_enable;
293 }
294
295 bool CPUParticles2D::get_fractional_delta() const {
296 return fractional_delta;
297 }
298
299 PackedStringArray CPUParticles2D::get_configuration_warnings() const {
300 PackedStringArray warnings = Node2D::get_configuration_warnings();
301
302 CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
303
304 if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
305 if (get_param_max(PARAM_ANIM_SPEED) != 0.0 || get_param_max(PARAM_ANIM_OFFSET) != 0.0 ||
306 get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid()) {
307 warnings.push_back(RTR("CPUParticles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
308 }
309 }
310
311 return warnings;
312 }
313
314 void CPUParticles2D::restart(bool p_keep_seed) {
315 time = 0;
316 frame_remainder = 0;
317 cycle = 0;
318 emitting = false;
319
320 {
321 int pc = particles.size();
322 Particle *w = particles.ptrw();
323
324 for (int i = 0; i < pc; i++) {
325 w[i].active = false;
326 }
327 }
328 if (!p_keep_seed && !use_fixed_seed) {
329 seed = Math::rand();
330 }
331
332 emitting = true;
333 _set_emitting();
334 }
335
336 void CPUParticles2D::set_direction(Vector2 p_direction) {
337 direction = p_direction;
338 }
339
340 Vector2 CPUParticles2D::get_direction() const {
341 return direction;
342 }
343
344 void CPUParticles2D::set_spread(real_t p_spread) {
345 spread = p_spread;
346 }
347
348 real_t CPUParticles2D::get_spread() const {
349 return spread;
350 }
351
352 void CPUParticles2D::set_param_min(Parameter p_param, real_t p_value) {
353 ERR_FAIL_INDEX(p_param, PARAM_MAX);
354
355 parameters_min[p_param] = p_value;
356 if (parameters_min[p_param] > parameters_max[p_param]) {
357 set_param_max(p_param, p_value);
358 }
359 }
360
361 real_t CPUParticles2D::get_param_min(Parameter p_param) const {
362 ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
363
364 return parameters_min[p_param];
365 }
366
367 void CPUParticles2D::set_param_max(Parameter p_param, real_t p_value) {
368 ERR_FAIL_INDEX(p_param, PARAM_MAX);
369
370 parameters_max[p_param] = p_value;
371 if (parameters_min[p_param] > parameters_max[p_param]) {
372 set_param_min(p_param, p_value);
373 }
374
375 update_configuration_warnings();
376 }
377
378 real_t CPUParticles2D::get_param_max(Parameter p_param) const {
379 ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
380
381 return parameters_max[p_param];
382 }
383
384 static void _adjust_curve_range(const Ref<Curve> &p_curve, real_t p_min, real_t p_max) {
385 Ref<Curve> curve = p_curve;
386 if (curve.is_null()) {
387 return;
388 }
389
390 curve->ensure_default_setup(p_min, p_max);
391 }
392
393 void CPUParticles2D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
394 ERR_FAIL_INDEX(p_param, PARAM_MAX);
395
396 curve_parameters[p_param] = p_curve;
397
398 switch (p_param) {
399 case PARAM_INITIAL_LINEAR_VELOCITY: {
400 //do none for this one
401 } break;
402 case PARAM_ANGULAR_VELOCITY: {
403 _adjust_curve_range(p_curve, -360, 360);
404 } break;
405 case PARAM_ORBIT_VELOCITY: {
406 _adjust_curve_range(p_curve, -500, 500);
407 } break;
408 case PARAM_LINEAR_ACCEL: {
409 _adjust_curve_range(p_curve, -200, 200);
410 } break;
411 case PARAM_RADIAL_ACCEL: {
412 _adjust_curve_range(p_curve, -200, 200);
413 } break;
414 case PARAM_TANGENTIAL_ACCEL: {
415 _adjust_curve_range(p_curve, -200, 200);
416 } break;
417 case PARAM_DAMPING: {
418 _adjust_curve_range(p_curve, 0, 100);
419 } break;
420 case PARAM_ANGLE: {
421 _adjust_curve_range(p_curve, -360, 360);
422 } break;
423 case PARAM_SCALE: {
424 } break;
425 case PARAM_HUE_VARIATION: {
426 _adjust_curve_range(p_curve, -1, 1);
427 } break;
428 case PARAM_ANIM_SPEED: {
429 _adjust_curve_range(p_curve, 0, 200);
430 } break;
431 case PARAM_ANIM_OFFSET: {
432 } break;
433 default: {
434 }
435 }
436
437 update_configuration_warnings();
438 }
439
440 Ref<Curve> CPUParticles2D::get_param_curve(Parameter p_param) const {
441 ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
442
443 return curve_parameters[p_param];
444 }
445
446 void CPUParticles2D::set_color(const Color &p_color) {
447 color = p_color;
448 }
449
450 Color CPUParticles2D::get_color() const {
451 return color;
452 }
453
454 void CPUParticles2D::set_color_ramp(const Ref<Gradient> &p_ramp) {
455 color_ramp = p_ramp;
456 }
457
458 Ref<Gradient> CPUParticles2D::get_color_ramp() const {
459 return color_ramp;
460 }
461
462 void CPUParticles2D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
463 color_initial_ramp = p_ramp;
464 }
465
466 Ref<Gradient> CPUParticles2D::get_color_initial_ramp() const {
467 return color_initial_ramp;
468 }
469
470 void CPUParticles2D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
471 ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
472 particle_flags[p_particle_flag] = p_enable;
473 }
474
475 bool CPUParticles2D::get_particle_flag(ParticleFlags p_particle_flag) const {
476 ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
477 return particle_flags[p_particle_flag];
478 }
479
480 void CPUParticles2D::set_emission_shape(EmissionShape p_shape) {
481 ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
482 emission_shape = p_shape;
483 notify_property_list_changed();
484 #ifdef TOOLS_ENABLED
485 if (Engine::get_singleton()->is_editor_hint()) {
486 queue_redraw();
487 }
488 #endif
489 }
490
491 void CPUParticles2D::set_emission_sphere_radius(real_t p_radius) {
492 if (p_radius == emission_sphere_radius) {
493 return;
494 }
495 emission_sphere_radius = p_radius;
496 #ifdef TOOLS_ENABLED
497 if (Engine::get_singleton()->is_editor_hint()) {
498 queue_redraw();
499 }
500 #endif
501 }
502
503 void CPUParticles2D::set_emission_rect_extents(Vector2 p_extents) {
504 if (p_extents == emission_rect_extents) {
505 return;
506 }
507 emission_rect_extents = p_extents;
508 #ifdef TOOLS_ENABLED
509 if (Engine::get_singleton()->is_editor_hint()) {
510 queue_redraw();
511 }
512 #endif
513 }
514
515 void CPUParticles2D::set_emission_points(const Vector<Vector2> &p_points) {
516 emission_points = p_points;
517 }
518
519 void CPUParticles2D::set_emission_normals(const Vector<Vector2> &p_normals) {
520 emission_normals = p_normals;
521 }
522
523 void CPUParticles2D::set_emission_colors(const Vector<Color> &p_colors) {
524 emission_colors = p_colors;
525 }
526
527 void CPUParticles2D::set_emission_ring_inner_radius(real_t p_inner_radius) {
528 emission_ring_inner_radius = p_inner_radius;
529 }
530
531 void CPUParticles2D::set_emission_ring_radius(real_t p_ring_radius) {
532 emission_ring_radius = p_ring_radius;
533 }
534
535 real_t CPUParticles2D::get_emission_sphere_radius() const {
536 return emission_sphere_radius;
537 }
538
539 Vector2 CPUParticles2D::get_emission_rect_extents() const {
540 return emission_rect_extents;
541 }
542
543 Vector<Vector2> CPUParticles2D::get_emission_points() const {
544 return emission_points;
545 }
546
547 Vector<Vector2> CPUParticles2D::get_emission_normals() const {
548 return emission_normals;
549 }
550
551 Vector<Color> CPUParticles2D::get_emission_colors() const {
552 return emission_colors;
553 }
554
555 real_t CPUParticles2D::get_emission_ring_inner_radius() const {
556 return emission_ring_inner_radius;
557 }
558
559 real_t CPUParticles2D::get_emission_ring_radius() const {
560 return emission_ring_radius;
561 }
562
563 CPUParticles2D::EmissionShape CPUParticles2D::get_emission_shape() const {
564 return emission_shape;
565 }
566
567 void CPUParticles2D::set_gravity(const Vector2 &p_gravity) {
568 gravity = p_gravity;
569 }
570
571 Vector2 CPUParticles2D::get_gravity() const {
572 return gravity;
573 }
574
575 void CPUParticles2D::set_scale_curve_x(Ref<Curve> p_scale_curve) {
576 scale_curve_x = p_scale_curve;
577 }
578
579 void CPUParticles2D::set_scale_curve_y(Ref<Curve> p_scale_curve) {
580 scale_curve_y = p_scale_curve;
581 }
582
583 void CPUParticles2D::set_split_scale(bool p_split_scale) {
584 split_scale = p_split_scale;
585 notify_property_list_changed();
586 }
587
588 Ref<Curve> CPUParticles2D::get_scale_curve_x() const {
589 return scale_curve_x;
590 }
591
592 Ref<Curve> CPUParticles2D::get_scale_curve_y() const {
593 return scale_curve_y;
594 }
595
596 bool CPUParticles2D::get_split_scale() {
597 return split_scale;
598 }
599
600 void CPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
601 if (p_use_fixed_seed == use_fixed_seed) {
602 return;
603 }
604 use_fixed_seed = p_use_fixed_seed;
605 notify_property_list_changed();
606 }
607
608 bool CPUParticles2D::get_use_fixed_seed() const {
609 return use_fixed_seed;
610 }
611
612 void CPUParticles2D::set_seed(uint32_t p_seed) {
613 seed = p_seed;
614 }
615
616 #ifdef TOOLS_ENABLED
617 void CPUParticles2D::set_show_gizmos(bool p_show_gizmos) {
618 if (show_gizmos == p_show_gizmos) {
619 return;
620 }
621 show_gizmos = p_show_gizmos;
622 queue_redraw();
623 }
624 #endif
625
626 uint32_t CPUParticles2D::get_seed() const {
627 return seed;
628 }
629
630 void CPUParticles2D::request_particles_process(real_t p_requested_process_time) {
631 _requested_process_time = p_requested_process_time;
632 }
633
634 void CPUParticles2D::_validate_property(PropertyInfo &p_property) const {
635 if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") {
636 p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
637 }
638
639 if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
640 p_property.usage = PROPERTY_USAGE_NONE;
641 }
642
643 if (p_property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
644 p_property.usage = PROPERTY_USAGE_NONE;
645 }
646
647 if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
648 p_property.usage = PROPERTY_USAGE_NONE;
649 }
650
651 if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
652 p_property.usage = PROPERTY_USAGE_NONE;
653 }
654
655 if (p_property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
656 p_property.usage = PROPERTY_USAGE_NONE;
657 }
658
659 if (p_property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
660 p_property.usage = PROPERTY_USAGE_NONE;
661 }
662 if (p_property.name.begins_with("scale_curve_") && !split_scale) {
663 p_property.usage = PROPERTY_USAGE_NONE;
664 }
665
666 if (p_property.name == "emission_ring_inner_radius" && emission_shape != EMISSION_SHAPE_RING) {
667 p_property.usage = PROPERTY_USAGE_NONE;
668 }
669 if (p_property.name == "emission_ring_radius" && emission_shape != EMISSION_SHAPE_RING) {
670 p_property.usage = PROPERTY_USAGE_NONE;
671 }
672
673 if (p_property.name == "seed" && !use_fixed_seed) {
674 p_property.usage = PROPERTY_USAGE_NONE;
675 }
676 }
677
678 static uint32_t idhash(uint32_t x) {
679 x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
680 x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
681 x = (x >> uint32_t(16)) ^ x;
682 return x;
683 }
684
685 static real_t rand_from_seed(uint32_t &seed) {
686 int k;
687 int s = int(seed);
688 if (s == 0) {
689 s = 305420679;
690 }
691 k = s / 127773;
692 s = 16807 * (s - k * 127773) - 2836 * k;
693 if (s < 0) {
694 s += 2147483647;
695 }
696 seed = uint32_t(s);
697 return (seed % uint32_t(65536)) / 65535.0;
698 }
699
700 void CPUParticles2D::_update_internal() {
701 if (particles.is_empty() || !is_visible_in_tree()) {
702 _set_do_redraw(false);
703 return;
704 }
705
706 // Change update mode?
707 _refresh_interpolation_state();
708
709 double delta = get_process_delta_time();
710 if (!active && !emitting) {
711 set_process_internal(false);
712 _set_do_redraw(false);
713
714 //reset variables
715 time = 0;
716 frame_remainder = 0;
717 cycle = 0;
718 return;
719 }
720 _set_do_redraw(true);
721 double frame_time;
722 if (fixed_fps > 0) {
723 frame_time = 1.0 / fixed_fps;
724 } else {
725 frame_time = 1.0 / 30.0;
726 }
727 double todo = _requested_process_time;
728 _requested_process_time = 0;
729 if (time == 0 && pre_process_time > 0.0) {
730 todo += pre_process_time;
731 }
732 real_t tmp_speed = speed_scale;
733 speed_scale = 1.0;
734 while (todo > 0) {
735 _particles_process(frame_time);
736 todo -= frame_time;
737 }
738 speed_scale = tmp_speed;
739
740 todo = 0.0;
741
742 if (fixed_fps > 0) {
743 double decr = frame_time;
744
745 double ldelta = delta;
746 if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
747 ldelta = 0.1;
748 } else if (ldelta <= 0.0) { //unlikely but..
749 ldelta = 0.001;
750 }
751 todo = frame_remainder + ldelta;
752
753 while (todo >= frame_time) {
754 _particles_process(frame_time);
755 todo -= decr;
756 }
757 frame_remainder = todo;
758
759 } else {
760 _particles_process(delta);
761 }
762
763 _update_particle_data_buffer();
764 }
765
766 void CPUParticles2D::_particles_process(double p_delta) {
767 p_delta *= speed_scale;
768
769 int pcount = particles.size();
770 Particle *w = particles.ptrw();
771
772 Particle *parray = w;
773
774 double prev_time = time;
775 time += p_delta;
776 if (time > lifetime) {
777 time = Math::fmod(time, lifetime);
778 cycle++;
779 if (one_shot && cycle > 0) {
780 set_emitting(false);
781 notify_property_list_changed();
782 }
783 }
784
785 Transform2D emission_xform;
786 Transform2D velocity_xform;
787 if (!local_coords) {
788 if (!_interpolation_data.interpolated_follow) {
789 emission_xform = get_global_transform();
790 } else {
791 TransformInterpolator::interpolate_transform_2d(_interpolation_data.global_xform_prev, _interpolation_data.global_xform_curr, emission_xform, Engine::get_singleton()->get_physics_interpolation_fraction());
792 }
793 velocity_xform = emission_xform;
794 velocity_xform[2] = Vector2();
795 }
796
797 double system_phase = time / lifetime;
798
799 bool should_be_active = false;
800 for (int i = 0; i < pcount; i++) {
801 Particle &p = parray[i];
802
803 if (!emitting && !p.active) {
804 continue;
805 }
806
807 double local_delta = p_delta;
808
809 // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
810 // While we use time in tests later on, for randomness we use the phase as done in the
811 // original shader code, and we later multiply by lifetime to get the time.
812 double restart_phase = double(i) / double(pcount);
813
814 if (randomness_ratio > 0.0) {
815 uint32_t _seed = cycle;
816 if (restart_phase >= system_phase) {
817 _seed -= uint32_t(1);
818 }
819 _seed *= uint32_t(pcount);
820 _seed += uint32_t(i);
821 double random = double(idhash(_seed) % uint32_t(65536)) / 65536.0;
822 restart_phase += randomness_ratio * random * 1.0 / double(pcount);
823 }
824
825 restart_phase *= (1.0 - explosiveness_ratio);
826 double restart_time = restart_phase * lifetime;
827 bool restart = false;
828
829 if (time > prev_time) {
830 // restart_time >= prev_time is used so particles emit in the first frame they are processed
831
832 if (restart_time >= prev_time && restart_time < time) {
833 restart = true;
834 if (fractional_delta) {
835 local_delta = time - restart_time;
836 }
837 }
838
839 } else if (local_delta > 0.0) {
840 if (restart_time >= prev_time) {
841 restart = true;
842 if (fractional_delta) {
843 local_delta = lifetime - restart_time + time;
844 }
845
846 } else if (restart_time < time) {
847 restart = true;
848 if (fractional_delta) {
849 local_delta = time - restart_time;
850 }
851 }
852 }
853
854 if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
855 restart = true;
856 }
857
858 float tv = 0.0;
859
860 if (restart) {
861 if (!emitting) {
862 p.active = false;
863 continue;
864 }
865 p.active = true;
866
867 /*real_t tex_linear_velocity = 0;
868 if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
869 tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0);
870 }*/
871
872 real_t tex_angle = 1.0;
873 if (curve_parameters[PARAM_ANGLE].is_valid()) {
874 tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
875 }
876
877 real_t tex_anim_offset = 1.0;
878 if (curve_parameters[PARAM_ANGLE].is_valid()) {
879 tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv);
880 }
881
882 p.seed = seed + uint32_t(i) + i + cycle;
883 rng->set_seed(p.seed);
884
885 p.angle_rand = rng->randf();
886 p.scale_rand = rng->randf();
887 p.hue_rot_rand = rng->randf();
888 p.anim_offset_rand = rng->randf();
889
890 if (color_initial_ramp.is_valid()) {
891 p.start_color_rand = color_initial_ramp->get_color_at_offset(rng->randf());
892 } else {
893 p.start_color_rand = Color(1, 1, 1, 1);
894 }
895
896 real_t angle1_rad = direction.angle() + Math::deg_to_rad((rng->randf() * 2.0 - 1.0) * spread);
897 Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
898 p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], rng->randf());
899
900 real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
901 p.rotation = Math::deg_to_rad(base_angle);
902
903 p.custom[0] = 0.0; // unused
904 p.custom[1] = 0.0; // phase [0..1]
905 p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand);
906 p.custom[3] = (1.0 - rng->randf() * lifetime_randomness);
907 p.transform = Transform2D();
908 p.time = 0;
909 p.lifetime = lifetime * p.custom[3];
910 p.base_color = Color(1, 1, 1, 1);
911
912 switch (emission_shape) {
913 case EMISSION_SHAPE_POINT: {
914 //do none
915 } break;
916 case EMISSION_SHAPE_SPHERE: {
917 real_t t = Math::TAU * rng->randf();
918 real_t radius = emission_sphere_radius * rng->randf();
919 p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
920 } break;
921 case EMISSION_SHAPE_SPHERE_SURFACE: {
922 real_t s = rng->randf(), t = Math::TAU * rng->randf();
923 real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
924 p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
925 } break;
926 case EMISSION_SHAPE_RECTANGLE: {
927 p.transform[2] = Vector2(rng->randf() * 2.0 - 1.0, rng->randf() * 2.0 - 1.0) * emission_rect_extents;
928 } break;
929 case EMISSION_SHAPE_POINTS:
930 case EMISSION_SHAPE_DIRECTED_POINTS: {
931 int pc = emission_points.size();
932 if (pc == 0) {
933 break;
934 }
935
936 int random_idx = Math::rand() % pc;
937
938 p.transform[2] = emission_points.get(random_idx);
939
940 if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
941 Vector2 normal = emission_normals.get(random_idx);
942 Transform2D m2;
943 m2.columns[0] = normal;
944 m2.columns[1] = normal.orthogonal();
945 p.velocity = m2.basis_xform(p.velocity);
946 }
947
948 if (emission_colors.size() == pc) {
949 p.base_color = emission_colors.get(random_idx);
950 }
951 } break;
952 case EMISSION_SHAPE_RING: {
953 real_t t = Math::TAU * Math::randf();
954 real_t outer_sq = emission_ring_radius * emission_ring_radius;
955 real_t inner_sq = emission_ring_inner_radius * emission_ring_inner_radius;
956 real_t radius = Math::sqrt(Math::randf() * (outer_sq - inner_sq) + inner_sq);
957 p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
958 } break;
959 case EMISSION_SHAPE_MAX: { // Max value for validity check.
960 break;
961 }
962 }
963
964 if (!local_coords) {
965 p.velocity = velocity_xform.xform(p.velocity);
966 p.transform = emission_xform * p.transform;
967 }
968
969 } else if (!p.active) {
970 continue;
971 } else if (p.time > p.lifetime) {
972 p.active = false;
973 tv = 1.0;
974 } else {
975 uint32_t _seed = p.seed;
976 p.time += local_delta;
977 p.custom[1] = p.time / lifetime;
978 tv = p.time / p.lifetime;
979
980 real_t tex_linear_velocity = 1.0;
981 if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
982 tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv);
983 }
984
985 real_t tex_orbit_velocity = 1.0;
986 if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
987 tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv);
988 }
989
990 real_t tex_angular_velocity = 1.0;
991 if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
992 tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv);
993 }
994
995 real_t tex_linear_accel = 1.0;
996 if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
997 tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv);
998 }
999
1000 real_t tex_tangential_accel = 1.0;
1001 if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
1002 tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv);
1003 }
1004
1005 real_t tex_radial_accel = 1.0;
1006 if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
1007 tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv);
1008 }
1009
1010 real_t tex_damping = 1.0;
1011 if (curve_parameters[PARAM_DAMPING].is_valid()) {
1012 tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv);
1013 }
1014
1015 real_t tex_angle = 1.0;
1016 if (curve_parameters[PARAM_ANGLE].is_valid()) {
1017 tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
1018 }
1019 real_t tex_anim_speed = 1.0;
1020 if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
1021 tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv);
1022 }
1023
1024 real_t tex_anim_offset = 1.0;
1025 if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
1026 tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv);
1027 }
1028
1029 Vector2 force = gravity;
1030 Vector2 pos = p.transform[2];
1031
1032 //apply linear acceleration
1033 force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(_seed)) : Vector2();
1034 //apply radial acceleration
1035 Vector2 org = emission_xform[2];
1036 Vector2 diff = pos - org;
1037 force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(_seed)) : Vector2();
1038 //apply tangential acceleration;
1039 Vector2 yx = Vector2(diff.y, diff.x);
1040 force += yx.length() > 0.0 ? (yx * Vector2(-1.0, 1.0)).normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(_seed))) : Vector2();
1041 //apply attractor forces
1042 p.velocity += force * local_delta;
1043 //orbit velocity
1044 real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(_seed));
1045 if (orbit_amount != 0.0) {
1046 real_t ang = orbit_amount * local_delta * Math::TAU;
1047 // Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix,
1048 // but we use -ang here to reproduce its behavior.
1049 Transform2D rot = Transform2D(-ang, Vector2());
1050 p.transform[2] -= diff;
1051 p.transform[2] += rot.basis_xform(diff);
1052 }
1053 if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
1054 p.velocity = p.velocity.normalized() * tex_linear_velocity;
1055 }
1056
1057 if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) {
1058 real_t v = p.velocity.length();
1059 real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(_seed));
1060 v -= damp * local_delta;
1061 if (v < 0.0) {
1062 p.velocity = Vector2();
1063 } else {
1064 p.velocity = p.velocity.normalized() * v;
1065 }
1066 }
1067 real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
1068 base_angle += p.custom[1] * lifetime * tex_angular_velocity * Math::lerp(parameters_min[PARAM_ANGULAR_VELOCITY], parameters_max[PARAM_ANGULAR_VELOCITY], rand_from_seed(_seed));
1069 p.rotation = Math::deg_to_rad(base_angle); //angle
1070 p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand) + tv * tex_anim_speed * Math::lerp(parameters_min[PARAM_ANIM_SPEED], parameters_max[PARAM_ANIM_SPEED], rand_from_seed(_seed));
1071 }
1072 //apply color
1073 //apply hue rotation
1074
1075 Vector2 tex_scale = Vector2(1.0, 1.0);
1076 if (split_scale) {
1077 if (scale_curve_x.is_valid()) {
1078 tex_scale.x = scale_curve_x->sample(tv);
1079 } else {
1080 tex_scale.x = 1.0;
1081 }
1082 if (scale_curve_y.is_valid()) {
1083 tex_scale.y = scale_curve_y->sample(tv);
1084 } else {
1085 tex_scale.y = 1.0;
1086 }
1087 } else {
1088 if (curve_parameters[PARAM_SCALE].is_valid()) {
1089 real_t tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv);
1090 tex_scale.x = tmp_scale;
1091 tex_scale.y = tmp_scale;
1092 }
1093 }
1094
1095 real_t tex_hue_variation = 0.0;
1096 if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
1097 tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv);
1098 }
1099
1100 real_t hue_rot_angle = (tex_hue_variation)*Math::TAU * Math::lerp(parameters_min[PARAM_HUE_VARIATION], parameters_max[PARAM_HUE_VARIATION], p.hue_rot_rand);
1101 real_t hue_rot_c = Math::cos(hue_rot_angle);
1102 real_t hue_rot_s = Math::sin(hue_rot_angle);
1103
1104 Basis hue_rot_mat;
1105 {
1106 Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
1107 Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
1108 Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
1109
1110 for (int j = 0; j < 3; j++) {
1111 hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
1112 }
1113 }
1114
1115 if (color_ramp.is_valid()) {
1116 p.color = color_ramp->get_color_at_offset(tv) * color;
1117 } else {
1118 p.color = color;
1119 }
1120
1121 Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
1122 p.color.r = color_rgb.x;
1123 p.color.g = color_rgb.y;
1124 p.color.b = color_rgb.z;
1125
1126 p.color *= p.base_color * p.start_color_rand;
1127
1128 if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
1129 if (p.velocity.length() > 0.0) {
1130 p.transform.columns[1] = p.velocity;
1131 }
1132
1133 p.transform.columns[1] = p.transform.columns[1].normalized();
1134 p.transform.columns[0] = p.transform.columns[1].orthogonal();
1135 } else {
1136 p.transform.columns[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
1137 p.transform.columns[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
1138 }
1139
1140 //scale by scale
1141 Vector2 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand);
1142 if (base_scale.x < 0.00001) {
1143 base_scale.x = 0.00001;
1144 }
1145 if (base_scale.y < 0.00001) {
1146 base_scale.y = 0.00001;
1147 }
1148 p.transform.columns[0] *= base_scale.x;
1149 p.transform.columns[1] *= base_scale.y;
1150
1151 p.transform[2] += p.velocity * local_delta;
1152
1153 should_be_active = true;
1154 }
1155 if (!Math::is_equal_approx(time, 0.0) && active && !should_be_active) {
1156 active = false;
1157 emit_signal(SceneStringName(finished));
1158 }
1159 }
1160
1161 void CPUParticles2D::_update_particle_data_buffer() {
1162 MutexLock lock(update_mutex);
1163
1164 int pc = particles.size();
1165
1166 int *ow;
1167 int *order = nullptr;
1168
1169 float *w = particle_data.ptrw();
1170 const Particle *r = particles.ptr();
1171 float *ptr = w;
1172
1173 if (draw_order != DRAW_ORDER_INDEX) {
1174 ow = particle_order.ptrw();
1175 order = ow;
1176
1177 for (int i = 0; i < pc; i++) {
1178 order[i] = i;
1179 }
1180 if (draw_order == DRAW_ORDER_LIFETIME) {
1181 SortArray<int, SortLifetime> sorter;
1182 sorter.compare.particles = r;
1183 sorter.sort(order, pc);
1184 }
1185 }
1186
1187 for (int i = 0; i < pc; i++) {
1188 int idx = order ? order[i] : i;
1189
1190 Transform2D t = r[idx].transform;
1191
1192 if (!local_coords) {
1193 t = inv_emission_transform * t;
1194 }
1195
1196 if (r[idx].active) {
1197 ptr[0] = t.columns[0][0];
1198 ptr[1] = t.columns[1][0];
1199 ptr[2] = 0;
1200 ptr[3] = t.columns[2][0];
1201 ptr[4] = t.columns[0][1];
1202 ptr[5] = t.columns[1][1];
1203 ptr[6] = 0;
1204 ptr[7] = t.columns[2][1];
1205
1206 } else {
1207 memset(ptr, 0, sizeof(float) * 8);
1208 }
1209
1210 Color c = r[idx].color;
1211
1212 ptr[8] = c.r;
1213 ptr[9] = c.g;
1214 ptr[10] = c.b;
1215 ptr[11] = c.a;
1216
1217 ptr[12] = r[idx].custom[0];
1218 ptr[13] = r[idx].custom[1];
1219 ptr[14] = r[idx].custom[2];
1220 ptr[15] = r[idx].custom[3];
1221
1222 ptr += 16;
1223 }
1224 }
1225
1226 void CPUParticles2D::_set_do_redraw(bool p_do_redraw) {
1227 if (do_redraw == p_do_redraw) {
1228 return;
1229 }
1230 do_redraw = p_do_redraw;
1231
1232 {
1233 MutexLock lock(update_mutex);
1234
1235 if (do_redraw) {
1236 RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
1237 RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
1238
1239 RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
1240 } else {
1241 if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread))) {
1242 RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
1243 }
1244 RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
1245
1246 RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
1247 }
1248 }
1249
1250 queue_redraw(); // redraw to update render list
1251 }
1252
1253 void CPUParticles2D::_update_render_thread() {
1254 MutexLock lock(update_mutex);
1255
1256 RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
1257 }
1258
1259 void CPUParticles2D::_notification(int p_what) {
1260 switch (p_what) {
1261 case NOTIFICATION_ENTER_TREE: {
1262 set_process_internal(emitting);
1263
1264 _refresh_interpolation_state();
1265
1266 // If we are interpolated following, then reset physics interpolation
1267 // when first appearing. This won't be called by canvas item, as in the
1268 // following mode, is_physics_interpolated() is actually FALSE.
1269 if (_interpolation_data.interpolated_follow) {
1270 notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
1271 }
1272 } break;
1273
1274 case NOTIFICATION_EXIT_TREE: {
1275 _set_do_redraw(false);
1276 } break;
1277
1278 case NOTIFICATION_DRAW: {
1279 // first update before rendering to avoid one frame delay after emitting starts
1280 if (emitting && (time == 0)) {
1281 _update_internal();
1282 }
1283
1284 if (!do_redraw) {
1285 return; // don't add to render list
1286 }
1287
1288 RID texrid;
1289 if (texture.is_valid()) {
1290 texrid = texture->get_rid();
1291 }
1292
1293 RS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid);
1294
1295 #ifdef TOOLS_ENABLED
1296 if (show_gizmos) {
1297 _draw_emission_gizmo();
1298 }
1299 #endif
1300
1301 } break;
1302
1303 case NOTIFICATION_INTERNAL_PROCESS: {
1304 _update_internal();
1305 } break;
1306
1307 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
1308 if (_interpolation_data.interpolated_follow) {
1309 // Keep the interpolated follow target updated.
1310 _interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
1311 _interpolation_data.global_xform_curr = get_global_transform();
1312 }
1313 } break;
1314
1315 case NOTIFICATION_TRANSFORM_CHANGED: {
1316 if (_interpolation_data.interpolated_follow) {
1317 // If the transform has been updated AFTER the physics tick, keep data flowing.
1318 if (Engine::get_singleton()->is_in_physics_frame()) {
1319 _interpolation_data.global_xform_curr = get_global_transform();
1320 }
1321 }
1322 #ifdef TOOLS_ENABLED
1323 if (!local_coords) {
1324 queue_redraw();
1325 }
1326 #endif
1327 } break;
1328
1329 case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
1330 // Make sure current is up to date with any pending global transform changes.
1331 _interpolation_data.global_xform_curr = get_global_transform_const();
1332 _interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
1333 } break;
1334 }
1335 }
1336
1337 #ifdef TOOLS_ENABLED
1338 void CPUParticles2D::_draw_emission_gizmo() {
1339 Color emission_ring_color = Color(0.8, 0.7, 0.4, 0.4);
1340 Transform2D gizmo_transform;
1341 if (!local_coords) {
1342 gizmo_transform = get_global_transform();
1343 }
1344
1345 draw_set_transform_matrix(gizmo_transform);
1346
1347 switch (emission_shape) {
1348 case CPUParticles2D::EMISSION_SHAPE_RECTANGLE:
1349 draw_rect(Rect2(-emission_rect_extents, emission_rect_extents * 2.0), emission_ring_color, false);
1350 break;
1351 case CPUParticles2D::EMISSION_SHAPE_SPHERE:
1352 case CPUParticles2D::EMISSION_SHAPE_SPHERE_SURFACE:
1353 draw_circle(Vector2(), emission_sphere_radius, emission_ring_color, false);
1354 break;
1355 default:
1356 break;
1357 }
1358 }
1359 #endif
1360
1361 void CPUParticles2D::convert_from_particles(Node *p_particles) {
1362 GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_particles);
1363 ERR_FAIL_NULL_MSG(gpu_particles, "Only GPUParticles2D nodes can be converted to CPUParticles2D.");
1364
1365 set_emitting(gpu_particles->is_emitting());
1366 set_amount(gpu_particles->get_amount());
1367 set_lifetime(gpu_particles->get_lifetime());
1368 set_one_shot(gpu_particles->get_one_shot());
1369 set_pre_process_time(gpu_particles->get_pre_process_time());
1370 set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
1371 set_randomness_ratio(gpu_particles->get_randomness_ratio());
1372 set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
1373 set_fixed_fps(gpu_particles->get_fixed_fps());
1374 set_fractional_delta(gpu_particles->get_fractional_delta());
1375 set_speed_scale(gpu_particles->get_speed_scale());
1376 set_draw_order(DrawOrder(gpu_particles->get_draw_order()));
1377 set_texture(gpu_particles->get_texture());
1378
1379 Ref<Material> mat = gpu_particles->get_material();
1380 if (mat.is_valid()) {
1381 set_material(mat);
1382 }
1383
1384 Ref<ParticleProcessMaterial> proc_mat = gpu_particles->get_process_material();
1385 if (proc_mat.is_null()) {
1386 return;
1387 }
1388
1389 Vector3 dir = proc_mat->get_direction();
1390 set_direction(Vector2(dir.x, dir.y));
1391 set_spread(proc_mat->get_spread());
1392
1393 set_color(proc_mat->get_color());
1394
1395 Ref<GradientTexture1D> gt = proc_mat->get_color_ramp();
1396 if (gt.is_valid()) {
1397 set_color_ramp(gt->get_gradient());
1398 }
1399
1400 Ref<GradientTexture1D> gti = proc_mat->get_color_initial_ramp();
1401 if (gti.is_valid()) {
1402 set_color_initial_ramp(gti->get_gradient());
1403 }
1404
1405 set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, proc_mat->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
1406
1407 set_emission_shape(EmissionShape(proc_mat->get_emission_shape()));
1408 set_emission_sphere_radius(proc_mat->get_emission_sphere_radius());
1409 Vector2 rect_extents = Vector2(proc_mat->get_emission_box_extents().x, proc_mat->get_emission_box_extents().y);
1410 set_emission_rect_extents(rect_extents);
1411
1412 set_emission_ring_radius(proc_mat->get_emission_ring_radius());
1413 set_emission_ring_inner_radius(proc_mat->get_emission_ring_inner_radius());
1414
1415 Ref<CurveXYZTexture> scale3D = proc_mat->get_param_texture(ParticleProcessMaterial::PARAM_SCALE);
1416 if (scale3D.is_valid()) {
1417 split_scale = true;
1418 scale_curve_x = scale3D->get_curve_x();
1419 scale_curve_y = scale3D->get_curve_y();
1420 }
1421 set_gravity(Vector2(proc_mat->get_gravity().x, proc_mat->get_gravity().y));
1422 set_lifetime_randomness(proc_mat->get_lifetime_randomness());
1423
1424 #define CONVERT_PARAM(m_param) \
1425 set_param_min(m_param, proc_mat->get_param_min(ParticleProcessMaterial::m_param)); \
1426 { \
1427 Ref<CurveTexture> ctex = proc_mat->get_param_texture(ParticleProcessMaterial::m_param); \
1428 if (ctex.is_valid()) \
1429 set_param_curve(m_param, ctex->get_curve()); \
1430 } \
1431 set_param_max(m_param, proc_mat->get_param_max(ParticleProcessMaterial::m_param));
1432
1433 CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
1434 CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
1435 CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
1436 CONVERT_PARAM(PARAM_LINEAR_ACCEL);
1437 CONVERT_PARAM(PARAM_RADIAL_ACCEL);
1438 CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
1439 CONVERT_PARAM(PARAM_DAMPING);
1440 CONVERT_PARAM(PARAM_ANGLE);
1441 CONVERT_PARAM(PARAM_SCALE);
1442 CONVERT_PARAM(PARAM_HUE_VARIATION);
1443 CONVERT_PARAM(PARAM_ANIM_SPEED);
1444 CONVERT_PARAM(PARAM_ANIM_OFFSET);
1445
1446 #undef CONVERT_PARAM
1447 }
1448
1449 void CPUParticles2D::_bind_methods() {
1450 ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
1451 ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
1452 ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
1453 ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
1454 ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
1455 ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
1456 ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
1457 ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
1458 ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
1459 ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
1460 ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
1461 ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
1462 ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &CPUParticles2D::request_particles_process);
1463
1464 ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
1465 ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
1466 ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
1467 ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
1468 ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
1469 ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
1470 ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
1471 ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
1472 ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
1473 ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
1474 ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
1475 ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
1476 ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &CPUParticles2D::set_use_fixed_seed);
1477 ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &CPUParticles2D::get_use_fixed_seed);
1478
1479 ClassDB::bind_method(D_METHOD("set_seed", "seed"), &CPUParticles2D::set_seed);
1480 ClassDB::bind_method(D_METHOD("get_seed"), &CPUParticles2D::get_seed);
1481
1482 ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
1483
1484 ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
1485
1486 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
1487 ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
1488
1489 ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &CPUParticles2D::restart, DEFVAL(false));
1490
1491 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
1492 ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount"); // FIXME: Evaluate support for `exp` in integer properties, or remove this.
1493 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
1494 ADD_GROUP("Time", "");
1495 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
1496 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
1497 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,10.0,0.01,or_greater,exp,suffix:s"), "set_pre_process_time", "get_pre_process_time");
1498 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
1499 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
1500 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
1501 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed"), "set_use_fixed_seed", "get_use_fixed_seed");
1502 ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
1503 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
1504 ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
1505 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
1506 ADD_GROUP("Drawing", "");
1507 // No visibility_rect property contrarily to Particles2D, it's updated automatically.
1508 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
1509 ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
1510
1511 BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
1512 BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
1513
1514 ADD_PROPERTY_DEFAULT("seed", 0);
1515
1516 ////////////////////////////////
1517
1518 ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
1519 ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
1520
1521 ClassDB::bind_method(D_METHOD("set_spread", "spread"), &CPUParticles2D::set_spread);
1522 ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
1523
1524 ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles2D::set_param_min);
1525 ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles2D::get_param_min);
1526
1527 ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles2D::set_param_max);
1528 ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles2D::get_param_max);
1529
1530 ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
1531 ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
1532
1533 ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
1534 ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
1535
1536 ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
1537 ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
1538
1539 ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
1540 ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);
1541
1542 ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles2D::set_particle_flag);
1543 ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles2D::get_particle_flag);
1544
1545 ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
1546 ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
1547
1548 ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
1549 ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
1550
1551 ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
1552 ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
1553
1554 ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
1555 ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
1556
1557 ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
1558 ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
1559
1560 ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
1561 ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
1562
1563 ClassDB::bind_method(D_METHOD("set_emission_ring_inner_radius", "inner_radius"), &CPUParticles2D::set_emission_ring_inner_radius);
1564 ClassDB::bind_method(D_METHOD("get_emission_ring_inner_radius"), &CPUParticles2D::get_emission_ring_inner_radius);
1565
1566 ClassDB::bind_method(D_METHOD("set_emission_ring_radius", "radius"), &CPUParticles2D::set_emission_ring_radius);
1567 ClassDB::bind_method(D_METHOD("get_emission_ring_radius"), &CPUParticles2D::get_emission_ring_radius);
1568
1569 ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
1570 ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
1571
1572 ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles2D::get_split_scale);
1573 ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles2D::set_split_scale);
1574
1575 ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles2D::get_scale_curve_x);
1576 ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles2D::set_scale_curve_x);
1577
1578 ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles2D::get_scale_curve_y);
1579 ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles2D::set_scale_curve_y);
1580
1581 ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
1582
1583 ADD_SIGNAL(MethodInfo("finished"));
1584
1585 ADD_GROUP("Emission Shape", "emission_");
1586 ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Rectangle,Points,Directed Points,Ring", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
1587 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,suffix:px"), "set_emission_sphere_radius", "get_emission_sphere_radius");
1588 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents", PROPERTY_HINT_NONE, "suffix:px"), "set_emission_rect_extents", "get_emission_rect_extents");
1589 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
1590 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
1591 ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
1592 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_inner_radius"), "set_emission_ring_inner_radius", "get_emission_ring_inner_radius");
1593 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_radius"), "set_emission_ring_radius", "get_emission_ring_radius");
1594 ADD_GROUP("Particle Flags", "particle_flag_");
1595 ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
1596 ADD_GROUP("Direction", "");
1597 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
1598 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
1599 ADD_GROUP("Gravity", "");
1600 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity", PROPERTY_HINT_NONE, U"suffix:px/s\u00B2"), "set_gravity", "get_gravity");
1601 ADD_GROUP("Initial Velocity", "initial_");
1602 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_min", "get_param_min", PARAM_INITIAL_LINEAR_VELOCITY);
1603 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_max", "get_param_max", PARAM_INITIAL_LINEAR_VELOCITY);
1604 ADD_GROUP("Angular Velocity", "angular_");
1605 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_min", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ANGULAR_VELOCITY);
1606 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_max", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ANGULAR_VELOCITY);
1607 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
1608 ADD_GROUP("Orbit Velocity", "orbit_");
1609 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_min", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ORBIT_VELOCITY);
1610 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_max", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ORBIT_VELOCITY);
1611 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
1612 ADD_GROUP("Linear Accel", "linear_");
1613 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_LINEAR_ACCEL);
1614 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_LINEAR_ACCEL);
1615 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
1616 ADD_GROUP("Radial Accel", "radial_");
1617 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_RADIAL_ACCEL);
1618 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_RADIAL_ACCEL);
1619 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
1620 ADD_GROUP("Tangential Accel", "tangential_");
1621 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_TANGENTIAL_ACCEL);
1622 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_TANGENTIAL_ACCEL);
1623 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
1624 ADD_GROUP("Damping", "");
1625 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING);
1626 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING);
1627 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
1628 ADD_GROUP("Angle", "");
1629 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_min", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_min", "get_param_min", PARAM_ANGLE);
1630 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_max", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_max", "get_param_max", PARAM_ANGLE);
1631 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
1632 ADD_GROUP("Scale", "");
1633 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_SCALE);
1634 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_SCALE);
1635 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
1636 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale");
1637 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_x", "get_scale_curve_x");
1638 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_y", "get_scale_curve_y");
1639
1640 ADD_GROUP("Color", "");
1641 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
1642 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
1643 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");
1644
1645 ADD_GROUP("Hue Variation", "hue_");
1646 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION);
1647 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION);
1648 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
1649 ADD_GROUP("Animation", "anim_");
1650 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_min", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_min", "get_param_min", PARAM_ANIM_SPEED);
1651 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_max", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_max", "get_param_max", PARAM_ANIM_SPEED);
1652 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
1653 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET);
1654 ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET);
1655 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
1656
1657 BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
1658 BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
1659 BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
1660 BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
1661 BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
1662 BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
1663 BIND_ENUM_CONSTANT(PARAM_DAMPING);
1664 BIND_ENUM_CONSTANT(PARAM_ANGLE);
1665 BIND_ENUM_CONSTANT(PARAM_SCALE);
1666 BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
1667 BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
1668 BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
1669 BIND_ENUM_CONSTANT(PARAM_MAX);
1670
1671 BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
1672 BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
1673 BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
1674 BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
1675
1676 BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
1677 BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
1678 BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
1679 BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
1680 BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
1681 BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
1682 BIND_ENUM_CONSTANT(EMISSION_SHAPE_RING);
1683 BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
1684 }
1685
1686 CPUParticles2D::CPUParticles2D() {
1687 mesh = RenderingServer::get_singleton()->mesh_create();
1688 multimesh = RenderingServer::get_singleton()->multimesh_create();
1689 RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
1690
1691 set_emitting(true);
1692 set_amount(8);
1693 set_use_local_coordinates(false);
1694 set_seed(Math::rand());
1695
1696 rng.instantiate();
1697
1698 set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1699 set_param_min(PARAM_ANGULAR_VELOCITY, 0);
1700 set_param_min(PARAM_ORBIT_VELOCITY, 0);
1701 set_param_min(PARAM_LINEAR_ACCEL, 0);
1702 set_param_min(PARAM_RADIAL_ACCEL, 0);
1703 set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
1704 set_param_min(PARAM_DAMPING, 0);
1705 set_param_min(PARAM_ANGLE, 0);
1706 set_param_min(PARAM_SCALE, 1);
1707 set_param_min(PARAM_HUE_VARIATION, 0);
1708 set_param_min(PARAM_ANIM_SPEED, 0);
1709 set_param_min(PARAM_ANIM_OFFSET, 0);
1710
1711 set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1712 set_param_max(PARAM_ANGULAR_VELOCITY, 0);
1713 set_param_max(PARAM_ORBIT_VELOCITY, 0);
1714 set_param_max(PARAM_LINEAR_ACCEL, 0);
1715 set_param_max(PARAM_RADIAL_ACCEL, 0);
1716 set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
1717 set_param_max(PARAM_DAMPING, 0);
1718 set_param_max(PARAM_ANGLE, 0);
1719 set_param_max(PARAM_SCALE, 1);
1720 set_param_max(PARAM_HUE_VARIATION, 0);
1721 set_param_max(PARAM_ANIM_SPEED, 0);
1722 set_param_max(PARAM_ANIM_OFFSET, 0);
1723
1724 for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
1725 particle_flags[i] = false;
1726 }
1727
1728 set_color(Color(1, 1, 1, 1));
1729
1730 _update_mesh_texture();
1731
1732 // CPUParticles2D defaults to interpolation off.
1733 // This is because the result often looks better when the particles are updated every frame.
1734 // Note that children will need to explicitly turn back on interpolation if they want to use it,
1735 // rather than relying on inherit mode.
1736 set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
1737 }
1738
1739 CPUParticles2D::~CPUParticles2D() {
1740 ERR_FAIL_NULL(RenderingServer::get_singleton());
1741 RS::get_singleton()->free_rid(multimesh);
1742 RS::get_singleton()->free_rid(mesh);
1743 }