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