1 /*************************************************************************/
2 /* baked_light_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_light_editor_plugin.h"
31 #include "io/marshalls.h"
32 #include "io/resource_saver.h"
33 #include "scene/3d/mesh_instance.h"
34 #include "scene/gui/box_container.h"
35
_end_baking()36 void BakedLightEditor::_end_baking() {
37
38 baker->clear();
39 set_process(false);
40 button_bake->set_pressed(false);
41 bake_info->set_text("");
42 }
43
_node_removed(Node * p_node)44 void BakedLightEditor::_node_removed(Node *p_node) {
45
46 if (p_node == node) {
47 _end_baking();
48 node = NULL;
49
50 hide();
51 }
52 }
53
_notification(int p_option)54 void BakedLightEditor::_notification(int p_option) {
55
56 if (p_option == NOTIFICATION_ENTER_TREE) {
57
58 button_bake->set_icon(get_icon("Bake", "EditorIcons"));
59 button_reset->set_icon(get_icon("Reload", "EditorIcons"));
60 button_make_lightmaps->set_icon(get_icon("LightMap", "EditorIcons"));
61 }
62
63 if (p_option == NOTIFICATION_PROCESS) {
64
65 if (baker->is_baking() && !baker->is_paused()) {
66
67 update_timeout -= get_process_delta_time();
68 if (update_timeout < 0) {
69
70 if (baker->get_baked_light() != node->get_baked_light()) {
71 _end_baking();
72 return;
73 }
74
75 uint64_t t = OS::get_singleton()->get_ticks_msec();
76
77 #ifdef DEBUG_CUBES
78 double norm = baker->get_normalization();
79 float max_lum = 0;
80
81 {
82 DVector<Color>::Write cw = colors.write();
83 BakedLightBaker::Octant *octants = baker->octant_pool.ptr();
84 BakedLightBaker::Octant *oct = &octants[baker->leaf_list];
85 int vert_idx = 0;
86
87 while (oct) {
88
89 Color colors[8];
90 for (int i = 0; i < 8; i++) {
91
92 colors[i].r = oct->light_accum[i][0] / norm;
93 colors[i].g = oct->light_accum[i][1] / norm;
94 colors[i].b = oct->light_accum[i][2] / norm;
95
96 float lum = colors[i].get_v();
97 //if (lum<0.05)
98 // color.a=0;
99 if (lum > max_lum)
100 max_lum = lum;
101 }
102 static const int vert2cub[36] = { 7, 3, 1, 1, 5, 7, 7, 6, 2, 2, 3, 7, 7, 5, 4, 4, 6, 7, 2, 6, 4, 4, 0, 2, 4, 5, 1, 1, 0, 4, 1, 3, 2, 2, 0, 1 };
103 for (int i = 0; i < 36; i++) {
104
105 cw[vert_idx++] = colors[vert2cub[i]];
106 }
107
108 if (oct->next_leaf)
109 oct = &octants[oct->next_leaf];
110 else
111 oct = NULL;
112 }
113 }
114 print_line("MSCOL: " + itos(OS::get_singleton()->get_ticks_msec() - t));
115 t = OS::get_singleton()->get_ticks_msec();
116
117 Array a;
118 a.resize(Mesh::ARRAY_MAX);
119 a[Mesh::ARRAY_VERTEX] = vertices;
120 a[Mesh::ARRAY_COLOR] = colors;
121 while (mesh->get_surface_count())
122 mesh->surface_remove(0);
123 mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, a);
124 mesh->surface_set_material(0, material);
125 #endif
126 ERR_FAIL_COND(node->get_baked_light().is_null());
127
128 baker->update_octree_images(octree_texture, light_texture);
129 baker->update_octree_sampler(octree_sampler);
130 //print_line("sampler size: "+itos(octree_sampler.size()*4));
131
132 #if 1
133 //debug
134 Image img(baker->baked_octree_texture_w, baker->baked_octree_texture_h, 0, Image::FORMAT_RGBA, octree_texture);
135 Ref<ImageTexture> it = memnew(ImageTexture);
136 it->create_from_image(img);
137 ResourceSaver::save("baked_octree.png", it);
138
139 #endif
140
141 uint64_t rays_snap = baker->get_rays_thrown();
142 int rays_sec = (rays_snap - last_rays_time) * 1.0 - (update_timeout);
143 last_rays_time = rays_snap;
144
145 bake_info->set_text("rays/s: " + itos(rays_sec));
146 update_timeout = 1;
147 print_line("MSUPDATE: " + itos(OS::get_singleton()->get_ticks_msec() - t));
148 t = OS::get_singleton()->get_ticks_msec();
149 node->get_baked_light()->set_octree(octree_texture);
150 node->get_baked_light()->set_light(light_texture);
151 node->get_baked_light()->set_sampler_octree(octree_sampler);
152 node->get_baked_light()->set_edited(true);
153
154 print_line("MSSET: " + itos(OS::get_singleton()->get_ticks_msec() - t));
155 }
156 }
157 }
158 }
159
_menu_option(int p_option)160 void BakedLightEditor::_menu_option(int p_option) {
161
162 switch (p_option) {
163
164 case MENU_OPTION_BAKE: {
165
166 ERR_FAIL_COND(!node);
167 ERR_FAIL_COND(node->get_baked_light().is_null());
168 baker->bake(node->get_baked_light(), node);
169 node->get_baked_light()->set_mode(BakedLight::MODE_OCTREE);
170 update_timeout = 0;
171 set_process(true);
172
173 } break;
174 case MENU_OPTION_CLEAR: {
175
176 } break;
177 }
178 }
179
_bake_pressed()180 void BakedLightEditor::_bake_pressed() {
181
182 ERR_FAIL_COND(!node);
183 const String conf_warning = node->get_configuration_warning();
184 if (!conf_warning.empty()) {
185 err_dialog->set_text(conf_warning);
186 err_dialog->popup_centered_minsize();
187 button_bake->set_pressed(false);
188 return;
189 }
190
191 if (baker->is_baking()) {
192
193 baker->set_pause(!button_bake->is_pressed());
194 if (baker->is_paused()) {
195
196 set_process(false);
197 bake_info->set_text("");
198 button_reset->show();
199 button_make_lightmaps->show();
200
201 } else {
202
203 update_timeout = 0;
204 set_process(true);
205 button_make_lightmaps->hide();
206 button_reset->hide();
207 }
208 } else {
209 baker->bake(node->get_baked_light(), node);
210 node->get_baked_light()->set_mode(BakedLight::MODE_OCTREE);
211 update_timeout = 0;
212
213 last_rays_time = 0;
214 button_bake->set_pressed(false);
215
216 set_process(true);
217 }
218 }
219
_clear_pressed()220 void BakedLightEditor::_clear_pressed() {
221
222 baker->clear();
223 button_bake->set_pressed(false);
224 bake_info->set_text("");
225 }
226
edit(BakedLightInstance * p_baked_light)227 void BakedLightEditor::edit(BakedLightInstance *p_baked_light) {
228
229 if (p_baked_light == NULL || node == p_baked_light) {
230 return;
231 }
232 if (node && node != p_baked_light)
233 _end_baking();
234
235 node = p_baked_light;
236 //_end_baking();
237 }
238
_bake_lightmaps()239 void BakedLightEditor::_bake_lightmaps() {
240
241 Error err = baker->transfer_to_lightmaps();
242 if (err) {
243
244 err_dialog->set_text("Error baking to lightmaps!\nMake sure that a bake has just\n happened and that lightmaps are\n configured. ");
245 err_dialog->popup_centered_minsize();
246 return;
247 }
248
249 node->get_baked_light()->set_mode(BakedLight::MODE_LIGHTMAPS);
250 }
251
_bind_methods()252 void BakedLightEditor::_bind_methods() {
253
254 ObjectTypeDB::bind_method("_menu_option", &BakedLightEditor::_menu_option);
255 ObjectTypeDB::bind_method("_bake_pressed", &BakedLightEditor::_bake_pressed);
256 ObjectTypeDB::bind_method("_clear_pressed", &BakedLightEditor::_clear_pressed);
257 ObjectTypeDB::bind_method("_bake_lightmaps", &BakedLightEditor::_bake_lightmaps);
258 }
259
BakedLightEditor()260 BakedLightEditor::BakedLightEditor() {
261
262 bake_hbox = memnew(HBoxContainer);
263 button_bake = memnew(ToolButton);
264 button_bake->set_text(TTR("Bake!"));
265 button_bake->set_toggle_mode(true);
266 button_reset = memnew(Button);
267 button_make_lightmaps = memnew(Button);
268 button_bake->set_tooltip("Start/Unpause the baking process.\nThis bakes lighting into the lightmap octree.");
269 button_make_lightmaps->set_tooltip("Convert the lightmap octree to lightmap textures\n(must have set up UV/Lightmaps properly before!).");
270
271 bake_info = memnew(Label);
272 bake_hbox->add_child(button_bake);
273 bake_hbox->add_child(button_reset);
274 bake_hbox->add_child(bake_info);
275
276 err_dialog = memnew(AcceptDialog);
277 add_child(err_dialog);
278 node = NULL;
279 baker = memnew(BakedLightBaker);
280
281 bake_hbox->add_child(button_make_lightmaps);
282 button_make_lightmaps->hide();
283
284 button_bake->connect("pressed", this, "_bake_pressed");
285 button_reset->connect("pressed", this, "_clear_pressed");
286 button_make_lightmaps->connect("pressed", this, "_bake_lightmaps");
287 button_reset->hide();
288 button_reset->set_tooltip(TTR("Reset the lightmap octree baking process (start over)."));
289
290 update_timeout = 0;
291 }
292
~BakedLightEditor()293 BakedLightEditor::~BakedLightEditor() {
294
295 memdelete(baker);
296 }
297
edit(Object * p_object)298 void BakedLightEditorPlugin::edit(Object *p_object) {
299
300 baked_light_editor->edit(p_object->cast_to<BakedLightInstance>());
301 }
302
handles(Object * p_object) const303 bool BakedLightEditorPlugin::handles(Object *p_object) const {
304
305 return p_object->is_type("BakedLightInstance");
306 }
307
make_visible(bool p_visible)308 void BakedLightEditorPlugin::make_visible(bool p_visible) {
309
310 if (p_visible) {
311 baked_light_editor->show();
312 baked_light_editor->bake_hbox->show();
313 } else {
314
315 baked_light_editor->hide();
316 baked_light_editor->bake_hbox->hide();
317 baked_light_editor->edit(NULL);
318 }
319 }
320
BakedLightEditorPlugin(EditorNode * p_node)321 BakedLightEditorPlugin::BakedLightEditorPlugin(EditorNode *p_node) {
322
323 editor = p_node;
324 baked_light_editor = memnew(BakedLightEditor);
325 editor->get_viewport()->add_child(baked_light_editor);
326 add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, baked_light_editor->bake_hbox);
327 baked_light_editor->hide();
328 baked_light_editor->bake_hbox->hide();
329 }
330
~BakedLightEditorPlugin()331 BakedLightEditorPlugin::~BakedLightEditorPlugin() {
332 }
333