mesh_editor_plugin.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* mesh_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "mesh_editor_plugin.h"
  31. void MeshEditor::_gui_input(Ref<InputEvent> p_event) {
  32. Ref<InputEventMouseMotion> mm = p_event;
  33. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  34. rot_x -= mm->get_relative().y * 0.01;
  35. rot_y -= mm->get_relative().x * 0.01;
  36. if (rot_x < -Math_PI / 2)
  37. rot_x = -Math_PI / 2;
  38. else if (rot_x > Math_PI / 2) {
  39. rot_x = Math_PI / 2;
  40. }
  41. _update_rotation();
  42. }
  43. }
  44. void MeshEditor::_notification(int p_what) {
  45. if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
  46. }
  47. if (p_what == NOTIFICATION_READY) {
  48. //get_scene()->connect("node_removed",this,"_node_removed");
  49. if (first_enter) {
  50. //it's in propertyeditor so. could be moved around
  51. light_1_switch->set_normal_texture(get_icon("MaterialPreviewLight1", "EditorIcons"));
  52. light_1_switch->set_pressed_texture(get_icon("MaterialPreviewLight1Off", "EditorIcons"));
  53. light_2_switch->set_normal_texture(get_icon("MaterialPreviewLight2", "EditorIcons"));
  54. light_2_switch->set_pressed_texture(get_icon("MaterialPreviewLight2Off", "EditorIcons"));
  55. first_enter = false;
  56. }
  57. }
  58. if (p_what == NOTIFICATION_DRAW) {
  59. Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
  60. Size2 size = get_size();
  61. //draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  62. }
  63. }
  64. void MeshEditor::_update_rotation() {
  65. Transform t;
  66. t.basis.rotate(Vector3(0, 1, 0), -rot_y);
  67. t.basis.rotate(Vector3(1, 0, 0), -rot_x);
  68. rotation->set_transform(t);
  69. }
  70. void MeshEditor::edit(Ref<Mesh> p_mesh) {
  71. mesh = p_mesh;
  72. mesh_instance->set_mesh(mesh);
  73. if (mesh.is_null()) {
  74. hide();
  75. } else {
  76. rot_x = 0;
  77. rot_y = 0;
  78. _update_rotation();
  79. Rect3 aabb = mesh->get_aabb();
  80. print_line("aabb: " + aabb);
  81. Vector3 ofs = aabb.position + aabb.size * 0.5;
  82. float m = aabb.get_longest_axis_size();
  83. if (m != 0) {
  84. m = 1.0 / m;
  85. m *= 0.5;
  86. //print_line("scale: "+rtos(m));
  87. Transform xform;
  88. xform.basis.scale(Vector3(m, m, m));
  89. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  90. //xform.origin.z -= aabb.get_longest_axis_size() * 2;
  91. mesh_instance->set_transform(xform);
  92. }
  93. }
  94. }
  95. void MeshEditor::_button_pressed(Node *p_button) {
  96. if (p_button == light_1_switch) {
  97. light1->set_visible(!light_1_switch->is_pressed());
  98. }
  99. if (p_button == light_2_switch) {
  100. light2->set_visible(!light_2_switch->is_pressed());
  101. }
  102. }
  103. void MeshEditor::_bind_methods() {
  104. ClassDB::bind_method(D_METHOD("_gui_input"), &MeshEditor::_gui_input);
  105. ClassDB::bind_method(D_METHOD("_button_pressed"), &MeshEditor::_button_pressed);
  106. }
  107. MeshEditor::MeshEditor() {
  108. viewport = memnew(Viewport);
  109. Ref<World> world;
  110. world.instance();
  111. viewport->set_world(world); //use own world
  112. add_child(viewport);
  113. viewport->set_disable_input(true);
  114. set_stretch(true);
  115. camera = memnew(Camera);
  116. camera->set_transform(Transform(Basis(), Vector3(0, 0, 1.1)));
  117. camera->set_perspective(45, 0.1, 10);
  118. viewport->add_child(camera);
  119. light1 = memnew(DirectionalLight);
  120. light1->set_transform(Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  121. viewport->add_child(light1);
  122. light2 = memnew(DirectionalLight);
  123. light2->set_transform(Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  124. light2->set_color(Color(0.7, 0.7, 0.7));
  125. viewport->add_child(light2);
  126. rotation = memnew(Spatial);
  127. viewport->add_child(rotation);
  128. mesh_instance = memnew(MeshInstance);
  129. rotation->add_child(mesh_instance);
  130. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  131. HBoxContainer *hb = memnew(HBoxContainer);
  132. add_child(hb);
  133. hb->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 2);
  134. hb->add_spacer();
  135. VBoxContainer *vb_light = memnew(VBoxContainer);
  136. hb->add_child(vb_light);
  137. light_1_switch = memnew(TextureButton);
  138. light_1_switch->set_toggle_mode(true);
  139. vb_light->add_child(light_1_switch);
  140. light_1_switch->connect("pressed", this, "_button_pressed", varray(light_1_switch));
  141. light_2_switch = memnew(TextureButton);
  142. light_2_switch->set_toggle_mode(true);
  143. vb_light->add_child(light_2_switch);
  144. light_2_switch->connect("pressed", this, "_button_pressed", varray(light_2_switch));
  145. first_enter = true;
  146. rot_x = 0;
  147. rot_y = 0;
  148. }
  149. void MeshEditorPlugin::edit(Object *p_object) {
  150. Mesh *s = Object::cast_to<Mesh>(p_object);
  151. if (!s)
  152. return;
  153. mesh_editor->edit(Ref<Mesh>(s));
  154. }
  155. bool MeshEditorPlugin::handles(Object *p_object) const {
  156. return p_object->is_class("Mesh");
  157. }
  158. void MeshEditorPlugin::make_visible(bool p_visible) {
  159. if (p_visible) {
  160. mesh_editor->show();
  161. //mesh_editor->set_process(true);
  162. } else {
  163. mesh_editor->hide();
  164. //mesh_editor->set_process(false);
  165. }
  166. }
  167. MeshEditorPlugin::MeshEditorPlugin(EditorNode *p_node) {
  168. editor = p_node;
  169. mesh_editor = memnew(MeshEditor);
  170. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, mesh_editor);
  171. mesh_editor->hide();
  172. }
  173. MeshEditorPlugin::~MeshEditorPlugin() {
  174. }