Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/cpu_particles_2d.cpp
20959 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_ANGULAR_VELOCITY:
400
case PARAM_ORBIT_VELOCITY:
401
case PARAM_LINEAR_ACCEL:
402
case PARAM_RADIAL_ACCEL:
403
case PARAM_TANGENTIAL_ACCEL:
404
case PARAM_ANGLE:
405
case PARAM_HUE_VARIATION: {
406
_adjust_curve_range(p_curve, -1, 1);
407
} break;
408
case PARAM_DAMPING:
409
case PARAM_SCALE:
410
case PARAM_ANIM_SPEED:
411
case PARAM_ANIM_OFFSET: {
412
_adjust_curve_range(p_curve, 0, 1);
413
} break;
414
case PARAM_INITIAL_LINEAR_VELOCITY:
415
case PARAM_MAX: {
416
// No curve available.
417
} break;
418
}
419
420
update_configuration_warnings();
421
}
422
423
Ref<Curve> CPUParticles2D::get_param_curve(Parameter p_param) const {
424
ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
425
426
return curve_parameters[p_param];
427
}
428
429
void CPUParticles2D::set_color(const Color &p_color) {
430
color = p_color;
431
}
432
433
Color CPUParticles2D::get_color() const {
434
return color;
435
}
436
437
void CPUParticles2D::set_color_ramp(const Ref<Gradient> &p_ramp) {
438
color_ramp = p_ramp;
439
}
440
441
Ref<Gradient> CPUParticles2D::get_color_ramp() const {
442
return color_ramp;
443
}
444
445
void CPUParticles2D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
446
color_initial_ramp = p_ramp;
447
}
448
449
Ref<Gradient> CPUParticles2D::get_color_initial_ramp() const {
450
return color_initial_ramp;
451
}
452
453
void CPUParticles2D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
454
ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
455
particle_flags[p_particle_flag] = p_enable;
456
}
457
458
bool CPUParticles2D::get_particle_flag(ParticleFlags p_particle_flag) const {
459
ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
460
return particle_flags[p_particle_flag];
461
}
462
463
void CPUParticles2D::set_emission_shape(EmissionShape p_shape) {
464
ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
465
emission_shape = p_shape;
466
notify_property_list_changed();
467
#ifdef TOOLS_ENABLED
468
if (Engine::get_singleton()->is_editor_hint()) {
469
queue_redraw();
470
}
471
#endif
472
}
473
474
void CPUParticles2D::set_emission_sphere_radius(real_t p_radius) {
475
if (p_radius == emission_sphere_radius) {
476
return;
477
}
478
emission_sphere_radius = p_radius;
479
#ifdef TOOLS_ENABLED
480
if (Engine::get_singleton()->is_editor_hint()) {
481
queue_redraw();
482
}
483
#endif
484
}
485
486
void CPUParticles2D::set_emission_rect_extents(Vector2 p_extents) {
487
if (p_extents == emission_rect_extents) {
488
return;
489
}
490
emission_rect_extents = p_extents;
491
#ifdef TOOLS_ENABLED
492
if (Engine::get_singleton()->is_editor_hint()) {
493
queue_redraw();
494
}
495
#endif
496
}
497
498
void CPUParticles2D::set_emission_points(const Vector<Vector2> &p_points) {
499
emission_points = p_points;
500
}
501
502
void CPUParticles2D::set_emission_normals(const Vector<Vector2> &p_normals) {
503
emission_normals = p_normals;
504
}
505
506
void CPUParticles2D::set_emission_colors(const Vector<Color> &p_colors) {
507
emission_colors = p_colors;
508
}
509
510
void CPUParticles2D::set_emission_ring_inner_radius(real_t p_inner_radius) {
511
emission_ring_inner_radius = p_inner_radius;
512
}
513
514
void CPUParticles2D::set_emission_ring_radius(real_t p_ring_radius) {
515
emission_ring_radius = p_ring_radius;
516
}
517
518
real_t CPUParticles2D::get_emission_sphere_radius() const {
519
return emission_sphere_radius;
520
}
521
522
Vector2 CPUParticles2D::get_emission_rect_extents() const {
523
return emission_rect_extents;
524
}
525
526
Vector<Vector2> CPUParticles2D::get_emission_points() const {
527
return emission_points;
528
}
529
530
Vector<Vector2> CPUParticles2D::get_emission_normals() const {
531
return emission_normals;
532
}
533
534
Vector<Color> CPUParticles2D::get_emission_colors() const {
535
return emission_colors;
536
}
537
538
real_t CPUParticles2D::get_emission_ring_inner_radius() const {
539
return emission_ring_inner_radius;
540
}
541
542
real_t CPUParticles2D::get_emission_ring_radius() const {
543
return emission_ring_radius;
544
}
545
546
CPUParticles2D::EmissionShape CPUParticles2D::get_emission_shape() const {
547
return emission_shape;
548
}
549
550
void CPUParticles2D::set_gravity(const Vector2 &p_gravity) {
551
gravity = p_gravity;
552
}
553
554
Vector2 CPUParticles2D::get_gravity() const {
555
return gravity;
556
}
557
558
void CPUParticles2D::set_scale_curve_x(Ref<Curve> p_scale_curve) {
559
scale_curve_x = p_scale_curve;
560
}
561
562
void CPUParticles2D::set_scale_curve_y(Ref<Curve> p_scale_curve) {
563
scale_curve_y = p_scale_curve;
564
}
565
566
void CPUParticles2D::set_split_scale(bool p_split_scale) {
567
split_scale = p_split_scale;
568
notify_property_list_changed();
569
}
570
571
Ref<Curve> CPUParticles2D::get_scale_curve_x() const {
572
return scale_curve_x;
573
}
574
575
Ref<Curve> CPUParticles2D::get_scale_curve_y() const {
576
return scale_curve_y;
577
}
578
579
bool CPUParticles2D::get_split_scale() {
580
return split_scale;
581
}
582
583
void CPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
584
if (p_use_fixed_seed == use_fixed_seed) {
585
return;
586
}
587
use_fixed_seed = p_use_fixed_seed;
588
notify_property_list_changed();
589
}
590
591
bool CPUParticles2D::get_use_fixed_seed() const {
592
return use_fixed_seed;
593
}
594
595
void CPUParticles2D::set_seed(uint32_t p_seed) {
596
seed = p_seed;
597
}
598
599
#ifdef TOOLS_ENABLED
600
void CPUParticles2D::set_show_gizmos(bool p_show_gizmos) {
601
if (show_gizmos == p_show_gizmos) {
602
return;
603
}
604
show_gizmos = p_show_gizmos;
605
queue_redraw();
606
}
607
#endif
608
609
uint32_t CPUParticles2D::get_seed() const {
610
return seed;
611
}
612
613
void CPUParticles2D::request_particles_process(real_t p_requested_process_time) {
614
_requested_process_time = p_requested_process_time;
615
}
616
617
void CPUParticles2D::_validate_property(PropertyInfo &p_property) const {
618
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") {
619
p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
620
}
621
622
if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
623
p_property.usage = PROPERTY_USAGE_NONE;
624
}
625
626
if (p_property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
627
p_property.usage = PROPERTY_USAGE_NONE;
628
}
629
630
if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
631
p_property.usage = PROPERTY_USAGE_NONE;
632
}
633
634
if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
635
p_property.usage = PROPERTY_USAGE_NONE;
636
}
637
638
if (p_property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
639
p_property.usage = PROPERTY_USAGE_NONE;
640
}
641
642
if (p_property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
643
p_property.usage = PROPERTY_USAGE_NONE;
644
}
645
if (p_property.name.begins_with("scale_curve_") && !split_scale) {
646
p_property.usage = PROPERTY_USAGE_NONE;
647
}
648
649
if (p_property.name == "emission_ring_inner_radius" && emission_shape != EMISSION_SHAPE_RING) {
650
p_property.usage = PROPERTY_USAGE_NONE;
651
}
652
if (p_property.name == "emission_ring_radius" && emission_shape != EMISSION_SHAPE_RING) {
653
p_property.usage = PROPERTY_USAGE_NONE;
654
}
655
656
if (p_property.name == "seed" && !use_fixed_seed) {
657
p_property.usage = PROPERTY_USAGE_NONE;
658
}
659
}
660
661
static uint32_t idhash(uint32_t x) {
662
x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
663
x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
664
x = (x >> uint32_t(16)) ^ x;
665
return x;
666
}
667
668
static real_t rand_from_seed(uint32_t &seed) {
669
int k;
670
int s = int(seed);
671
if (s == 0) {
672
s = 305420679;
673
}
674
k = s / 127773;
675
s = 16807 * (s - k * 127773) - 2836 * k;
676
if (s < 0) {
677
s += 2147483647;
678
}
679
seed = uint32_t(s);
680
return (seed % uint32_t(65536)) / 65535.0;
681
}
682
683
void CPUParticles2D::_update_internal() {
684
if (particles.is_empty() || !is_visible_in_tree()) {
685
_set_do_redraw(false);
686
return;
687
}
688
689
// Change update mode?
690
_refresh_interpolation_state();
691
692
double delta = get_process_delta_time();
693
if (!active && !emitting) {
694
set_process_internal(false);
695
_set_do_redraw(false);
696
697
//reset variables
698
time = 0;
699
frame_remainder = 0;
700
cycle = 0;
701
return;
702
}
703
_set_do_redraw(true);
704
double frame_time;
705
if (fixed_fps > 0) {
706
frame_time = 1.0 / fixed_fps;
707
} else {
708
frame_time = 1.0 / 30.0;
709
}
710
double todo = _requested_process_time;
711
_requested_process_time = 0;
712
if (time == 0 && pre_process_time > 0.0) {
713
todo += pre_process_time;
714
}
715
real_t tmp_speed = speed_scale;
716
speed_scale = 1.0;
717
while (todo > 0) {
718
_particles_process(frame_time);
719
todo -= frame_time;
720
}
721
speed_scale = tmp_speed;
722
723
todo = 0.0;
724
725
if (fixed_fps > 0) {
726
double decr = frame_time;
727
728
double ldelta = delta;
729
if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
730
ldelta = 0.1;
731
} else if (ldelta <= 0.0) { //unlikely but..
732
ldelta = 0.001;
733
}
734
todo = frame_remainder + ldelta;
735
736
while (todo >= frame_time) {
737
_particles_process(frame_time);
738
todo -= decr;
739
}
740
frame_remainder = todo;
741
742
} else {
743
_particles_process(delta);
744
}
745
746
_update_particle_data_buffer();
747
}
748
749
void CPUParticles2D::_particles_process(double p_delta) {
750
p_delta *= speed_scale;
751
752
int pcount = particles.size();
753
Particle *w = particles.ptrw();
754
755
Particle *parray = w;
756
757
double prev_time = time;
758
time += p_delta;
759
if (time > lifetime) {
760
time = Math::fmod(time, lifetime);
761
cycle++;
762
if (one_shot && cycle > 0) {
763
set_emitting(false);
764
notify_property_list_changed();
765
}
766
}
767
768
Transform2D emission_xform;
769
Transform2D velocity_xform;
770
if (!local_coords) {
771
if (!_interpolation_data.interpolated_follow) {
772
emission_xform = get_global_transform();
773
} else {
774
TransformInterpolator::interpolate_transform_2d(_interpolation_data.global_xform_prev, _interpolation_data.global_xform_curr, emission_xform, Engine::get_singleton()->get_physics_interpolation_fraction());
775
}
776
velocity_xform = emission_xform;
777
velocity_xform[2] = Vector2();
778
}
779
780
double system_phase = time / lifetime;
781
782
bool should_be_active = false;
783
for (int i = 0; i < pcount; i++) {
784
Particle &p = parray[i];
785
786
if (!emitting && !p.active) {
787
continue;
788
}
789
790
double local_delta = p_delta;
791
792
// The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
793
// While we use time in tests later on, for randomness we use the phase as done in the
794
// original shader code, and we later multiply by lifetime to get the time.
795
double restart_phase = double(i) / double(pcount);
796
797
if (randomness_ratio > 0.0) {
798
uint32_t _seed = cycle;
799
if (restart_phase >= system_phase) {
800
_seed -= uint32_t(1);
801
}
802
_seed *= uint32_t(pcount);
803
_seed += uint32_t(i);
804
double random = double(idhash(_seed) % uint32_t(65536)) / 65536.0;
805
restart_phase += randomness_ratio * random * 1.0 / double(pcount);
806
}
807
808
restart_phase *= (1.0 - explosiveness_ratio);
809
double restart_time = restart_phase * lifetime;
810
bool restart = false;
811
812
if (time > prev_time) {
813
// restart_time >= prev_time is used so particles emit in the first frame they are processed
814
815
if (restart_time >= prev_time && restart_time < time) {
816
restart = true;
817
if (fractional_delta) {
818
local_delta = time - restart_time;
819
}
820
}
821
822
} else if (local_delta > 0.0) {
823
if (restart_time >= prev_time) {
824
restart = true;
825
if (fractional_delta) {
826
local_delta = lifetime - restart_time + time;
827
}
828
829
} else if (restart_time < time) {
830
restart = true;
831
if (fractional_delta) {
832
local_delta = time - restart_time;
833
}
834
}
835
}
836
837
if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
838
restart = true;
839
}
840
841
float tv = 0.0;
842
843
if (restart) {
844
if (!emitting) {
845
p.active = false;
846
continue;
847
}
848
p.active = true;
849
850
/*real_t tex_linear_velocity = 0;
851
if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
852
tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0);
853
}*/
854
855
real_t tex_angle = 1.0;
856
if (curve_parameters[PARAM_ANGLE].is_valid()) {
857
tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
858
}
859
860
real_t tex_anim_offset = 1.0;
861
if (curve_parameters[PARAM_ANGLE].is_valid()) {
862
tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv);
863
}
864
865
p.seed = seed + uint32_t(i) + i + cycle;
866
rng->set_seed(p.seed);
867
868
p.angle_rand = rng->randf();
869
p.scale_rand = rng->randf();
870
p.hue_rot_rand = rng->randf();
871
p.anim_offset_rand = rng->randf();
872
873
if (color_initial_ramp.is_valid()) {
874
p.start_color_rand = color_initial_ramp->get_color_at_offset(rng->randf());
875
} else {
876
p.start_color_rand = Color(1, 1, 1, 1);
877
}
878
879
real_t angle1_rad = direction.angle() + Math::deg_to_rad((rng->randf() * 2.0 - 1.0) * spread);
880
Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
881
p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], rng->randf());
882
883
real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
884
p.rotation = Math::deg_to_rad(base_angle);
885
886
p.custom[0] = 0.0; // unused
887
p.custom[1] = 0.0; // phase [0..1]
888
p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand);
889
p.custom[3] = (1.0 - rng->randf() * lifetime_randomness);
890
p.transform = Transform2D();
891
p.time = 0;
892
p.lifetime = lifetime * p.custom[3];
893
p.base_color = Color(1, 1, 1, 1);
894
895
switch (emission_shape) {
896
case EMISSION_SHAPE_POINT: {
897
//do none
898
} break;
899
case EMISSION_SHAPE_SPHERE: {
900
real_t t = Math::TAU * rng->randf();
901
real_t radius = emission_sphere_radius * rng->randf();
902
p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
903
} break;
904
case EMISSION_SHAPE_SPHERE_SURFACE: {
905
real_t s = rng->randf(), t = Math::TAU * rng->randf();
906
real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
907
p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
908
} break;
909
case EMISSION_SHAPE_RECTANGLE: {
910
p.transform[2] = Vector2(rng->randf() * 2.0 - 1.0, rng->randf() * 2.0 - 1.0) * emission_rect_extents;
911
} break;
912
case EMISSION_SHAPE_POINTS:
913
case EMISSION_SHAPE_DIRECTED_POINTS: {
914
int pc = emission_points.size();
915
if (pc == 0) {
916
break;
917
}
918
919
int random_idx = Math::rand() % pc;
920
921
p.transform[2] = emission_points.get(random_idx);
922
923
if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
924
Vector2 normal = emission_normals.get(random_idx);
925
Transform2D m2;
926
m2.columns[0] = normal;
927
m2.columns[1] = normal.orthogonal();
928
p.velocity = m2.basis_xform(p.velocity);
929
}
930
931
if (emission_colors.size() == pc) {
932
p.base_color = emission_colors.get(random_idx);
933
}
934
} break;
935
case EMISSION_SHAPE_RING: {
936
real_t t = Math::TAU * Math::randf();
937
real_t outer_sq = emission_ring_radius * emission_ring_radius;
938
real_t inner_sq = emission_ring_inner_radius * emission_ring_inner_radius;
939
real_t radius = Math::sqrt(Math::randf() * (outer_sq - inner_sq) + inner_sq);
940
p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
941
} break;
942
case EMISSION_SHAPE_MAX: { // Max value for validity check.
943
break;
944
}
945
}
946
947
if (!local_coords) {
948
p.velocity = velocity_xform.xform(p.velocity);
949
p.transform = emission_xform * p.transform;
950
}
951
952
} else if (!p.active) {
953
continue;
954
} else if (p.time > p.lifetime) {
955
p.active = false;
956
tv = 1.0;
957
} else {
958
uint32_t _seed = p.seed;
959
p.time += local_delta;
960
p.custom[1] = p.time / lifetime;
961
tv = p.time / p.lifetime;
962
963
real_t tex_linear_velocity = 1.0;
964
if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
965
tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv);
966
}
967
968
real_t tex_orbit_velocity = 1.0;
969
if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
970
tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv);
971
}
972
973
real_t tex_angular_velocity = 1.0;
974
if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
975
tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv);
976
}
977
978
real_t tex_linear_accel = 1.0;
979
if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
980
tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv);
981
}
982
983
real_t tex_tangential_accel = 1.0;
984
if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
985
tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv);
986
}
987
988
real_t tex_radial_accel = 1.0;
989
if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
990
tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv);
991
}
992
993
real_t tex_damping = 1.0;
994
if (curve_parameters[PARAM_DAMPING].is_valid()) {
995
tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv);
996
}
997
998
real_t tex_angle = 1.0;
999
if (curve_parameters[PARAM_ANGLE].is_valid()) {
1000
tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
1001
}
1002
real_t tex_anim_speed = 1.0;
1003
if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
1004
tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv);
1005
}
1006
1007
real_t tex_anim_offset = 1.0;
1008
if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
1009
tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv);
1010
}
1011
1012
Vector2 force = gravity;
1013
Vector2 pos = p.transform[2];
1014
1015
//apply linear acceleration
1016
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();
1017
//apply radial acceleration
1018
Vector2 org = emission_xform[2];
1019
Vector2 diff = pos - org;
1020
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();
1021
//apply tangential acceleration;
1022
Vector2 yx = Vector2(diff.y, diff.x);
1023
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();
1024
//apply attractor forces
1025
p.velocity += force * local_delta;
1026
//orbit velocity
1027
real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(_seed));
1028
if (orbit_amount != 0.0) {
1029
real_t ang = orbit_amount * local_delta * Math::TAU;
1030
// Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix,
1031
// but we use -ang here to reproduce its behavior.
1032
Transform2D rot = Transform2D(-ang, Vector2());
1033
p.transform[2] -= diff;
1034
p.transform[2] += rot.basis_xform(diff);
1035
}
1036
if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
1037
p.velocity = p.velocity.normalized() * tex_linear_velocity;
1038
}
1039
1040
if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) {
1041
real_t v = p.velocity.length();
1042
real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(_seed));
1043
v -= damp * local_delta;
1044
if (v < 0.0) {
1045
p.velocity = Vector2();
1046
} else {
1047
p.velocity = p.velocity.normalized() * v;
1048
}
1049
}
1050
real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
1051
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));
1052
p.rotation = Math::deg_to_rad(base_angle); //angle
1053
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));
1054
}
1055
//apply color
1056
//apply hue rotation
1057
1058
Vector2 tex_scale = Vector2(1.0, 1.0);
1059
if (split_scale) {
1060
if (scale_curve_x.is_valid()) {
1061
tex_scale.x = scale_curve_x->sample(tv);
1062
} else {
1063
tex_scale.x = 1.0;
1064
}
1065
if (scale_curve_y.is_valid()) {
1066
tex_scale.y = scale_curve_y->sample(tv);
1067
} else {
1068
tex_scale.y = 1.0;
1069
}
1070
} else {
1071
if (curve_parameters[PARAM_SCALE].is_valid()) {
1072
real_t tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv);
1073
tex_scale.x = tmp_scale;
1074
tex_scale.y = tmp_scale;
1075
}
1076
}
1077
1078
real_t tex_hue_variation = 0.0;
1079
if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
1080
tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv);
1081
}
1082
1083
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);
1084
real_t hue_rot_c = Math::cos(hue_rot_angle);
1085
real_t hue_rot_s = Math::sin(hue_rot_angle);
1086
1087
Basis hue_rot_mat;
1088
{
1089
Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
1090
Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
1091
Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
1092
1093
for (int j = 0; j < 3; j++) {
1094
hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
1095
}
1096
}
1097
1098
if (color_ramp.is_valid()) {
1099
p.color = color_ramp->get_color_at_offset(tv) * color;
1100
} else {
1101
p.color = color;
1102
}
1103
1104
Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
1105
p.color.r = color_rgb.x;
1106
p.color.g = color_rgb.y;
1107
p.color.b = color_rgb.z;
1108
1109
p.color *= p.base_color * p.start_color_rand;
1110
1111
if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
1112
if (p.velocity.length() > 0.0) {
1113
p.transform.columns[1] = p.velocity;
1114
}
1115
1116
p.transform.columns[1] = p.transform.columns[1].normalized();
1117
p.transform.columns[0] = p.transform.columns[1].orthogonal();
1118
} else {
1119
p.transform.columns[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
1120
p.transform.columns[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
1121
}
1122
1123
//scale by scale
1124
Vector2 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand);
1125
if (base_scale.x < 0.00001) {
1126
base_scale.x = 0.00001;
1127
}
1128
if (base_scale.y < 0.00001) {
1129
base_scale.y = 0.00001;
1130
}
1131
p.transform.columns[0] *= base_scale.x;
1132
p.transform.columns[1] *= base_scale.y;
1133
1134
p.transform[2] += p.velocity * local_delta;
1135
1136
should_be_active = true;
1137
}
1138
if (!Math::is_equal_approx(time, 0.0) && active && !should_be_active) {
1139
active = false;
1140
emit_signal(SceneStringName(finished));
1141
}
1142
}
1143
1144
void CPUParticles2D::_update_particle_data_buffer() {
1145
MutexLock lock(update_mutex);
1146
1147
int pc = particles.size();
1148
1149
int *ow;
1150
int *order = nullptr;
1151
1152
float *w = particle_data.ptrw();
1153
const Particle *r = particles.ptr();
1154
float *ptr = w;
1155
1156
if (draw_order != DRAW_ORDER_INDEX) {
1157
ow = particle_order.ptrw();
1158
order = ow;
1159
1160
for (int i = 0; i < pc; i++) {
1161
order[i] = i;
1162
}
1163
if (draw_order == DRAW_ORDER_LIFETIME) {
1164
SortArray<int, SortLifetime> sorter;
1165
sorter.compare.particles = r;
1166
sorter.sort(order, pc);
1167
}
1168
}
1169
1170
for (int i = 0; i < pc; i++) {
1171
int idx = order ? order[i] : i;
1172
1173
Transform2D t = r[idx].transform;
1174
1175
if (!local_coords) {
1176
t = inv_emission_transform * t;
1177
}
1178
1179
if (r[idx].active) {
1180
ptr[0] = t.columns[0][0];
1181
ptr[1] = t.columns[1][0];
1182
ptr[2] = 0;
1183
ptr[3] = t.columns[2][0];
1184
ptr[4] = t.columns[0][1];
1185
ptr[5] = t.columns[1][1];
1186
ptr[6] = 0;
1187
ptr[7] = t.columns[2][1];
1188
1189
} else {
1190
memset(ptr, 0, sizeof(float) * 8);
1191
}
1192
1193
Color c = r[idx].color;
1194
1195
ptr[8] = c.r;
1196
ptr[9] = c.g;
1197
ptr[10] = c.b;
1198
ptr[11] = c.a;
1199
1200
ptr[12] = r[idx].custom[0];
1201
ptr[13] = r[idx].custom[1];
1202
ptr[14] = r[idx].custom[2];
1203
ptr[15] = r[idx].custom[3];
1204
1205
ptr += 16;
1206
}
1207
}
1208
1209
void CPUParticles2D::_set_do_redraw(bool p_do_redraw) {
1210
if (do_redraw == p_do_redraw) {
1211
return;
1212
}
1213
do_redraw = p_do_redraw;
1214
1215
{
1216
MutexLock lock(update_mutex);
1217
1218
if (do_redraw) {
1219
RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
1220
RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
1221
1222
RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
1223
} else {
1224
if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread))) {
1225
RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
1226
}
1227
RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
1228
1229
RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
1230
}
1231
}
1232
1233
queue_redraw(); // redraw to update render list
1234
}
1235
1236
void CPUParticles2D::_update_render_thread() {
1237
MutexLock lock(update_mutex);
1238
1239
RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
1240
}
1241
1242
void CPUParticles2D::_notification(int p_what) {
1243
switch (p_what) {
1244
case NOTIFICATION_ENTER_TREE: {
1245
set_process_internal(emitting);
1246
1247
_refresh_interpolation_state();
1248
1249
// If we are interpolated following, then reset physics interpolation
1250
// when first appearing. This won't be called by canvas item, as in the
1251
// following mode, is_physics_interpolated() is actually FALSE.
1252
if (_interpolation_data.interpolated_follow) {
1253
notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
1254
}
1255
} break;
1256
1257
case NOTIFICATION_EXIT_TREE: {
1258
_set_do_redraw(false);
1259
} break;
1260
1261
case NOTIFICATION_DRAW: {
1262
// first update before rendering to avoid one frame delay after emitting starts
1263
if (emitting && (time == 0)) {
1264
_update_internal();
1265
}
1266
1267
if (!do_redraw) {
1268
return; // don't add to render list
1269
}
1270
1271
RID texrid;
1272
if (texture.is_valid()) {
1273
texrid = texture->get_rid();
1274
}
1275
1276
RS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid);
1277
1278
#ifdef TOOLS_ENABLED
1279
if (show_gizmos) {
1280
_draw_emission_gizmo();
1281
}
1282
#endif
1283
1284
} break;
1285
1286
case NOTIFICATION_INTERNAL_PROCESS: {
1287
_update_internal();
1288
} break;
1289
1290
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
1291
if (_interpolation_data.interpolated_follow) {
1292
// Keep the interpolated follow target updated.
1293
_interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
1294
_interpolation_data.global_xform_curr = get_global_transform();
1295
}
1296
} break;
1297
1298
case NOTIFICATION_TRANSFORM_CHANGED: {
1299
if (_interpolation_data.interpolated_follow) {
1300
// If the transform has been updated AFTER the physics tick, keep data flowing.
1301
if (Engine::get_singleton()->is_in_physics_frame()) {
1302
_interpolation_data.global_xform_curr = get_global_transform();
1303
}
1304
}
1305
#ifdef TOOLS_ENABLED
1306
if (!local_coords) {
1307
queue_redraw();
1308
}
1309
#endif
1310
} break;
1311
1312
case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
1313
// Make sure current is up to date with any pending global transform changes.
1314
_interpolation_data.global_xform_curr = get_global_transform_const();
1315
_interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
1316
} break;
1317
}
1318
}
1319
1320
#ifdef TOOLS_ENABLED
1321
void CPUParticles2D::_draw_emission_gizmo() {
1322
Color emission_ring_color = Color(0.8, 0.7, 0.4, 0.4);
1323
Transform2D gizmo_transform;
1324
if (!local_coords) {
1325
gizmo_transform = get_global_transform();
1326
}
1327
1328
draw_set_transform_matrix(gizmo_transform);
1329
1330
switch (emission_shape) {
1331
case CPUParticles2D::EMISSION_SHAPE_RECTANGLE:
1332
draw_rect(Rect2(-emission_rect_extents, emission_rect_extents * 2.0), emission_ring_color, false);
1333
break;
1334
case CPUParticles2D::EMISSION_SHAPE_SPHERE:
1335
case CPUParticles2D::EMISSION_SHAPE_SPHERE_SURFACE:
1336
draw_circle(Vector2(), emission_sphere_radius, emission_ring_color, false);
1337
break;
1338
default:
1339
break;
1340
}
1341
}
1342
#endif
1343
1344
void CPUParticles2D::convert_from_particles(Node *p_particles) {
1345
GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_particles);
1346
ERR_FAIL_NULL_MSG(gpu_particles, "Only GPUParticles2D nodes can be converted to CPUParticles2D.");
1347
1348
set_emitting(gpu_particles->is_emitting());
1349
set_amount(gpu_particles->get_amount());
1350
set_lifetime(gpu_particles->get_lifetime());
1351
set_one_shot(gpu_particles->get_one_shot());
1352
set_pre_process_time(gpu_particles->get_pre_process_time());
1353
set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
1354
set_randomness_ratio(gpu_particles->get_randomness_ratio());
1355
set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
1356
set_fixed_fps(gpu_particles->get_fixed_fps());
1357
set_fractional_delta(gpu_particles->get_fractional_delta());
1358
set_speed_scale(gpu_particles->get_speed_scale());
1359
set_draw_order(DrawOrder(gpu_particles->get_draw_order()));
1360
set_texture(gpu_particles->get_texture());
1361
1362
Ref<Material> mat = gpu_particles->get_material();
1363
if (mat.is_valid()) {
1364
set_material(mat);
1365
}
1366
1367
Ref<ParticleProcessMaterial> proc_mat = gpu_particles->get_process_material();
1368
if (proc_mat.is_null()) {
1369
return;
1370
}
1371
1372
Vector3 dir = proc_mat->get_direction();
1373
set_direction(Vector2(dir.x, dir.y));
1374
set_spread(proc_mat->get_spread());
1375
1376
set_color(proc_mat->get_color());
1377
1378
Ref<GradientTexture1D> gt = proc_mat->get_color_ramp();
1379
if (gt.is_valid()) {
1380
set_color_ramp(gt->get_gradient());
1381
}
1382
1383
Ref<GradientTexture1D> gti = proc_mat->get_color_initial_ramp();
1384
if (gti.is_valid()) {
1385
set_color_initial_ramp(gti->get_gradient());
1386
}
1387
1388
set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, proc_mat->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
1389
1390
set_emission_shape(EmissionShape(proc_mat->get_emission_shape()));
1391
set_emission_sphere_radius(proc_mat->get_emission_sphere_radius());
1392
Vector2 rect_extents = Vector2(proc_mat->get_emission_box_extents().x, proc_mat->get_emission_box_extents().y);
1393
set_emission_rect_extents(rect_extents);
1394
1395
set_emission_ring_radius(proc_mat->get_emission_ring_radius());
1396
set_emission_ring_inner_radius(proc_mat->get_emission_ring_inner_radius());
1397
1398
Ref<CurveXYZTexture> scale3D = proc_mat->get_param_texture(ParticleProcessMaterial::PARAM_SCALE);
1399
if (scale3D.is_valid()) {
1400
split_scale = true;
1401
scale_curve_x = scale3D->get_curve_x();
1402
scale_curve_y = scale3D->get_curve_y();
1403
}
1404
set_gravity(Vector2(proc_mat->get_gravity().x, proc_mat->get_gravity().y));
1405
set_lifetime_randomness(proc_mat->get_lifetime_randomness());
1406
1407
#define CONVERT_PARAM(m_param) \
1408
set_param_min(m_param, proc_mat->get_param_min(ParticleProcessMaterial::m_param)); \
1409
{ \
1410
Ref<CurveTexture> ctex = proc_mat->get_param_texture(ParticleProcessMaterial::m_param); \
1411
if (ctex.is_valid()) \
1412
set_param_curve(m_param, ctex->get_curve()); \
1413
} \
1414
set_param_max(m_param, proc_mat->get_param_max(ParticleProcessMaterial::m_param));
1415
1416
CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
1417
CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
1418
CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
1419
CONVERT_PARAM(PARAM_LINEAR_ACCEL);
1420
CONVERT_PARAM(PARAM_RADIAL_ACCEL);
1421
CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
1422
CONVERT_PARAM(PARAM_DAMPING);
1423
CONVERT_PARAM(PARAM_ANGLE);
1424
CONVERT_PARAM(PARAM_SCALE);
1425
CONVERT_PARAM(PARAM_HUE_VARIATION);
1426
CONVERT_PARAM(PARAM_ANIM_SPEED);
1427
CONVERT_PARAM(PARAM_ANIM_OFFSET);
1428
1429
#undef CONVERT_PARAM
1430
}
1431
1432
void CPUParticles2D::_bind_methods() {
1433
ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
1434
ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
1435
ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
1436
ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
1437
ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
1438
ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
1439
ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
1440
ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
1441
ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
1442
ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
1443
ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
1444
ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
1445
ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &CPUParticles2D::request_particles_process);
1446
1447
ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
1448
ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
1449
ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
1450
ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
1451
ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
1452
ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
1453
ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
1454
ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
1455
ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
1456
ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
1457
ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
1458
ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
1459
ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &CPUParticles2D::set_use_fixed_seed);
1460
ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &CPUParticles2D::get_use_fixed_seed);
1461
1462
ClassDB::bind_method(D_METHOD("set_seed", "seed"), &CPUParticles2D::set_seed);
1463
ClassDB::bind_method(D_METHOD("get_seed"), &CPUParticles2D::get_seed);
1464
1465
ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
1466
1467
ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
1468
1469
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
1470
ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
1471
1472
ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &CPUParticles2D::restart, DEFVAL(false));
1473
1474
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
1475
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.
1476
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture", "get_texture");
1477
ADD_GROUP("Time", "");
1478
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
1479
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
1480
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");
1481
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
1482
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
1483
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
1484
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed"), "set_use_fixed_seed", "get_use_fixed_seed");
1485
ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
1486
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
1487
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
1488
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
1489
ADD_GROUP("Drawing", "");
1490
// No visibility_rect property contrarily to Particles2D, it's updated automatically.
1491
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
1492
ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
1493
1494
BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
1495
BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
1496
1497
ADD_PROPERTY_DEFAULT("seed", 0);
1498
1499
////////////////////////////////
1500
1501
ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
1502
ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
1503
1504
ClassDB::bind_method(D_METHOD("set_spread", "spread"), &CPUParticles2D::set_spread);
1505
ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
1506
1507
ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles2D::set_param_min);
1508
ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles2D::get_param_min);
1509
1510
ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles2D::set_param_max);
1511
ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles2D::get_param_max);
1512
1513
ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
1514
ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
1515
1516
ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
1517
ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
1518
1519
ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
1520
ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
1521
1522
ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
1523
ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);
1524
1525
ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles2D::set_particle_flag);
1526
ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles2D::get_particle_flag);
1527
1528
ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
1529
ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
1530
1531
ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
1532
ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
1533
1534
ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
1535
ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
1536
1537
ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
1538
ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
1539
1540
ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
1541
ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
1542
1543
ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
1544
ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
1545
1546
ClassDB::bind_method(D_METHOD("set_emission_ring_inner_radius", "inner_radius"), &CPUParticles2D::set_emission_ring_inner_radius);
1547
ClassDB::bind_method(D_METHOD("get_emission_ring_inner_radius"), &CPUParticles2D::get_emission_ring_inner_radius);
1548
1549
ClassDB::bind_method(D_METHOD("set_emission_ring_radius", "radius"), &CPUParticles2D::set_emission_ring_radius);
1550
ClassDB::bind_method(D_METHOD("get_emission_ring_radius"), &CPUParticles2D::get_emission_ring_radius);
1551
1552
ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
1553
ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
1554
1555
ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles2D::get_split_scale);
1556
ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles2D::set_split_scale);
1557
1558
ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles2D::get_scale_curve_x);
1559
ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles2D::set_scale_curve_x);
1560
1561
ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles2D::get_scale_curve_y);
1562
ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles2D::set_scale_curve_y);
1563
1564
ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
1565
1566
ADD_SIGNAL(MethodInfo("finished"));
1567
1568
ADD_GROUP("Emission Shape", "emission_");
1569
ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Rectangle,Points,Directed Points,Ring", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
1570
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");
1571
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents", PROPERTY_HINT_NONE, "suffix:px"), "set_emission_rect_extents", "get_emission_rect_extents");
1572
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
1573
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
1574
ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
1575
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_inner_radius"), "set_emission_ring_inner_radius", "get_emission_ring_inner_radius");
1576
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_radius"), "set_emission_ring_radius", "get_emission_ring_radius");
1577
ADD_GROUP("Particle Flags", "particle_flag_");
1578
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
1579
ADD_GROUP("Direction", "");
1580
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
1581
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
1582
ADD_GROUP("Gravity", "");
1583
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity", PROPERTY_HINT_NONE, U"suffix:px/s\u00B2"), "set_gravity", "get_gravity");
1584
ADD_GROUP("Initial Velocity", "initial_");
1585
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);
1586
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);
1587
ADD_GROUP("Angular Velocity", "angular_");
1588
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);
1589
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);
1590
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
1591
ADD_GROUP("Orbit Velocity", "orbit_");
1592
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);
1593
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);
1594
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
1595
ADD_GROUP("Linear Accel", "linear_");
1596
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);
1597
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);
1598
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
1599
ADD_GROUP("Radial Accel", "radial_");
1600
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);
1601
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);
1602
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
1603
ADD_GROUP("Tangential Accel", "tangential_");
1604
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);
1605
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);
1606
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
1607
ADD_GROUP("Damping", "");
1608
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING);
1609
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING);
1610
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_DAMPING);
1611
ADD_GROUP("Angle", "");
1612
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);
1613
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);
1614
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_ANGLE);
1615
ADD_GROUP("Scale", "");
1616
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);
1617
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);
1618
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_SCALE);
1619
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale");
1620
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_scale_curve_x", "get_scale_curve_x");
1621
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_scale_curve_y", "get_scale_curve_y");
1622
1623
ADD_GROUP("Color", "");
1624
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
1625
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_ramp", "get_color_ramp");
1626
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_initial_ramp", "get_color_initial_ramp");
1627
1628
ADD_GROUP("Hue Variation", "hue_");
1629
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION);
1630
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION);
1631
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
1632
ADD_GROUP("Animation", "anim_");
1633
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);
1634
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);
1635
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
1636
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET);
1637
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET);
1638
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
1639
1640
BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
1641
BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
1642
BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
1643
BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
1644
BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
1645
BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
1646
BIND_ENUM_CONSTANT(PARAM_DAMPING);
1647
BIND_ENUM_CONSTANT(PARAM_ANGLE);
1648
BIND_ENUM_CONSTANT(PARAM_SCALE);
1649
BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
1650
BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
1651
BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
1652
BIND_ENUM_CONSTANT(PARAM_MAX);
1653
1654
BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
1655
BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
1656
BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
1657
BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
1658
1659
BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
1660
BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
1661
BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
1662
BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
1663
BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
1664
BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
1665
BIND_ENUM_CONSTANT(EMISSION_SHAPE_RING);
1666
BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
1667
}
1668
1669
CPUParticles2D::CPUParticles2D() {
1670
mesh = RenderingServer::get_singleton()->mesh_create();
1671
multimesh = RenderingServer::get_singleton()->multimesh_create();
1672
RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
1673
1674
set_emitting(true);
1675
set_amount(8);
1676
set_use_local_coordinates(false);
1677
set_seed(Math::rand());
1678
1679
rng.instantiate();
1680
1681
set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1682
set_param_min(PARAM_ANGULAR_VELOCITY, 0);
1683
set_param_min(PARAM_ORBIT_VELOCITY, 0);
1684
set_param_min(PARAM_LINEAR_ACCEL, 0);
1685
set_param_min(PARAM_RADIAL_ACCEL, 0);
1686
set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
1687
set_param_min(PARAM_DAMPING, 0);
1688
set_param_min(PARAM_ANGLE, 0);
1689
set_param_min(PARAM_SCALE, 1);
1690
set_param_min(PARAM_HUE_VARIATION, 0);
1691
set_param_min(PARAM_ANIM_SPEED, 0);
1692
set_param_min(PARAM_ANIM_OFFSET, 0);
1693
1694
set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1695
set_param_max(PARAM_ANGULAR_VELOCITY, 0);
1696
set_param_max(PARAM_ORBIT_VELOCITY, 0);
1697
set_param_max(PARAM_LINEAR_ACCEL, 0);
1698
set_param_max(PARAM_RADIAL_ACCEL, 0);
1699
set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
1700
set_param_max(PARAM_DAMPING, 0);
1701
set_param_max(PARAM_ANGLE, 0);
1702
set_param_max(PARAM_SCALE, 1);
1703
set_param_max(PARAM_HUE_VARIATION, 0);
1704
set_param_max(PARAM_ANIM_SPEED, 0);
1705
set_param_max(PARAM_ANIM_OFFSET, 0);
1706
1707
for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
1708
particle_flags[i] = false;
1709
}
1710
1711
set_color(Color(1, 1, 1, 1));
1712
1713
_update_mesh_texture();
1714
1715
// CPUParticles2D defaults to interpolation off.
1716
// This is because the result often looks better when the particles are updated every frame.
1717
// Note that children will need to explicitly turn back on interpolation if they want to use it,
1718
// rather than relying on inherit mode.
1719
set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
1720
}
1721
1722
CPUParticles2D::~CPUParticles2D() {
1723
ERR_FAIL_NULL(RenderingServer::get_singleton());
1724
RS::get_singleton()->free_rid(multimesh);
1725
RS::get_singleton()->free_rid(mesh);
1726
}
1727
1728