gpu_particles_collision_sdf_editor_plugin.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. void GPUParticlesCollisionSDFEditorPlugin::_bake() {
  32. if (col_sdf) {
  33. if (col_sdf->get_texture().is_null() || !col_sdf->get_texture()->get_path().is_resource_file()) {
  34. String path = get_tree()->get_edited_scene_root()->get_filename();
  35. if (path == String()) {
  36. path = "res://" + col_sdf->get_name() + "_data.exr";
  37. } else {
  38. String ext = path.get_extension();
  39. path = path.get_basename() + "." + col_sdf->get_name() + "_data.exr";
  40. }
  41. probe_file->set_current_path(path);
  42. probe_file->popup_file_dialog();
  43. return;
  44. }
  45. _sdf_save_path_and_bake(col_sdf->get_texture()->get_path());
  46. }
  47. }
  48. void GPUParticlesCollisionSDFEditorPlugin::edit(Object *p_object) {
  49. GPUParticlesCollisionSDF *s = Object::cast_to<GPUParticlesCollisionSDF>(p_object);
  50. if (!s) {
  51. return;
  52. }
  53. col_sdf = s;
  54. }
  55. bool GPUParticlesCollisionSDFEditorPlugin::handles(Object *p_object) const {
  56. return p_object->is_class("GPUParticlesCollisionSDF");
  57. }
  58. void GPUParticlesCollisionSDFEditorPlugin::_notification(int p_what) {
  59. if (p_what == NOTIFICATION_PROCESS) {
  60. if (!col_sdf) {
  61. return;
  62. }
  63. const Vector3i size = col_sdf->get_estimated_cell_size();
  64. String text = vformat(String::utf8("%d × %d × %d"), size.x, size.y, size.z);
  65. int data_size = 2;
  66. const double size_mb = size.x * size.y * size.z * data_size / (1024.0 * 1024.0);
  67. text += " - " + vformat(TTR("VRAM Size: %s MB"), String::num(size_mb, 2));
  68. if (bake_info->get_text() == text) {
  69. return;
  70. }
  71. // Color the label depending on the estimated performance level.
  72. Color color;
  73. if (size_mb <= 16.0 + CMP_EPSILON) {
  74. // Fast.
  75. color = bake_info->get_theme_color("success_color", "Editor");
  76. } else if (size_mb <= 64.0 + CMP_EPSILON) {
  77. // Medium.
  78. color = bake_info->get_theme_color("warning_color", "Editor");
  79. } else {
  80. // Slow.
  81. color = bake_info->get_theme_color("error_color", "Editor");
  82. }
  83. bake_info->add_theme_color_override("font_color", color);
  84. bake_info->set_text(text);
  85. }
  86. }
  87. void GPUParticlesCollisionSDFEditorPlugin::make_visible(bool p_visible) {
  88. if (p_visible) {
  89. bake_hb->show();
  90. set_process(true);
  91. } else {
  92. bake_hb->hide();
  93. set_process(false);
  94. }
  95. }
  96. EditorProgress *GPUParticlesCollisionSDFEditorPlugin::tmp_progress = nullptr;
  97. void GPUParticlesCollisionSDFEditorPlugin::bake_func_begin(int p_steps) {
  98. ERR_FAIL_COND(tmp_progress != nullptr);
  99. tmp_progress = memnew(EditorProgress("bake_sdf", TTR("Bake SDF"), p_steps));
  100. }
  101. void GPUParticlesCollisionSDFEditorPlugin::bake_func_step(int p_step, const String &p_description) {
  102. ERR_FAIL_COND(tmp_progress == nullptr);
  103. tmp_progress->step(p_description, p_step, false);
  104. }
  105. void GPUParticlesCollisionSDFEditorPlugin::bake_func_end() {
  106. ERR_FAIL_COND(tmp_progress == nullptr);
  107. memdelete(tmp_progress);
  108. tmp_progress = nullptr;
  109. }
  110. void GPUParticlesCollisionSDFEditorPlugin::_sdf_save_path_and_bake(const String &p_path) {
  111. probe_file->hide();
  112. if (col_sdf) {
  113. Ref<Image> bake_img = col_sdf->bake();
  114. if (bake_img.is_null()) {
  115. EditorNode::get_singleton()->show_warning("Bake Error.");
  116. return;
  117. }
  118. Ref<ConfigFile> config;
  119. config.instance();
  120. if (FileAccess::exists(p_path + ".import")) {
  121. config->load(p_path + ".import");
  122. }
  123. config->set_value("remap", "importer", "3d_texture");
  124. config->set_value("remap", "type", "StreamTexture3D");
  125. if (!config->has_section_key("params", "compress/mode")) {
  126. config->set_value("params", "compress/mode", 3); //user may want another compression, so leave it be
  127. }
  128. config->set_value("params", "compress/channel_pack", 1);
  129. config->set_value("params", "mipmaps/generate", false);
  130. config->set_value("params", "slices/horizontal", 1);
  131. config->set_value("params", "slices/vertical", bake_img->get_meta("depth"));
  132. config->save(p_path + ".import");
  133. Error err = bake_img->save_exr(p_path, false);
  134. ERR_FAIL_COND(err);
  135. ResourceLoader::import(p_path);
  136. Ref<Texture> t = ResourceLoader::load(p_path); //if already loaded, it will be updated on refocus?
  137. ERR_FAIL_COND(t.is_null());
  138. col_sdf->set_texture(t);
  139. }
  140. }
  141. void GPUParticlesCollisionSDFEditorPlugin::_bind_methods() {
  142. }
  143. GPUParticlesCollisionSDFEditorPlugin::GPUParticlesCollisionSDFEditorPlugin(EditorNode *p_node) {
  144. editor = p_node;
  145. bake_hb = memnew(HBoxContainer);
  146. bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  147. bake_hb->hide();
  148. bake = memnew(Button);
  149. bake->set_flat(true);
  150. bake->set_icon(editor->get_gui_base()->get_theme_icon("Bake", "EditorIcons"));
  151. bake->set_text(TTR("Bake SDF"));
  152. bake->connect("pressed", callable_mp(this, &GPUParticlesCollisionSDFEditorPlugin::_bake));
  153. bake_hb->add_child(bake);
  154. bake_info = memnew(Label);
  155. bake_info->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  156. bake_info->set_clip_text(true);
  157. bake_hb->add_child(bake_info);
  158. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);
  159. col_sdf = nullptr;
  160. probe_file = memnew(EditorFileDialog);
  161. probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  162. probe_file->add_filter("*.exr");
  163. probe_file->connect("file_selected", callable_mp(this, &GPUParticlesCollisionSDFEditorPlugin::_sdf_save_path_and_bake));
  164. get_editor_interface()->get_base_control()->add_child(probe_file);
  165. probe_file->set_title(TTR("Select path for SDF Texture"));
  166. GPUParticlesCollisionSDF::bake_begin_function = bake_func_begin;
  167. GPUParticlesCollisionSDF::bake_step_function = bake_func_step;
  168. GPUParticlesCollisionSDF::bake_end_function = bake_func_end;
  169. }
  170. GPUParticlesCollisionSDFEditorPlugin::~GPUParticlesCollisionSDFEditorPlugin() {
  171. }