Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/gpu_particles_3d.cpp
20911 views
1
/**************************************************************************/
2
/* gpu_particles_3d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "gpu_particles_3d.h"
32
#include "gpu_particles_3d.compat.inc"
33
34
#include "scene/3d/cpu_particles_3d.h"
35
#include "scene/resources/curve_texture.h"
36
#include "scene/resources/gradient_texture.h"
37
#include "scene/resources/mesh.h"
38
#include "scene/resources/particle_process_material.h"
39
40
AABB GPUParticles3D::get_aabb() const {
41
return AABB();
42
}
43
44
void GPUParticles3D::set_emitting(bool p_emitting) {
45
// Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.
46
if (p_emitting && p_emitting != emitting && !use_fixed_seed && one_shot) {
47
set_seed(Math::rand());
48
}
49
if (p_emitting && one_shot) {
50
if (!active && !emitting) {
51
// Last cycle ended.
52
active = true;
53
time = 0;
54
signal_canceled = false;
55
emission_time = lifetime;
56
active_time = lifetime * (2 - explosiveness_ratio);
57
} else {
58
signal_canceled = true;
59
}
60
set_process_internal(true);
61
} else if (!p_emitting) {
62
if (one_shot) {
63
set_process_internal(true);
64
} else {
65
set_process_internal(false);
66
}
67
} else {
68
set_process_internal(true);
69
}
70
71
emitting = p_emitting;
72
RS::get_singleton()->particles_set_emitting(particles, p_emitting);
73
}
74
75
void GPUParticles3D::set_amount(int p_amount) {
76
ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
77
amount = p_amount;
78
RS::get_singleton()->particles_set_amount(particles, amount);
79
}
80
81
void GPUParticles3D::set_lifetime(double p_lifetime) {
82
ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
83
lifetime = p_lifetime;
84
RS::get_singleton()->particles_set_lifetime(particles, lifetime);
85
}
86
87
void GPUParticles3D::set_interp_to_end(float p_interp) {
88
interp_to_end_factor = CLAMP(p_interp, 0.0, 1.0);
89
RS::get_singleton()->particles_set_interp_to_end(particles, interp_to_end_factor);
90
}
91
92
void GPUParticles3D::set_one_shot(bool p_one_shot) {
93
one_shot = p_one_shot;
94
RS::get_singleton()->particles_set_one_shot(particles, one_shot);
95
96
if (is_emitting()) {
97
if (!one_shot) {
98
RenderingServer::get_singleton()->particles_restart(particles);
99
}
100
}
101
}
102
103
void GPUParticles3D::set_use_fixed_seed(bool p_use_fixed_seed) {
104
if (p_use_fixed_seed == use_fixed_seed) {
105
return;
106
}
107
use_fixed_seed = p_use_fixed_seed;
108
notify_property_list_changed();
109
}
110
111
bool GPUParticles3D::get_use_fixed_seed() const {
112
return use_fixed_seed;
113
}
114
115
void GPUParticles3D::set_seed(uint32_t p_seed) {
116
seed = p_seed;
117
RS::get_singleton()->particles_set_seed(particles, p_seed);
118
}
119
120
uint32_t GPUParticles3D::get_seed() const {
121
return seed;
122
}
123
124
void GPUParticles3D::set_pre_process_time(double p_time) {
125
pre_process_time = p_time;
126
RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
127
}
128
129
void GPUParticles3D::set_explosiveness_ratio(real_t p_ratio) {
130
explosiveness_ratio = p_ratio;
131
RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
132
}
133
134
void GPUParticles3D::set_randomness_ratio(real_t p_ratio) {
135
randomness_ratio = p_ratio;
136
RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
137
}
138
139
void GPUParticles3D::set_visibility_aabb(const AABB &p_aabb) {
140
visibility_aabb = p_aabb;
141
RS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
142
update_gizmos();
143
}
144
145
void GPUParticles3D::set_use_local_coordinates(bool p_enable) {
146
local_coords = p_enable;
147
RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
148
}
149
150
void GPUParticles3D::set_process_material(const Ref<Material> &p_material) {
151
#ifdef TOOLS_ENABLED
152
if (process_material.is_valid()) {
153
if (Ref<ParticleProcessMaterial>(process_material).is_valid()) {
154
process_material->disconnect("emission_shape_changed", callable_mp((Node3D *)this, &GPUParticles3D::update_gizmos));
155
}
156
}
157
#endif
158
159
process_material = p_material;
160
RID material_rid;
161
if (process_material.is_valid()) {
162
material_rid = process_material->get_rid();
163
#ifdef TOOLS_ENABLED
164
if (Ref<ParticleProcessMaterial>(process_material).is_valid()) {
165
process_material->connect("emission_shape_changed", callable_mp((Node3D *)this, &GPUParticles3D::update_gizmos));
166
}
167
#endif
168
}
169
RS::get_singleton()->particles_set_process_material(particles, material_rid);
170
171
update_configuration_warnings();
172
}
173
174
void GPUParticles3D::set_speed_scale(double p_scale) {
175
speed_scale = p_scale;
176
RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
177
}
178
179
void GPUParticles3D::set_collision_base_size(real_t p_size) {
180
collision_base_size = p_size;
181
RS::get_singleton()->particles_set_collision_base_size(particles, p_size);
182
}
183
184
bool GPUParticles3D::is_emitting() const {
185
return emitting;
186
}
187
188
int GPUParticles3D::get_amount() const {
189
return amount;
190
}
191
192
double GPUParticles3D::get_lifetime() const {
193
return lifetime;
194
}
195
196
float GPUParticles3D::get_interp_to_end() const {
197
return interp_to_end_factor;
198
}
199
200
bool GPUParticles3D::get_one_shot() const {
201
return one_shot;
202
}
203
204
double GPUParticles3D::get_pre_process_time() const {
205
return pre_process_time;
206
}
207
208
real_t GPUParticles3D::get_explosiveness_ratio() const {
209
return explosiveness_ratio;
210
}
211
212
real_t GPUParticles3D::get_randomness_ratio() const {
213
return randomness_ratio;
214
}
215
216
AABB GPUParticles3D::get_visibility_aabb() const {
217
return visibility_aabb;
218
}
219
220
bool GPUParticles3D::get_use_local_coordinates() const {
221
return local_coords;
222
}
223
224
Ref<Material> GPUParticles3D::get_process_material() const {
225
return process_material;
226
}
227
228
double GPUParticles3D::get_speed_scale() const {
229
return speed_scale;
230
}
231
232
real_t GPUParticles3D::get_collision_base_size() const {
233
return collision_base_size;
234
}
235
236
void GPUParticles3D::set_draw_order(DrawOrder p_order) {
237
draw_order = p_order;
238
RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
239
}
240
241
void GPUParticles3D::set_trail_enabled(bool p_enabled) {
242
trail_enabled = p_enabled;
243
RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
244
update_configuration_warnings();
245
}
246
247
void GPUParticles3D::set_trail_lifetime(double p_seconds) {
248
ERR_FAIL_COND(p_seconds < 0.01 - CMP_EPSILON);
249
trail_lifetime = p_seconds;
250
RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
251
}
252
253
bool GPUParticles3D::is_trail_enabled() const {
254
return trail_enabled;
255
}
256
257
double GPUParticles3D::get_trail_lifetime() const {
258
return trail_lifetime;
259
}
260
261
GPUParticles3D::DrawOrder GPUParticles3D::get_draw_order() const {
262
return draw_order;
263
}
264
265
void GPUParticles3D::set_draw_passes(int p_count) {
266
ERR_FAIL_COND(p_count < 1);
267
for (int i = p_count; i < draw_passes.size(); i++) {
268
set_draw_pass_mesh(i, Ref<Mesh>());
269
}
270
draw_passes.resize(p_count);
271
RS::get_singleton()->particles_set_draw_passes(particles, p_count);
272
notify_property_list_changed();
273
}
274
275
int GPUParticles3D::get_draw_passes() const {
276
return draw_passes.size();
277
}
278
279
void GPUParticles3D::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
280
ERR_FAIL_INDEX(p_pass, draw_passes.size());
281
282
if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
283
draw_passes.write[p_pass]->disconnect_changed(callable_mp((Node *)this, &Node::update_configuration_warnings));
284
}
285
286
draw_passes.write[p_pass] = p_mesh;
287
288
if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
289
draw_passes.write[p_pass]->connect_changed(callable_mp((Node *)this, &Node::update_configuration_warnings), CONNECT_DEFERRED);
290
}
291
292
RID mesh_rid;
293
if (p_mesh.is_valid()) {
294
mesh_rid = p_mesh->get_rid();
295
}
296
297
RS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
298
299
_skinning_changed();
300
update_configuration_warnings();
301
}
302
303
Ref<Mesh> GPUParticles3D::get_draw_pass_mesh(int p_pass) const {
304
ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
305
306
return draw_passes[p_pass];
307
}
308
309
void GPUParticles3D::set_fixed_fps(int p_count) {
310
fixed_fps = p_count;
311
RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
312
}
313
314
int GPUParticles3D::get_fixed_fps() const {
315
return fixed_fps;
316
}
317
318
void GPUParticles3D::set_fractional_delta(bool p_enable) {
319
fractional_delta = p_enable;
320
RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
321
}
322
323
bool GPUParticles3D::get_fractional_delta() const {
324
return fractional_delta;
325
}
326
327
void GPUParticles3D::set_interpolate(bool p_enable) {
328
interpolate = p_enable;
329
RS::get_singleton()->particles_set_interpolate(particles, p_enable);
330
}
331
332
bool GPUParticles3D::get_interpolate() const {
333
return interpolate;
334
}
335
336
PackedStringArray GPUParticles3D::get_configuration_warnings() const {
337
PackedStringArray warnings = GeometryInstance3D::get_configuration_warnings();
338
339
bool meshes_found = false;
340
bool anim_material_found = false;
341
342
for (int i = 0; i < draw_passes.size(); i++) {
343
if (draw_passes[i].is_valid()) {
344
meshes_found = true;
345
for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
346
anim_material_found = Object::cast_to<ShaderMaterial>(draw_passes[i]->surface_get_material(j).ptr()) != nullptr;
347
BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
348
anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
349
}
350
if (anim_material_found) {
351
break;
352
}
353
}
354
}
355
356
anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr;
357
{
358
BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
359
anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == BaseMaterial3D::BILLBOARD_PARTICLES);
360
}
361
362
if (!meshes_found) {
363
warnings.push_back(RTR("Nothing is visible because meshes have not been assigned to draw passes."));
364
}
365
366
if (process_material.is_null()) {
367
warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
368
} else {
369
const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
370
if (!anim_material_found && process &&
371
(process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
372
process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
373
warnings.push_back(RTR("Particles animation requires the usage of a BaseMaterial3D whose Billboard Mode is set to \"Particle Billboard\"."));
374
}
375
}
376
377
if (trail_enabled) {
378
int dp_count = 0;
379
bool missing_trails = false;
380
bool no_materials = false;
381
382
for (int i = 0; i < draw_passes.size(); i++) {
383
Ref<Mesh> draw_pass = draw_passes[i];
384
if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
385
dp_count++;
386
}
387
388
if (draw_pass.is_valid()) {
389
int mats_found = 0;
390
for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
391
BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
392
if (spat) {
393
mats_found++;
394
}
395
if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
396
missing_trails = true;
397
}
398
}
399
400
if (mats_found != draw_passes[i]->get_surface_count()) {
401
no_materials = true;
402
}
403
}
404
}
405
406
BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
407
if (spat) {
408
no_materials = false;
409
}
410
if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
411
missing_trails = true;
412
}
413
414
if (dp_count && skin.is_valid()) {
415
warnings.push_back(RTR("Using Trail meshes with a skin causes Skin to override Trail poses. Suggest removing the Skin."));
416
} else if (dp_count == 0 && skin.is_null()) {
417
warnings.push_back(RTR("Trails active, but neither Trail meshes or a Skin were found."));
418
} else if (dp_count > 1) {
419
warnings.push_back(RTR("Only one Trail mesh is supported. If you want to use more than a single mesh, a Skin is needed (see documentation)."));
420
}
421
422
if ((dp_count || skin.is_valid()) && (missing_trails || no_materials)) {
423
warnings.push_back(RTR("Trails enabled, but one or more mesh materials are either missing or not set for trails rendering."));
424
}
425
if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy") {
426
warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile renderer."));
427
}
428
}
429
430
if (sub_emitter != NodePath() && (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy")) {
431
warnings.push_back(RTR("Particle sub-emitters are only available when using the Forward+ or Mobile renderer."));
432
}
433
434
return warnings;
435
}
436
437
void GPUParticles3D::restart(bool p_keep_seed) {
438
if (!p_keep_seed && !use_fixed_seed) {
439
set_seed(Math::rand());
440
}
441
RenderingServer::get_singleton()->particles_restart(particles);
442
RenderingServer::get_singleton()->particles_set_emitting(particles, true);
443
444
emitting = true;
445
active = true;
446
signal_canceled = false;
447
time = 0;
448
emission_time = lifetime * (1 - explosiveness_ratio);
449
active_time = lifetime * (2 - explosiveness_ratio);
450
set_process_internal(true);
451
}
452
453
AABB GPUParticles3D::capture_aabb() const {
454
return RS::get_singleton()->particles_get_current_aabb(particles);
455
}
456
457
void GPUParticles3D::_validate_property(PropertyInfo &p_property) const {
458
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") {
459
p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
460
}
461
462
if (p_property.name.begins_with("draw_pass_")) {
463
int index = p_property.name.get_slicec('_', 2).to_int() - 1;
464
if (index >= draw_passes.size()) {
465
p_property.usage = PROPERTY_USAGE_NONE;
466
return;
467
}
468
}
469
if (p_property.name == "seed" && !use_fixed_seed) {
470
p_property.usage = PROPERTY_USAGE_NONE;
471
}
472
}
473
474
void GPUParticles3D::request_particles_process(real_t p_requested_process_time) {
475
RS::get_singleton()->particles_request_process_time(particles, p_requested_process_time);
476
}
477
478
void GPUParticles3D::emit_particle(const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
479
RS::get_singleton()->particles_emit(particles, p_transform, p_velocity, p_color, p_custom, p_emit_flags);
480
}
481
482
void GPUParticles3D::_attach_sub_emitter() {
483
Node *n = get_node_or_null(sub_emitter);
484
if (n) {
485
GPUParticles3D *sen = Object::cast_to<GPUParticles3D>(n);
486
if (sen && sen != this) {
487
RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
488
}
489
}
490
}
491
492
void GPUParticles3D::set_sub_emitter(const NodePath &p_path) {
493
if (is_inside_tree()) {
494
RS::get_singleton()->particles_set_subemitter(particles, RID());
495
}
496
497
sub_emitter = p_path;
498
499
if (is_inside_tree() && sub_emitter != NodePath()) {
500
_attach_sub_emitter();
501
}
502
update_configuration_warnings();
503
}
504
505
NodePath GPUParticles3D::get_sub_emitter() const {
506
return sub_emitter;
507
}
508
509
void GPUParticles3D::_notification(int p_what) {
510
switch (p_what) {
511
// Use internal process when emitting and one_shot is on so that when
512
// the shot ends the editor can properly update.
513
case NOTIFICATION_INTERNAL_PROCESS: {
514
const Vector3 velocity = (get_global_position() - previous_position) / get_process_delta_time();
515
516
if (velocity != previous_velocity) {
517
RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
518
previous_velocity = velocity;
519
}
520
previous_position = get_global_position();
521
522
if (one_shot) {
523
time += get_process_delta_time();
524
if (time > emission_time) {
525
emitting = false;
526
if (!active) {
527
set_process_internal(false);
528
}
529
}
530
if (time > active_time) {
531
if (active && !signal_canceled) {
532
emit_signal(SceneStringName(finished));
533
}
534
active = false;
535
if (!emitting) {
536
set_process_internal(false);
537
}
538
}
539
}
540
} break;
541
542
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
543
// Update velocity in physics process, so that velocity calculations remain correct
544
// if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
545
const Vector3 velocity = (get_global_position() - previous_position) / get_physics_process_delta_time();
546
547
if (velocity != previous_velocity) {
548
RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
549
previous_velocity = velocity;
550
}
551
previous_position = get_global_position();
552
} break;
553
554
case NOTIFICATION_ENTER_TREE: {
555
set_process_internal(false);
556
set_physics_process_internal(false);
557
if (sub_emitter != NodePath()) {
558
_attach_sub_emitter();
559
}
560
if (can_process()) {
561
RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
562
} else {
563
RS::get_singleton()->particles_set_speed_scale(particles, 0);
564
}
565
previous_position = get_global_transform().origin;
566
set_process_internal(true);
567
set_physics_process_internal(true);
568
} break;
569
570
case NOTIFICATION_EXIT_TREE: {
571
RS::get_singleton()->particles_set_subemitter(particles, RID());
572
} break;
573
574
case NOTIFICATION_SUSPENDED:
575
case NOTIFICATION_UNSUSPENDED:
576
case NOTIFICATION_PAUSED:
577
case NOTIFICATION_UNPAUSED: {
578
if (is_inside_tree()) {
579
if (can_process()) {
580
RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
581
} else {
582
RS::get_singleton()->particles_set_speed_scale(particles, 0);
583
}
584
}
585
} break;
586
587
case NOTIFICATION_VISIBILITY_CHANGED: {
588
// Make sure particles are updated before rendering occurs if they were active before.
589
if (is_visible_in_tree() && !RS::get_singleton()->particles_is_inactive(particles)) {
590
RS::get_singleton()->particles_request_process(particles);
591
}
592
} break;
593
}
594
}
595
596
void GPUParticles3D::_skinning_changed() {
597
Vector<Transform3D> xforms;
598
if (skin.is_valid()) {
599
xforms.resize(skin->get_bind_count());
600
for (int i = 0; i < skin->get_bind_count(); i++) {
601
xforms.write[i] = skin->get_bind_pose(i);
602
}
603
} else {
604
for (int i = 0; i < draw_passes.size(); i++) {
605
Ref<Mesh> draw_pass = draw_passes[i];
606
if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
607
xforms.resize(draw_pass->get_builtin_bind_pose_count());
608
for (int j = 0; j < draw_pass->get_builtin_bind_pose_count(); j++) {
609
xforms.write[j] = draw_pass->get_builtin_bind_pose(j);
610
}
611
break;
612
}
613
}
614
}
615
616
RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
617
update_configuration_warnings();
618
}
619
620
void GPUParticles3D::set_skin(const Ref<Skin> &p_skin) {
621
skin = p_skin;
622
_skinning_changed();
623
}
624
625
Ref<Skin> GPUParticles3D::get_skin() const {
626
return skin;
627
}
628
629
void GPUParticles3D::set_transform_align(TransformAlign p_align) {
630
ERR_FAIL_INDEX(uint32_t(p_align), 4);
631
transform_align = p_align;
632
RS::get_singleton()->particles_set_transform_align(particles, RS::ParticlesTransformAlign(transform_align));
633
}
634
635
GPUParticles3D::TransformAlign GPUParticles3D::get_transform_align() const {
636
return transform_align;
637
}
638
639
void GPUParticles3D::convert_from_particles(Node *p_particles) {
640
CPUParticles3D *cpu_particles = Object::cast_to<CPUParticles3D>(p_particles);
641
ERR_FAIL_NULL_MSG(cpu_particles, "Only CPUParticles3D nodes can be converted to GPUParticles3D.");
642
643
set_emitting(cpu_particles->is_emitting());
644
set_amount(cpu_particles->get_amount());
645
set_lifetime(cpu_particles->get_lifetime());
646
set_one_shot(cpu_particles->get_one_shot());
647
set_pre_process_time(cpu_particles->get_pre_process_time());
648
set_explosiveness_ratio(cpu_particles->get_explosiveness_ratio());
649
set_randomness_ratio(cpu_particles->get_randomness_ratio());
650
set_use_local_coordinates(cpu_particles->get_use_local_coordinates());
651
set_fixed_fps(cpu_particles->get_fixed_fps());
652
set_fractional_delta(cpu_particles->get_fractional_delta());
653
set_speed_scale(cpu_particles->get_speed_scale());
654
set_draw_order(DrawOrder(cpu_particles->get_draw_order()));
655
set_draw_pass_mesh(0, cpu_particles->get_mesh());
656
657
Ref<ParticleProcessMaterial> proc_mat = memnew(ParticleProcessMaterial);
658
set_process_material(proc_mat);
659
660
proc_mat->set_direction(cpu_particles->get_direction());
661
proc_mat->set_spread(cpu_particles->get_spread());
662
proc_mat->set_flatness(cpu_particles->get_flatness());
663
proc_mat->set_color(cpu_particles->get_color());
664
665
Ref<Gradient> grad = cpu_particles->get_color_ramp();
666
if (grad.is_valid()) {
667
Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
668
tex->set_gradient(grad);
669
proc_mat->set_color_ramp(tex);
670
}
671
672
Ref<Gradient> grad_init = cpu_particles->get_color_initial_ramp();
673
if (grad_init.is_valid()) {
674
Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
675
tex->set_gradient(grad_init);
676
proc_mat->set_color_initial_ramp(tex);
677
}
678
679
proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
680
proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ROTATE_Y, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_ROTATE_Y));
681
proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_DISABLE_Z));
682
683
proc_mat->set_emission_shape(ParticleProcessMaterial::EmissionShape(cpu_particles->get_emission_shape()));
684
proc_mat->set_emission_sphere_radius(cpu_particles->get_emission_sphere_radius());
685
proc_mat->set_emission_box_extents(cpu_particles->get_emission_box_extents());
686
proc_mat->set_emission_ring_height(cpu_particles->get_emission_ring_height());
687
proc_mat->set_emission_ring_radius(cpu_particles->get_emission_ring_radius());
688
proc_mat->set_emission_ring_inner_radius(cpu_particles->get_emission_ring_inner_radius());
689
proc_mat->set_emission_ring_cone_angle(cpu_particles->get_emission_ring_cone_angle());
690
691
if (cpu_particles->get_split_scale()) {
692
Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
693
scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
694
scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
695
scale3D->set_curve_z(cpu_particles->get_scale_curve_z());
696
proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
697
}
698
699
proc_mat->set_gravity(cpu_particles->get_gravity());
700
proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
701
702
#define CONVERT_PARAM(m_param) \
703
proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles3D::m_param)); \
704
{ \
705
Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles3D::m_param); \
706
if (curve.is_valid()) { \
707
Ref<CurveTexture> tex = memnew(CurveTexture); \
708
tex->set_curve(curve); \
709
proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
710
} \
711
} \
712
proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles3D::m_param));
713
714
CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
715
CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
716
CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
717
CONVERT_PARAM(PARAM_LINEAR_ACCEL);
718
CONVERT_PARAM(PARAM_RADIAL_ACCEL);
719
CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
720
CONVERT_PARAM(PARAM_DAMPING);
721
CONVERT_PARAM(PARAM_ANGLE);
722
CONVERT_PARAM(PARAM_SCALE);
723
CONVERT_PARAM(PARAM_HUE_VARIATION);
724
CONVERT_PARAM(PARAM_ANIM_SPEED);
725
CONVERT_PARAM(PARAM_ANIM_OFFSET);
726
727
#undef CONVERT_PARAM
728
}
729
730
void GPUParticles3D::set_amount_ratio(float p_ratio) {
731
amount_ratio = p_ratio;
732
RS::get_singleton()->particles_set_amount_ratio(particles, p_ratio);
733
}
734
735
float GPUParticles3D::get_amount_ratio() const {
736
return amount_ratio;
737
}
738
739
void GPUParticles3D::_bind_methods() {
740
ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles3D::set_emitting);
741
ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles3D::set_amount);
742
ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles3D::set_lifetime);
743
ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &GPUParticles3D::set_one_shot);
744
ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles3D::set_pre_process_time);
745
ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles3D::set_explosiveness_ratio);
746
ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles3D::set_randomness_ratio);
747
ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &GPUParticles3D::set_visibility_aabb);
748
ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles3D::set_use_local_coordinates);
749
ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles3D::set_fixed_fps);
750
ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles3D::set_fractional_delta);
751
ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles3D::set_interpolate);
752
ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles3D::set_process_material);
753
ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles3D::set_speed_scale);
754
ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles3D::set_collision_base_size);
755
ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles3D::set_interp_to_end);
756
757
ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles3D::is_emitting);
758
ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles3D::get_amount);
759
ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles3D::get_lifetime);
760
ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles3D::get_one_shot);
761
ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles3D::get_pre_process_time);
762
ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles3D::get_explosiveness_ratio);
763
ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles3D::get_randomness_ratio);
764
ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &GPUParticles3D::get_visibility_aabb);
765
ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles3D::get_use_local_coordinates);
766
ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles3D::get_fixed_fps);
767
ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles3D::get_fractional_delta);
768
ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles3D::get_interpolate);
769
ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles3D::get_process_material);
770
ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles3D::get_speed_scale);
771
ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles3D::get_collision_base_size);
772
ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles3D::get_interp_to_end);
773
774
ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &GPUParticles3D::set_use_fixed_seed);
775
ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &GPUParticles3D::get_use_fixed_seed);
776
777
ClassDB::bind_method(D_METHOD("set_seed", "seed"), &GPUParticles3D::set_seed);
778
ClassDB::bind_method(D_METHOD("get_seed"), &GPUParticles3D::get_seed);
779
780
ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles3D::set_draw_order);
781
782
ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles3D::get_draw_order);
783
784
ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &GPUParticles3D::set_draw_passes);
785
ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &GPUParticles3D::set_draw_pass_mesh);
786
787
ClassDB::bind_method(D_METHOD("get_draw_passes"), &GPUParticles3D::get_draw_passes);
788
ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &GPUParticles3D::get_draw_pass_mesh);
789
790
ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GPUParticles3D::set_skin);
791
ClassDB::bind_method(D_METHOD("get_skin"), &GPUParticles3D::get_skin);
792
793
ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &GPUParticles3D::restart, DEFVAL(false));
794
ClassDB::bind_method(D_METHOD("capture_aabb"), &GPUParticles3D::capture_aabb);
795
796
ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles3D::set_sub_emitter);
797
ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles3D::get_sub_emitter);
798
799
ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles3D::emit_particle);
800
801
ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles3D::set_trail_enabled);
802
ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles3D::set_trail_lifetime);
803
804
ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles3D::is_trail_enabled);
805
ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles3D::get_trail_lifetime);
806
807
ClassDB::bind_method(D_METHOD("set_transform_align", "align"), &GPUParticles3D::set_transform_align);
808
ClassDB::bind_method(D_METHOD("get_transform_align"), &GPUParticles3D::get_transform_align);
809
810
ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles3D::convert_from_particles);
811
812
ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles3D::set_amount_ratio);
813
ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles3D::get_amount_ratio);
814
815
ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &GPUParticles3D::request_particles_process);
816
817
ADD_SIGNAL(MethodInfo("finished"));
818
819
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
820
ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
821
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.
822
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
823
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles3D"), "set_sub_emitter", "get_sub_emitter");
824
ADD_GROUP("Time", "");
825
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
826
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interp_to_end", PROPERTY_HINT_RANGE, "0.00,1.0,0.01"), "set_interp_to_end", "get_interp_to_end");
827
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
828
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");
829
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
830
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
831
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
832
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed"), "set_use_fixed_seed", "get_use_fixed_seed");
833
ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
834
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
835
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
836
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
837
838
ADD_GROUP("Collision", "collision_");
839
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_base_size", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,suffix:m"), "set_collision_base_size", "get_collision_base_size");
840
ADD_GROUP("Drawing", "");
841
ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_visibility_aabb", "get_visibility_aabb");
842
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
843
ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime,View Depth"), "set_draw_order", "get_draw_order");
844
ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_align", PROPERTY_HINT_ENUM, "Disabled,Z-Billboard,Y to Velocity,Z-Billboard + Y to Velocity"), "set_transform_align", "get_transform_align");
845
ADD_GROUP("Trails", "trail_");
846
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_trail_enabled", "is_trail_enabled");
847
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trail_lifetime", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater,suffix:s"), "set_trail_lifetime", "get_trail_lifetime");
848
ADD_GROUP("Process Material", "");
849
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
850
ADD_GROUP("Draw Passes", "draw_");
851
ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
852
for (int i = 0; i < MAX_DRAW_PASSES; i++) {
853
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, Mesh::get_class_static()), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
854
}
855
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "draw_skin", PROPERTY_HINT_RESOURCE_TYPE, Skin::get_class_static()), "set_skin", "get_skin");
856
857
BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
858
BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
859
BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
860
BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
861
862
BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
863
BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
864
BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
865
BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
866
BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
867
868
BIND_CONSTANT(MAX_DRAW_PASSES);
869
870
BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_DISABLED);
871
BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD);
872
BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Y_TO_VELOCITY);
873
BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY);
874
875
ADD_PROPERTY_DEFAULT("seed", 0);
876
}
877
878
GPUParticles3D::GPUParticles3D() {
879
particles = RS::get_singleton()->particles_create();
880
RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_3D);
881
set_base(particles);
882
one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
883
set_emitting(true);
884
set_one_shot(false);
885
set_seed(Math::rand());
886
set_amount_ratio(1.0);
887
set_amount(8);
888
set_lifetime(1);
889
set_fixed_fps(30);
890
set_fractional_delta(true);
891
set_interpolate(true);
892
set_pre_process_time(0);
893
set_explosiveness_ratio(0);
894
set_randomness_ratio(0);
895
set_trail_lifetime(0.3);
896
set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
897
set_use_local_coordinates(false);
898
set_draw_passes(1);
899
set_draw_order(DRAW_ORDER_INDEX);
900
set_speed_scale(1);
901
set_collision_base_size(collision_base_size);
902
set_transform_align(TRANSFORM_ALIGN_DISABLED);
903
set_use_fixed_seed(false);
904
}
905
906
GPUParticles3D::~GPUParticles3D() {
907
ERR_FAIL_NULL(RenderingServer::get_singleton());
908
RS::get_singleton()->free_rid(particles);
909
}
910
911