mesh_editor_plugin.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "mesh_editor_plugin.h"
  2. void MeshEditor::_input_event(InputEvent p_event) {
  3. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  4. rot_x-=p_event.mouse_motion.relative_y*0.01;
  5. rot_y-=p_event.mouse_motion.relative_x*0.01;
  6. if (rot_x<-Math_PI/2)
  7. rot_x=-Math_PI/2;
  8. else if (rot_x>Math_PI/2) {
  9. rot_x=Math_PI/2;
  10. }
  11. _update_rotation();
  12. }
  13. }
  14. void MeshEditor::_notification(int p_what) {
  15. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  16. }
  17. if (p_what==NOTIFICATION_READY) {
  18. //get_scene()->connect("node_removed",this,"_node_removed");
  19. if (first_enter) {
  20. //it's in propertyeditor so.. could be moved around
  21. light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1","EditorIcons"));
  22. light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off","EditorIcons"));
  23. light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2","EditorIcons"));
  24. light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off","EditorIcons"));
  25. first_enter=false;
  26. }
  27. }
  28. if (p_what==NOTIFICATION_DRAW) {
  29. Ref<Texture> checkerboard = get_icon("Checkerboard","EditorIcons");
  30. Size2 size = get_size();
  31. draw_texture_rect(checkerboard,Rect2(Point2(),size),true);
  32. }
  33. }
  34. void MeshEditor::_update_rotation() {
  35. Transform t;
  36. t.basis.rotate(Vector3(0, 1, 0), rot_y);
  37. t.basis.rotate(Vector3(1, 0, 0), rot_x);
  38. mesh_instance->set_transform(t);
  39. }
  40. void MeshEditor::edit(Ref<Mesh> p_mesh) {
  41. mesh=p_mesh;
  42. mesh_instance->set_mesh(mesh);
  43. if (mesh.is_null()) {
  44. hide();
  45. } else {
  46. rot_x=0;
  47. rot_y=0;
  48. _update_rotation();
  49. AABB aabb= mesh->get_aabb();
  50. Vector3 ofs = aabb.pos + aabb.size*0.5;
  51. aabb.pos-=ofs;
  52. float m = MAX(aabb.size.x,aabb.size.y)*0.5;
  53. if (m!=0) {
  54. m=1.0/m;
  55. m*=0.5;
  56. //print_line("scale: "+rtos(m));
  57. Transform xform;
  58. xform.basis.scale(Vector3(m,m,m));
  59. xform.origin=-xform.basis.xform(ofs); //-ofs*m;
  60. xform.origin.z-=aabb.size.z*2;
  61. mesh_instance->set_transform(xform);
  62. }
  63. }
  64. }
  65. void MeshEditor::_button_pressed(Node* p_button) {
  66. if (p_button==light_1_switch) {
  67. light1->set_enabled(!light_1_switch->is_pressed());
  68. }
  69. if (p_button==light_2_switch) {
  70. light2->set_enabled(!light_2_switch->is_pressed());
  71. }
  72. }
  73. void MeshEditor::_bind_methods() {
  74. ObjectTypeDB::bind_method(_MD("_input_event"),&MeshEditor::_input_event);
  75. ObjectTypeDB::bind_method(_MD("_button_pressed"),&MeshEditor::_button_pressed);
  76. }
  77. MeshEditor::MeshEditor() {
  78. viewport = memnew( Viewport );
  79. Ref<World> world;
  80. world.instance();
  81. viewport->set_world(world); //use own world
  82. add_child(viewport);
  83. viewport->set_process_input(false);
  84. camera = memnew( Camera );
  85. camera->set_transform(Transform(Matrix3(),Vector3(0,0,3)));
  86. camera->set_perspective(45,0.1,10);
  87. viewport->add_child(camera);
  88. light1 = memnew( DirectionalLight );
  89. light1->set_transform(Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0)));
  90. viewport->add_child(light1);
  91. light2 = memnew( DirectionalLight );
  92. light2->set_transform(Transform().looking_at(Vector3(0,1,0),Vector3(0,0,1)));
  93. light2->set_color(Light::COLOR_DIFFUSE,Color(0.7,0.7,0.7));
  94. light2->set_color(Light::COLOR_SPECULAR,Color(0.7,0.7,0.7));
  95. viewport->add_child(light2);
  96. mesh_instance = memnew( MeshInstance );
  97. viewport->add_child(mesh_instance);
  98. set_custom_minimum_size(Size2(1,150)*EDSCALE);
  99. HBoxContainer *hb = memnew( HBoxContainer );
  100. add_child(hb);
  101. hb->set_area_as_parent_rect(2);
  102. hb->add_spacer();
  103. VBoxContainer *vb_light = memnew( VBoxContainer );
  104. hb->add_child(vb_light);
  105. light_1_switch = memnew( TextureButton );
  106. light_1_switch->set_toggle_mode(true);
  107. vb_light->add_child(light_1_switch);
  108. light_1_switch->connect("pressed",this,"_button_pressed",varray(light_1_switch));
  109. light_2_switch = memnew( TextureButton );
  110. light_2_switch->set_toggle_mode(true);
  111. vb_light->add_child(light_2_switch);
  112. light_2_switch->connect("pressed",this,"_button_pressed",varray(light_2_switch));
  113. first_enter=true;
  114. rot_x=0;
  115. rot_y=0;
  116. }
  117. void MeshEditorPlugin::edit(Object *p_object) {
  118. Mesh * s = p_object->cast_to<Mesh>();
  119. if (!s)
  120. return;
  121. mesh_editor->edit(Ref<Mesh>(s));
  122. }
  123. bool MeshEditorPlugin::handles(Object *p_object) const {
  124. return p_object->is_type("Mesh");
  125. }
  126. void MeshEditorPlugin::make_visible(bool p_visible) {
  127. if (p_visible) {
  128. mesh_editor->show();
  129. // mesh_editor->set_process(true);
  130. } else {
  131. mesh_editor->hide();
  132. // mesh_editor->set_process(false);
  133. }
  134. }
  135. MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) {
  136. editor=p_node;
  137. mesh_editor = memnew( MeshEditor );
  138. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM,mesh_editor);
  139. mesh_editor->hide();
  140. }
  141. MeshEditorPlugin::~MeshEditorPlugin()
  142. {
  143. }