Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/lightmap_gi_editor_plugin.cpp
9903 views
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
37
#include "modules/modules_enabled.gen.h" // For lightmapper_rd.
38
39
void LightmapGIEditorPlugin::_bake_select_file(const String &p_file) {
40
if (lightmap) {
41
LightmapGI::BakeError err = LightmapGI::BAKE_ERROR_OK;
42
const uint64_t time_started = OS::get_singleton()->get_ticks_msec();
43
if (get_tree()->get_edited_scene_root()) {
44
Ref<LightmapGIData> lightmapGIData = lightmap->get_light_data();
45
46
if (lightmapGIData.is_valid()) {
47
String path = lightmapGIData->get_path();
48
if (!path.is_resource_file()) {
49
int srpos = path.find("::");
50
if (srpos != -1) {
51
String base = path.substr(0, srpos);
52
if (ResourceLoader::get_resource_type(base) == "PackedScene") {
53
if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
54
err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
55
}
56
} else {
57
if (FileAccess::exists(base + ".import")) {
58
err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
59
}
60
}
61
}
62
} else {
63
if (FileAccess::exists(path + ".import")) {
64
err = LightmapGI::BAKE_ERROR_FOREIGN_DATA;
65
}
66
}
67
}
68
69
if (err == LightmapGI::BAKE_ERROR_OK) {
70
if (get_tree()->get_edited_scene_root() == lightmap) {
71
err = lightmap->bake(lightmap, p_file, bake_func_step);
72
} else {
73
err = lightmap->bake(lightmap->get_parent(), p_file, bake_func_step);
74
}
75
}
76
} else {
77
err = LightmapGI::BAKE_ERROR_NO_SCENE_ROOT;
78
}
79
80
bake_func_end(time_started);
81
82
switch (err) {
83
case LightmapGI::BAKE_ERROR_NO_SAVE_PATH: {
84
String scene_path = lightmap->get_scene_file_path();
85
if (scene_path.is_empty() && lightmap->get_owner()) {
86
scene_path = lightmap->get_owner()->get_scene_file_path();
87
}
88
if (scene_path.is_empty()) {
89
EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for lightmap images.\nSave your scene and try again."));
90
break;
91
}
92
scene_path = scene_path.get_basename() + ".lmbake";
93
94
file_dialog->set_current_path(scene_path);
95
file_dialog->popup_file_dialog();
96
} break;
97
case LightmapGI::BAKE_ERROR_NO_MESHES: {
98
EditorNode::get_singleton()->show_warning(
99
TTR("No meshes with lightmapping support to bake. Make sure they contain UV2 data and their Global Illumination property is set to Static.") +
100
String::utf8("\n\n• ") + TTR("To import a scene with lightmapping support, set Meshes > Light Baking to Static Lightmaps in the Import dock.") +
101
String::utf8("\n• ") + TTR("To enable lightmapping support on a primitive mesh, edit the PrimitiveMesh resource in the inspector and check Add UV2.") +
102
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."));
103
} break;
104
case LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE: {
105
EditorNode::get_singleton()->show_warning(TTR("Failed creating lightmap images. Make sure the lightmap destination path is writable."));
106
} break;
107
case LightmapGI::BAKE_ERROR_NO_SCENE_ROOT: {
108
EditorNode::get_singleton()->show_warning(TTR("No editor scene root found."));
109
} break;
110
case LightmapGI::BAKE_ERROR_FOREIGN_DATA: {
111
EditorNode::get_singleton()->show_warning(TTR("Lightmap data is not local to the scene."));
112
} break;
113
case LightmapGI::BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL: {
114
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."));
115
} break;
116
case LightmapGI::BAKE_ERROR_LIGHTMAP_TOO_SMALL: {
117
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."));
118
} break;
119
case LightmapGI::BAKE_ERROR_ATLAS_TOO_SMALL: {
120
EditorNode::get_singleton()->show_warning(TTR("Failed fitting a lightmap image into an atlas. This should never happen and should be reported."));
121
} break;
122
default: {
123
} break;
124
}
125
}
126
}
127
128
void LightmapGIEditorPlugin::_bake() {
129
_bake_select_file("");
130
}
131
132
void LightmapGIEditorPlugin::edit(Object *p_object) {
133
LightmapGI *s = Object::cast_to<LightmapGI>(p_object);
134
if (!s) {
135
return;
136
}
137
138
lightmap = s;
139
}
140
141
bool LightmapGIEditorPlugin::handles(Object *p_object) const {
142
return p_object->is_class("LightmapGI");
143
}
144
145
void LightmapGIEditorPlugin::make_visible(bool p_visible) {
146
if (p_visible) {
147
bake->show();
148
} else {
149
bake->hide();
150
}
151
}
152
153
EditorProgress *LightmapGIEditorPlugin::tmp_progress = nullptr;
154
155
bool LightmapGIEditorPlugin::bake_func_step(float p_progress, const String &p_description, void *, bool p_refresh) {
156
if (!tmp_progress) {
157
tmp_progress = memnew(EditorProgress("bake_lightmaps", TTR("Bake Lightmaps"), 1000, true));
158
ERR_FAIL_NULL_V(tmp_progress, false);
159
}
160
return tmp_progress->step(p_description, p_progress * 1000, p_refresh);
161
}
162
163
void LightmapGIEditorPlugin::bake_func_end(uint64_t p_time_started) {
164
if (tmp_progress != nullptr) {
165
memdelete(tmp_progress);
166
tmp_progress = nullptr;
167
}
168
169
const int time_taken = (OS::get_singleton()->get_ticks_msec() - p_time_started) * 0.001;
170
print_line(vformat("Done baking lightmaps in %02d:%02d:%02d.", time_taken / 3600, (time_taken % 3600) / 60, time_taken % 60));
171
// Request attention in case the user was doing something else.
172
// Baking lightmaps is likely the editor task that can take the most time,
173
// so only request the attention for baking lightmaps.
174
DisplayServer::get_singleton()->window_request_attention();
175
}
176
177
void LightmapGIEditorPlugin::_bind_methods() {
178
ClassDB::bind_method("_bake", &LightmapGIEditorPlugin::_bake);
179
}
180
181
LightmapGIEditorPlugin::LightmapGIEditorPlugin() {
182
bake = memnew(Button);
183
bake->set_theme_type_variation(SceneStringName(FlatButton));
184
// TODO: Rework this as a dedicated toolbar control so we can hook into theme changes and update it
185
// when the editor theme updates.
186
bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
187
bake->set_text(TTR("Bake Lightmaps"));
188
189
#ifdef MODULE_LIGHTMAPPER_RD_ENABLED
190
// Disable lightmap baking if not supported on the current GPU.
191
if (!DisplayServer::get_singleton()->can_create_rendering_device()) {
192
bake->set_disabled(true);
193
bake->set_tooltip_text(vformat(TTR("Lightmap baking is not supported on this GPU (%s)."), RenderingServer::get_singleton()->get_video_adapter_name()));
194
}
195
#else
196
// Disable lightmap baking if the module is disabled at compile-time.
197
bake->set_disabled(true);
198
#if defined(ANDROID_ENABLED) || defined(APPLE_EMBEDDED_ENABLED)
199
bake->set_tooltip_text(vformat(TTR("Lightmaps cannot be baked on %s."), OS::get_singleton()->get_name()));
200
#else
201
bake->set_tooltip_text(TTR("Lightmaps cannot be baked, as the `lightmapper_rd` module was disabled at compile-time."));
202
#endif
203
#endif // MODULE_LIGHTMAPPER_RD_ENABLED
204
205
bake->hide();
206
bake->connect(SceneStringName(pressed), Callable(this, "_bake"));
207
add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
208
lightmap = nullptr;
209
210
file_dialog = memnew(EditorFileDialog);
211
file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
212
file_dialog->add_filter("*.lmbake", TTR("LightMap Bake"));
213
file_dialog->set_title(TTR("Select lightmap bake file:"));
214
file_dialog->connect("file_selected", callable_mp(this, &LightmapGIEditorPlugin::_bake_select_file));
215
bake->add_child(file_dialog);
216
}
217
218