voxel_gi_editor_plugin.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "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. if (voxel_gi->get_probe_data().is_null()) {
  36. String path = get_tree()->get_edited_scene_root()->get_scene_file_path();
  37. if (path.is_empty()) {
  38. path = "res://" + voxel_gi->get_name() + "_data.res";
  39. } else {
  40. String ext = path.get_extension();
  41. path = path.get_basename() + "." + voxel_gi->get_name() + "_data.res";
  42. }
  43. probe_file->set_current_path(path);
  44. probe_file->popup_file_dialog();
  45. return;
  46. }
  47. voxel_gi->bake();
  48. }
  49. }
  50. void VoxelGIEditorPlugin::edit(Object *p_object) {
  51. VoxelGI *s = Object::cast_to<VoxelGI>(p_object);
  52. if (!s) {
  53. return;
  54. }
  55. voxel_gi = s;
  56. }
  57. bool VoxelGIEditorPlugin::handles(Object *p_object) const {
  58. return p_object->is_class("VoxelGI");
  59. }
  60. void VoxelGIEditorPlugin::_notification(int p_what) {
  61. switch (p_what) {
  62. case NOTIFICATION_PROCESS: {
  63. if (!voxel_gi) {
  64. return;
  65. }
  66. // Set information tooltip on the Bake button. This information is useful
  67. // to optimize performance (video RAM size) and reduce light leaking (individual cell size).
  68. const Vector3i size = voxel_gi->get_estimated_cell_size();
  69. const Vector3 extents = voxel_gi->get_extents();
  70. const int data_size = 4;
  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 VoxelGI node is using a lot of VRAM.
  73. String size_quality;
  74. if (size_mb < 16.0) {
  75. size_quality = TTR("Low");
  76. } else if (size_mb < 64.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);
  90. } break;
  91. }
  92. }
  93. void VoxelGIEditorPlugin::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 *VoxelGIEditorPlugin::tmp_progress = nullptr;
  103. void VoxelGIEditorPlugin::bake_func_begin(int p_steps) {
  104. ERR_FAIL_COND(tmp_progress != nullptr);
  105. tmp_progress = memnew(EditorProgress("bake_gi", TTR("Bake VoxelGI"), p_steps));
  106. }
  107. void VoxelGIEditorPlugin::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 VoxelGIEditorPlugin::bake_func_end() {
  112. ERR_FAIL_COND(tmp_progress == nullptr);
  113. memdelete(tmp_progress);
  114. tmp_progress = nullptr;
  115. }
  116. void VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake(const String &p_path) {
  117. probe_file->hide();
  118. if (voxel_gi) {
  119. voxel_gi->bake();
  120. ERR_FAIL_COND(voxel_gi->get_probe_data().is_null());
  121. ResourceSaver::save(voxel_gi->get_probe_data(), p_path, ResourceSaver::FLAG_CHANGE_PATH);
  122. }
  123. }
  124. void VoxelGIEditorPlugin::_bind_methods() {
  125. }
  126. VoxelGIEditorPlugin::VoxelGIEditorPlugin() {
  127. bake_hb = memnew(HBoxContainer);
  128. bake_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  129. bake_hb->hide();
  130. bake = memnew(Button);
  131. bake->set_flat(true);
  132. bake->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
  133. bake->set_text(TTR("Bake VoxelGI"));
  134. bake->connect("pressed", callable_mp(this, &VoxelGIEditorPlugin::_bake));
  135. bake_hb->add_child(bake);
  136. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake_hb);
  137. voxel_gi = nullptr;
  138. probe_file = memnew(EditorFileDialog);
  139. probe_file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  140. probe_file->add_filter("*.res");
  141. probe_file->connect("file_selected", callable_mp(this, &VoxelGIEditorPlugin::_voxel_gi_save_path_and_bake));
  142. get_editor_interface()->get_base_control()->add_child(probe_file);
  143. probe_file->set_title(TTR("Select path for VoxelGI Data File"));
  144. VoxelGI::bake_begin_function = bake_func_begin;
  145. VoxelGI::bake_step_function = bake_func_step;
  146. VoxelGI::bake_end_function = bake_func_end;
  147. }
  148. VoxelGIEditorPlugin::~VoxelGIEditorPlugin() {
  149. }