baked_lightmap_editor_plugin.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* baked_lightmap_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 "baked_lightmap_editor_plugin.h"
  31. void BakedLightmapEditorPlugin::_bake_select_file(const String &p_file) {
  32. if (lightmap) {
  33. BakedLightmap::BakeError err;
  34. if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == lightmap) {
  35. err = lightmap->bake(lightmap, p_file);
  36. } else {
  37. err = lightmap->bake(lightmap->get_parent(), p_file);
  38. }
  39. switch (err) {
  40. case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH: {
  41. String scene_path = lightmap->get_filename();
  42. if (scene_path == String()) {
  43. scene_path = lightmap->get_owner()->get_filename();
  44. }
  45. if (scene_path == String()) {
  46. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));
  47. break;
  48. }
  49. scene_path = scene_path.get_basename() + ".lmbake";
  50. file_dialog->set_current_path(scene_path);
  51. file_dialog->popup_centered_ratio();
  52. } break;
  53. case BakedLightmap::BAKE_ERROR_NO_MESHES:
  54. EditorNode::get_singleton()->show_warning(TTR("No meshes to bake. Make sure they contain an UV2 channel and that the 'Use In Baked Light' and 'Generate Lightmap' flags are on."));
  55. break;
  56. case BakedLightmap::BAKE_ERROR_CANT_CREATE_IMAGE:
  57. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
  58. break;
  59. case BakedLightmap::BAKE_ERROR_LIGHTMAP_SIZE:
  60. EditorNode::get_singleton()->show_warning(TTR("Failed determining lightmap size. Maximum lightmap size too small?"));
  61. break;
  62. case BakedLightmap::BAKE_ERROR_INVALID_MESH:
  63. EditorNode::get_singleton()->show_warning(TTR("Some mesh is invalid. Make sure the UV2 channel values are contained within the [0.0,1.0] square region."));
  64. break;
  65. case BakedLightmap::BAKE_ERROR_NO_LIGHTMAPPER:
  66. EditorNode::get_singleton()->show_warning(TTR("Godot editor was built without ray tracing support, lightmaps can't be baked."));
  67. break;
  68. default: {
  69. }
  70. }
  71. }
  72. }
  73. void BakedLightmapEditorPlugin::_bake() {
  74. _bake_select_file("");
  75. }
  76. void BakedLightmapEditorPlugin::edit(Object *p_object) {
  77. BakedLightmap *s = Object::cast_to<BakedLightmap>(p_object);
  78. if (!s) {
  79. return;
  80. }
  81. lightmap = s;
  82. }
  83. bool BakedLightmapEditorPlugin::handles(Object *p_object) const {
  84. return p_object->is_class("BakedLightmap");
  85. }
  86. void BakedLightmapEditorPlugin::make_visible(bool p_visible) {
  87. if (p_visible) {
  88. bake->show();
  89. } else {
  90. bake->hide();
  91. }
  92. }
  93. EditorProgress *BakedLightmapEditorPlugin::tmp_progress = nullptr;
  94. EditorProgress *BakedLightmapEditorPlugin::tmp_subprogress = nullptr;
  95. bool BakedLightmapEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_force_refresh) {
  96. if (!tmp_progress) {
  97. tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, true));
  98. ERR_FAIL_COND_V(tmp_progress == nullptr, false);
  99. }
  100. return tmp_progress->step(p_description, p_progress * 1000, p_force_refresh);
  101. }
  102. bool BakedLightmapEditorPlugin::bake_func_substep(float p_progress, const String &p_description, void *, bool p_force_refresh) {
  103. if (!tmp_subprogress) {
  104. tmp_subprogress = memnew(EditorProgress("bake_lightmaps_substep", "", 1000, true));
  105. ERR_FAIL_COND_V(tmp_subprogress == nullptr, false);
  106. }
  107. return tmp_subprogress->step(p_description, p_progress * 1000, p_force_refresh);
  108. }
  109. void BakedLightmapEditorPlugin::bake_func_end(uint32_t p_time_started) {
  110. if (tmp_progress != nullptr) {
  111. memdelete(tmp_progress);
  112. tmp_progress = nullptr;
  113. }
  114. if (tmp_subprogress != nullptr) {
  115. memdelete(tmp_subprogress);
  116. tmp_subprogress = nullptr;
  117. }
  118. const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;
  119. if (time_taken >= 1) {
  120. // Only print a message and request attention if baking lightmaps took at least 1 second.
  121. // Otherwise, attempting to bake in an erroneous situation (e.g. no meshes to bake)
  122. // would print the "done baking lightmaps" message and request attention for no good reason.
  123. print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));
  124. // Request attention in case the user was doing something else.
  125. // Baking lightmaps is likely the editor task that can take the most time,
  126. // so only request the attention for baking lightmaps.
  127. OS::get_singleton()->request_attention();
  128. }
  129. }
  130. void BakedLightmapEditorPlugin::_bind_methods() {
  131. ClassDB::bind_method("_bake", &BakedLightmapEditorPlugin::_bake);
  132. ClassDB::bind_method("_bake_select_file", &BakedLightmapEditorPlugin::_bake_select_file);
  133. }
  134. BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
  135. editor = p_node;
  136. bake = memnew(ToolButton);
  137. bake->set_icon(editor->get_gui_base()->get_icon("Bake", "EditorIcons"));
  138. bake->set_text(TTR("Bake Lightmaps"));
  139. bake->hide();
  140. bake->connect("pressed", this, "_bake");
  141. file_dialog = memnew(EditorFileDialog);
  142. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  143. file_dialog->add_filter("*.lmbake ; " + TTR("LightMap Bake"));
  144. file_dialog->set_title(TTR("Select lightmap bake file:"));
  145. file_dialog->connect("file_selected", this, "_bake_select_file");
  146. bake->add_child(file_dialog);
  147. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  148. lightmap = nullptr;
  149. BakedLightmap::bake_step_function = bake_func_step;
  150. BakedLightmap::bake_substep_function = bake_func_substep;
  151. BakedLightmap::bake_end_function = bake_func_end;
  152. }
  153. BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {
  154. }