voxel_gi_editor_plugin.cpp 7.5 KB

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