baked_light_editor_plugin.cpp 10 KB

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