voxel_gi_editor_plugin.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************/
  2. /* voxel_gi_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 "voxel_gi_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 VoxelGIEditorPlugin::_bake() {
  36. if (voxel_gi) {
  37. Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data();
  38. if (voxel_gi_data.is_null()) {
  39. String path = get_tree()->get_edited_scene_root()->get_scene_file_path();
  40. if (path.is_empty()) {
  41. path = "res://" + voxel_gi->get_name() + "_data.res";
  42. } else {
  43. String ext = path.get_extension();
  44. path = path.get_basename() + "." + voxel_gi->get_name() + "_data.res";
  45. }
  46. probe_file->set_current_path(path);
  47. probe_file->popup_file_dialog();
  48. return;
  49. } else {
  50. String path = voxel_gi_data->get_path();
  51. if (!path.is_resource_file()) {
  52. int srpos = path.find("::");
  53. if (srpos != -1) {
  54. String base = path.substr(0, srpos);
  55. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  56. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  57. EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is not local to the scene."));
  58. return;
  59. }
  60. } else {
  61. if (FileAccess::exists(base + ".import")) {
  62. EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is part of an imported resource."));
  63. return;
  64. }
  65. }
  66. }
  67. } else {
  68. if (FileAccess::exists(path + ".import")) {
  69. EditorNode::get_singleton()->show_warning(TTR("Voxel GI data is an imported resource."));
  70. return;
  71. }
  72. }
  73. }
  74. voxel_gi->bake();
  75. }
  76. }
  77. void VoxelGIEditorPlugin::edit(Object *p_object) {
  78. VoxelGI *s = Object::cast_to<VoxelGI>(p_object);
  79. if (!s) {
  80. return;
  81. }
  82. voxel_gi = s;
  83. }
  84. bool VoxelGIEditorPlugin::handles(Object *p_object) const {
  85. return p_object->is_class("VoxelGI");
  86. }
  87. void VoxelGIEditorPlugin::_notification(int p_what) {
  88. switch (p_what) {
  89. case NOTIFICATION_PROCESS: {
  90. if (!voxel_gi) {
  91. return;
  92. }
  93. // Set information tooltip on the Bake button. This information is useful
  94. // to optimize performance (video RAM size) and reduce light leaking (individual cell size).
  95. const Vector3i cell_size = voxel_gi->get_estimated_cell_size();
  96. const Vector3 half_size = voxel_gi->get_size() / 2;
  97. const int data_size = 4;
  98. const double size_mb = cell_size.x * cell_size.y * cell_size.z * data_size / (1024.0 * 1024.0);
  99. // Add a qualitative measurement to help the user assess whether a VoxelGI node is using a lot of VRAM.
  100. String size_quality;
  101. if (size_mb < 16.0) {
  102. size_quality = TTR("Low");
  103. } else if (size_mb < 64.0) {
  104. size_quality = TTR("Moderate");
  105. } else {
  106. size_quality = TTR("High");
  107. }
  108. String text;
  109. text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", cell_size.x, cell_size.y, cell_size.z)) + "\n";
  110. text += vformat(TTR("Cell size: %s"), vformat(U"%.3f × %.3f × %.3f", half_size.x / cell_size.x, half_size.y / cell_size.y, half_size.z / cell_size.z)) + "\n";
  111. text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality);
  112. // Only update the tooltip when needed to avoid constant redrawing.
  113. if (bake->get_tooltip(Point2()) == text) {
  114. return;
  115. }
  116. bake->set_tooltip_text(text);
  117. } break;
  118. }
  119. }
  120. void VoxelGIEditorPlugin::make_visible(bool p_visible) {
  121. if (p_visible) {
  122. bake_hb->show();
  123. set_process(true);
  124. } else {
  125. bake_hb->hide();
  126. set_process(false);
  127. }
  128. }
  129. EditorProgress *VoxelGIEditorPlugin::tmp_progress = nullptr;
  130. void VoxelGIEditorPlugin::bake_func_begin() {
  131. ERR_FAIL_COND(tmp_progress != nullptr);
  132. tmp_progress = memnew(EditorProgress("bake_gi", TTR("Bake VoxelGI"), 1000, true));
  133. }
  134. bool VoxelGIEditorPlugin::bake_func_step(int p_progress, const String &p_description) {
  135. ERR_FAIL_NULL_V(tmp_progress, false);
  136. return tmp_progress->step(p_description, p_progress, false);
  137. }
  138. void VoxelGIEditorPlugin::bake_func_end() {
  139. ERR_FAIL_NULL(tmp_progress);
  140. memdelete(tmp_progress);
  141. tmp_progress = nullptr;
  142. }
  143. void VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake(const String &p_path) {
  144. probe_file->hide();
  145. if (voxel_gi) {
  146. voxel_gi->bake();
  147. // Ensure the VoxelGIData is always saved to an external resource.
  148. // This avoids bloating the scene file with large binary data,
  149. // which would be serialized as Base64 if the scene is a `.tscn` file.
  150. Ref<VoxelGIData> voxel_gi_data = voxel_gi->get_probe_data();
  151. ERR_FAIL_COND(voxel_gi_data.is_null());
  152. voxel_gi_data->set_path(p_path);
  153. ResourceSaver::save(voxel_gi_data, p_path, ResourceSaver::FLAG_CHANGE_PATH);
  154. }
  155. }
  156. VoxelGIEditorPlugin::VoxelGIEditorPlugin() {
  157. bake_hb = memnew(HBoxContainer);
  158. bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  159. bake_hb->hide();
  160. bake = memnew(Button);
  161. bake->set_theme_type_variation(SceneStringName(FlatButton));
  162. // TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it
  163. // when the editor theme updates.
  164. bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  165. bake->set_text(TTR("Bake VoxelGI"));
  166. bake->connect(SceneStringName(pressed), callable_mp(this, &VoxelGIEditorPlugin::_bake));
  167. bake_hb->add_child(bake);
  168. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);
  169. voxel_gi = nullptr;
  170. probe_file = memnew(EditorFileDialog);
  171. probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  172. probe_file->add_filter("*.res");
  173. probe_file->connect("file_selected", callable_mp(this, &VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake));
  174. EditorInterface::get_singleton()->get_base_control()->add_child(probe_file);
  175. probe_file->set_title(TTR("Select path for VoxelGI Data File"));
  176. VoxelGI::bake_begin_function = bake_func_begin;
  177. VoxelGI::bake_step_function = bake_func_step;
  178. VoxelGI::bake_end_function = bake_func_end;
  179. }
  180. VoxelGIEditorPlugin::~VoxelGIEditorPlugin() {
  181. }