mesh_editor_plugin.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "core/config/project_settings.h"
  32. #include "editor/editor_scale.h"
  33. void MeshEditor::gui_input(const Ref<InputEvent> &p_event) {
  34. ERR_FAIL_COND(p_event.is_null());
  35. Ref<InputEventMouseMotion> mm = p_event;
  36. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  37. rot_x -= mm->get_relative().y * 0.01;
  38. rot_y -= mm->get_relative().x * 0.01;
  39. if (rot_x < -Math_PI / 2) {
  40. rot_x = -Math_PI / 2;
  41. } else if (rot_x > Math_PI / 2) {
  42. rot_x = Math_PI / 2;
  43. }
  44. _update_rotation();
  45. }
  46. }
  47. void MeshEditor::_update_theme_item_cache() {
  48. SubViewportContainer::_update_theme_item_cache();
  49. theme_cache.light_1_on = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons"));
  50. theme_cache.light_1_off = get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons"));
  51. theme_cache.light_2_on = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons"));
  52. theme_cache.light_2_off = get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons"));
  53. }
  54. void MeshEditor::_notification(int p_what) {
  55. switch (p_what) {
  56. case NOTIFICATION_THEME_CHANGED: {
  57. light_1_switch->set_normal_texture(theme_cache.light_1_on);
  58. light_1_switch->set_pressed_texture(theme_cache.light_1_off);
  59. light_2_switch->set_normal_texture(theme_cache.light_2_on);
  60. light_2_switch->set_pressed_texture(theme_cache.light_2_off);
  61. } break;
  62. }
  63. }
  64. void MeshEditor::_update_rotation() {
  65. Transform3D 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. rot_x = Math::deg_to_rad(-15.0);
  74. rot_y = Math::deg_to_rad(30.0);
  75. _update_rotation();
  76. AABB aabb = mesh->get_aabb();
  77. Vector3 ofs = aabb.get_center();
  78. float m = aabb.get_longest_axis_size();
  79. if (m != 0) {
  80. m = 1.0 / m;
  81. m *= 0.5;
  82. Transform3D xform;
  83. xform.basis.scale(Vector3(m, m, m));
  84. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  85. //xform.origin.z -= aabb.get_longest_axis_size() * 2;
  86. mesh_instance->set_transform(xform);
  87. }
  88. }
  89. void MeshEditor::_button_pressed(Node *p_button) {
  90. if (p_button == light_1_switch) {
  91. light1->set_visible(!light_1_switch->is_pressed());
  92. }
  93. if (p_button == light_2_switch) {
  94. light2->set_visible(!light_2_switch->is_pressed());
  95. }
  96. }
  97. MeshEditor::MeshEditor() {
  98. viewport = memnew(SubViewport);
  99. Ref<World3D> world_3d;
  100. world_3d.instantiate();
  101. viewport->set_world_3d(world_3d); //use own world
  102. add_child(viewport);
  103. viewport->set_disable_input(true);
  104. viewport->set_msaa_3d(Viewport::MSAA_4X);
  105. set_stretch(true);
  106. camera = memnew(Camera3D);
  107. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
  108. camera->set_perspective(45, 0.1, 10);
  109. viewport->add_child(camera);
  110. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  111. camera_attributes.instantiate();
  112. camera->set_attributes(camera_attributes);
  113. }
  114. light1 = memnew(DirectionalLight3D);
  115. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  116. viewport->add_child(light1);
  117. light2 = memnew(DirectionalLight3D);
  118. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  119. light2->set_color(Color(0.7, 0.7, 0.7));
  120. viewport->add_child(light2);
  121. rotation = memnew(Node3D);
  122. viewport->add_child(rotation);
  123. mesh_instance = memnew(MeshInstance3D);
  124. rotation->add_child(mesh_instance);
  125. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  126. HBoxContainer *hb = memnew(HBoxContainer);
  127. add_child(hb);
  128. hb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  129. hb->add_spacer();
  130. VBoxContainer *vb_light = memnew(VBoxContainer);
  131. hb->add_child(vb_light);
  132. light_1_switch = memnew(TextureButton);
  133. light_1_switch->set_toggle_mode(true);
  134. vb_light->add_child(light_1_switch);
  135. light_1_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_1_switch));
  136. light_2_switch = memnew(TextureButton);
  137. light_2_switch->set_toggle_mode(true);
  138. vb_light->add_child(light_2_switch);
  139. light_2_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_2_switch));
  140. rot_x = 0;
  141. rot_y = 0;
  142. }
  143. ///////////////////////
  144. bool EditorInspectorPluginMesh::can_handle(Object *p_object) {
  145. return Object::cast_to<Mesh>(p_object) != nullptr;
  146. }
  147. void EditorInspectorPluginMesh::parse_begin(Object *p_object) {
  148. Mesh *mesh = Object::cast_to<Mesh>(p_object);
  149. if (!mesh) {
  150. return;
  151. }
  152. Ref<Mesh> m(mesh);
  153. MeshEditor *editor = memnew(MeshEditor);
  154. editor->edit(m);
  155. add_custom_control(editor);
  156. }
  157. MeshEditorPlugin::MeshEditorPlugin() {
  158. Ref<EditorInspectorPluginMesh> plugin;
  159. plugin.instantiate();
  160. add_inspector_plugin(plugin);
  161. }