|
32 | 32 |
|
33 | 33 | #include "core/object/callable_mp.h" |
34 | 34 | #include "core/os/os.h" |
| 35 | +#include "editor/editor_interface.h" |
35 | 36 | #include "editor/editor_node.h" |
36 | 37 | #include "editor/editor_undo_redo_manager.h" |
37 | 38 | #include "editor/scene/scene_tree_editor.h" |
|
41 | 42 | #include "scene/gui/box_container.h" |
42 | 43 | #include "scene/gui/option_button.h" |
43 | 44 | #include "scene/gui/spin_box.h" |
| 45 | +#include "scene/resources/3d/primitive_meshes.h" |
44 | 46 | #include "scene/resources/image_texture.h" |
45 | 47 | #include "scene/resources/particle_process_material.h" |
46 | 48 |
|
| 49 | +Ref<Mesh> Particles3DEditorPlugin::get_mesh_for_node_created_in_editor() const { |
| 50 | + Ref<QuadMesh> quad_mesh = memnew(QuadMesh); |
| 51 | + quad_mesh->set_size(Vector2(0.1, 0.1)); |
| 52 | + |
| 53 | + // Create material to display billboarded texture that follows the color and scale ramps |
| 54 | + // defined in ParticleProcessMaterial. |
| 55 | + Ref<StandardMaterial3D> material = memnew(StandardMaterial3D); |
| 56 | + material->set_shading_mode(BaseMaterial3D::SHADING_MODE_UNSHADED); |
| 57 | + material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA); |
| 58 | + material->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); |
| 59 | + material->set_flag(BaseMaterial3D::FLAG_BILLBOARD_KEEP_SCALE, true); |
| 60 | + material->set_billboard_mode(BaseMaterial3D::BILLBOARD_PARTICLES); |
| 61 | + |
| 62 | + material->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, get_texture_for_object_created_in_editor()); |
| 63 | + quad_mesh->set_material(material); |
| 64 | + |
| 65 | + return quad_mesh; |
| 66 | +} |
| 67 | + |
47 | 68 | void Particles3DEditorPlugin::_generate_aabb() { |
48 | 69 | double time = generate_seconds->get_value(); |
49 | 70 |
|
@@ -316,6 +337,42 @@ Particles3DEditorPlugin::Particles3DEditorPlugin() { |
316 | 337 | emission_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_emission_points)); |
317 | 338 | } |
318 | 339 |
|
| 340 | +void GPUParticles3DEditorPlugin::_notification(int p_what) { |
| 341 | + switch (p_what) { |
| 342 | + case NOTIFICATION_READY: { |
| 343 | + EditorInterface::get_singleton()->connect("object_created_in_editor", callable_mp(this, &GPUParticles3DEditorPlugin::_object_created_in_editor)); |
| 344 | + } break; |
| 345 | + } |
| 346 | +} |
| 347 | + |
| 348 | +void GPUParticles3DEditorPlugin::_object_created_in_editor(Object *p_object) { |
| 349 | + GPUParticles3D *gpu_particles_3d = Object::cast_to<GPUParticles3D>(p_object); |
| 350 | + if (gpu_particles_3d) { |
| 351 | + // Increase the particle amount somewhat to make moving particle systems more visible out of the box. |
| 352 | + gpu_particles_3d->set_amount(30); |
| 353 | + |
| 354 | + // Simulate particles at the render framerate to avoid issues with interpolation. |
| 355 | + // Enabling trails will internally force fixed FPS to 30 anyway (without changing the value of the property). |
| 356 | + gpu_particles_3d->set_fixed_fps(0); |
| 357 | + |
| 358 | + gpu_particles_3d->set_process_material(get_material_for_object_created_in_editor()); |
| 359 | + Ref<ParticleProcessMaterial> process_material = gpu_particles_3d->get_process_material(); |
| 360 | + if (process_material.is_valid()) { |
| 361 | + // Set gravity according to the 3D project settings. |
| 362 | + const float default_gravity = GLOBAL_GET("physics/3d/default_gravity"); |
| 363 | + const Vector3 default_gravity_vector = GLOBAL_GET("physics/3d/default_gravity_vector"); |
| 364 | + process_material->set_gravity(default_gravity_vector * default_gravity); |
| 365 | + |
| 366 | + // Emit particles upwards, so that they are visible even when the particles node is placed on the ground. |
| 367 | + // Adjust for the default gravity, so that particles are emitted upwards even when gravity is set to a high value. |
| 368 | + process_material->set_direction(Vector3(0, 1, 0)); |
| 369 | + process_material->set_param_min(ParticleProcessMaterial::PARAM_INITIAL_LINEAR_VELOCITY, MAX(1.0, default_gravity * 0.4)); |
| 370 | + } |
| 371 | + |
| 372 | + gpu_particles_3d->set_draw_pass_mesh(0, get_mesh_for_node_created_in_editor()); |
| 373 | + } |
| 374 | +} |
| 375 | + |
319 | 376 | Node *GPUParticles3DEditorPlugin::_convert_particles() { |
320 | 377 | GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node); |
321 | 378 |
|
@@ -417,6 +474,57 @@ GPUParticles3DEditorPlugin::GPUParticles3DEditorPlugin() { |
417 | 474 | conversion_option_name = TTR("Convert to CPUParticles3D"); |
418 | 475 | } |
419 | 476 |
|
| 477 | +void CPUParticles3DEditorPlugin::_notification(int p_what) { |
| 478 | + switch (p_what) { |
| 479 | + case NOTIFICATION_READY: { |
| 480 | + EditorInterface::get_singleton()->connect("object_created_in_editor", callable_mp(this, &CPUParticles3DEditorPlugin::_object_created_in_editor)); |
| 481 | + } break; |
| 482 | + } |
| 483 | +} |
| 484 | + |
| 485 | +void CPUParticles3DEditorPlugin::_object_created_in_editor(Object *p_object) { |
| 486 | + CPUParticles3D *cpu_particles_3d = Object::cast_to<CPUParticles3D>(p_object); |
| 487 | + if (cpu_particles_3d) { |
| 488 | + // Increase the particle amount somewhat to make moving particle systems more visible out of the box. |
| 489 | + cpu_particles_3d->set_amount(30); |
| 490 | + |
| 491 | + // Reduce particle spread to make the particle node's rotation easier to notice. |
| 492 | + cpu_particles_3d->set_spread(5.0); |
| 493 | + |
| 494 | + // Randomize initial particle rotation. |
| 495 | + cpu_particles_3d->set_param_min(CPUParticles3D::PARAM_ANGLE, 0.0); |
| 496 | + cpu_particles_3d->set_param_max(CPUParticles3D::PARAM_ANGLE, 360.0); |
| 497 | + |
| 498 | + // Scale particles up and down as the lifetime progresses. |
| 499 | + Ref<Curve> curve = memnew(Curve); |
| 500 | + // The wind-up occurs significantly faster than the fade-out. |
| 501 | + curve->add_point(Vector2(0.0, 0.0)); |
| 502 | + curve->add_point(Vector2(0.1, 1.0)); |
| 503 | + curve->add_point(Vector2(1.0, 0.0)); |
| 504 | + cpu_particles_3d->set_param_curve(CPUParticles3D::PARAM_SCALE, curve); |
| 505 | + |
| 506 | + // Fade particles with transparency as the lifetime progresses. |
| 507 | + Ref<Gradient> gradient = memnew(Gradient); |
| 508 | + // The fade-in occurs significantly faster than the fade-out. |
| 509 | + gradient->set_color(0, Color(1, 1, 1, 0)); |
| 510 | + gradient->set_color(1, Color(1, 1, 1, 0)); |
| 511 | + gradient->add_point(0.1, Color(1, 1, 1, 1)); |
| 512 | + cpu_particles_3d->set_color_ramp(gradient); |
| 513 | + |
| 514 | + // Set gravity according to the 3D project settings. |
| 515 | + const float default_gravity = GLOBAL_GET("physics/3d/default_gravity"); |
| 516 | + const Vector3 default_gravity_vector = GLOBAL_GET("physics/3d/default_gravity_vector"); |
| 517 | + cpu_particles_3d->set_gravity(default_gravity_vector * default_gravity); |
| 518 | + |
| 519 | + // Emit particles upwards, so that they are visible even when the particles node is placed on the ground. |
| 520 | + // Adjust for the default gravity, so that particles are emitted upwards even when gravity is set to a high value. |
| 521 | + cpu_particles_3d->set_direction(Vector3(0, 1, 0)); |
| 522 | + cpu_particles_3d->set_param_min(CPUParticles3D::PARAM_INITIAL_LINEAR_VELOCITY, MAX(1.0, default_gravity * 0.4)); |
| 523 | + |
| 524 | + cpu_particles_3d->set_mesh(get_mesh_for_node_created_in_editor()); |
| 525 | + } |
| 526 | +} |
| 527 | + |
420 | 528 | Node *CPUParticles3DEditorPlugin::_convert_particles() { |
421 | 529 | CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node); |
422 | 530 |
|
|
0 commit comments