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