mesh_editor_plugin.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "core/config/project_settings.h"
  32. #include "editor/themes/editor_scale.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/main/viewport.h"
  35. void MeshEditor::gui_input(const Ref<InputEvent> &p_event) {
  36. ERR_FAIL_COND(p_event.is_null());
  37. Ref<InputEventMouseMotion> mm = p_event;
  38. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  39. rot_x -= mm->get_relative().y * 0.01;
  40. rot_y -= mm->get_relative().x * 0.01;
  41. rot_x = CLAMP(rot_x, -Math_PI / 2, Math_PI / 2);
  42. _update_rotation();
  43. }
  44. }
  45. void MeshEditor::_update_theme_item_cache() {
  46. SubViewportContainer::_update_theme_item_cache();
  47. theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
  48. theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
  49. }
  50. void MeshEditor::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_THEME_CHANGED: {
  53. light_1_switch->set_button_icon(theme_cache.light_1_icon);
  54. light_2_switch->set_button_icon(theme_cache.light_2_icon);
  55. } break;
  56. }
  57. }
  58. void MeshEditor::_update_rotation() {
  59. Transform3D 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::deg_to_rad(-15.0);
  68. rot_y = Math::deg_to_rad(30.0);
  69. _update_rotation();
  70. AABB aabb = mesh->get_aabb();
  71. Vector3 ofs = aabb.get_center();
  72. float m = aabb.get_longest_axis_size();
  73. if (m != 0) {
  74. m = 1.0 / m;
  75. m *= 0.5;
  76. Transform3D 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::_on_light_1_switch_pressed() {
  84. light1->set_visible(light_1_switch->is_pressed());
  85. }
  86. void MeshEditor::_on_light_2_switch_pressed() {
  87. light2->set_visible(light_2_switch->is_pressed());
  88. }
  89. MeshEditor::MeshEditor() {
  90. viewport = memnew(SubViewport);
  91. Ref<World3D> world_3d;
  92. world_3d.instantiate();
  93. viewport->set_world_3d(world_3d); // Use own world.
  94. add_child(viewport);
  95. viewport->set_disable_input(true);
  96. viewport->set_msaa_3d(Viewport::MSAA_4X);
  97. set_stretch(true);
  98. camera = memnew(Camera3D);
  99. camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
  100. camera->set_perspective(45, 0.1, 10);
  101. viewport->add_child(camera);
  102. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  103. camera_attributes.instantiate();
  104. camera->set_attributes(camera_attributes);
  105. }
  106. light1 = memnew(DirectionalLight3D);
  107. light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  108. viewport->add_child(light1);
  109. light2 = memnew(DirectionalLight3D);
  110. light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  111. light2->set_color(Color(0.7, 0.7, 0.7));
  112. viewport->add_child(light2);
  113. rotation = memnew(Node3D);
  114. viewport->add_child(rotation);
  115. mesh_instance = memnew(MeshInstance3D);
  116. rotation->add_child(mesh_instance);
  117. set_custom_minimum_size(Size2(1, 150) * EDSCALE);
  118. HBoxContainer *hb = memnew(HBoxContainer);
  119. add_child(hb);
  120. hb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  121. hb->add_spacer();
  122. VBoxContainer *vb_light = memnew(VBoxContainer);
  123. hb->add_child(vb_light);
  124. light_1_switch = memnew(Button);
  125. light_1_switch->set_theme_type_variation("PreviewLightButton");
  126. light_1_switch->set_toggle_mode(true);
  127. light_1_switch->set_pressed(true);
  128. vb_light->add_child(light_1_switch);
  129. light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MeshEditor::_on_light_1_switch_pressed));
  130. light_2_switch = memnew(Button);
  131. light_2_switch->set_theme_type_variation("PreviewLightButton");
  132. light_2_switch->set_toggle_mode(true);
  133. light_2_switch->set_pressed(true);
  134. vb_light->add_child(light_2_switch);
  135. light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MeshEditor::_on_light_2_switch_pressed));
  136. rot_x = 0;
  137. rot_y = 0;
  138. }
  139. ///////////////////////
  140. bool EditorInspectorPluginMesh::can_handle(Object *p_object) {
  141. return Object::cast_to<Mesh>(p_object) != nullptr;
  142. }
  143. void EditorInspectorPluginMesh::parse_begin(Object *p_object) {
  144. Mesh *mesh = Object::cast_to<Mesh>(p_object);
  145. if (!mesh) {
  146. return;
  147. }
  148. Ref<Mesh> m(mesh);
  149. MeshEditor *editor = memnew(MeshEditor);
  150. editor->edit(m);
  151. add_custom_control(editor);
  152. }
  153. MeshEditorPlugin::MeshEditorPlugin() {
  154. Ref<EditorInspectorPluginMesh> plugin;
  155. plugin.instantiate();
  156. add_inspector_plugin(plugin);
  157. }