Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/gpu_particles_2d.cpp
9898 views
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 renderers."));
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
Vector2 rect_extents = cpu_particles->get_emission_rect_extents();
561
proc_mat->set_emission_box_extents(Vector3(rect_extents.x, rect_extents.y, 0));
562
563
if (cpu_particles->get_split_scale()) {
564
Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
565
scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
566
scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
567
proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
568
}
569
570
Vector2 gravity = cpu_particles->get_gravity();
571
proc_mat->set_gravity(Vector3(gravity.x, gravity.y, 0));
572
proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
573
574
#define CONVERT_PARAM(m_param) \
575
proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles2D::m_param)); \
576
{ \
577
Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles2D::m_param); \
578
if (curve.is_valid()) { \
579
Ref<CurveTexture> tex = memnew(CurveTexture); \
580
tex->set_curve(curve); \
581
proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
582
} \
583
} \
584
proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles2D::m_param));
585
586
CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
587
CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
588
CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
589
CONVERT_PARAM(PARAM_LINEAR_ACCEL);
590
CONVERT_PARAM(PARAM_RADIAL_ACCEL);
591
CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
592
CONVERT_PARAM(PARAM_DAMPING);
593
CONVERT_PARAM(PARAM_ANGLE);
594
CONVERT_PARAM(PARAM_SCALE);
595
CONVERT_PARAM(PARAM_HUE_VARIATION);
596
CONVERT_PARAM(PARAM_ANIM_SPEED);
597
CONVERT_PARAM(PARAM_ANIM_OFFSET);
598
599
#undef CONVERT_PARAM
600
}
601
602
void GPUParticles2D::_notification(int p_what) {
603
switch (p_what) {
604
case NOTIFICATION_DRAW: {
605
RID texture_rid;
606
Size2 size;
607
if (texture.is_valid()) {
608
texture_rid = texture->get_rid();
609
size = texture->get_size();
610
} else {
611
size = Size2(1, 1);
612
}
613
614
if (trail_enabled) {
615
RS::get_singleton()->mesh_clear(mesh);
616
PackedVector2Array points;
617
PackedVector2Array uvs;
618
PackedInt32Array bone_indices;
619
PackedFloat32Array bone_weights;
620
PackedInt32Array indices;
621
622
int total_segments = trail_sections * trail_section_subdivisions;
623
real_t depth = size.height * trail_sections;
624
625
for (int j = 0; j <= total_segments; j++) {
626
real_t v = j;
627
v /= total_segments;
628
629
real_t y = depth * v;
630
y = (depth * 0.5) - y;
631
632
int bone = j / trail_section_subdivisions;
633
real_t blend = 1.0 - real_t(j % trail_section_subdivisions) / real_t(trail_section_subdivisions);
634
635
real_t s = size.width;
636
637
points.push_back(Vector2(-s * 0.5, 0));
638
points.push_back(Vector2(+s * 0.5, 0));
639
640
uvs.push_back(Vector2(0, v));
641
uvs.push_back(Vector2(1, v));
642
643
for (int i = 0; i < 2; i++) {
644
bone_indices.push_back(bone);
645
bone_indices.push_back(MIN(trail_sections, bone + 1));
646
bone_indices.push_back(0);
647
bone_indices.push_back(0);
648
649
bone_weights.push_back(blend);
650
bone_weights.push_back(1.0 - blend);
651
bone_weights.push_back(0);
652
bone_weights.push_back(0);
653
}
654
655
if (j > 0) {
656
int base = j * 2 - 2;
657
indices.push_back(base + 0);
658
indices.push_back(base + 1);
659
indices.push_back(base + 2);
660
661
indices.push_back(base + 1);
662
indices.push_back(base + 3);
663
indices.push_back(base + 2);
664
}
665
}
666
667
Array arr;
668
arr.resize(RS::ARRAY_MAX);
669
arr[RS::ARRAY_VERTEX] = points;
670
arr[RS::ARRAY_TEX_UV] = uvs;
671
arr[RS::ARRAY_BONES] = bone_indices;
672
arr[RS::ARRAY_WEIGHTS] = bone_weights;
673
arr[RS::ARRAY_INDEX] = indices;
674
675
RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
676
677
Vector<Transform3D> xforms;
678
for (int i = 0; i <= trail_sections; i++) {
679
Transform3D xform;
680
/*
681
xform.origin.y = depth / 2.0 - size.height * real_t(i);
682
xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y */
683
xforms.push_back(xform);
684
}
685
686
RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
687
688
} else {
689
RS::get_singleton()->mesh_clear(mesh);
690
691
Vector<Vector2> points = {
692
Vector2(-size.x / 2.0, -size.y / 2.0),
693
Vector2(size.x / 2.0, -size.y / 2.0),
694
Vector2(size.x / 2.0, size.y / 2.0),
695
Vector2(-size.x / 2.0, size.y / 2.0)
696
};
697
698
Vector<Vector2> uvs;
699
AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
700
if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
701
Rect2 region_rect = atlas_texture->get_region();
702
Size2 atlas_size = atlas_texture->get_atlas()->get_size();
703
uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
704
uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
705
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));
706
uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
707
} else {
708
uvs.push_back(Vector2(0, 0));
709
uvs.push_back(Vector2(1, 0));
710
uvs.push_back(Vector2(1, 1));
711
uvs.push_back(Vector2(0, 1));
712
}
713
714
Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
715
716
Array arr;
717
arr.resize(RS::ARRAY_MAX);
718
arr[RS::ARRAY_VERTEX] = points;
719
arr[RS::ARRAY_TEX_UV] = uvs;
720
arr[RS::ARRAY_INDEX] = indices;
721
722
RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
723
RS::get_singleton()->particles_set_trail_bind_poses(particles, Vector<Transform3D>());
724
}
725
RS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid);
726
727
#ifdef TOOLS_ENABLED
728
if (show_gizmos) {
729
draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
730
_draw_emission_gizmo();
731
}
732
#endif
733
} break;
734
735
case NOTIFICATION_ENTER_TREE: {
736
if (sub_emitter != NodePath()) {
737
_attach_sub_emitter();
738
}
739
if (can_process()) {
740
RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
741
} else {
742
RS::get_singleton()->particles_set_speed_scale(particles, 0);
743
}
744
set_process_internal(true);
745
set_physics_process_internal(true);
746
previous_position = get_global_position();
747
} break;
748
749
case NOTIFICATION_EXIT_TREE: {
750
RS::get_singleton()->particles_set_subemitter(particles, RID());
751
} break;
752
753
case NOTIFICATION_SUSPENDED:
754
case NOTIFICATION_UNSUSPENDED:
755
case NOTIFICATION_PAUSED:
756
case NOTIFICATION_UNPAUSED: {
757
if (is_inside_tree()) {
758
if (can_process()) {
759
RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
760
} else {
761
RS::get_singleton()->particles_set_speed_scale(particles, 0);
762
}
763
}
764
} break;
765
766
case NOTIFICATION_TRANSFORM_CHANGED: {
767
_update_particle_emission_transform();
768
} break;
769
770
case NOTIFICATION_INTERNAL_PROCESS: {
771
if (one_shot) {
772
time += get_process_delta_time();
773
if (time > emission_time) {
774
emitting = false;
775
if (!active) {
776
set_process_internal(false);
777
}
778
}
779
if (time > active_time) {
780
if (active && !signal_canceled) {
781
emit_signal(SceneStringName(finished));
782
}
783
active = false;
784
if (!emitting) {
785
set_process_internal(false);
786
}
787
}
788
}
789
} break;
790
791
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
792
// Update velocity in physics process, so that velocity calculations remain correct
793
// if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
794
const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
795
get_physics_process_delta_time();
796
797
if (velocity != previous_velocity) {
798
RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
799
previous_velocity = velocity;
800
}
801
previous_position = get_global_position();
802
} break;
803
}
804
}
805
806
#ifdef TOOLS_ENABLED
807
void GPUParticles2D::_draw_emission_gizmo() {
808
Ref<ParticleProcessMaterial> pm = process_material;
809
Color emission_ring_color = Color(0.8, 0.7, 0.4, 0.4);
810
if (pm.is_null()) {
811
return;
812
}
813
draw_set_transform(
814
Vector2(pm->get_emission_shape_offset().x, pm->get_emission_shape_offset().y),
815
0.0,
816
Vector2(pm->get_emission_shape_scale().x, pm->get_emission_shape_scale().y));
817
818
switch (pm->get_emission_shape()) {
819
case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_BOX: {
820
Vector2 extents2d = Vector2(pm->get_emission_box_extents().x, pm->get_emission_box_extents().y);
821
draw_rect(Rect2(-extents2d, extents2d * 2.0), emission_ring_color, false);
822
break;
823
}
824
case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE:
825
case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE_SURFACE: {
826
draw_circle(Vector2(), pm->get_emission_sphere_radius(), emission_ring_color, false);
827
break;
828
}
829
case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_RING: {
830
Vector3 ring_axis = pm->get_emission_ring_axis();
831
if (ring_axis.is_equal_approx(Vector3(0.0, 0.0, 1.0)) || ring_axis.is_zero_approx()) {
832
draw_circle(Vector2(), pm->get_emission_ring_inner_radius(), emission_ring_color, false);
833
draw_circle(Vector2(), pm->get_emission_ring_radius(), emission_ring_color, false);
834
} else {
835
Vector2 a = Vector2(pm->get_emission_ring_height() / -2.0, pm->get_emission_ring_radius() / -1.0);
836
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));
837
Vector2 c = Vector2(b.x, -b.y);
838
Vector2 d = Vector2(a.x, -a.y);
839
if (ring_axis.is_equal_approx(Vector3(1.0, 0.0, 0.0))) {
840
Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
841
draw_multiline(pos, emission_ring_color);
842
} else if (ring_axis.is_equal_approx(Vector3(0.0, 1.0, 0.0))) {
843
a = Vector2(a.y, a.x);
844
b = Vector2(b.y, b.x);
845
c = Vector2(c.y, c.x);
846
d = Vector2(d.y, d.x);
847
Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
848
draw_multiline(pos, emission_ring_color);
849
}
850
}
851
break;
852
}
853
default: {
854
break;
855
}
856
}
857
}
858
#endif
859
860
void GPUParticles2D::_bind_methods() {
861
ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles2D::set_emitting);
862
ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles2D::set_amount);
863
ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles2D::set_lifetime);
864
ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &GPUParticles2D::set_one_shot);
865
ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles2D::set_pre_process_time);
866
ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles2D::set_explosiveness_ratio);
867
ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles2D::set_randomness_ratio);
868
ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &GPUParticles2D::set_visibility_rect);
869
ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles2D::set_use_local_coordinates);
870
ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles2D::set_fixed_fps);
871
ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles2D::set_fractional_delta);
872
ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles2D::set_interpolate);
873
ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles2D::set_process_material);
874
ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles2D::set_speed_scale);
875
ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles2D::set_collision_base_size);
876
ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles2D::set_interp_to_end);
877
878
ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &GPUParticles2D::request_particles_process);
879
880
ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles2D::is_emitting);
881
ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles2D::get_amount);
882
ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles2D::get_lifetime);
883
ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles2D::get_one_shot);
884
ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles2D::get_pre_process_time);
885
ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles2D::get_explosiveness_ratio);
886
ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles2D::get_randomness_ratio);
887
ClassDB::bind_method(D_METHOD("get_visibility_rect"), &GPUParticles2D::get_visibility_rect);
888
ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles2D::get_use_local_coordinates);
889
ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles2D::get_fixed_fps);
890
ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles2D::get_fractional_delta);
891
ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles2D::get_interpolate);
892
ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles2D::get_process_material);
893
ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles2D::get_speed_scale);
894
ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles2D::get_collision_base_size);
895
ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles2D::get_interp_to_end);
896
897
ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles2D::set_draw_order);
898
ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles2D::get_draw_order);
899
900
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticles2D::set_texture);
901
ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticles2D::get_texture);
902
903
ClassDB::bind_method(D_METHOD("capture_rect"), &GPUParticles2D::capture_rect);
904
905
ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &GPUParticles2D::restart, DEFVAL(false));
906
907
ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles2D::set_sub_emitter);
908
ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles2D::get_sub_emitter);
909
910
ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles2D::emit_particle);
911
912
ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles2D::set_trail_enabled);
913
ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles2D::set_trail_lifetime);
914
915
ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles2D::is_trail_enabled);
916
ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles2D::get_trail_lifetime);
917
918
ClassDB::bind_method(D_METHOD("set_trail_sections", "sections"), &GPUParticles2D::set_trail_sections);
919
ClassDB::bind_method(D_METHOD("get_trail_sections"), &GPUParticles2D::get_trail_sections);
920
921
ClassDB::bind_method(D_METHOD("set_trail_section_subdivisions", "subdivisions"), &GPUParticles2D::set_trail_section_subdivisions);
922
ClassDB::bind_method(D_METHOD("get_trail_section_subdivisions"), &GPUParticles2D::get_trail_section_subdivisions);
923
924
ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles2D::convert_from_particles);
925
926
ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles2D::set_amount_ratio);
927
ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles2D::get_amount_ratio);
928
929
ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &GPUParticles2D::set_use_fixed_seed);
930
ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &GPUParticles2D::get_use_fixed_seed);
931
932
ClassDB::bind_method(D_METHOD("set_seed", "seed"), &GPUParticles2D::set_seed);
933
ClassDB::bind_method(D_METHOD("get_seed"), &GPUParticles2D::get_seed);
934
935
ADD_SIGNAL(MethodInfo("finished"));
936
937
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
938
ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
939
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.
940
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
941
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles2D"), "set_sub_emitter", "get_sub_emitter");
942
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
943
ADD_GROUP("Time", "");
944
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
945
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");
946
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
947
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");
948
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
949
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
950
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
951
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed", PROPERTY_HINT_ENUM), "set_use_fixed_seed", "get_use_fixed_seed");
952
ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
953
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
954
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
955
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
956
ADD_GROUP("Collision", "collision_");
957
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");
958
ADD_GROUP("Drawing", "");
959
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_visibility_rect", "get_visibility_rect");
960
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
961
ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime"), "set_draw_order", "get_draw_order");
962
ADD_GROUP("Trails", "trail_");
963
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_trail_enabled", "is_trail_enabled");
964
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");
965
ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_trail_sections", "get_trail_sections");
966
ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_section_subdivisions", PROPERTY_HINT_RANGE, "1,1024,1"), "set_trail_section_subdivisions", "get_trail_section_subdivisions");
967
ADD_GROUP("Process Material", "");
968
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
969
BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
970
BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
971
BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
972
973
BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
974
BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
975
BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
976
BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
977
BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
978
979
ADD_PROPERTY_DEFAULT("seed", 0);
980
}
981
982
GPUParticles2D::GPUParticles2D() {
983
particles = RS::get_singleton()->particles_create();
984
RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_2D);
985
986
mesh = RS::get_singleton()->mesh_create();
987
RS::get_singleton()->particles_set_draw_passes(particles, 1);
988
RS::get_singleton()->particles_set_draw_pass_mesh(particles, 0, mesh);
989
990
one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
991
set_emitting(true);
992
set_one_shot(false);
993
set_seed(Math::rand());
994
set_use_fixed_seed(false);
995
set_amount(8);
996
set_amount_ratio(1.0);
997
set_lifetime(1);
998
set_fixed_fps(0);
999
set_fractional_delta(true);
1000
set_interpolate(true);
1001
set_pre_process_time(0);
1002
set_explosiveness_ratio(0);
1003
set_randomness_ratio(0);
1004
set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
1005
set_use_local_coordinates(false);
1006
set_draw_order(DRAW_ORDER_LIFETIME);
1007
set_speed_scale(1);
1008
set_fixed_fps(30);
1009
set_collision_base_size(collision_base_size);
1010
}
1011
1012
GPUParticles2D::~GPUParticles2D() {
1013
ERR_FAIL_NULL(RenderingServer::get_singleton());
1014
RS::get_singleton()->free(particles);
1015
RS::get_singleton()->free(mesh);
1016
}
1017
1018