baked_lightmap_editor_plugin.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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) 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 "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. bake_func_end();
  40. switch (err) {
  41. case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH: {
  42. String scene_path = lightmap->get_filename();
  43. if (scene_path == String()) {
  44. scene_path = lightmap->get_owner()->get_filename();
  45. }
  46. if (scene_path == String()) {
  47. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));
  48. break;
  49. }
  50. scene_path = scene_path.get_basename() + ".lmbake";
  51. file_dialog->set_current_path(scene_path);
  52. file_dialog->popup_centered_ratio();
  53. } break;
  54. case BakedLightmap::BAKE_ERROR_NO_MESHES:
  55. 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."));
  56. break;
  57. case BakedLightmap::BAKE_ERROR_CANT_CREATE_IMAGE:
  58. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
  59. break;
  60. case BakedLightmap::BAKE_ERROR_LIGHTMAP_SIZE:
  61. EditorNode::get_singleton()->show_warning(TTR("Failed determining lightmap size. Maximum lightmap size too small?"));
  62. break;
  63. case BakedLightmap::BAKE_ERROR_INVALID_MESH:
  64. 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."));
  65. break;
  66. case BakedLightmap::BAKE_ERROR_NO_LIGHTMAPPER:
  67. EditorNode::get_singleton()->show_warning(TTR("Godot editor was built without ray tracing support, lightmaps can't be baked."));
  68. break;
  69. default: {
  70. }
  71. }
  72. }
  73. }
  74. void BakedLightmapEditorPlugin::_bake() {
  75. _bake_select_file("");
  76. }
  77. void BakedLightmapEditorPlugin::edit(Object *p_object) {
  78. BakedLightmap *s = Object::cast_to<BakedLightmap>(p_object);
  79. if (!s)
  80. return;
  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 = NULL;
  94. EditorProgress *BakedLightmapEditorPlugin::tmp_subprogress = NULL;
  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() {
  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. }
  119. void BakedLightmapEditorPlugin::_bind_methods() {
  120. ClassDB::bind_method("_bake", &BakedLightmapEditorPlugin::_bake);
  121. ClassDB::bind_method("_bake_select_file", &BakedLightmapEditorPlugin::_bake_select_file);
  122. }
  123. BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
  124. editor = p_node;
  125. bake = memnew(ToolButton);
  126. bake->set_icon(editor->get_gui_base()->get_icon("Bake", "EditorIcons"));
  127. bake->set_text(TTR("Bake Lightmaps"));
  128. bake->hide();
  129. bake->connect("pressed", this, "_bake");
  130. file_dialog = memnew(EditorFileDialog);
  131. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  132. file_dialog->add_filter("*.lmbake ; LightMap Bake");
  133. file_dialog->set_title(TTR("Select lightmap bake file:"));
  134. file_dialog->connect("file_selected", this, "_bake_select_file");
  135. bake->add_child(file_dialog);
  136. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  137. lightmap = NULL;
  138. BakedLightmap::bake_step_function = bake_func_step;
  139. BakedLightmap::bake_substep_function = bake_func_substep;
  140. }
  141. BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {
  142. }