lightmap_gi_editor_plugin.cpp 6.0 KB

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