lightmap_gi_editor_plugin.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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) 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 "lightmap_gi_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/gui/editor_file_dialog.h"
  34. void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {
  35. if (lightmap) {
  36. LightmapGI::BakeError err = LightmapGI::BAKE_ERROR_OK;
  37. const uint64_t time_started = OS::get_singleton()->get_ticks_msec();
  38. if (get_tree()->get_edited_scene_root()) {
  39. Ref<LightmapGIData> lightmapGIData = lightmap->get_light_data();
  40. if (lightmapGIData.is_valid()) {
  41. String path = lightmapGIData->get_path();
  42. if (!path.is_resource_file()) {
  43. int srpos = path.find("::");
  44. if (srpos != -1) {
  45. String base = path.substr(0, srpos);
  46. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  47. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  48. err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
  49. }
  50. } else {
  51. if (FileAccess::exists(base + ".import")) {
  52. err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
  53. }
  54. }
  55. }
  56. } else {
  57. if (FileAccess::exists(path + ".import")) {
  58. err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
  59. }
  60. }
  61. }
  62. if (err == LightmapGI::BAKE_ERROR_OK) {
  63. if (get_tree()->get_edited_scene_root() == lightmap) {
  64. err = lightmap->bake(lightmap, p_file, bake_func_step);
  65. } else {
  66. err = lightmap->bake(lightmap->get_parent(), p_file, bake_func_step);
  67. }
  68. }
  69. } else {
  70. err = LightmapGI::BAKE_ERROR_NO_SCENE_ROOT;
  71. }
  72. bake_func_end(time_started);
  73. switch (err) {
  74. case LightmapGI::BAKE_ERROR_NO_SAVE_PATH: {
  75. String scene_path = lightmap->get_scene_file_path();
  76. if (scene_path.is_empty() && lightmap->get_owner()) {
  77. scene_path = lightmap->get_owner()->get_scene_file_path();
  78. }
  79. if (scene_path.is_empty()) {
  80. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));
  81. break;
  82. }
  83. scene_path = scene_path.get_basename() + ".lmbake";
  84. file_dialog->set_current_path(scene_path);
  85. file_dialog->popup_file_dialog();
  86. } break;
  87. case LightmapGI::BAKE_ERROR_NO_MESHES: {
  88. 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."));
  89. } break;
  90. case LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE: {
  91. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
  92. } break;
  93. case LightmapGI::BAKE_ERROR_NO_SCENE_ROOT: {
  94. EditorNode::get_singleton()->show_warning(TTR("No editor scene root found."));
  95. } break;
  96. case LightmapGI::BAKE_ERROR_FOREIGN_DATA: {
  97. EditorNode::get_singleton()->show_warning(TTR("Lightmap data is not local to the scene."));
  98. } break;
  99. case LightmapGI::BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL: {
  100. EditorNode::get_singleton()->show_warning(TTR("Maximum texture size is too small for the lightmap images."));
  101. } break;
  102. default: {
  103. } break;
  104. }
  105. }
  106. }
  107. void LightmapGIEditorPlugin::_bake() {
  108. _bake_select_file("");
  109. }
  110. void LightmapGIEditorPlugin::edit(Object *p_object) {
  111. LightmapGI *s = Object::cast_to<LightmapGI>(p_object);
  112. if (!s) {
  113. return;
  114. }
  115. lightmap = s;
  116. }
  117. bool LightmapGIEditorPlugin::handles(Object *p_object) const {
  118. return p_object->is_class("LightmapGI");
  119. }
  120. void LightmapGIEditorPlugin::make_visible(bool p_visible) {
  121. if (p_visible) {
  122. bake->show();
  123. } else {
  124. bake->hide();
  125. }
  126. }
  127. EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;
  128. bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
  129. if (!tmp_progress) {
  130. tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, false));
  131. ERR_FAIL_NULL_V(tmp_progress, false);
  132. }
  133. return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
  134. }
  135. void LightmapGIEditorPlugin::bake_func_end(uint64_t p_time_started) {
  136. if (tmp_progress != nullptr) {
  137. memdelete(tmp_progress);
  138. tmp_progress = nullptr;
  139. }
  140. const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;
  141. print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));
  142. // Request attention in case the user was doing something else.
  143. // Baking lightmaps is likely the editor task that can take the most time,
  144. // so only request the attention for baking lightmaps.
  145. DisplayServer::get_singleton()->window_request_attention();
  146. }
  147. void LightmapGIEditorPlugin::_bind_methods() {
  148. ClassDB::bind_method("_bake", &LightmapGIEditorPlugin::_bake);
  149. }
  150. LightmapGIEditorPlugin::LightmapGIEditorPlugin() {
  151. bake = memnew(Button);
  152. bake->set_theme_type_variation("FlatButton");
  153. // TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it
  154. // when the editor theme updates.
  155. bake->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  156. bake->set_text(TTR("Bake Lightmaps"));
  157. bake->hide();
  158. bake->connect("pressed", Callable(this, "_bake"));
  159. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  160. lightmap = nullptr;
  161. file_dialog = memnew(EditorFileDialog);
  162. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  163. file_dialog->add_filter("*.lmbake", TTR("LightMap Bake"));
  164. file_dialog->set_title(TTR("Select lightmap bake file:"));
  165. file_dialog->connect("file_selected", callable_mp(this, &LightmapGIEditorPlugin::_bake_select_file));
  166. bake->add_child(file_dialog);
  167. }
  168. LightmapGIEditorPlugin::~LightmapGIEditorPlugin() {
  169. }