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