mesh_editor_plugin.cpp 6.5 KB

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