baked_lightmap_editor_plugin.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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() {
  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);
  36. } else {
  37. err = lightmap->bake(lightmap->get_parent());
  38. }
  39. switch (err) {
  40. case BakedLightmap::BAKE_ERROR_NO_SAVE_PATH:
  41. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene (for images to be saved in the same dir), or pick a save path from the BakedLightmap properties."));
  42. break;
  43. case BakedLightmap::BAKE_ERROR_NO_MESHES:
  44. 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."));
  45. break;
  46. case BakedLightmap::BAKE_ERROR_CANT_CREATE_IMAGE:
  47. EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images, make sure path is writable."));
  48. break;
  49. default: {}
  50. }
  51. }
  52. }
  53. void BakedLightmapEditorPlugin::edit(Object *p_object) {
  54. BakedLightmap *s = Object::cast_to<BakedLightmap>(p_object);
  55. if (!s)
  56. return;
  57. lightmap = s;
  58. }
  59. bool BakedLightmapEditorPlugin::handles(Object *p_object) const {
  60. return p_object->is_class("BakedLightmap");
  61. }
  62. void BakedLightmapEditorPlugin::make_visible(bool p_visible) {
  63. if (p_visible) {
  64. bake->show();
  65. } else {
  66. bake->hide();
  67. }
  68. }
  69. EditorProgress *BakedLightmapEditorPlugin::tmp_progress = NULL;
  70. void BakedLightmapEditorPlugin::bake_func_begin(int p_steps) {
  71. ERR_FAIL_COND(tmp_progress != NULL);
  72. tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), p_steps, true));
  73. }
  74. bool BakedLightmapEditorPlugin::bake_func_step(int p_step, const String &p_description) {
  75. ERR_FAIL_COND_V(tmp_progress == NULL, false);
  76. return tmp_progress->step(p_description, p_step, false);
  77. }
  78. void BakedLightmapEditorPlugin::bake_func_end() {
  79. ERR_FAIL_COND(tmp_progress == NULL);
  80. memdelete(tmp_progress);
  81. tmp_progress = NULL;
  82. }
  83. void BakedLightmapEditorPlugin::_bind_methods() {
  84. ClassDB::bind_method("_bake", &BakedLightmapEditorPlugin::_bake);
  85. }
  86. BakedLightmapEditorPlugin::BakedLightmapEditorPlugin(EditorNode *p_node) {
  87. editor = p_node;
  88. bake = memnew(ToolButton);
  89. bake->set_icon(editor->get_gui_base()->get_icon("Bake", "EditorIcons"));
  90. bake->set_text(TTR("Bake Lightmaps"));
  91. bake->hide();
  92. bake->connect("pressed", this, "_bake");
  93. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  94. lightmap = NULL;
  95. BakedLightmap::bake_begin_function = bake_func_begin;
  96. BakedLightmap::bake_step_function = bake_func_step;
  97. BakedLightmap::bake_end_function = bake_func_end;
  98. }
  99. BakedLightmapEditorPlugin::~BakedLightmapEditorPlugin() {
  100. }