Debian Codegraph @

scene/2d/gpu_particles_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gpu_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 "gpu_particles_2d.h"
32 #include "gpu_particles_2d.compat.inc"
33
34 #include "scene/2d/cpu_particles_2d.h"
35 #include "scene/resources/atlas_texture.h"
36 #include "scene/resources/canvas_item_material.h"
37 #include "scene/resources/curve_texture.h"
38 #include "scene/resources/gradient_texture.h"
39 #include "scene/resources/particle_process_material.h"
40
41 void GPUParticles2D::set_emitting(bool p_emitting) {
42 // Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.
43
44 if (p_emitting && p_emitting != emitting && !use_fixed_seed && one_shot) {
45 set_seed(Math::rand());
46 }
47 if (p_emitting && one_shot) {
48 if (!active && !emitting) {
49 // Last cycle ended.
50 active = true;
51 time = 0;
52 signal_canceled = false;
53 emission_time = lifetime;
54 active_time = lifetime * (2 - explosiveness_ratio);
55 } else {
56 signal_canceled = true;
57 }
58 set_process_internal(true);
59 } else if (!p_emitting) {
60 if (one_shot) {
61 set_process_internal(true);
62 } else {
63 set_process_internal(false);
64 }
65 }
66
67 emitting = p_emitting;
68 RS::get_singleton()->particles_set_emitting(particles, p_emitting);
69 }
70
71 void GPUParticles2D::set_amount(int p_amount) {
72 ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
73 amount = p_amount;
74 RS::get_singleton()->particles_set_amount(particles, amount);
75 }
76
77 void GPUParticles2D::set_lifetime(double p_lifetime) {
78 ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
79 lifetime = p_lifetime;
80 RS::get_singleton()->particles_set_lifetime(particles, lifetime);
81 }
82
83 void GPUParticles2D::set_one_shot(bool p_enable) {
84 one_shot = p_enable;
85 RS::get_singleton()->particles_set_one_shot(particles, one_shot);
86
87 if (is_emitting()) {
88 set_process_internal(true);
89 if (!one_shot) {
90 RenderingServer::get_singleton()->particles_restart(particles);
91 }
92 }
93
94 if (!one_shot) {
95 set_process_internal(false);
96 }
97 }
98
99 void GPUParticles2D::set_pre_process_time(double p_time) {
100 pre_process_time = p_time;
101 RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
102 }
103
104 void GPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
105 explosiveness_ratio = p_ratio;
106 RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
107 }
108
109 void GPUParticles2D::set_randomness_ratio(real_t p_ratio) {
110 randomness_ratio = p_ratio;
111 RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
112 }
113
114 void GPUParticles2D::set_visibility_rect(const Rect2 &p_visibility_rect) {
115 visibility_rect = p_visibility_rect;
116 AABB aabb;
117 aabb.position.x = p_visibility_rect.position.x;
118 aabb.position.y = p_visibility_rect.position.y;
119 aabb.size.x = p_visibility_rect.size.x;
120 aabb.size.y = p_visibility_rect.size.y;
121
122 RS::get_singleton()->particles_set_custom_aabb(particles, aabb);
123
124 queue_redraw();
125 }
126
127 void GPUParticles2D::set_use_local_coordinates(bool p_enable) {
128 local_coords = p_enable;
129 RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
130 set_notify_transform(!p_enable);
131 if (!p_enable && is_inside_tree()) {
132 _update_particle_emission_transform();
133 }
134 }
135
136 void GPUParticles2D::_update_particle_emission_transform() {
137 Transform2D xf2d = get_global_transform();
138 Transform3D xf;
139 xf.basis.set_column(0, Vector3(xf2d.columns[0].x, xf2d.columns[0].y, 0));
140 xf.basis.set_column(1, Vector3(xf2d.columns[1].x, xf2d.columns[1].y, 0));
141 xf.set_origin(Vector3(xf2d.get_origin().x, xf2d.get_origin().y, 0));
142
143 RS::get_singleton()->particles_set_emission_transform(particles, xf);
144 }
145
146 void GPUParticles2D::set_process_material(const Ref<Material> &p_material) {
147 if (process_material == p_material) {
148 return;
149 }
150
151 if (process_material.is_valid() && process_material->is_class("ParticleProcessMaterial")) {
152 process_material->disconnect("emission_shape_changed", callable_mp((CanvasItem *)this, &GPUParticles2D::queue_redraw));
153 }
154
155 process_material = p_material;
156
157 if (process_material.is_valid() && process_material->is_class("ParticleProcessMaterial")) {
158 process_material->connect("emission_shape_changed", callable_mp((CanvasItem *)this, &GPUParticles2D::queue_redraw));
159 }
160
161 queue_redraw();
162
163 Ref<ParticleProcessMaterial> pm = p_material;
164 if (pm.is_valid() && !pm->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z) && pm->get_gravity() == Vector3(0, -9.8, 0)) {
165 // Likely a new (3D) material, modify it to match 2D space
166 pm->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, true);
167 pm->set_gravity(Vector3(0, 98, 0));
168 }
169 RID material_rid;
170 if (process_material.is_valid()) {
171 material_rid = process_material->get_rid();
172 }
173 RS::get_singleton()->particles_set_process_material(particles, material_rid);
174
175 update_configuration_warnings();
176 }
177
178 void GPUParticles2D::set_trail_enabled(bool p_enabled) {
179 trail_enabled = p_enabled;
180 RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
181 queue_redraw();
182 update_configuration_warnings();
183
184 RS::get_singleton()->particles_set_transform_align(particles, p_enabled ? RS::PARTICLES_TRANSFORM_ALIGN_Y_TO_VELOCITY : RS::PARTICLES_TRANSFORM_ALIGN_DISABLED);
185 }
186
187 void GPUParticles2D::set_trail_lifetime(double p_seconds) {
188 ERR_FAIL_COND(p_seconds < 0.01 - CMP_EPSILON);
189 trail_lifetime = p_seconds;
190 RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
191 queue_redraw();
192 }
193
194 void GPUParticles2D::set_trail_sections(int p_sections) {
195 ERR_FAIL_COND(p_sections < 2);
196 ERR_FAIL_COND(p_sections > 128);
197
198 trail_sections = p_sections;
199 queue_redraw();
200 }
201
202 void GPUParticles2D::set_trail_section_subdivisions(int p_subdivisions) {
203 ERR_FAIL_COND(p_subdivisions < 1);
204 ERR_FAIL_COND(p_subdivisions > 1024);
205
206 trail_section_subdivisions = p_subdivisions;
207 queue_redraw();
208 }
209
210 void GPUParticles2D::set_interp_to_end(float p_interp) {
211 interp_to_end_factor = CLAMP(p_interp, 0.0, 1.0);
212 RS::get_singleton()->particles_set_interp_to_end(particles, interp_to_end_factor);
213 }
214
215 #ifdef TOOLS_ENABLED
216 void GPUParticles2D::set_show_gizmos(bool p_show_gizmos) {
217 if (show_gizmos == p_show_gizmos) {
218 return;
219 }
220 show_gizmos = p_show_gizmos;
221 queue_redraw();
222 }
223 #endif
224
225 bool GPUParticles2D::is_trail_enabled() const {
226 return trail_enabled;
227 }
228
229 double GPUParticles2D::get_trail_lifetime() const {
230 return trail_lifetime;
231 }
232
233 void GPUParticles2D::_update_collision_size() {
234 real_t csize = collision_base_size;
235
236 if (texture.is_valid()) {
237 csize *= (texture->get_width() + texture->get_height()) / 4.0; //half size since its a radius
238 }
239
240 RS::get_singleton()->particles_set_collision_base_size(particles, csize);
241 }
242
243 void GPUParticles2D::set_collision_base_size(real_t p_size) {
244 collision_base_size = p_size;
245 _update_collision_size();
246 }
247
248 real_t GPUParticles2D::get_collision_base_size() const {
249 return collision_base_size;
250 }
251
252 void GPUParticles2D::set_speed_scale(double p_scale) {
253 speed_scale = p_scale;
254 RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
255 }
256
257 bool GPUParticles2D::is_emitting() const {
258 return emitting;
259 }
260
261 int GPUParticles2D::get_amount() const {
262 return amount;
263 }
264
265 double GPUParticles2D::get_lifetime() const {
266 return lifetime;
267 }
268
269 int GPUParticles2D::get_trail_sections() const {
270 return trail_sections;
271 }
272 int GPUParticles2D::get_trail_section_subdivisions() const {
273 return trail_section_subdivisions;
274 }
275
276 bool GPUParticles2D::get_one_shot() const {
277 return one_shot;
278 }
279
280 double GPUParticles2D::get_pre_process_time() const {
281 return pre_process_time;
282 }
283
284 real_t GPUParticles2D::get_explosiveness_ratio() const {
285 return explosiveness_ratio;
286 }
287
288 real_t GPUParticles2D::get_randomness_ratio() const {
289 return randomness_ratio;
290 }
291
292 Rect2 GPUParticles2D::get_visibility_rect() const {
293 return visibility_rect;
294 }
295
296 bool GPUParticles2D::get_use_local_coordinates() const {
297 return local_coords;
298 }
299
300 Ref<Material> GPUParticles2D::get_process_material() const {
301 return process_material;
302 }
303
304 double GPUParticles2D::get_speed_scale() const {
305 return speed_scale;
306 }
307
308 void GPUParticles2D::set_draw_order(DrawOrder p_order) {
309 draw_order = p_order;
310 RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
311 }
312
313 GPUParticles2D::DrawOrder GPUParticles2D::get_draw_order() const {
314 return draw_order;
315 }
316
317 void GPUParticles2D::set_fixed_fps(int p_count) {
318 fixed_fps = p_count;
319 RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
320 }
321
322 int GPUParticles2D::get_fixed_fps() const {
323 return fixed_fps;
324 }
325
326 void GPUParticles2D::set_fractional_delta(bool p_enable) {
327 fractional_delta = p_enable;
328 RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
329 }
330
331 bool GPUParticles2D::get_fractional_delta() const {
332 return fractional_delta;
333 }
334
335 void GPUParticles2D::set_interpolate(bool p_enable) {
336 interpolate = p_enable;
337 RS::get_singleton()->particles_set_interpolate(particles, p_enable);
338 }
339
340 bool GPUParticles2D::get_interpolate() const {
341 return interpolate;
342 }
343
344 float GPUParticles2D::get_interp_to_end() const {
345 return interp_to_end_factor;
346 }
347
348 void GPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
349 if (p_use_fixed_seed == use_fixed_seed) {
350 return;
351 }
352 use_fixed_seed = p_use_fixed_seed;
353 notify_property_list_changed();
354 }
355
356 bool GPUParticles2D::get_use_fixed_seed() const {
357 return use_fixed_seed;
358 }
359
360 void GPUParticles2D::set_seed(uint32_t p_seed) {
361 seed = p_seed;
362 RS::get_singleton()->particles_set_seed(particles, p_seed);
363 }
364
365 uint32_t GPUParticles2D::get_seed() const {
366 return seed;
367 }
368
369 void GPUParticles2D::request_particles_process(real_t p_requested_process_time) {
370 RS::get_singleton()->particles_request_process_time(particles, p_requested_process_time);
371 }
372
373 PackedStringArray GPUParticles2D::get_configuration_warnings() const {
374 PackedStringArray warnings = Node2D::get_configuration_warnings();
375
376 if (process_material.is_null()) {
377 warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
378 } else {
379 CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
380
381 if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
382 const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
383 if (process &&
384 (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
385 process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
386 warnings.push_back(RTR("Particles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
387 }
388 }
389 }
390
391 if (trail_enabled && (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy")) {
392 warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile renderer."));
393 }
394
395 if (sub_emitter != NodePath() && (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy")) {
396 warnings.push_back(RTR("Particle sub-emitters are not available when using the Compatibility renderer."));
397 }
398
399 return warnings;
400 }
401
402 Rect2 GPUParticles2D::capture_rect() const {
403 AABB aabb = RS::get_singleton()->particles_get_current_aabb(particles);
404 Rect2 r;
405 r.position.x = aabb.position.x;
406 r.position.y = aabb.position.y;
407 r.size.x = aabb.size.x;
408 r.size.y = aabb.size.y;
409 return r;
410 }
411
412 void GPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
413 if (texture.is_valid()) {
414 texture->disconnect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
415 }
416
417 texture = p_texture;
418
419 if (texture.is_valid()) {
420 texture->connect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
421 }
422 _update_collision_size();
423 queue_redraw();
424 }
425
426 Ref<Texture2D> GPUParticles2D::get_texture() const {
427 return texture;
428 }
429
430 void GPUParticles2D::_validate_property(PropertyInfo &p_property) const {
431 if (p_property.name == "seed" && !use_fixed_seed) {
432 p_property.usage = PROPERTY_USAGE_NONE;
433 }
434 if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") {
435 p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
436 }
437 }
438
439 void GPUParticles2D::emit_particle(const Transform2D &p_transform2d, const Vector2 &p_velocity2d, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
440 Transform3D emit_transform;
441 emit_transform.basis.set_column(0, Vector3(p_transform2d.columns[0].x, p_transform2d.columns[0].y, 0));
442 emit_transform.basis.set_column(1, Vector3(p_transform2d.columns[1].x, p_transform2d.columns[1].y, 0));
443 emit_transform.set_origin(Vector3(p_transform2d.get_origin().x, p_transform2d.get_origin().y, 0));
444 Vector3 velocity = Vector3(p_velocity2d.x, p_velocity2d.y, 0);
445
446 RS::get_singleton()->particles_emit(particles, emit_transform, velocity, p_color, p_custom, p_emit_flags);
447 }
448
449 void GPUParticles2D::_attach_sub_emitter() {
450 Node *n = get_node_or_null(sub_emitter);
451 if (n) {
452 GPUParticles2D *sen = Object::cast_to<GPUParticles2D>(n);
453 if (sen && sen != this) {
454 RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
455 }
456 }
457 }
458
459 void GPUParticles2D::_texture_changed() {
460 // Changes to the texture need to trigger an update to make
461 // the editor redraw the sprite with the updated texture.
462 if (texture.is_valid()) {
463 queue_redraw();
464 }
465 }
466
467 void GPUParticles2D::set_sub_emitter(const NodePath &p_path) {
468 if (is_inside_tree()) {
469 RS::get_singleton()->particles_set_subemitter(particles, RID());
470 }
471
472 sub_emitter = p_path;
473
474 if (is_inside_tree() && sub_emitter != NodePath()) {
475 _attach_sub_emitter();
476 }
477 update_configuration_warnings();
478 }
479
480 NodePath GPUParticles2D::get_sub_emitter() const {
481 return sub_emitter;
482 }
483
484 void GPUParticles2D::set_amount_ratio(float p_ratio) {
485 amount_ratio = p_ratio;
486 RenderingServer::get_singleton()->particles_set_amount_ratio(particles, p_ratio);
487 }
488
489 float GPUParticles2D::get_amount_ratio() const {
490 return amount_ratio;
491 }
492
493 void GPUParticles2D::restart(bool p_keep_seed) {
494 if (!p_keep_seed && !use_fixed_seed) {
495 set_seed(Math::rand());
496 }
497 RS::get_singleton()->particles_restart(particles);
498 RS::get_singleton()->particles_set_emitting(particles, true);
499
500 emitting = true;
501 active = true;
502 signal_canceled = false;
503 time = 0;
504 emission_time = lifetime;
505 active_time = lifetime * (2 - explosiveness_ratio);
506 if (one_shot) {
507 set_process_internal(true);
508 }
509 }
510
511 void GPUParticles2D::convert_from_particles(Node *p_particles) {
512 CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_particles);
513 ERR_FAIL_NULL_MSG(cpu_particles, "Only CPUParticles2D nodes can be converted to GPUParticles2D.");
514
515 set_emitting(cpu_particles->is_emitting());
516 set_amount(cpu_particles->get_amount());
517 set_lifetime(cpu_particles->get_lifetime());
518 set_one_shot(cpu_particles->get_one_shot());
519 set_pre_process_time(cpu_particles->get_pre_process_time());
520 set_explosiveness_ratio(cpu_particles->get_explosiveness_ratio());
521 set_randomness_ratio(cpu_particles->get_randomness_ratio());
522 set_use_local_coordinates(cpu_particles->get_use_local_coordinates());
523 set_fixed_fps(cpu_particles->get_fixed_fps());
524 set_fractional_delta(cpu_particles->get_fractional_delta());
525 set_speed_scale(cpu_particles->get_speed_scale());
526 set_draw_order(DrawOrder(cpu_particles->get_draw_order()));
527 set_texture(cpu_particles->get_texture());
528
529 Ref<Material> mat = cpu_particles->get_material();
530 if (mat.is_valid()) {
531 set_material(mat);
532 }
533
534 Ref<ParticleProcessMaterial> proc_mat = memnew(ParticleProcessMaterial);
535 set_process_material(proc_mat);
536 Vector2 dir = cpu_particles->get_direction();
537 proc_mat->set_direction(Vector3(dir.x, dir.y, 0));
538 proc_mat->set_spread(cpu_particles->get_spread());
539 proc_mat->set_color(cpu_particles->get_color());
540
541 Ref<Gradient> color_grad = cpu_particles->get_color_ramp();
542 if (color_grad.is_valid()) {
543 Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
544 tex->set_gradient(color_grad);
545 proc_mat->set_color_ramp(tex);
546 }
547
548 Ref<Gradient> color_init_grad = cpu_particles->get_color_initial_ramp();
549 if (color_init_grad.is_valid()) {
550 Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
551 tex->set_gradient(color_init_grad);
552 proc_mat->set_color_initial_ramp(tex);
553 }
554
555 proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, cpu_particles->get_particle_flag(CPUParticles2D::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
556
557 proc_mat->set_emission_shape(ParticleProcessMaterial::EmissionShape(cpu_particles->get_emission_shape()));
558 proc_mat->set_emission_sphere_radius(cpu_particles->get_emission_sphere_radius());
559
560 proc_mat->set_emission_ring_radius(cpu_particles->get_emission_ring_radius());
561 proc_mat->set_emission_ring_inner_radius(cpu_particles->get_emission_ring_inner_radius());
562
563 Vector2 rect_extents = cpu_particles->get_emission_rect_extents();
564 proc_mat->set_emission_box_extents(Vector3(rect_extents.x, rect_extents.y, 0));
565
566 if (cpu_particles->get_split_scale()) {
567 Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
568 scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
569 scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
570 proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
571 }
572
573 Vector2 gravity = cpu_particles->get_gravity();
574 proc_mat->set_gravity(Vector3(gravity.x, gravity.y, 0));
575 proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
576
577 #define CONVERT_PARAM(m_param) \
578 proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles2D::m_param)); \
579 { \
580 Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles2D::m_param); \
581 if (curve.is_valid()) { \
582 Ref<CurveTexture> tex = memnew(CurveTexture); \
583 tex->set_curve(curve); \
584 proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
585 } \
586 } \
587 proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles2D::m_param));
588
589 CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
590 CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
591 CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
592 CONVERT_PARAM(PARAM_LINEAR_ACCEL);
593 CONVERT_PARAM(PARAM_RADIAL_ACCEL);
594 CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
595 CONVERT_PARAM(PARAM_DAMPING);
596 CONVERT_PARAM(PARAM_ANGLE);
597 CONVERT_PARAM(PARAM_SCALE);
598 CONVERT_PARAM(PARAM_HUE_VARIATION);
599 CONVERT_PARAM(PARAM_ANIM_SPEED);
600 CONVERT_PARAM(PARAM_ANIM_OFFSET);
601
602 #undef CONVERT_PARAM
603 }
604
605 void GPUParticles2D::_notification(int p_what) {
606 switch (p_what) {
607 case NOTIFICATION_DRAW: {
608 RID texture_rid;
609 Size2 size;
610 if (texture.is_valid()) {
611 texture_rid = texture->get_rid();
612 size = texture->get_size();
613 } else {
614 size = Size2(1, 1);
615 }
616
617 if (trail_enabled) {
618 RS::get_singleton()->mesh_clear(mesh);
619 PackedVector2Array points;
620 PackedVector2Array uvs;
621 PackedInt32Array bone_indices;
622 PackedFloat32Array bone_weights;
623 PackedInt32Array indices;
624
625 int total_segments = trail_sections * trail_section_subdivisions;
626 real_t depth = size.height * trail_sections;
627
628 for (int j = 0; j <= total_segments; j++) {
629 real_t v = j;
630 v /= total_segments;
631
632 real_t y = depth * v;
633 y = (depth * 0.5) - y;
634
635 int bone = j / trail_section_subdivisions;
636 real_t blend = 1.0 - real_t(j % trail_section_subdivisions) / real_t(trail_section_subdivisions);
637
638 real_t s = size.width;
639
640 points.push_back(Vector2(-s * 0.5, 0));
641 points.push_back(Vector2(+s * 0.5, 0));
642
643 uvs.push_back(Vector2(0, v));
644 uvs.push_back(Vector2(1, v));
645
646 for (int i = 0; i < 2; i++) {
647 bone_indices.push_back(bone);
648 bone_indices.push_back(MIN(trail_sections, bone + 1));
649 bone_indices.push_back(0);
650 bone_indices.push_back(0);
651
652 bone_weights.push_back(blend);
653 bone_weights.push_back(1.0 - blend);
654 bone_weights.push_back(0);
655 bone_weights.push_back(0);
656 }
657
658 if (j > 0) {
659 int base = j * 2 - 2;
660 indices.push_back(base + 0);
661 indices.push_back(base + 1);
662 indices.push_back(base + 2);
663
664 indices.push_back(base + 1);
665 indices.push_back(base + 3);
666 indices.push_back(base + 2);
667 }
668 }
669
670 Array arr;
671 arr.resize(RS::ARRAY_MAX);
672 arr[RS::ARRAY_VERTEX] = points;
673 arr[RS::ARRAY_TEX_UV] = uvs;
674 arr[RS::ARRAY_BONES] = bone_indices;
675 arr[RS::ARRAY_WEIGHTS] = bone_weights;
676 arr[RS::ARRAY_INDEX] = indices;
677
678 RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
679
680 Vector<Transform3D> xforms;
681 for (int i = 0; i <= trail_sections; i++) {
682 Transform3D xform;
683 /*
684 xform.origin.y = depth / 2.0 - size.height * real_t(i);
685 xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y */
686 xforms.push_back(xform);
687 }
688
689 RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
690
691 } else {
692 RS::get_singleton()->mesh_clear(mesh);
693
694 Vector<Vector2> points = {
695 Vector2(-size.x / 2.0, -size.y / 2.0),
696 Vector2(size.x / 2.0, -size.y / 2.0),
697 Vector2(size.x / 2.0, size.y / 2.0),
698 Vector2(-size.x / 2.0, size.y / 2.0)
699 };
700
701 Vector<Vector2> uvs;
702 AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
703 if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
704 Rect2 region_rect = atlas_texture->get_region();
705 Size2 atlas_size = atlas_texture->get_atlas()->get_size();
706 uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
707 uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
708 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));
709 uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
710 } else {
711 uvs.push_back(Vector2(0, 0));
712 uvs.push_back(Vector2(1, 0));
713 uvs.push_back(Vector2(1, 1));
714 uvs.push_back(Vector2(0, 1));
715 }
716
717 Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
718
719 Array arr;
720 arr.resize(RS::ARRAY_MAX);
721 arr[RS::ARRAY_VERTEX] = points;
722 arr[RS::ARRAY_TEX_UV] = uvs;
723 arr[RS::ARRAY_INDEX] = indices;
724
725 RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
726 RS::get_singleton()->particles_set_trail_bind_poses(particles, Vector<Transform3D>());
727 }
728 RS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid);
729
730 #ifdef TOOLS_ENABLED
731 if (show_gizmos) {
732 draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
733 _draw_emission_gizmo();
734 }
735 #endif
736 } break;
737
738 case NOTIFICATION_ENTER_TREE: {
739 if (sub_emitter != NodePath()) {
740 _attach_sub_emitter();
741 }
742 if (can_process()) {
743 RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
744 } else {
745 RS::get_singleton()->particles_set_speed_scale(particles, 0);
746 }
747 set_process_internal(true);
748 set_physics_process_internal(true);
749 previous_position = get_global_position();
750 } break;
751
752 case NOTIFICATION_EXIT_TREE: {
753 RS::get_singleton()->particles_set_subemitter(particles, RID());
754 } break;
755
756 case NOTIFICATION_SUSPENDED:
757 case NOTIFICATION_UNSUSPENDED:
758 case NOTIFICATION_PAUSED:
759 case NOTIFICATION_UNPAUSED: {
760 if (is_inside_tree()) {
761 if (can_process()) {
762 RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
763 } else {
764 RS::get_singleton()->particles_set_speed_scale(particles, 0);
765 }
766 }
767 } break;
768
769 case NOTIFICATION_TRANSFORM_CHANGED: {
770 _update_particle_emission_transform();
771 } break;
772
773 case NOTIFICATION_INTERNAL_PROCESS: {
774 if (one_shot) {
775 time += get_process_delta_time();
776 if (time > emission_time) {
777 emitting = false;
778 if (!active) {
779 set_process_internal(false);
780 }
781 }
782 if (time > active_time) {
783 if (active && !signal_canceled) {
784 emit_signal(SceneStringName(finished));
785 }
786 active = false;
787 if (!emitting) {
788 set_process_internal(false);
789 }
790 }
791 }
792 } break;
793
794 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
795 // Update velocity in physics process, so that velocity calculations remain correct
796 // if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
797 const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
798 get_physics_process_delta_time();
799
800 if (velocity != previous_velocity) {
801 RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
802 previous_velocity = velocity;
803 }
804 previous_position = get_global_position();
805 } break;
806 }
807 }
808
809 #ifdef TOOLS_ENABLED
810 void GPUParticles2D::_draw_emission_gizmo() {
811 Ref<ParticleProcessMaterial> pm = process_material;
812 Color emission_ring_color = Color(0.8, 0.7, 0.4, 0.4);
813 if (pm.is_null()) {
814 return;
815 }
816 draw_set_transform(
817 Vector2(pm->get_emission_shape_offset().x, pm->get_emission_shape_offset().y),
818 0.0,
819 Vector2(pm->get_emission_shape_scale().x, pm->get_emission_shape_scale().y));
820
821 switch (pm->get_emission_shape()) {
822 case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_BOX: {
823 Vector2 extents2d = Vector2(pm->get_emission_box_extents().x, pm->get_emission_box_extents().y);
824 draw_rect(Rect2(-extents2d, extents2d * 2.0), emission_ring_color, false);
825 break;
826 }
827 case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE:
828 case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE_SURFACE: {
829 draw_circle(Vector2(), pm->get_emission_sphere_radius(), emission_ring_color, false);
830 break;
831 }
832 case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_RING: {
833 Vector3 ring_axis = pm->get_emission_ring_axis();
834 if (ring_axis.is_equal_approx(Vector3(0.0, 0.0, 1.0)) || ring_axis.is_zero_approx()) {
835 draw_circle(Vector2(), pm->get_emission_ring_inner_radius(), emission_ring_color, false);
836 draw_circle(Vector2(), pm->get_emission_ring_radius(), emission_ring_color, false);
837 } else {
838 Vector2 a = Vector2(pm->get_emission_ring_height() / -2.0, pm->get_emission_ring_radius() / -1.0);
839 Vector2 b = Vector2(-a.x, MIN(a.y + std::tan((90.0 - pm->get_emission_ring_cone_angle()) * 0.01745329) * pm->get_emission_ring_height(), 0.0));
840 Vector2 c = Vector2(b.x, -b.y);
841 Vector2 d = Vector2(a.x, -a.y);
842 if (ring_axis.is_equal_approx(Vector3(1.0, 0.0, 0.0))) {
843 Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
844 draw_multiline(pos, emission_ring_color);
845 } else if (ring_axis.is_equal_approx(Vector3(0.0, 1.0, 0.0))) {
846 a = Vector2(a.y, a.x);
847 b = Vector2(b.y, b.x);
848 c = Vector2(c.y, c.x);
849 d = Vector2(d.y, d.x);
850 Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
851 draw_multiline(pos, emission_ring_color);
852 }
853 }
854 break;
855 }
856 default: {
857 break;
858 }
859 }
860 }
861 #endif
862
863 void GPUParticles2D::_bind_methods() {
864 ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles2D::set_emitting);
865 ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles2D::set_amount);
866 ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles2D::set_lifetime);
867 ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &GPUParticles2D::set_one_shot);
868 ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles2D::set_pre_process_time);
869 ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles2D::set_explosiveness_ratio);
870 ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles2D::set_randomness_ratio);
871 ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &GPUParticles2D::set_visibility_rect);
872 ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles2D::set_use_local_coordinates);
873 ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles2D::set_fixed_fps);
874 ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles2D::set_fractional_delta);
875 ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles2D::set_interpolate);
876 ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles2D::set_process_material);
877 ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles2D::set_speed_scale);
878 ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles2D::set_collision_base_size);
879 ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles2D::set_interp_to_end);
880
881 ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &GPUParticles2D::request_particles_process);
882
883 ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles2D::is_emitting);
884 ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles2D::get_amount);
885 ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles2D::get_lifetime);
886 ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles2D::get_one_shot);
887 ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles2D::get_pre_process_time);
888 ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles2D::get_explosiveness_ratio);
889 ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles2D::get_randomness_ratio);
890 ClassDB::bind_method(D_METHOD("get_visibility_rect"), &GPUParticles2D::get_visibility_rect);
891 ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles2D::get_use_local_coordinates);
892 ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles2D::get_fixed_fps);
893 ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles2D::get_fractional_delta);
894 ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles2D::get_interpolate);
895 ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles2D::get_process_material);
896 ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles2D::get_speed_scale);
897 ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles2D::get_collision_base_size);
898 ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles2D::get_interp_to_end);
899
900 ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles2D::set_draw_order);
901 ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles2D::get_draw_order);
902
903 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticles2D::set_texture);
904 ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticles2D::get_texture);
905
906 ClassDB::bind_method(D_METHOD("capture_rect"), &GPUParticles2D::capture_rect);
907
908 ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &GPUParticles2D::restart, DEFVAL(false));
909
910 ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles2D::set_sub_emitter);
911 ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles2D::get_sub_emitter);
912
913 ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles2D::emit_particle);
914
915 ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles2D::set_trail_enabled);
916 ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles2D::set_trail_lifetime);
917
918 ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles2D::is_trail_enabled);
919 ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles2D::get_trail_lifetime);
920
921 ClassDB::bind_method(D_METHOD("set_trail_sections", "sections"), &GPUParticles2D::set_trail_sections);
922 ClassDB::bind_method(D_METHOD("get_trail_sections"), &GPUParticles2D::get_trail_sections);
923
924 ClassDB::bind_method(D_METHOD("set_trail_section_subdivisions", "subdivisions"), &GPUParticles2D::set_trail_section_subdivisions);
925 ClassDB::bind_method(D_METHOD("get_trail_section_subdivisions"), &GPUParticles2D::get_trail_section_subdivisions);
926
927 ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles2D::convert_from_particles);
928
929 ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles2D::set_amount_ratio);
930 ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles2D::get_amount_ratio);
931
932 ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &GPUParticles2D::set_use_fixed_seed);
933 ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &GPUParticles2D::get_use_fixed_seed);
934
935 ClassDB::bind_method(D_METHOD("set_seed", "seed"), &GPUParticles2D::set_seed);
936 ClassDB::bind_method(D_METHOD("get_seed"), &GPUParticles2D::get_seed);
937
938 ADD_SIGNAL(MethodInfo("finished"));
939
940 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
941 ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
942 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.
943 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
944 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles2D"), "set_sub_emitter", "get_sub_emitter");
945 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
946 ADD_GROUP("Time", "");
947 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
948 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interp_to_end", PROPERTY_HINT_RANGE, "0.00,1.0,0.001"), "set_interp_to_end", "get_interp_to_end");
949 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
950 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");
951 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
952 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
953 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
954 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed", PROPERTY_HINT_ENUM), "set_use_fixed_seed", "get_use_fixed_seed");
955 ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
956 ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
957 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
958 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
959 ADD_GROUP("Collision", "collision_");
960 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_base_size", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_collision_base_size", "get_collision_base_size");
961 ADD_GROUP("Drawing", "");
962 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_visibility_rect", "get_visibility_rect");
963 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
964 ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime"), "set_draw_order", "get_draw_order");
965 ADD_GROUP("Trails", "trail_");
966 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_trail_enabled", "is_trail_enabled");
967 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trail_lifetime", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater,suffix:s"), "set_trail_lifetime", "get_trail_lifetime");
968 ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_trail_sections", "get_trail_sections");
969 ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_section_subdivisions", PROPERTY_HINT_RANGE, "1,1024,1"), "set_trail_section_subdivisions", "get_trail_section_subdivisions");
970 ADD_GROUP("Process Material", "");
971 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
972 BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
973 BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
974 BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
975
976 BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
977 BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
978 BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
979 BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
980 BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
981
982 ADD_PROPERTY_DEFAULT("seed", 0);
983 }
984
985 GPUParticles2D::GPUParticles2D() {
986 particles = RS::get_singleton()->particles_create();
987 RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_2D);
988
989 mesh = RS::get_singleton()->mesh_create();
990 RS::get_singleton()->particles_set_draw_passes(particles, 1);
991 RS::get_singleton()->particles_set_draw_pass_mesh(particles, 0, mesh);
992
993 one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
994 set_emitting(true);
995 set_one_shot(false);
996 set_seed(Math::rand());
997 set_use_fixed_seed(false);
998 set_amount(8);
999 set_amount_ratio(1.0);
1000 set_lifetime(1);
1001 set_fixed_fps(0);
1002 set_fractional_delta(true);
1003 set_interpolate(true);
1004 set_pre_process_time(0);
1005 set_explosiveness_ratio(0);
1006 set_randomness_ratio(0);
1007 set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
1008 set_use_local_coordinates(false);
1009 set_draw_order(DRAW_ORDER_LIFETIME);
1010 set_speed_scale(1);
1011 set_fixed_fps(30);
1012 set_collision_base_size(collision_base_size);
1013 }
1014
1015 GPUParticles2D::~GPUParticles2D() {
1016 ERR_FAIL_NULL(RenderingServer::get_singleton());
1017 RS::get_singleton()->free_rid(particles);
1018 RS::get_singleton()->free_rid(mesh);
1019 }