baked_light_editor_plugin.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "baked_light_editor_plugin.h"
  2. #include "scene/gui/box_container.h"
  3. #include "scene/3d/mesh_instance.h"
  4. #include "io/marshalls.h"
  5. #include "io/resource_saver.h"
  6. void BakedLightEditor::_end_baking() {
  7. baker->clear();
  8. set_process(false);
  9. button_bake->set_pressed(false);
  10. bake_info->set_text("");
  11. }
  12. void BakedLightEditor::_node_removed(Node *p_node) {
  13. if(p_node==node) {
  14. _end_baking();
  15. node=NULL;
  16. hide();
  17. }
  18. }
  19. void BakedLightEditor::_notification(int p_option) {
  20. if (p_option==NOTIFICATION_ENTER_SCENE) {
  21. button_bake->set_icon(get_icon("Bake","EditorIcons"));
  22. button_reset->set_icon(get_icon("Reload","EditorIcons"));
  23. button_make_lightmaps->set_icon(get_icon("LightMap","EditorIcons"));
  24. }
  25. if (p_option==NOTIFICATION_PROCESS) {
  26. if (baker->is_baking() && !baker->is_paused()) {
  27. update_timeout-=get_process_delta_time();
  28. if (update_timeout<0) {
  29. if (baker->get_baked_light()!=node->get_baked_light()) {
  30. _end_baking();
  31. return;
  32. }
  33. uint64_t t = OS::get_singleton()->get_ticks_msec();
  34. #ifdef DEBUG_CUBES
  35. double norm = baker->get_normalization();
  36. float max_lum=0;
  37. {
  38. DVector<Color>::Write cw=colors.write();
  39. BakedLightBaker::Octant *octants=baker->octant_pool.ptr();
  40. BakedLightBaker::Octant *oct = &octants[baker->leaf_list];
  41. int vert_idx=0;
  42. while(oct) {
  43. Color colors[8];
  44. for(int i=0;i<8;i++) {
  45. colors[i].r=oct->light_accum[i][0]/norm;
  46. colors[i].g=oct->light_accum[i][1]/norm;
  47. colors[i].b=oct->light_accum[i][2]/norm;
  48. float lum = colors[i].get_v();
  49. //if (lum<0.05)
  50. // color.a=0;
  51. if (lum>max_lum)
  52. max_lum=lum;
  53. }
  54. 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};
  55. for (int i=0;i<36;i++) {
  56. cw[vert_idx++]=colors[vert2cub[i]];
  57. }
  58. if (oct->next_leaf)
  59. oct=&octants[oct->next_leaf];
  60. else
  61. oct=NULL;
  62. }
  63. }
  64. print_line("MSCOL: "+itos(OS::get_singleton()->get_ticks_msec()-t));
  65. t = OS::get_singleton()->get_ticks_msec();
  66. Array a;
  67. a.resize(Mesh::ARRAY_MAX);
  68. a[Mesh::ARRAY_VERTEX]=vertices;
  69. a[Mesh::ARRAY_COLOR]=colors;
  70. while(mesh->get_surface_count())
  71. mesh->surface_remove(0);
  72. mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES,a);
  73. mesh->surface_set_material(0,material);
  74. #endif
  75. ERR_FAIL_COND(node->get_baked_light().is_null());
  76. baker->update_octree_image(octree_texture);
  77. #if 1
  78. //debug
  79. Image img(baker->baked_octree_texture_w,baker->baked_octree_texture_h,0,Image::FORMAT_RGBA,octree_texture);
  80. Ref<ImageTexture> it = memnew( ImageTexture );
  81. it->create_from_image(img);
  82. ResourceSaver::save("baked_octree.png",it);
  83. #endif
  84. bake_info->set_text("rays/s: "+itos(baker->get_rays_sec()));
  85. update_timeout=1;
  86. print_line("MSUPDATE: "+itos(OS::get_singleton()->get_ticks_msec()-t));
  87. t=OS::get_singleton()->get_ticks_msec();
  88. node->get_baked_light()->set_octree(octree_texture);
  89. node->get_baked_light()->set_edited(true);
  90. print_line("MSSET: "+itos(OS::get_singleton()->get_ticks_msec()-t));
  91. }
  92. }
  93. }
  94. }
  95. void BakedLightEditor::_menu_option(int p_option) {
  96. switch(p_option) {
  97. case MENU_OPTION_BAKE: {
  98. ERR_FAIL_COND(!node);
  99. ERR_FAIL_COND(node->get_baked_light().is_null());
  100. baker->bake(node->get_baked_light(),node);
  101. node->get_baked_light()->set_mode(BakedLight::MODE_OCTREE);
  102. update_timeout=0;
  103. set_process(true);
  104. } break;
  105. case MENU_OPTION_CLEAR: {
  106. } break;
  107. }
  108. }
  109. void BakedLightEditor::_bake_pressed() {
  110. ERR_FAIL_COND(!node);
  111. if (node->get_baked_light().is_null()) {
  112. err_dialog->set_text("BakedLightInstance does not contain a BakedLight resource.");
  113. err_dialog->popup_centered(Size2(350,70));
  114. button_bake->set_pressed(false);
  115. return;
  116. }
  117. if (baker->is_baking()) {
  118. baker->set_pause(!button_bake->is_pressed());
  119. if (baker->is_paused()) {
  120. set_process(false);
  121. bake_info->set_text("");
  122. button_reset->show();
  123. button_make_lightmaps->show();
  124. } else {
  125. update_timeout=0;
  126. set_process(true);
  127. button_make_lightmaps->hide();
  128. button_reset->hide();
  129. }
  130. } else {
  131. baker->bake(node->get_baked_light(),node);
  132. node->get_baked_light()->set_mode(BakedLight::MODE_OCTREE);
  133. update_timeout=0;
  134. set_process(true);
  135. }
  136. }
  137. void BakedLightEditor::_clear_pressed(){
  138. baker->clear();
  139. button_bake->set_pressed(false);
  140. bake_info->set_text("");
  141. }
  142. void BakedLightEditor::edit(BakedLightInstance *p_baked_light) {
  143. if (p_baked_light==NULL || node==p_baked_light) {
  144. return;
  145. }
  146. if (node && node!=p_baked_light)
  147. _end_baking();
  148. node=p_baked_light;
  149. //_end_baking();
  150. }
  151. void BakedLightEditor::_bake_lightmaps() {
  152. Error err = baker->transfer_to_lightmaps();
  153. if (err) {
  154. err_dialog->set_text("Error baking to lightmaps!\nMake sure that a bake has just\n happened and that lightmaps are\n configured. ");
  155. err_dialog->popup_centered(Size2(350,70));
  156. return;
  157. }
  158. node->get_baked_light()->set_mode(BakedLight::MODE_LIGHTMAPS);
  159. }
  160. void BakedLightEditor::_bind_methods() {
  161. ObjectTypeDB::bind_method("_menu_option",&BakedLightEditor::_menu_option);
  162. ObjectTypeDB::bind_method("_bake_pressed",&BakedLightEditor::_bake_pressed);
  163. ObjectTypeDB::bind_method("_clear_pressed",&BakedLightEditor::_clear_pressed);
  164. ObjectTypeDB::bind_method("_bake_lightmaps",&BakedLightEditor::_bake_lightmaps);
  165. }
  166. BakedLightEditor::BakedLightEditor() {
  167. bake_hbox = memnew( HBoxContainer );
  168. button_bake = memnew( ToolButton );
  169. button_bake->set_text("Bake!");
  170. button_bake->set_toggle_mode(true);
  171. button_reset = memnew( Button );
  172. button_make_lightmaps = memnew( Button );
  173. button_bake->set_tooltip("Start/Unpause the baking process.\nThis bakes lighting into the lightmap octree.");
  174. button_make_lightmaps ->set_tooltip("Convert the lightmap octree to lightmap textures\n(must have set up UV/Lightmaps properly before!).");
  175. bake_info = memnew( Label );
  176. bake_hbox->add_child( button_bake );
  177. bake_hbox->add_child( button_reset );
  178. bake_hbox->add_child( bake_info );
  179. err_dialog = memnew( AcceptDialog );
  180. add_child(err_dialog);
  181. node=NULL;
  182. baker = memnew( BakedLightBaker );
  183. bake_hbox->add_child(button_make_lightmaps);
  184. button_make_lightmaps->hide();
  185. button_bake->connect("pressed",this,"_bake_pressed");
  186. button_reset->connect("pressed",this,"_clear_pressed");
  187. button_make_lightmaps->connect("pressed",this,"_bake_lightmaps");
  188. button_reset->hide();
  189. button_reset->set_tooltip("Reset the lightmap octree baking process (start over).");
  190. update_timeout=0;
  191. }
  192. BakedLightEditor::~BakedLightEditor() {
  193. memdelete(baker);
  194. }
  195. void BakedLightEditorPlugin::edit(Object *p_object) {
  196. baked_light_editor->edit(p_object->cast_to<BakedLightInstance>());
  197. }
  198. bool BakedLightEditorPlugin::handles(Object *p_object) const {
  199. return p_object->is_type("BakedLightInstance");
  200. }
  201. void BakedLightEditorPlugin::make_visible(bool p_visible) {
  202. if (p_visible) {
  203. baked_light_editor->show();
  204. baked_light_editor->bake_hbox->show();
  205. } else {
  206. baked_light_editor->hide();
  207. baked_light_editor->bake_hbox->hide();
  208. baked_light_editor->edit(NULL);
  209. }
  210. }
  211. BakedLightEditorPlugin::BakedLightEditorPlugin(EditorNode *p_node) {
  212. editor=p_node;
  213. baked_light_editor = memnew( BakedLightEditor );
  214. editor->get_viewport()->add_child(baked_light_editor);
  215. add_custom_control(CONTAINER_SPATIAL_EDITOR_MENU,baked_light_editor->bake_hbox);
  216. baked_light_editor->hide();
  217. baked_light_editor->bake_hbox->hide();
  218. }
  219. BakedLightEditorPlugin::~BakedLightEditorPlugin()
  220. {
  221. }