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