gpu_particles_collision_sdf_editor_plugin.cpp 8.1 KB

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