gpu_particles_collision_sdf_editor_plugin.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************/
  2. /* gpu_particles_collision_sdf_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 "gpu_particles_collision_sdf_editor_plugin.h"
  31. #include "editor/editor_interface.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/gui/editor_file_dialog.h"
  35. void GPUParticlesCollisionSDF3DEditorPlugin::_bake() {
  36. if (col_sdf) {
  37. if (col_sdf->get_texture().is_null() || !col_sdf->get_texture()->get_path().is_resource_file()) {
  38. String path = get_tree()->get_edited_scene_root()->get_scene_file_path();
  39. if (path.is_empty()) {
  40. path = "res://" + col_sdf->get_name() + "_data.exr";
  41. } else {
  42. String ext = path.get_extension();
  43. path = path.get_basename() + "." + col_sdf->get_name() + "_data.exr";
  44. }
  45. probe_file->set_current_path(path);
  46. probe_file->popup_file_dialog();
  47. return;
  48. }
  49. _sdf_save_path_and_bake(col_sdf->get_texture()->get_path());
  50. }
  51. }
  52. void GPUParticlesCollisionSDF3DEditorPlugin::edit(Object *p_object) {
  53. GPUParticlesCollisionSDF3D *s = Object::cast_to<GPUParticlesCollisionSDF3D>(p_object);
  54. if (!s) {
  55. return;
  56. }
  57. col_sdf = s;
  58. }
  59. bool GPUParticlesCollisionSDF3DEditorPlugin::handles(Object *p_object) const {
  60. return p_object->is_class("GPUParticlesCollisionSDF3D");
  61. }
  62. void GPUParticlesCollisionSDF3DEditorPlugin::_notification(int p_what) {
  63. switch (p_what) {
  64. case NOTIFICATION_PROCESS: {
  65. if (!col_sdf) {
  66. return;
  67. }
  68. // Set information tooltip on the Bake button. This information is useful
  69. // to optimize performance (video RAM size) and reduce collision tunneling (individual cell size).
  70. const Vector3i size = col_sdf->get_estimated_cell_size();
  71. const Vector3 extents = col_sdf->get_size() / 2;
  72. int data_size = 2;
  73. const double size_mb = size.x * size.y * size.z * data_size / (1024.0 * 1024.0);
  74. // Add a qualitative measurement to help the user assess whether a GPUParticlesCollisionSDF3D node is using a lot of VRAM.
  75. String size_quality;
  76. if (size_mb < 8.0) {
  77. size_quality = TTR("Low");
  78. } else if (size_mb < 32.0) {
  79. size_quality = TTR("Moderate");
  80. } else {
  81. size_quality = TTR("High");
  82. }
  83. String text;
  84. text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", size.x, size.y, size.z)) + "\n";
  85. text += vformat(TTR("Cell size: %s"), vformat(U"%.3f × %.3f × %.3f", extents.x / size.x, extents.y / size.y, extents.z / size.z)) + "\n";
  86. text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality);
  87. // Only update the tooltip when needed to avoid constant redrawing.
  88. if (bake->get_tooltip(Point2()) == text) {
  89. return;
  90. }
  91. bake->set_tooltip_text(text);
  92. } break;
  93. }
  94. }
  95. void GPUParticlesCollisionSDF3DEditorPlugin::make_visible(bool p_visible) {
  96. if (p_visible) {
  97. bake_hb->show();
  98. set_process(true);
  99. } else {
  100. bake_hb->hide();
  101. set_process(false);
  102. }
  103. }
  104. EditorProgress *GPUParticlesCollisionSDF3DEditorPlugin::tmp_progress = nullptr;
  105. void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_begin(int p_steps) {
  106. ERR_FAIL_COND(tmp_progress != nullptr);
  107. tmp_progress = memnew(EditorProgress("bake_sdf", TTR("Bake SDF"), p_steps));
  108. }
  109. void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_step(int p_step, const String &p_description) {
  110. ERR_FAIL_NULL(tmp_progress);
  111. tmp_progress->step(p_description, p_step, false);
  112. }
  113. void GPUParticlesCollisionSDF3DEditorPlugin::bake_func_end() {
  114. ERR_FAIL_NULL(tmp_progress);
  115. memdelete(tmp_progress);
  116. tmp_progress = nullptr;
  117. }
  118. void GPUParticlesCollisionSDF3DEditorPlugin::_sdf_save_path_and_bake(const String &p_path) {
  119. probe_file->hide();
  120. if (col_sdf) {
  121. Ref<Image> bake_img = col_sdf->bake();
  122. if (bake_img.is_null()) {
  123. EditorNode::get_singleton()->show_warning(TTR("No faces detected during GPUParticlesCollisionSDF3D bake.\nCheck whether there are visible meshes matching the bake mask within its extents."));
  124. return;
  125. }
  126. Ref<ConfigFile> config;
  127. config.instantiate();
  128. if (FileAccess::exists(p_path + ".import")) {
  129. config->load(p_path + ".import");
  130. }
  131. config->set_value("remap", "importer", "3d_texture");
  132. config->set_value("remap", "type", "CompressedTexture3D");
  133. if (!config->has_section_key("params", "compress/mode")) {
  134. config->set_value("params", "compress/mode", 3); //user may want another compression, so leave it be
  135. }
  136. config->set_value("params", "compress/channel_pack", 1);
  137. config->set_value("params", "mipmaps/generate", false);
  138. config->set_value("params", "slices/horizontal", 1);
  139. config->set_value("params", "slices/vertical", bake_img->get_meta("depth"));
  140. config->save(p_path + ".import");
  141. Error err = bake_img->save_exr(p_path, false);
  142. ERR_FAIL_COND(err);
  143. ResourceLoader::import(p_path);
  144. Ref<Texture> t = ResourceLoader::load(p_path); //if already loaded, it will be updated on refocus?
  145. ERR_FAIL_COND(t.is_null());
  146. col_sdf->set_texture(t);
  147. }
  148. }
  149. GPUParticlesCollisionSDF3DEditorPlugin::GPUParticlesCollisionSDF3DEditorPlugin() {
  150. bake_hb = memnew(HBoxContainer);
  151. bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  152. bake_hb->hide();
  153. bake = memnew(Button);
  154. bake->set_theme_type_variation(SceneStringName(FlatButton));
  155. bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  156. bake->set_text(TTR("Bake SDF"));
  157. bake->connect(SceneStringName(pressed), callable_mp(this, &GPUParticlesCollisionSDF3DEditorPlugin::_bake));
  158. bake_hb->add_child(bake);
  159. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);
  160. col_sdf = nullptr;
  161. probe_file = memnew(EditorFileDialog);
  162. probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  163. probe_file->add_filter("*.exr");
  164. probe_file->connect("file_selected", callable_mp(this, &GPUParticlesCollisionSDF3DEditorPlugin::_sdf_save_path_and_bake));
  165. EditorInterface::get_singleton()->get_base_control()->add_child(probe_file);
  166. probe_file->set_title(TTR("Select path for SDF Texture"));
  167. GPUParticlesCollisionSDF3D::bake_begin_function = bake_func_begin;
  168. GPUParticlesCollisionSDF3D::bake_step_function = bake_func_step;
  169. GPUParticlesCollisionSDF3D::bake_end_function = bake_func_end;
  170. }