lightmap_gi_editor_plugin.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(
  89. TTR("No meshes with lightmapping support to bake. Make sure they contain UV2 data and their Global Illumination property is set to Static.") +
  90. String::utf8("\n\n• ") + TTR("To import a scene with lightmapping support, set Meshes > Light Baking to Static Lightmaps in the Import dock.") +
  91. String::utf8("\n• ") + TTR("To enable lightmapping support on a primitive mesh, edit the PrimitiveMesh resource in the inspector and check Add UV2.") +
  92. String::utf8("\n• ") + TTR("To enable lightmapping support on a CSG mesh, select the root CSG node and choose CSG > Bake Mesh Instance at the top of the 3D editor viewport.\nSelect the generated MeshInstance3D node and choose Mesh > Unwrap UV2 for Lightmap/AO at the top of the 3D editor viewport."));
  93. } break;
  94. case LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE: {
  95. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure the lightmap destination path is writable."));
  96. } break;
  97. case LightmapGI::BAKE_ERROR_NO_SCENE_ROOT: {
  98. EditorNode::get_singleton()->show_warning(TTR("No editor scene root found."));
  99. } break;
  100. case LightmapGI::BAKE_ERROR_FOREIGN_DATA: {
  101. EditorNode::get_singleton()->show_warning(TTR("Lightmap data is not local to the scene."));
  102. } break;
  103. case LightmapGI::BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL: {
  104. EditorNode::get_singleton()->show_warning(TTR("Maximum texture size is too small for the lightmap images.\nWhile this can be fixed by increasing the maximum texture size, it is recommended you split the scene into more objects instead."));
  105. } break;
  106. case LightmapGI::BAKE_ERROR_LIGHTMAP_TOO_SMALL: {
  107. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure all meshes to bake have the Lightmap Size Hint property set high enough, and the LightmapGI's Texel Scale value is not too low."));
  108. } break;
  109. case LightmapGI::BAKE_ERROR_ATLAS_TOO_SMALL: {
  110. EditorNode::get_singleton()->show_warning(TTR("Failed fitting a lightmap image into an atlas. This should never happen and should be reported."));
  111. } break;
  112. default: {
  113. } break;
  114. }
  115. }
  116. }
  117. void LightmapGIEditorPlugin::_bake() {
  118. _bake_select_file("");
  119. }
  120. void LightmapGIEditorPlugin::edit(Object *p_object) {
  121. LightmapGI *s = Object::cast_to<LightmapGI>(p_object);
  122. if (!s) {
  123. return;
  124. }
  125. lightmap = s;
  126. }
  127. bool LightmapGIEditorPlugin::handles(Object *p_object) const {
  128. return p_object->is_class("LightmapGI");
  129. }
  130. void LightmapGIEditorPlugin::make_visible(bool p_visible) {
  131. if (p_visible) {
  132. bake->show();
  133. } else {
  134. bake->hide();
  135. }
  136. }
  137. EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;
  138. bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
  139. if (!tmp_progress) {
  140. tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, false));
  141. ERR_FAIL_NULL_V(tmp_progress, false);
  142. }
  143. return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
  144. }
  145. void LightmapGIEditorPlugin::bake_func_end(uint64_t p_time_started) {
  146. if (tmp_progress != nullptr) {
  147. memdelete(tmp_progress);
  148. tmp_progress = nullptr;
  149. }
  150. const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;
  151. print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));
  152. // Request attention in case the user was doing something else.
  153. // Baking lightmaps is likely the editor task that can take the most time,
  154. // so only request the attention for baking lightmaps.
  155. DisplayServer::get_singleton()->window_request_attention();
  156. }
  157. void LightmapGIEditorPlugin::_bind_methods() {
  158. ClassDB::bind_method("_bake", &LightmapGIEditorPlugin::_bake);
  159. }
  160. LightmapGIEditorPlugin::LightmapGIEditorPlugin() {
  161. bake = memnew(Button);
  162. bake->set_theme_type_variation("FlatButton");
  163. // TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it
  164. // when the editor theme updates.
  165. bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  166. bake->set_text(TTR("Bake Lightmaps"));
  167. #ifdef MODULE_LIGHTMAPPER_RD_ENABLED
  168. // Disable lightmap baking if not supported on the current GPU.
  169. if (!DisplayServer::get_singleton()->can_create_rendering_device()) {
  170. bake->set_disabled(true);
  171. bake->set_tooltip_text(vformat(TTR("Lightmap baking is not supported on this GPU (%s)."), RenderingServer::get_singleton()->get_video_adapter_name()));
  172. }
  173. #else
  174. // Disable lightmap baking if the module is disabled at compile-time.
  175. bake->set_disabled(true);
  176. #if defined(ANDROID_ENABLED) || defined(IOS_ENABLED)
  177. bake->set_tooltip_text(vformat(TTR("Lightmaps cannot be baked on %s."), OS::get_singleton()->get_name()));
  178. #else
  179. bake->set_tooltip_text(TTR("Lightmaps cannot be baked, as the `lightmapper_rd` module was disabled at compile-time."));
  180. #endif
  181. #endif // MODULE_LIGHTMAPPER_RD_ENABLED
  182. bake->hide();
  183. bake->connect(SceneStringName(pressed), Callable(this, "_bake"));
  184. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  185. lightmap = nullptr;
  186. file_dialog = memnew(EditorFileDialog);
  187. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  188. file_dialog->add_filter("*.lmbake", TTR("LightMap Bake"));
  189. file_dialog->set_title(TTR("Select lightmap bake file:"));
  190. file_dialog->connect("file_selected", callable_mp(this, &LightmapGIEditorPlugin::_bake_select_file));
  191. bake->add_child(file_dialog);
  192. }
  193. LightmapGIEditorPlugin::~LightmapGIEditorPlugin() {
  194. }