cpu_particles_3d_editor_plugin.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* cpu_particles_3d_editor_plugin.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. #include "cpu_particles_3d_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/gui/scene_tree_editor.h"
  35. #include "editor/plugins/node_3d_editor_plugin.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "scene/gui/menu_button.h"
  38. void CPUParticles3DEditor::_node_removed(Node *p_node) {
  39. if (p_node == node) {
  40. node = nullptr;
  41. hide();
  42. }
  43. }
  44. void CPUParticles3DEditor::_notification(int p_notification) {
  45. switch (p_notification) {
  46. case NOTIFICATION_ENTER_TREE: {
  47. options->set_icon(get_editor_theme_icon(SNAME("CPUParticles3D")));
  48. } break;
  49. }
  50. }
  51. void CPUParticles3DEditor::_menu_option(int p_option) {
  52. switch (p_option) {
  53. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  54. emission_tree_dialog->popup_scenetree_dialog();
  55. } break;
  56. case MENU_OPTION_RESTART: {
  57. node->restart();
  58. } break;
  59. case MENU_OPTION_CONVERT_TO_GPU_PARTICLES: {
  60. GPUParticles3D *gpu_particles = memnew(GPUParticles3D);
  61. gpu_particles->convert_from_particles(node);
  62. gpu_particles->set_name(node->get_name());
  63. gpu_particles->set_transform(node->get_transform());
  64. gpu_particles->set_visible(node->is_visible());
  65. gpu_particles->set_process_mode(node->get_process_mode());
  66. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  67. ur->create_action(TTR("Convert to GPUParticles3D"), UndoRedo::MERGE_DISABLE, node);
  68. SceneTreeDock::get_singleton()->replace_node(node, gpu_particles);
  69. ur->commit_action(false);
  70. } break;
  71. case MENU_OPTION_GENERATE_AABB: {
  72. // Add one second to the default generation lifetime, since the progress is updated every second.
  73. generate_seconds->set_value(MAX(1.0, trunc(node->get_lifetime()) + 1.0));
  74. if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  75. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  76. generate_aabb->popup_centered();
  77. } else {
  78. // Generate the visibility AABB immediately.
  79. _generate_aabb();
  80. }
  81. } break;
  82. }
  83. }
  84. void CPUParticles3DEditor::_generate_aabb() {
  85. double time = generate_seconds->get_value();
  86. double running = 0.0;
  87. EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
  88. bool was_emitting = node->is_emitting();
  89. if (!was_emitting) {
  90. node->set_emitting(true);
  91. OS::get_singleton()->delay_usec(1000);
  92. }
  93. AABB rect;
  94. while (running < time) {
  95. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  96. ep.step("Generating...", int(running), true);
  97. OS::get_singleton()->delay_usec(1000);
  98. AABB capture = node->capture_aabb();
  99. if (rect == AABB()) {
  100. rect = capture;
  101. } else {
  102. rect.merge_with(capture);
  103. }
  104. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  105. }
  106. if (!was_emitting) {
  107. node->set_emitting(false);
  108. }
  109. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  110. ur->create_action(TTR("Generate Visibility AABB"));
  111. ur->add_do_method(node, "set_visibility_aabb", rect);
  112. ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
  113. ur->commit_action();
  114. }
  115. void CPUParticles3DEditor::edit(CPUParticles3D *p_particles) {
  116. base_node = p_particles;
  117. node = p_particles;
  118. }
  119. void CPUParticles3DEditor::_generate_emission_points() {
  120. /// hacer codigo aca
  121. Vector<Vector3> points;
  122. Vector<Vector3> normals;
  123. if (!_generate(points, normals)) {
  124. return;
  125. }
  126. if (normals.size() == 0) {
  127. node->set_emission_shape(CPUParticles3D::EMISSION_SHAPE_POINTS);
  128. node->set_emission_points(points);
  129. } else {
  130. node->set_emission_shape(CPUParticles3D::EMISSION_SHAPE_DIRECTED_POINTS);
  131. node->set_emission_points(points);
  132. node->set_emission_normals(normals);
  133. }
  134. }
  135. void CPUParticles3DEditor::_bind_methods() {
  136. }
  137. CPUParticles3DEditor::CPUParticles3DEditor() {
  138. particles_editor_hb = memnew(HBoxContainer);
  139. Node3DEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  140. options = memnew(MenuButton);
  141. options->set_switch_on_hover(true);
  142. particles_editor_hb->add_child(options);
  143. particles_editor_hb->hide();
  144. options->set_text(TTR("CPUParticles3D"));
  145. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("particles/restart_emission"), MENU_OPTION_RESTART);
  146. options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  147. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  148. options->get_popup()->add_item(TTR("Convert to GPUParticles3D"), MENU_OPTION_CONVERT_TO_GPU_PARTICLES);
  149. options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &CPUParticles3DEditor::_menu_option));
  150. generate_aabb = memnew(ConfirmationDialog);
  151. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  152. VBoxContainer *genvb = memnew(VBoxContainer);
  153. generate_aabb->add_child(genvb);
  154. generate_seconds = memnew(SpinBox);
  155. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  156. generate_seconds->set_min(0.1);
  157. generate_seconds->set_max(25);
  158. generate_seconds->set_value(2);
  159. add_child(generate_aabb);
  160. generate_aabb->connect("confirmed", callable_mp(this, &CPUParticles3DEditor::_generate_aabb));
  161. }
  162. void CPUParticles3DEditorPlugin::edit(Object *p_object) {
  163. particles_editor->edit(Object::cast_to<CPUParticles3D>(p_object));
  164. }
  165. bool CPUParticles3DEditorPlugin::handles(Object *p_object) const {
  166. return p_object->is_class("CPUParticles3D");
  167. }
  168. void CPUParticles3DEditorPlugin::make_visible(bool p_visible) {
  169. if (p_visible) {
  170. particles_editor->show();
  171. particles_editor->particles_editor_hb->show();
  172. } else {
  173. particles_editor->particles_editor_hb->hide();
  174. particles_editor->hide();
  175. particles_editor->edit(nullptr);
  176. }
  177. }
  178. CPUParticles3DEditorPlugin::CPUParticles3DEditorPlugin() {
  179. particles_editor = memnew(CPUParticles3DEditor);
  180. EditorNode::get_singleton()->get_main_screen_control()->add_child(particles_editor);
  181. particles_editor->hide();
  182. }
  183. CPUParticles3DEditorPlugin::~CPUParticles3DEditorPlugin() {
  184. }