lightmap_gi_editor_plugin.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*************************************************************************/
  2. /* lightmap_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 "lightmap_gi_editor_plugin.h"
  31. #include "editor/editor_file_dialog.h"
  32. #include "editor/editor_node.h"
  33. void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {
  34. if (lightmap) {
  35. LightmapGI::BakeError err;
  36. const uint64_t time_started = OS::get_singleton()->get_ticks_msec();
  37. if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == lightmap) {
  38. err = lightmap->bake(lightmap, p_file, bake_func_step);
  39. } else {
  40. err = lightmap->bake(lightmap->get_parent(), p_file, bake_func_step);
  41. }
  42. bake_func_end(time_started);
  43. switch (err) {
  44. case LightmapGI::BAKE_ERROR_NO_SAVE_PATH: {
  45. String scene_path = lightmap->get_scene_file_path();
  46. if (scene_path.is_empty()) {
  47. scene_path = lightmap->get_owner()->get_scene_file_path();
  48. }
  49. if (scene_path.is_empty()) {
  50. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));
  51. break;
  52. }
  53. scene_path = scene_path.get_basename() + ".lmbake";
  54. file_dialog->set_current_path(scene_path);
  55. file_dialog->popup_file_dialog();
  56. } break;
  57. case LightmapGI::BAKE_ERROR_NO_MESHES:
  58. EditorNode::get_singleton()->show_warning(TTR("No meshes to bake. Make sure they contain an UV2 channel and that the 'Bake Light' flag is on."));
  59. break;
  60. case LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE:
  61. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
  62. break;
  63. default: {
  64. }
  65. }
  66. }
  67. }
  68. void LightmapGIEditorPlugin::_bake() {
  69. _bake_select_file("");
  70. }
  71. void LightmapGIEditorPlugin::edit(Object *p_object) {
  72. LightmapGI *s = Object::cast_to<LightmapGI>(p_object);
  73. if (!s) {
  74. return;
  75. }
  76. lightmap = s;
  77. }
  78. bool LightmapGIEditorPlugin::handles(Object *p_object) const {
  79. return p_object->is_class("LightmapGI");
  80. }
  81. void LightmapGIEditorPlugin::make_visible(bool p_visible) {
  82. if (p_visible) {
  83. bake->show();
  84. } else {
  85. bake->hide();
  86. }
  87. }
  88. EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;
  89. bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
  90. if (!tmp_progress) {
  91. tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, false));
  92. ERR_FAIL_COND_V(tmp_progress == nullptr, false);
  93. }
  94. return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
  95. }
  96. void LightmapGIEditorPlugin::bake_func_end(uint64_t p_time_started) {
  97. if (tmp_progress != nullptr) {
  98. memdelete(tmp_progress);
  99. tmp_progress = nullptr;
  100. }
  101. const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;
  102. print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));
  103. // Request attention in case the user was doing something else.
  104. // Baking lightmaps is likely the editor task that can take the most time,
  105. // so only request the attention for baking lightmaps.
  106. DisplayServer::get_singleton()->window_request_attention();
  107. }
  108. void LightmapGIEditorPlugin::_bind_methods() {
  109. ClassDB::bind_method("_bake", &LightmapGIEditorPlugin::_bake);
  110. }
  111. LightmapGIEditorPlugin::LightmapGIEditorPlugin() {
  112. bake = memnew(Button);
  113. bake->set_flat(true);
  114. bake->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
  115. bake->set_text(TTR("Bake Lightmaps"));
  116. bake->hide();
  117. bake->connect("pressed", Callable(this, "_bake"));
  118. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  119. lightmap = nullptr;
  120. file_dialog = memnew(EditorFileDialog);
  121. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  122. file_dialog->add_filter("*.lmbake", TTR("LightMap Bake"));
  123. file_dialog->set_title(TTR("Select lightmap bake file:"));
  124. file_dialog->connect("file_selected", callable_mp(this, &LightmapGIEditorPlugin::_bake_select_file));
  125. bake->add_child(file_dialog);
  126. }
  127. LightmapGIEditorPlugin::~LightmapGIEditorPlugin() {
  128. }