Debian Codegraph @

editor/scene/3d/mesh_instance_3d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* mesh_instance_3d_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_instance_3d_editor_plugin.h"
32
33 #include "editor/editor_node.h"
34 #include "editor/editor_string_names.h"
35 #include "editor/editor_undo_redo_manager.h"
36 #include "editor/inspector/multi_node_edit.h"
37 #include "editor/scene/3d/node_3d_editor_plugin.h"
38 #include "editor/themes/editor_scale.h"
39 #include "scene/3d/navigation/navigation_region_3d.h"
40 #include "scene/3d/physics/collision_shape_3d.h"
41 #include "scene/3d/physics/static_body_3d.h"
42 #include "scene/gui/aspect_ratio_container.h"
43 #include "scene/gui/box_container.h"
44 #include "scene/gui/dialogs.h"
45 #include "scene/gui/menu_button.h"
46 #include "scene/gui/spin_box.h"
47 #include "scene/resources/3d/box_shape_3d.h"
48 #include "scene/resources/3d/capsule_shape_3d.h"
49 #include "scene/resources/3d/concave_polygon_shape_3d.h"
50 #include "scene/resources/3d/convex_polygon_shape_3d.h"
51 #include "scene/resources/3d/cylinder_shape_3d.h"
52 #include "scene/resources/3d/primitive_meshes.h"
53 #include "scene/resources/3d/sphere_shape_3d.h"
54
55 void MeshInstance3DEditor::_node_removed(Node *p_node) {
56 if (p_node == node) {
57 node = nullptr;
58 options->hide();
59 }
60 }
61
62 void MeshInstance3DEditor::edit(MeshInstance3D *p_mesh) {
63 node = p_mesh;
64 }
65
66 Vector<Ref<Shape3D>> MeshInstance3DEditor::create_shape_from_mesh(Ref<Mesh> p_mesh, int p_option, bool p_verbose) {
67 Vector<Ref<Shape3D>> shapes;
68 switch (p_option) {
69 case SHAPE_TYPE_TRIMESH: {
70 shapes.push_back(p_mesh->create_trimesh_shape());
71
72 if (p_verbose && shapes.is_empty()) {
73 err_dialog->set_text(TTR("Couldn't create a Trimesh collision shape."));
74 err_dialog->popup_centered();
75 }
76 } break;
77
78 case SHAPE_TYPE_SINGLE_CONVEX: {
79 shapes.push_back(p_mesh->create_convex_shape(true, false));
80
81 if (p_verbose && shapes.is_empty()) {
82 err_dialog->set_text(TTR("Couldn't create a single collision shape."));
83 err_dialog->popup_centered();
84 }
85 } break;
86
87 case SHAPE_TYPE_SIMPLIFIED_CONVEX: {
88 shapes.push_back(p_mesh->create_convex_shape(true, true));
89
90 if (p_verbose && shapes.is_empty()) {
91 err_dialog->set_text(TTR("Couldn't create a simplified collision shape."));
92 err_dialog->popup_centered();
93 }
94 } break;
95
96 case SHAPE_TYPE_MULTIPLE_CONVEX: {
97 Ref<MeshConvexDecompositionSettings> settings;
98 settings.instantiate();
99 settings->set_max_convex_hulls(32);
100 settings->set_max_concavity(0.001);
101
102 shapes = p_mesh->convex_decompose(settings);
103
104 if (p_verbose && shapes.is_empty()) {
105 err_dialog->set_text(TTR("Couldn't create any collision shapes."));
106 err_dialog->popup_centered();
107 }
108 } break;
109
110 case SHAPE_TYPE_BOUNDING_BOX: {
111 const Ref<BoxMesh> box_mesh = p_mesh;
112 if (box_mesh.is_valid()) {
113 Ref<BoxShape3D> box_shape;
114 box_shape.instantiate();
115 box_shape->set_size(box_mesh->get_size().maxf(0.001));
116 shapes.push_back(box_shape);
117 } else {
118 Ref<BoxShape3D> box_shape;
119 box_shape.instantiate();
120 AABB mesh_aabb = p_mesh->get_aabb();
121 box_shape->set_size(mesh_aabb.get_size().maxf(0.001));
122 shapes.push_back(box_shape);
123 shape_offset_transform.origin = mesh_aabb.get_center();
124 }
125
126 if (p_verbose && shapes.is_empty()) {
127 err_dialog->set_text(TTR("Couldn't create a bounding box shape."));
128 err_dialog->popup_centered();
129 }
130 } break;
131
132 case SHAPE_TYPE_CAPSULE: {
133 const Ref<CapsuleMesh> capsule_mesh = p_mesh;
134 if (capsule_mesh.is_valid()) {
135 Ref<CapsuleShape3D> capsule_shape;
136 capsule_shape.instantiate();
137 capsule_shape->set_height(capsule_mesh->get_height());
138 capsule_shape->set_radius(MAX(capsule_mesh->get_radius(), 0.001));
139 shapes.push_back(capsule_shape);
140 } else {
141 // Use AABB to estimate shape.
142 Ref<CapsuleShape3D> capsule_shape;
143 capsule_shape.instantiate();
144 AABB mesh_aabb = p_mesh->get_aabb();
145 int axis = shape_axis->get_selected_id();
146 if (axis == (int)SHAPE_AXIS_LONGEST) {
147 axis = mesh_aabb.get_longest_axis_index();
148 }
149 int perpendicular_axis = axis == 0 ? 1 : 0;
150
151 capsule_shape->set_height(mesh_aabb.get_size()[axis]);
152 capsule_shape->set_radius(MAX(mesh_aabb.get_size()[perpendicular_axis] / 2.0, 0.001));
153 shapes.push_back(capsule_shape);
154
155 shape_offset_transform.origin = mesh_aabb.get_center();
156 if (axis == Vector3::AXIS_X) {
157 shape_offset_transform.rotate_basis(Vector3(0, 0, 1), Math::PI / 2.0);
158 } else if (axis == Vector3::AXIS_Z) {
159 shape_offset_transform.rotate_basis(Vector3(1, 0, 0), -Math::PI / 2.0);
160 }
161 }
162
163 if (p_verbose && shapes.is_empty()) {
164 err_dialog->set_text(TTR("Couldn't create a capsule shape."));
165 err_dialog->popup_centered();
166 }
167 } break;
168
169 case SHAPE_TYPE_CYLINDER: {
170 const Ref<CylinderMesh> cylinder_mesh = p_mesh;
171 if (cylinder_mesh.is_valid()) {
172 Ref<CylinderShape3D> cylinder_shape;
173 cylinder_shape.instantiate();
174 cylinder_shape->set_height(MAX(cylinder_mesh->get_height(), 0.001));
175 cylinder_shape->set_radius((cylinder_mesh->get_top_radius() + cylinder_mesh->get_bottom_radius()) / 2.0);
176 shapes.push_back(cylinder_shape);
177 } else {
178 // Use AABB to estimate shape.
179 Ref<CylinderShape3D> cylinder_shape;
180 cylinder_shape.instantiate();
181 AABB mesh_aabb = p_mesh->get_aabb();
182 int axis = shape_axis->get_selected_id();
183 if (axis == (int)SHAPE_AXIS_LONGEST) {
184 axis = mesh_aabb.get_longest_axis_index();
185 }
186 int perpendicular_axis = axis == 0 ? 1 : 0;
187
188 cylinder_shape->set_height(MAX(mesh_aabb.get_size()[axis], 0.001));
189 cylinder_shape->set_radius(mesh_aabb.get_size()[perpendicular_axis] / 2.0);
190 shapes.push_back(cylinder_shape);
191
192 shape_offset_transform.origin = mesh_aabb.get_center();
193 if (axis == Vector3::AXIS_X) {
194 shape_offset_transform.rotate_basis(Vector3(0, 0, 1), Math::PI / 2.0);
195 } else if (axis == Vector3::AXIS_Z) {
196 shape_offset_transform.rotate_basis(Vector3(1, 0, 0), -Math::PI / 2.0);
197 }
198 }
199
200 if (p_verbose && shapes.is_empty()) {
201 err_dialog->set_text(TTR("Couldn't create a cylinder shape."));
202 err_dialog->popup_centered();
203 }
204 } break;
205
206 case SHAPE_TYPE_SPHERE: {
207 const Ref<SphereMesh> sphere_mesh = p_mesh;
208 if (sphere_mesh.is_valid()) {
209 Ref<SphereShape3D> sphere_shape;
210 sphere_shape.instantiate();
211 sphere_shape->set_radius(MAX(sphere_mesh->get_radius(), 0.001));
212 shapes.push_back(sphere_shape);
213 } else {
214 // Use AABB to estimate shape.
215 Ref<SphereShape3D> sphere_shape;
216 sphere_shape.instantiate();
217 AABB mesh_aabb = p_mesh->get_aabb();
218 sphere_shape->set_radius(MAX(mesh_aabb.get_size()[mesh_aabb.get_size().max_axis_index()] / 2.0, 0.001));
219 shapes.push_back(sphere_shape);
220 shape_offset_transform.origin = mesh_aabb.get_center();
221 }
222
223 if (p_verbose && shapes.is_empty()) {
224 err_dialog->set_text(TTR("Couldn't create a sphere shape."));
225 err_dialog->popup_centered();
226 }
227 } break;
228
229 case SHAPE_TYPE_PRIMITIVE: {
230 const Ref<BoxMesh> box_mesh = p_mesh;
231 if (box_mesh.is_valid()) {
232 Ref<BoxShape3D> box_shape;
233 box_shape.instantiate();
234 box_shape->set_size(box_mesh->get_size().maxf(0.001));
235 shapes.push_back(box_shape);
236 }
237
238 const Ref<CapsuleMesh> capsule_mesh = p_mesh;
239 if (capsule_mesh.is_valid()) {
240 Ref<CapsuleShape3D> capsule_shape;
241 capsule_shape.instantiate();
242 capsule_shape->set_height(capsule_mesh->get_height());
243 capsule_shape->set_radius(MAX(capsule_mesh->get_radius(), 0.001));
244 shapes.push_back(capsule_shape);
245 }
246
247 const Ref<CylinderMesh> cylinder_mesh = p_mesh;
248 if (cylinder_mesh.is_valid()) {
249 Ref<CylinderShape3D> cylinder_shape;
250 cylinder_shape.instantiate();
251 cylinder_shape->set_height(MAX(cylinder_mesh->get_height(), 0.001));
252 cylinder_shape->set_radius((cylinder_mesh->get_top_radius() + cylinder_mesh->get_bottom_radius()) / 2.0);
253 shapes.push_back(cylinder_shape);
254 }
255
256 const Ref<SphereMesh> sphere_mesh = p_mesh;
257 if (sphere_mesh.is_valid()) {
258 Ref<SphereShape3D> sphere_shape;
259 sphere_shape.instantiate();
260 sphere_shape->set_radius(MAX(sphere_mesh->get_radius(), 0.001));
261 shapes.push_back(sphere_shape);
262 }
263
264 if (p_verbose && shapes.is_empty()) {
265 err_dialog->set_text(TTR("Couldn't create a primitive collision shape."));
266 err_dialog->popup_centered();
267 }
268 } break;
269
270 default:
271 break;
272 }
273 return shapes;
274 }
275
276 void MeshInstance3DEditor::_shape_dialog_about_to_popup() {
277 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
278 List<Node *> selection = editor_selection->get_top_selected_node_list();
279 if (selection.is_empty()) {
280 selection.push_back(node);
281 }
282
283 bool disable_primitive = true;
284 for (Node *E : selection) {
285 MeshInstance3D *instance = Object::cast_to<MeshInstance3D>(E);
286 if (!instance) {
287 continue;
288 }
289 Ref<Mesh> m = instance->get_mesh();
290 if (m.is_null()) {
291 continue;
292 }
293 if (((Ref<BoxMesh>)m).is_valid() || ((Ref<CapsuleMesh>)m).is_valid() || ((Ref<CylinderMesh>)m).is_valid() || ((Ref<SphereMesh>)m).is_valid()) {
294 disable_primitive = false;
295 break;
296 }
297 }
298 if (disable_primitive && shape_type->get_selected() == SHAPE_TYPE_PRIMITIVE) {
299 shape_type->select(SHAPE_TYPE_TRIMESH);
300 }
301
302 shape_type->set_item_disabled(shape_type->get_popup()->get_item_index(SHAPE_TYPE_PRIMITIVE), disable_primitive);
303
304 ShapeType selected_shape_type = (ShapeType)shape_type->get_selected_id();
305 bool shape_axis_visible = selected_shape_type == SHAPE_TYPE_CAPSULE || selected_shape_type == SHAPE_TYPE_CYLINDER;
306 shape_axis->set_visible(shape_axis_visible);
307 shape_axis_label->set_visible(shape_axis_visible);
308 }
309
310 void MeshInstance3DEditor::_shape_type_selected(int p_option) {
311 bool shape_axis_visible = (ShapeType)p_option == SHAPE_TYPE_CAPSULE || (ShapeType)p_option == SHAPE_TYPE_CYLINDER;
312 shape_axis->set_visible(shape_axis_visible);
313 shape_axis_label->set_visible(shape_axis_visible);
314 }
315
316 void MeshInstance3DEditor::_create_collision_shape() {
317 int placement_option = shape_placement->get_selected();
318 int shape_type_option = shape_type->get_selected();
319
320 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
321 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
322
323 String placement_action_name = placement_option == SHAPE_PLACEMENT_SIBLING ? TTR("Create %s Collision Shape Sibling") : TTR("Create %s Static Body Child");
324
325 switch (shape_type_option) {
326 case SHAPE_TYPE_TRIMESH: {
327 ur->create_action(vformat(placement_action_name, TTR("Trimesh")));
328 } break;
329 case SHAPE_TYPE_SINGLE_CONVEX: {
330 ur->create_action(vformat(placement_action_name, TTR("Single Convex")));
331 } break;
332 case SHAPE_TYPE_SIMPLIFIED_CONVEX: {
333 ur->create_action(vformat(placement_action_name, TTR("Simplified Convex")));
334 } break;
335 case SHAPE_TYPE_MULTIPLE_CONVEX: {
336 placement_action_name = placement_option == SHAPE_PLACEMENT_SIBLING ? TTR("Create %s Collision Shape Siblings") : TTR("Create %s Static Body Children");
337 ur->create_action(vformat(placement_action_name, TTR("Multiple Convex")));
338 } break;
339 case SHAPE_TYPE_BOUNDING_BOX: {
340 ur->create_action(vformat(placement_action_name, TTR("Bounding Box")));
341 } break;
342 case SHAPE_TYPE_CAPSULE: {
343 ur->create_action(vformat(placement_action_name, TTR("Capsule")));
344 } break;
345 case SHAPE_TYPE_CYLINDER: {
346 ur->create_action(vformat(placement_action_name, TTR("Cylinder")));
347 } break;
348 case SHAPE_TYPE_SPHERE: {
349 ur->create_action(vformat(placement_action_name, TTR("Sphere")));
350 } break;
351 case SHAPE_TYPE_PRIMITIVE: {
352 ur->create_action(vformat(placement_action_name, TTR("Primitive")));
353 } break;
354 default:
355 break;
356 }
357
358 List<Node *> selection = editor_selection->get_top_selected_node_list();
359
360 bool verbose = false;
361 if (selection.is_empty()) {
362 selection.push_back(node);
363 verbose = true;
364 }
365
366 for (Node *E : selection) {
367 if (placement_option == SHAPE_PLACEMENT_SIBLING && E == get_tree()->get_edited_scene_root()) {
368 if (verbose) {
369 err_dialog->set_text(TTR("Can't create a collision shape as sibling for the scene root."));
370 err_dialog->popup_centered();
371 }
372 continue;
373 }
374
375 MeshInstance3D *instance = Object::cast_to<MeshInstance3D>(E);
376 if (!instance) {
377 continue;
378 }
379
380 Ref<Mesh> m = instance->get_mesh();
381 if (m.is_null()) {
382 continue;
383 }
384
385 shape_offset_transform = Transform3D();
386 Vector<Ref<Shape3D>> shapes = create_shape_from_mesh(m, shape_type_option, verbose);
387 if (shapes.is_empty()) {
388 continue;
389 }
390
391 Node *owner = get_tree()->get_edited_scene_root();
392 if (placement_option == SHAPE_PLACEMENT_STATIC_BODY_CHILD) {
393 StaticBody3D *body = memnew(StaticBody3D);
394 body->set_transform(shape_offset_transform);
395
396 ur->add_do_method(instance, "add_child", body, true);
397 ur->add_do_method(body, "set_owner", owner);
398 ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), body);
399
400 for (Ref<Shape3D> shape : shapes) {
401 CollisionShape3D *cshape = memnew(CollisionShape3D);
402 cshape->set_shape(shape);
403 body->add_child(cshape, true);
404 ur->add_do_method(cshape, "set_owner", owner);
405 ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), cshape);
406 }
407 ur->add_do_reference(body);
408 ur->add_undo_method(instance, "remove_child", body);
409 } else {
410 for (Ref<Shape3D> shape : shapes) {
411 CollisionShape3D *cshape = memnew(CollisionShape3D);
412 cshape->set_shape(shape);
413 cshape->set_name("CollisionShape3D");
414 cshape->set_transform(instance->get_transform() * shape_offset_transform);
415 ur->add_do_method(E, "add_sibling", cshape, true);
416 ur->add_do_method(cshape, "set_owner", owner);
417 ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), cshape);
418 ur->add_do_reference(cshape);
419 ur->add_undo_method(instance->get_parent(), "remove_child", cshape);
420 }
421 }
422 }
423
424 ur->commit_action();
425 }
426
427 void MeshInstance3DEditor::_menu_option(int p_option) {
428 Ref<Mesh> mesh = node->get_mesh();
429 if (mesh.is_null()) {
430 err_dialog->set_text(TTR("Mesh is empty!"));
431 err_dialog->popup_centered();
432 return;
433 }
434
435 switch (p_option) {
436 case MENU_OPTION_CREATE_COLLISION_SHAPE: {
437 shape_dialog->popup_centered();
438 } break;
439
440 case MENU_OPTION_CREATE_NAVMESH: {
441 navigation_mesh_dialog->popup_centered(Vector2(200, 90));
442 } break;
443
444 case MENU_OPTION_CREATE_OUTLINE_MESH: {
445 outline_dialog->popup_centered(Vector2(200, 90));
446 } break;
447 case MENU_OPTION_CREATE_DEBUG_TANGENTS: {
448 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
449 ur->create_action(TTR("Create Debug Tangents"));
450
451 MeshInstance3D *tangents = node->create_debug_tangents_node();
452
453 if (tangents) {
454 Node *owner = get_tree()->get_edited_scene_root();
455
456 ur->add_do_reference(tangents);
457 ur->add_do_method(node, "add_child", tangents, true);
458 ur->add_do_method(tangents, "set_owner", owner);
459
460 ur->add_undo_method(node, "remove_child", tangents);
461 }
462
463 ur->commit_action();
464 } break;
465 case MENU_OPTION_CREATE_UV2: {
466 Ref<Mesh> mesh2 = node->get_mesh();
467 if (mesh.is_null()) {
468 err_dialog->set_text(TTR("No mesh to unwrap."));
469 err_dialog->popup_centered();
470 return;
471 }
472
473 // Test if we are allowed to unwrap this mesh resource.
474 String path = mesh2->get_path();
475 int srpos = path.find("::");
476 if (srpos != -1) {
477 String base = path.substr(0, srpos);
478 if (ResourceLoader::get_resource_type(base) == "PackedScene") {
479 if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
480 err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
481 err_dialog->popup_centered();
482 return;
483 }
484 } else {
485 if (FileAccess::exists(path + ".import")) {
486 err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
487 err_dialog->popup_centered();
488 return;
489 }
490 }
491 } else {
492 if (FileAccess::exists(path + ".import")) {
493 err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
494 err_dialog->popup_centered();
495 return;
496 }
497 }
498
499 Ref<PrimitiveMesh> primitive_mesh = mesh2;
500 if (primitive_mesh.is_valid()) {
501 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
502 ur->create_action(TTR("Unwrap UV2"));
503 ur->add_do_method(*primitive_mesh, "set_add_uv2", true);
504 ur->add_undo_method(*primitive_mesh, "set_add_uv2", primitive_mesh->get_add_uv2());
505 ur->commit_action();
506 } else {
507 Ref<ArrayMesh> array_mesh = mesh2;
508 if (array_mesh.is_null()) {
509 err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
510 err_dialog->popup_centered();
511 return;
512 }
513
514 // Preemptively evaluate common fail cases for lightmap unwrapping.
515 {
516 if (array_mesh->get_blend_shape_count() > 0) {
517 err_dialog->set_text(TTR("Can't unwrap mesh with blend shapes."));
518 err_dialog->popup_centered();
519 return;
520 }
521
522 for (int i = 0; i < array_mesh->get_surface_count(); i++) {
523 Mesh::PrimitiveType primitive = array_mesh->surface_get_primitive_type(i);
524
525 if (primitive != Mesh::PRIMITIVE_TRIANGLES) {
526 err_dialog->set_text(TTR("Only triangles are supported for lightmap unwrap."));
527 err_dialog->popup_centered();
528 return;
529 }
530
531 uint64_t format = array_mesh->surface_get_format(i);
532 if (!(format & Mesh::ArrayFormat::ARRAY_FORMAT_NORMAL)) {
533 err_dialog->set_text(TTR("Normals are required for lightmap unwrap."));
534 err_dialog->popup_centered();
535 return;
536 }
537 }
538 }
539
540 Ref<ArrayMesh> unwrapped_mesh = array_mesh->duplicate(false);
541
542 Error err = unwrapped_mesh->lightmap_unwrap(node->get_global_transform());
543 if (err != OK) {
544 err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
545 err_dialog->popup_centered();
546 return;
547 }
548
549 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
550 ur->create_action(TTR("Unwrap UV2"));
551
552 ur->add_do_method(node, "set_mesh", unwrapped_mesh);
553 ur->add_do_reference(unwrapped_mesh.ptr());
554 ur->add_undo_method(node, "set_mesh", array_mesh);
555
556 ur->commit_action();
557 }
558 } break;
559 case MENU_OPTION_DEBUG_UV1: {
560 Ref<Mesh> mesh2 = node->get_mesh();
561 if (mesh2.is_null()) {
562 err_dialog->set_text(TTR("No mesh to debug."));
563 err_dialog->popup_centered();
564 return;
565 }
566 _create_uv_lines(0);
567 } break;
568 case MENU_OPTION_DEBUG_UV2: {
569 Ref<Mesh> mesh2 = node->get_mesh();
570 if (mesh2.is_null()) {
571 err_dialog->set_text(TTR("No mesh to debug."));
572 err_dialog->popup_centered();
573 return;
574 }
575 _create_uv_lines(1);
576 } break;
577 }
578 }
579
580 struct MeshInstance3DEditorEdgeSort {
581 Vector2 a;
582 Vector2 b;
583
584 static uint32_t hash(const MeshInstance3DEditorEdgeSort &p_edge) {
585 uint32_t h = hash_murmur3_one_32(HashMapHasherDefault::hash(p_edge.a));
586 return hash_fmix32(hash_murmur3_one_32(HashMapHasherDefault::hash(p_edge.b), h));
587 }
588
589 bool operator==(const MeshInstance3DEditorEdgeSort &p_b) const {
590 return a == p_b.a && b == p_b.b;
591 }
592
593 MeshInstance3DEditorEdgeSort() {}
594 MeshInstance3DEditorEdgeSort(const Vector2 &p_a, const Vector2 &p_b) {
595 if (p_a < p_b) {
596 a = p_a;
597 b = p_b;
598 } else {
599 b = p_a;
600 a = p_b;
601 }
602 }
603 };
604
605 void MeshInstance3DEditor::_create_uv_lines(int p_layer) {
606 Ref<Mesh> mesh = node->get_mesh();
607 ERR_FAIL_COND(mesh.is_null());
608
609 HashSet<MeshInstance3DEditorEdgeSort, MeshInstance3DEditorEdgeSort> edges;
610 uv_lines.clear();
611 for (int i = 0; i < mesh->get_surface_count(); i++) {
612 if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
613 continue;
614 }
615 Array a = mesh->surface_get_arrays(i);
616
617 Vector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
618 if (uv.is_empty()) {
619 err_dialog->set_text(vformat(TTR("Mesh has no UV in layer %d."), p_layer + 1));
620 err_dialog->popup_centered();
621 return;
622 }
623
624 const Vector2 *r = uv.ptr();
625
626 Vector<int> indices = a[Mesh::ARRAY_INDEX];
627 const int *ri = nullptr;
628
629 int ic;
630
631 if (indices.size()) {
632 ic = indices.size();
633 ri = indices.ptr();
634 } else {
635 ic = uv.size();
636 }
637
638 for (int j = 0; j < ic; j += 3) {
639 for (int k = 0; k < 3; k++) {
640 MeshInstance3DEditorEdgeSort edge;
641 if (ri) {
642 edge.a = r[ri[j + k]];
643 edge.b = r[ri[j + ((k + 1) % 3)]];
644 } else {
645 edge.a = r[j + k];
646 edge.b = r[j + ((k + 1) % 3)];
647 }
648
649 if (edges.has(edge)) {
650 continue;
651 }
652
653 uv_lines.push_back(edge.a);
654 uv_lines.push_back(edge.b);
655 edges.insert(edge);
656 }
657 }
658 }
659
660 debug_uv_dialog->popup_centered();
661 }
662
663 void MeshInstance3DEditor::_debug_uv_draw() {
664 if (uv_lines.is_empty()) {
665 return;
666 }
667
668 debug_uv->set_clip_contents(true);
669 debug_uv->draw_rect(
670 Rect2(Vector2(), debug_uv->get_size()),
671 get_theme_color(SNAME("dark_color_3"), EditorStringName(Editor)));
672
673 // Draw an outline to represent the UV2's beginning and end area (useful on Black OLED theme).
674 // Top-left coordinate needs to be `(1, 1)` to prevent `clip_contents` from clipping the top and left lines.
675 debug_uv->draw_rect(
676 Rect2(Vector2(1, 1), debug_uv->get_size() - Vector2(1, 1)),
677 get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.125),
678 false,
679 Math::round(EDSCALE));
680
681 for (int x = 1; x <= 7; x++) {
682 debug_uv->draw_line(
683 Vector2(debug_uv->get_size().x * 0.125 * x, 0),
684 Vector2(debug_uv->get_size().x * 0.125 * x, debug_uv->get_size().y),
685 get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.125),
686 Math::round(EDSCALE));
687 }
688
689 for (int y = 1; y <= 7; y++) {
690 debug_uv->draw_line(
691 Vector2(0, debug_uv->get_size().y * 0.125 * y),
692 Vector2(debug_uv->get_size().x, debug_uv->get_size().y * 0.125 * y),
693 get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.125),
694 Math::round(EDSCALE));
695 }
696
697 debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
698
699 // Use a translucent color to allow overlapping triangles to be visible.
700 // Divide line width by the drawing scale set above, so that line width is consistent regardless of dialog size.
701 // Aspect ratio is preserved by the parent AspectRatioContainer, so we only need to check the X size which is always equal to Y.
702 debug_uv->draw_multiline(
703 uv_lines,
704 get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.5),
705 Math::round(EDSCALE) / debug_uv->get_size().x);
706 }
707
708 void MeshInstance3DEditor::_create_navigation_mesh() {
709 Ref<Mesh> mesh = node->get_mesh();
710 if (mesh.is_null()) {
711 return;
712 }
713
714 Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
715
716 if (nmesh.is_null()) {
717 return;
718 }
719
720 nmesh->create_from_mesh(mesh);
721 NavigationRegion3D *nmi = memnew(NavigationRegion3D);
722 nmi->set_navigation_mesh(nmesh);
723
724 Node *owner = get_tree()->get_edited_scene_root();
725
726 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
727 ur->create_action(TTR("Create Navigation Mesh"));
728
729 ur->add_do_method(node, "add_child", nmi, true);
730 ur->add_do_method(nmi, "set_owner", owner);
731 ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), nmi);
732
733 ur->add_do_reference(nmi);
734 ur->add_undo_method(node, "remove_child", nmi);
735 ur->commit_action();
736 }
737
738 void MeshInstance3DEditor::_create_outline_mesh() {
739 Ref<Mesh> mesh = node->get_mesh();
740 if (mesh.is_null()) {
741 err_dialog->set_text(TTR("MeshInstance3D lacks a Mesh."));
742 err_dialog->popup_centered();
743 return;
744 }
745
746 if (mesh->get_surface_count() == 0) {
747 err_dialog->set_text(TTR("Mesh has no surface to create outlines from."));
748 err_dialog->popup_centered();
749 return;
750 } else if (mesh->get_surface_count() == 1 && mesh->surface_get_primitive_type(0) != Mesh::PRIMITIVE_TRIANGLES) {
751 err_dialog->set_text(TTR("Mesh primitive type is not PRIMITIVE_TRIANGLES."));
752 err_dialog->popup_centered();
753 return;
754 }
755
756 Ref<Mesh> mesho = mesh->create_outline(outline_size->get_value());
757
758 if (mesho.is_null()) {
759 err_dialog->set_text(TTR("Could not create outline."));
760 err_dialog->popup_centered();
761 return;
762 }
763
764 MeshInstance3D *mi = memnew(MeshInstance3D);
765 mi->set_mesh(mesho);
766
767 Node *skeleton = node->get_node_or_null(node->get_skeleton_path());
768 if (skeleton && node->get_skin().is_valid()) {
769 mi->set_skin(node->get_skin());
770 mi->set_skeleton_path("../" + String(node->get_path_to(skeleton)));
771 }
772
773 Node *owner = get_tree()->get_edited_scene_root();
774
775 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
776
777 ur->create_action(TTR("Create Outline"));
778
779 ur->add_do_method(node, "add_child", mi, true);
780 ur->add_do_method(mi, "set_owner", owner);
781 ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), mi);
782
783 ur->add_do_reference(mi);
784 ur->add_undo_method(node, "remove_child", mi);
785 ur->commit_action();
786 }
787
788 void MeshInstance3DEditor::_notification(int p_what) {
789 switch (p_what) {
790 case NOTIFICATION_THEME_CHANGED: {
791 options->set_button_icon(get_editor_theme_icon(SNAME("MeshInstance3D")));
792 } break;
793 }
794 }
795
796 MeshInstance3DEditor::MeshInstance3DEditor() {
797 options = memnew(MenuButton);
798 options->set_text(TTR("Mesh"));
799 options->set_switch_on_hover(true);
800 options->set_flat(false);
801 options->set_theme_type_variation("FlatMenuButtonNoIconTint");
802 Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
803
804 options->get_popup()->add_item(TTR("Create Collision Shape..."), MENU_OPTION_CREATE_COLLISION_SHAPE);
805 options->get_popup()->add_item(TTR("Create Navigation Mesh"), MENU_OPTION_CREATE_NAVMESH);
806 options->get_popup()->add_separator();
807 options->get_popup()->add_item(TTR("Create Outline Mesh..."), MENU_OPTION_CREATE_OUTLINE_MESH);
808 options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a static outline mesh. The outline mesh will have its normals flipped automatically.\nThis can be used instead of the StandardMaterial Grow property when using that property isn't possible."));
809 options->get_popup()->add_item(TTR("Create Debug Tangents"), MENU_OPTION_CREATE_DEBUG_TANGENTS);
810 options->get_popup()->add_separator();
811 options->get_popup()->add_item(TTR("View UV1"), MENU_OPTION_DEBUG_UV1);
812 options->get_popup()->add_item(TTR("View UV2"), MENU_OPTION_DEBUG_UV2);
813 options->get_popup()->add_item(TTR("Unwrap UV2 for Lightmap/AO"), MENU_OPTION_CREATE_UV2);
814
815 options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &MeshInstance3DEditor::_menu_option));
816
817 outline_dialog = memnew(ConfirmationDialog);
818 outline_dialog->set_title(TTR("Create Outline Mesh"));
819 outline_dialog->set_ok_button_text(TTR("Create"));
820
821 VBoxContainer *outline_dialog_vbc = memnew(VBoxContainer);
822 outline_dialog->add_child(outline_dialog_vbc);
823 //outline_dialog->set_child_rect(outline_dialog_vbc);
824
825 outline_size = memnew(SpinBox);
826 outline_size->set_accessibility_name(TTRC("Outline Size:"));
827 outline_size->set_min(0.001);
828 outline_size->set_max(1024);
829 outline_size->set_step(0.001);
830 outline_size->set_value(0.05);
831 outline_dialog_vbc->add_margin_child(TTR("Outline Size:"), outline_size);
832
833 add_child(outline_dialog);
834 outline_dialog->connect(SceneStringName(confirmed), callable_mp(this, &MeshInstance3DEditor::_create_outline_mesh));
835
836 shape_dialog = memnew(ConfirmationDialog);
837 shape_dialog->set_title(TTR("Create Collision Shape"));
838 shape_dialog->set_ok_button_text(TTR("Create"));
839 shape_dialog->connect("about_to_popup", callable_mp(this, &MeshInstance3DEditor::_shape_dialog_about_to_popup));
840
841 VBoxContainer *shape_dialog_vbc = memnew(VBoxContainer);
842 shape_dialog->add_child(shape_dialog_vbc);
843
844 Label *l = memnew(Label);
845 l->set_text(TTR("Collision Shape Placement"));
846 shape_dialog_vbc->add_child(l);
847
848 shape_placement = memnew(OptionButton);
849 shape_placement->set_accessibility_name(TTRC("Collision Shape Placement"));
850 shape_placement->set_h_size_flags(SIZE_EXPAND_FILL);
851 shape_placement->add_item(TTR("Sibling"), SHAPE_PLACEMENT_SIBLING);
852 shape_placement->set_item_tooltip(-1, TTR("Creates collision shapes as Sibling."));
853 shape_placement->add_item(TTR("Static Body Child"), SHAPE_PLACEMENT_STATIC_BODY_CHILD);
854 shape_placement->set_item_tooltip(-1, TTR("Creates a StaticBody3D as child and assigns collision shapes to it."));
855 shape_dialog_vbc->add_child(shape_placement);
856
857 l = memnew(Label);
858 l->set_text(TTR("Collision Shape Type"));
859 shape_dialog_vbc->add_child(l);
860
861 shape_type = memnew(OptionButton);
862 shape_type->set_accessibility_name(TTRC("Collision Shape Type"));
863 shape_type->set_h_size_flags(SIZE_EXPAND_FILL);
864 shape_type->add_item(TTR("Trimesh"), SHAPE_TYPE_TRIMESH);
865 shape_type->set_item_tooltip(-1, TTR("Creates a polygon-based collision shape.\nThis is the most accurate (but slowest) option for collision detection."));
866 shape_type->add_item(TTR("Single Convex"), SHAPE_TYPE_SINGLE_CONVEX);
867 shape_type->set_item_tooltip(-1, TTR("Creates a single convex collision shape.\nThis is the faster than the trimesh or multiple convex option, but is less accurate for collision detection."));
868 shape_type->add_item(TTR("Simplified Convex"), SHAPE_TYPE_SIMPLIFIED_CONVEX);
869 shape_type->set_item_tooltip(-1, TTR("Creates a simplified convex collision shape.\nThis is similar to single collision shape, but can result in a simpler geometry in some cases, at the cost of accuracy."));
870 shape_type->add_item(TTR("Multiple Convex"), SHAPE_TYPE_MULTIPLE_CONVEX);
871 shape_type->set_item_tooltip(-1, TTR("Creates multiple convex collision shapes. These are decomposed from the original mesh.\nThis is a performance and accuracy middle-ground between a single convex collision and a polygon-based trimesh collision."));
872 shape_type->add_item(TTR("Bounding Box"), SHAPE_TYPE_BOUNDING_BOX);
873 shape_type->set_item_tooltip(-1, TTR("Creates an bounding box collision shape.\nThis will use the mesh's AABB if the shape is not a built-in BoxMesh.\nThis is faster than the convex collision shape option for collision detection."));
874 shape_type->add_item(TTR("Capsule"), SHAPE_TYPE_CAPSULE);
875 shape_type->set_item_tooltip(-1, TTR("Creates a capsule collision shape.\nThis will use the mesh's AABB if the shape is not a built-in CapsuleMesh.\nThis is faster than the convex collision shape option for collision detection."));
876 shape_type->add_item(TTR("Cylinder"), SHAPE_TYPE_CYLINDER);
877 shape_type->set_item_tooltip(-1, TTR("Creates a cylinder collision shape.\nThis will use the mesh's AABB if the shape is not a built-in CylinderMesh.\nThis is faster than the convex collision shape option for collision detection."));
878 shape_type->add_item(TTR("Sphere"), SHAPE_TYPE_SPHERE);
879 shape_type->set_item_tooltip(-1, TTR("Creates a sphere collision shape.\nThis will use the mesh's AABB if the shape is not a built-in SphereMesh.\nThis is faster than the convex collision shape option for collision detection."));
880 shape_type->add_item(TTR("Primitive"), SHAPE_TYPE_PRIMITIVE);
881 shape_type->set_item_tooltip(-1, TTR("Creates a box, capsule, cylinder, or sphere primitive collision shape if the mesh is a primitive.\nThe mesh must use the built-in BoxMesh, CapsuleMesh, CylinderMesh, or SphereMesh primitive.\nThis is faster than the convex collision shape option for collision detection."));
882 shape_dialog_vbc->add_child(shape_type);
883 shape_type->connect(SceneStringName(item_selected), callable_mp(this, &MeshInstance3DEditor::_shape_type_selected));
884
885 shape_axis_label = memnew(Label);
886 shape_axis_label->set_text(TTR("Alignment Axis"));
887 shape_dialog_vbc->add_child(shape_axis_label);
888
889 shape_axis = memnew(OptionButton);
890 shape_axis->set_h_size_flags(SIZE_EXPAND_FILL);
891 shape_axis->add_item(TTR("Longest Axis"), SHAPE_AXIS_LONGEST);
892 shape_axis->set_item_tooltip(-1, TTR("Create the shape along the longest axis of the mesh's AABB."));
893 shape_axis->add_item(TTR("X-Axis"), SHAPE_AXIS_X);
894 shape_axis->set_item_tooltip(-1, TTR("Create the shape along the local X-Axis."));
895 shape_axis->add_item(TTR("Y-Axis"), SHAPE_AXIS_Y);
896 shape_axis->set_item_tooltip(-1, TTR("Create the shape along the local Y-Axis."));
897 shape_axis->add_item(TTR("Z-Axis"), SHAPE_AXIS_Z);
898 shape_axis->set_item_tooltip(-1, TTR("Create the shape along the local Z-Axis."));
899 shape_dialog_vbc->add_child(shape_axis);
900
901 add_child(shape_dialog);
902 shape_dialog->connect(SceneStringName(confirmed), callable_mp(this, &MeshInstance3DEditor::_create_collision_shape));
903
904 err_dialog = memnew(AcceptDialog);
905 add_child(err_dialog);
906
907 debug_uv_dialog = memnew(AcceptDialog);
908 debug_uv_dialog->set_title(TTR("UV Channel Debug"));
909 add_child(debug_uv_dialog);
910
911 debug_uv_arc = memnew(AspectRatioContainer);
912 debug_uv_dialog->add_child(debug_uv_arc);
913
914 debug_uv = memnew(Control);
915 debug_uv->set_custom_minimum_size(Size2(600, 600) * EDSCALE);
916 debug_uv->connect(SceneStringName(draw), callable_mp(this, &MeshInstance3DEditor::_debug_uv_draw));
917 debug_uv_arc->add_child(debug_uv);
918
919 navigation_mesh_dialog = memnew(ConfirmationDialog);
920 navigation_mesh_dialog->set_title(TTR("Create NavigationMesh"));
921 navigation_mesh_dialog->set_ok_button_text(TTR("Create"));
922
923 VBoxContainer *navigation_mesh_dialog_vbc = memnew(VBoxContainer);
924 navigation_mesh_dialog->add_child(navigation_mesh_dialog_vbc);
925
926 Label *navigation_mesh_l = memnew(Label);
927 navigation_mesh_l->set_focus_mode(FOCUS_ACCESSIBILITY);
928 navigation_mesh_l->set_text(TTR("Before converting a rendering mesh to a navigation mesh, please verify:\n\n- The mesh is two-dimensional.\n- The mesh has no surface overlap.\n- The mesh has no self-intersection.\n- The mesh surfaces have indices.\n\nIf the mesh does not fulfill these requirements, the pathfinding will be broken."));
929 navigation_mesh_dialog_vbc->add_child(navigation_mesh_l);
930
931 add_child(navigation_mesh_dialog);
932 navigation_mesh_dialog->connect(SceneStringName(confirmed), callable_mp(this, &MeshInstance3DEditor::_create_navigation_mesh));
933 }
934
935 void MeshInstance3DEditorPlugin::edit(Object *p_object) {
936 {
937 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_object);
938 if (mi) {
939 mesh_editor->edit(mi);
940 return;
941 }
942 }
943
944 Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
945 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
946 if (mne.is_valid() && edited_scene) {
947 for (int i = 0; i < mne->get_node_count(); i++) {
948 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(edited_scene->get_node(mne->get_node(i)));
949 if (mi) {
950 mesh_editor->edit(mi);
951 return;
952 }
953 }
954 }
955 mesh_editor->edit(nullptr);
956 }
957
958 bool MeshInstance3DEditorPlugin::handles(Object *p_object) const {
959 if (Object::cast_to<MeshInstance3D>(p_object)) {
960 return true;
961 }
962
963 Ref<MultiNodeEdit> mne = Ref<MultiNodeEdit>(p_object);
964 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
965 if (mne.is_valid() && edited_scene) {
966 bool has_mesh = false;
967 for (int i = 0; i < mne->get_node_count(); i++) {
968 if (Object::cast_to<MeshInstance3D>(edited_scene->get_node(mne->get_node(i)))) {
969 if (has_mesh) {
970 return true;
971 } else {
972 has_mesh = true;
973 }
974 }
975 }
976 }
977 return false;
978 }
979
980 void MeshInstance3DEditorPlugin::make_visible(bool p_visible) {
981 if (p_visible) {
982 mesh_editor->options->show();
983 } else {
984 mesh_editor->options->hide();
985 mesh_editor->edit(nullptr);
986 }
987 }
988
989 MeshInstance3DEditorPlugin::MeshInstance3DEditorPlugin() {
990 mesh_editor = memnew(MeshInstance3DEditor);
991 EditorNode::get_singleton()->get_gui_base()->add_child(mesh_editor);
992
993 mesh_editor->options->hide();
994 }