2017-01-16 08:04:19 +01:00
/*************************************************************************/
2020-03-27 08:44:44 +01:00
/* mesh_instance_3d_editor_plugin.cpp */
2017-01-16 08:04:19 +01:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2017-01-16 08:04:19 +01:00
/*************************************************************************/
2022-01-03 21:27:34 +01:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-01-16 08:04:19 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2020-03-27 08:44:44 +01:00
# include "mesh_instance_3d_editor_plugin.h"
2016-05-23 22:10:26 +02:00
2022-02-12 02:46:22 +01:00
# include "editor/editor_node.h"
2019-12-24 08:17:23 +01:00
# include "editor/editor_scale.h"
2022-03-25 18:06:46 +01:00
# include "editor/editor_undo_redo_manager.h"
2020-03-27 08:44:44 +01:00
# include "node_3d_editor_plugin.h"
2020-03-26 22:49:16 +01:00
# include "scene/3d/collision_shape_3d.h"
# include "scene/3d/navigation_region_3d.h"
# include "scene/3d/physics_body_3d.h"
2017-03-05 16:44:50 +01:00
# include "scene/gui/box_container.h"
2016-05-23 22:10:26 +02:00
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _node_removed ( Node * p_node ) {
2017-03-05 16:44:50 +01:00
if ( p_node = = node ) {
2020-04-02 01:20:12 +02:00
node = nullptr ;
2016-05-23 22:10:26 +02:00
options - > hide ( ) ;
}
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : edit ( MeshInstance3D * p_mesh ) {
2017-03-05 16:44:50 +01:00
node = p_mesh ;
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _menu_option ( int p_option ) {
2016-05-23 22:10:26 +02:00
Ref < Mesh > mesh = node - > get_mesh ( ) ;
if ( mesh . is_null ( ) ) {
err_dialog - > set_text ( TTR ( " Mesh is empty! " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-05-23 22:10:26 +02:00
return ;
}
2017-03-05 16:44:50 +01:00
switch ( p_option ) {
2020-01-31 16:30:27 +01:00
case MENU_OPTION_CREATE_STATIC_TRIMESH_BODY : {
2016-05-23 22:10:26 +02:00
EditorSelection * editor_selection = EditorNode : : get_singleton ( ) - > get_editor_selection ( ) ;
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2016-05-23 22:10:26 +02:00
2020-12-15 13:04:21 +01:00
if ( selection . is_empty ( ) ) {
2020-03-26 22:49:16 +01:00
Ref < Shape3D > shape = mesh - > create_trimesh_shape ( ) ;
2020-01-31 16:30:27 +01:00
if ( shape . is_null ( ) ) {
err_dialog - > set_text ( TTR ( " Couldn't create a Trimesh collision shape. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-05-23 22:10:26 +02:00
return ;
2020-01-31 16:30:27 +01:00
}
2016-05-23 22:10:26 +02:00
2020-03-26 22:49:16 +01:00
CollisionShape3D * cshape = memnew ( CollisionShape3D ) ;
2016-05-23 22:10:26 +02:00
cshape - > set_shape ( shape ) ;
2020-03-26 22:49:16 +01:00
StaticBody3D * body = memnew ( StaticBody3D ) ;
2022-03-31 18:02:18 +02:00
body - > add_child ( cshape , true ) ;
2016-05-23 22:10:26 +02:00
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2016-05-23 22:10:26 +02:00
2020-01-31 16:30:27 +01:00
ur - > create_action ( TTR ( " Create Static Trimesh Body " ) ) ;
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( node , " add_child " , body , true ) ;
2017-03-05 16:44:50 +01:00
ur - > add_do_method ( body , " set_owner " , owner ) ;
ur - > add_do_method ( cshape , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , body ) ;
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , cshape ) ;
2016-05-23 22:10:26 +02:00
ur - > add_do_reference ( body ) ;
2017-03-05 16:44:50 +01:00
ur - > add_undo_method ( node , " remove_child " , body ) ;
2016-05-23 22:10:26 +02:00
ur - > commit_action ( ) ;
return ;
}
2019-04-10 22:46:04 +02:00
ur - > create_action ( TTR ( " Create Static Trimesh Body " ) ) ;
2016-05-23 22:10:26 +02:00
2021-07-16 05:45:57 +02:00
for ( Node * E : selection ) {
MeshInstance3D * instance = Object : : cast_to < MeshInstance3D > ( E ) ;
2020-05-14 16:41:43 +02:00
if ( ! instance ) {
2016-05-23 22:10:26 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2016-05-23 22:10:26 +02:00
Ref < Mesh > m = instance - > get_mesh ( ) ;
2020-05-14 16:41:43 +02:00
if ( m . is_null ( ) ) {
2016-05-23 22:10:26 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2016-05-23 22:10:26 +02:00
2020-03-26 22:49:16 +01:00
Ref < Shape3D > shape = m - > create_trimesh_shape ( ) ;
2020-05-14 16:41:43 +02:00
if ( shape . is_null ( ) ) {
2016-05-23 22:10:26 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2016-05-23 22:10:26 +02:00
2020-03-26 22:49:16 +01:00
CollisionShape3D * cshape = memnew ( CollisionShape3D ) ;
2016-05-23 22:10:26 +02:00
cshape - > set_shape ( shape ) ;
2020-03-26 22:49:16 +01:00
StaticBody3D * body = memnew ( StaticBody3D ) ;
2022-03-31 18:02:18 +02:00
body - > add_child ( cshape , true ) ;
2016-05-23 22:10:26 +02:00
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2016-05-23 22:10:26 +02:00
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( instance , " add_child " , body , true ) ;
2017-03-05 16:44:50 +01:00
ur - > add_do_method ( body , " set_owner " , owner ) ;
ur - > add_do_method ( cshape , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , body ) ;
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , cshape ) ;
2016-05-23 22:10:26 +02:00
ur - > add_do_reference ( body ) ;
2017-03-05 16:44:50 +01:00
ur - > add_undo_method ( instance , " remove_child " , body ) ;
2016-05-23 22:10:26 +02:00
}
ur - > commit_action ( ) ;
} break ;
2019-04-10 22:46:04 +02:00
case MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE : {
2017-03-05 16:44:50 +01:00
if ( node = = get_tree ( ) - > get_edited_scene_root ( ) ) {
2016-05-23 22:10:26 +02:00
err_dialog - > set_text ( TTR ( " This doesn't work on scene root! " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-05-23 22:10:26 +02:00
return ;
}
2020-03-26 22:49:16 +01:00
Ref < Shape3D > shape = mesh - > create_trimesh_shape ( ) ;
2020-05-14 16:41:43 +02:00
if ( shape . is_null ( ) ) {
2016-05-23 22:10:26 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-05-23 22:10:26 +02:00
2020-03-26 22:49:16 +01:00
CollisionShape3D * cshape = memnew ( CollisionShape3D ) ;
2016-05-23 22:10:26 +02:00
cshape - > set_shape ( shape ) ;
2020-09-14 00:42:52 +02:00
cshape - > set_transform ( node - > get_transform ( ) ) ;
2016-05-23 22:10:26 +02:00
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2016-05-23 22:10:26 +02:00
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2016-05-23 22:10:26 +02:00
2019-04-10 22:46:04 +02:00
ur - > create_action ( TTR ( " Create Trimesh Static Shape " ) ) ;
2016-05-23 22:10:26 +02:00
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( node - > get_parent ( ) , " add_child " , cshape , true ) ;
2017-03-05 16:44:50 +01:00
ur - > add_do_method ( node - > get_parent ( ) , " move_child " , cshape , node - > get_index ( ) + 1 ) ;
ur - > add_do_method ( cshape , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , cshape ) ;
2016-05-23 22:10:26 +02:00
ur - > add_do_reference ( cshape ) ;
2017-03-05 16:44:50 +01:00
ur - > add_undo_method ( node - > get_parent ( ) , " remove_child " , cshape ) ;
2016-05-23 22:10:26 +02:00
ur - > commit_action ( ) ;
2019-04-10 22:46:04 +02:00
} break ;
2021-07-07 21:14:12 +02:00
case MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE :
case MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE : {
2019-04-10 22:46:04 +02:00
if ( node = = get_tree ( ) - > get_edited_scene_root ( ) ) {
2020-01-31 16:30:27 +01:00
err_dialog - > set_text ( TTR ( " Can't create a single convex collision shape for the scene root. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2020-01-31 16:30:27 +01:00
return ;
}
2021-07-07 21:14:12 +02:00
bool simplify = ( p_option = = MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE ) ;
Ref < Shape3D > shape = mesh - > create_convex_shape ( true , simplify ) ;
2020-01-31 16:30:27 +01:00
if ( shape . is_null ( ) ) {
err_dialog - > set_text ( TTR ( " Couldn't create a single convex collision shape. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2020-01-31 16:30:27 +01:00
return ;
}
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2020-01-31 16:30:27 +01:00
2021-07-07 21:14:12 +02:00
if ( simplify ) {
ur - > create_action ( TTR ( " Create Simplified Convex Shape " ) ) ;
} else {
ur - > create_action ( TTR ( " Create Single Convex Shape " ) ) ;
}
2020-01-31 16:30:27 +01:00
2020-03-26 22:49:16 +01:00
CollisionShape3D * cshape = memnew ( CollisionShape3D ) ;
2020-01-31 16:30:27 +01:00
cshape - > set_shape ( shape ) ;
cshape - > set_transform ( node - > get_transform ( ) ) ;
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2020-01-31 16:30:27 +01:00
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( node - > get_parent ( ) , " add_child " , cshape , true ) ;
2020-01-31 16:30:27 +01:00
ur - > add_do_method ( node - > get_parent ( ) , " move_child " , cshape , node - > get_index ( ) + 1 ) ;
ur - > add_do_method ( cshape , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , cshape ) ;
2020-01-31 16:30:27 +01:00
ur - > add_do_reference ( cshape ) ;
ur - > add_undo_method ( node - > get_parent ( ) , " remove_child " , cshape ) ;
ur - > commit_action ( ) ;
} break ;
2021-07-07 21:14:12 +02:00
2020-01-31 16:30:27 +01:00
case MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES : {
if ( node = = get_tree ( ) - > get_edited_scene_root ( ) ) {
err_dialog - > set_text ( TTR ( " Can't create multiple convex collision shapes for the scene root. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2019-04-10 22:46:04 +02:00
return ;
}
Improve collision generation usability in the new 3D scene import workflow.
With this PR it's possible to add a collision during the Mesh import, directly in editor.
To generate the shape is possible to chose between the following options:
- Decompose Convex: The Mesh is decomposed in one or many Convex Shapes (Using the VHACD library).
- Simple Convex: Is generated a convex shape that enclose the entire mesh.
- Trimesh: Generate a trimesh shape using the Mesh faces.
- Box: Add a primitive box shape, where you can tweak the `size`, `position`, `rotation`.
- Sphere: Add a primitive sphere shape, where you can tweak the `radius`, `position`, `rotation`.
- Cylinder: Add a primitive cylinder shape, where you can tweak the `height`, `radius`, `position`, `rotation`.
- Capsule: Add a primitive capsule shape, where you can tweak the `height`, `radius`, `position`, `rotation`.
It's also possible to chose the generated body, so you can create:
- Rigid Body
- Static Body
- Area
2021-08-22 18:19:13 +02:00
Mesh : : ConvexDecompositionSettings settings ;
Vector < Ref < Shape3D > > shapes = mesh - > convex_decompose ( settings ) ;
2019-04-10 22:46:04 +02:00
if ( ! shapes . size ( ) ) {
2020-01-31 16:30:27 +01:00
err_dialog - > set_text ( TTR ( " Couldn't create any collision shapes. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2019-04-10 22:46:04 +02:00
return ;
}
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2019-04-10 22:46:04 +02:00
2020-01-31 16:30:27 +01:00
ur - > create_action ( TTR ( " Create Multiple Convex Shapes " ) ) ;
2019-04-10 22:46:04 +02:00
for ( int i = 0 ; i < shapes . size ( ) ; i + + ) {
2020-03-26 22:49:16 +01:00
CollisionShape3D * cshape = memnew ( CollisionShape3D ) ;
2022-04-29 08:06:48 +02:00
cshape - > set_name ( " CollisionShape3D " ) ;
2019-04-10 22:46:04 +02:00
cshape - > set_shape ( shapes [ i ] ) ;
2019-11-02 15:08:50 +01:00
cshape - > set_transform ( node - > get_transform ( ) ) ;
2019-04-10 22:46:04 +02:00
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2019-04-10 22:46:04 +02:00
ur - > add_do_method ( node - > get_parent ( ) , " add_child " , cshape ) ;
ur - > add_do_method ( node - > get_parent ( ) , " move_child " , cshape , node - > get_index ( ) + 1 ) ;
ur - > add_do_method ( cshape , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , cshape ) ;
2019-04-10 22:46:04 +02:00
ur - > add_do_reference ( cshape ) ;
ur - > add_undo_method ( node - > get_parent ( ) , " remove_child " , cshape ) ;
}
ur - > commit_action ( ) ;
2016-05-23 22:10:26 +02:00
} break ;
case MENU_OPTION_CREATE_NAVMESH : {
2017-03-05 16:44:50 +01:00
Ref < NavigationMesh > nmesh = memnew ( NavigationMesh ) ;
2016-05-23 22:10:26 +02:00
2020-05-14 16:41:43 +02:00
if ( nmesh . is_null ( ) ) {
2016-05-23 22:10:26 +02:00
return ;
2020-05-14 16:41:43 +02:00
}
2016-05-23 22:10:26 +02:00
nmesh - > create_from_mesh ( mesh ) ;
2020-03-26 22:49:16 +01:00
NavigationRegion3D * nmi = memnew ( NavigationRegion3D ) ;
2016-05-23 22:10:26 +02:00
nmi - > set_navigation_mesh ( nmesh ) ;
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2016-05-23 22:10:26 +02:00
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2016-05-23 22:10:26 +02:00
ur - > create_action ( TTR ( " Create Navigation Mesh " ) ) ;
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( node , " add_child " , nmi , true ) ;
2017-03-05 16:44:50 +01:00
ur - > add_do_method ( nmi , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , nmi ) ;
2016-05-23 22:10:26 +02:00
ur - > add_do_reference ( nmi ) ;
2017-03-05 16:44:50 +01:00
ur - > add_undo_method ( node , " remove_child " , nmi ) ;
2016-05-23 22:10:26 +02:00
ur - > commit_action ( ) ;
} break ;
case MENU_OPTION_CREATE_OUTLINE_MESH : {
outline_dialog - > popup_centered ( Vector2 ( 200 , 90 ) ) ;
} break ;
2017-12-09 18:11:26 +01:00
case MENU_OPTION_CREATE_UV2 : {
2019-02-12 21:10:08 +01:00
Ref < ArrayMesh > mesh2 = node - > get_mesh ( ) ;
if ( ! mesh2 . is_valid ( ) ) {
2017-12-09 18:11:26 +01:00
err_dialog - > set_text ( TTR ( " Contained Mesh is not of type ArrayMesh. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
return ;
}
2022-04-29 08:06:48 +02:00
String path = mesh2 - > get_path ( ) ;
int srpos = path . find ( " :: " ) ;
if ( srpos ! = - 1 ) {
String base = path . substr ( 0 , srpos ) ;
if ( ResourceLoader : : get_resource_type ( base ) = = " PackedScene " ) {
if ( ! get_tree ( ) - > get_edited_scene_root ( ) | | get_tree ( ) - > get_edited_scene_root ( ) - > get_scene_file_path ( ) ! = base ) {
err_dialog - > set_text ( TTR ( " Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
} else {
if ( FileAccess : : exists ( path + " .import " ) ) {
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. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
}
} else {
if ( FileAccess : : exists ( path + " .import " ) ) {
err_dialog - > set_text ( TTR ( " Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first. " ) ) ;
err_dialog - > popup_centered ( ) ;
return ;
}
}
Ref < ArrayMesh > unwrapped_mesh = mesh2 - > duplicate ( false ) ;
Error err = unwrapped_mesh - > lightmap_unwrap ( node - > get_global_transform ( ) ) ;
2017-12-09 18:11:26 +01:00
if ( err ! = OK ) {
err_dialog - > set_text ( TTR ( " UV Unwrap failed, mesh may not be manifold? " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
return ;
}
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2022-04-29 08:06:48 +02:00
ur - > create_action ( TTR ( " Unwrap UV2 " ) ) ;
ur - > add_do_method ( node , " set_mesh " , unwrapped_mesh ) ;
ur - > add_do_reference ( node ) ;
ur - > add_do_reference ( mesh2 . ptr ( ) ) ;
ur - > add_undo_method ( node , " set_mesh " , mesh2 ) ;
ur - > add_undo_reference ( unwrapped_mesh . ptr ( ) ) ;
ur - > commit_action ( ) ;
2017-12-09 18:11:26 +01:00
} break ;
case MENU_OPTION_DEBUG_UV1 : {
2019-02-12 21:10:08 +01:00
Ref < Mesh > mesh2 = node - > get_mesh ( ) ;
if ( ! mesh2 . is_valid ( ) ) {
2017-12-09 18:11:26 +01:00
err_dialog - > set_text ( TTR ( " No mesh to debug. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
return ;
}
_create_uv_lines ( 0 ) ;
} break ;
case MENU_OPTION_DEBUG_UV2 : {
2019-02-12 21:10:08 +01:00
Ref < Mesh > mesh2 = node - > get_mesh ( ) ;
if ( ! mesh2 . is_valid ( ) ) {
2017-12-09 18:11:26 +01:00
err_dialog - > set_text ( TTR ( " No mesh to debug. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
return ;
}
_create_uv_lines ( 1 ) ;
} break ;
}
}
2020-03-27 08:44:44 +01:00
struct MeshInstance3DEditorEdgeSort {
2017-12-09 18:11:26 +01:00
Vector2 a ;
Vector2 b ;
2022-05-19 17:00:06 +02:00
static uint32_t hash ( const MeshInstance3DEditorEdgeSort & p_edge ) {
2022-06-18 16:20:55 +02:00
uint32_t h = hash_murmur3_one_32 ( HashMapHasherDefault : : hash ( p_edge . a ) ) ;
return hash_fmix32 ( hash_murmur3_one_32 ( HashMapHasherDefault : : hash ( p_edge . b ) , h ) ) ;
2022-05-19 17:00:06 +02:00
}
bool operator = = ( const MeshInstance3DEditorEdgeSort & p_b ) const {
return a = = p_b . a & & b = = p_b . b ;
2017-12-09 18:11:26 +01:00
}
2020-03-27 08:44:44 +01:00
MeshInstance3DEditorEdgeSort ( ) { }
MeshInstance3DEditorEdgeSort ( const Vector2 & p_a , const Vector2 & p_b ) {
2017-12-09 18:11:26 +01:00
if ( p_a < p_b ) {
a = p_a ;
b = p_b ;
} else {
b = p_a ;
a = p_b ;
}
}
} ;
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _create_uv_lines ( int p_layer ) {
2017-12-09 18:11:26 +01:00
Ref < Mesh > mesh = node - > get_mesh ( ) ;
ERR_FAIL_COND ( ! mesh . is_valid ( ) ) ;
2022-05-19 17:00:06 +02:00
HashSet < MeshInstance3DEditorEdgeSort , MeshInstance3DEditorEdgeSort > edges ;
2017-12-09 18:11:26 +01:00
uv_lines . clear ( ) ;
for ( int i = 0 ; i < mesh - > get_surface_count ( ) ; i + + ) {
2020-05-14 16:41:43 +02:00
if ( mesh - > surface_get_primitive_type ( i ) ! = Mesh : : PRIMITIVE_TRIANGLES ) {
2017-12-09 18:11:26 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-12-09 18:11:26 +01:00
Array a = mesh - > surface_get_arrays ( i ) ;
2020-02-17 22:06:54 +01:00
Vector < Vector2 > uv = a [ p_layer = = 0 ? Mesh : : ARRAY_TEX_UV : Mesh : : ARRAY_TEX_UV2 ] ;
2017-12-09 18:11:26 +01:00
if ( uv . size ( ) = = 0 ) {
2021-07-25 01:11:24 +02:00
err_dialog - > set_text ( vformat ( TTR ( " Mesh has no UV in layer %d. " ) , p_layer + 1 ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
return ;
}
2020-02-17 22:06:54 +01:00
const Vector2 * r = uv . ptr ( ) ;
2017-12-09 18:11:26 +01:00
2020-02-17 22:06:54 +01:00
Vector < int > indices = a [ Mesh : : ARRAY_INDEX ] ;
2020-04-02 01:20:12 +02:00
const int * ri = nullptr ;
2017-12-09 18:11:26 +01:00
int ic ;
if ( indices . size ( ) ) {
ic = indices . size ( ) ;
2020-02-17 22:06:54 +01:00
ri = indices . ptr ( ) ;
2017-12-09 18:11:26 +01:00
} else {
ic = uv . size ( ) ;
}
for ( int j = 0 ; j < ic ; j + = 3 ) {
for ( int k = 0 ; k < 3 ; k + + ) {
2020-03-27 08:44:44 +01:00
MeshInstance3DEditorEdgeSort edge ;
2020-03-31 13:50:25 +02:00
if ( ri ) {
2017-12-09 18:11:26 +01:00
edge . a = r [ ri [ j + k ] ] ;
edge . b = r [ ri [ j + ( ( k + 1 ) % 3 ) ] ] ;
} else {
edge . a = r [ j + k ] ;
edge . b = r [ j + ( ( k + 1 ) % 3 ) ] ;
}
2020-05-14 16:41:43 +02:00
if ( edges . has ( edge ) ) {
2017-12-09 18:11:26 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-12-09 18:11:26 +01:00
uv_lines . push_back ( edge . a ) ;
uv_lines . push_back ( edge . b ) ;
edges . insert ( edge ) ;
}
}
2016-05-23 22:10:26 +02:00
}
2017-12-09 18:11:26 +01:00
2020-03-06 18:00:16 +01:00
debug_uv_dialog - > popup_centered ( ) ;
2017-12-09 18:11:26 +01:00
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _debug_uv_draw ( ) {
2020-05-14 16:41:43 +02:00
if ( uv_lines . size ( ) = = 0 ) {
2017-12-09 18:11:26 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-12-09 18:11:26 +01:00
debug_uv - > set_clip_contents ( true ) ;
2021-07-25 01:11:24 +02:00
debug_uv - > draw_rect ( Rect2 ( Vector2 ( ) , debug_uv - > get_size ( ) ) , get_theme_color ( SNAME ( " dark_color_3 " ) , SNAME ( " Editor " ) ) ) ;
2017-12-09 18:11:26 +01:00
debug_uv - > draw_set_transform ( Vector2 ( ) , 0 , debug_uv - > get_size ( ) ) ;
2021-07-25 01:11:24 +02:00
// Use a translucent color to allow overlapping triangles to be visible.
debug_uv - > draw_multiline ( uv_lines , get_theme_color ( SNAME ( " mono_color " ) , SNAME ( " Editor " ) ) * Color ( 1 , 1 , 1 , 0.5 ) , Math : : round ( EDSCALE ) ) ;
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _create_outline_mesh ( ) {
2016-05-23 22:10:26 +02:00
Ref < Mesh > mesh = node - > get_mesh ( ) ;
if ( mesh . is_null ( ) ) {
2020-03-30 18:22:57 +02:00
err_dialog - > set_text ( TTR ( " MeshInstance3D lacks a Mesh. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-05-23 22:10:26 +02:00
return ;
}
2016-06-21 00:57:21 +02:00
if ( mesh - > get_surface_count ( ) = = 0 ) {
2021-12-12 13:29:46 +01:00
err_dialog - > set_text ( TTR ( " Mesh has no surface to create outlines from. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2018-04-01 17:06:47 +02:00
return ;
} else if ( mesh - > get_surface_count ( ) = = 1 & & mesh - > surface_get_primitive_type ( 0 ) ! = Mesh : : PRIMITIVE_TRIANGLES ) {
2020-03-30 18:22:57 +02:00
err_dialog - > set_text ( TTR ( " Mesh primitive type is not PRIMITIVE_TRIANGLES. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-06-21 00:57:21 +02:00
return ;
}
2017-01-04 05:16:14 +01:00
Ref < Mesh > mesho = mesh - > create_outline ( outline_size - > get_value ( ) ) ;
2016-05-23 22:10:26 +02:00
if ( mesho . is_null ( ) ) {
2020-03-30 18:22:57 +02:00
err_dialog - > set_text ( TTR ( " Could not create outline. " ) ) ;
2020-03-06 18:00:16 +01:00
err_dialog - > popup_centered ( ) ;
2016-05-23 22:10:26 +02:00
return ;
}
2020-03-26 22:49:16 +01:00
MeshInstance3D * mi = memnew ( MeshInstance3D ) ;
2016-05-23 22:10:26 +02:00
mi - > set_mesh ( mesho ) ;
2022-04-29 08:06:48 +02:00
Node * owner = get_tree ( ) - > get_edited_scene_root ( ) ;
2016-05-23 22:10:26 +02:00
2022-03-25 18:06:46 +01:00
Ref < EditorUndoRedoManager > & ur = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2016-05-23 22:10:26 +02:00
ur - > create_action ( TTR ( " Create Outline " ) ) ;
2021-10-21 16:46:07 +02:00
ur - > add_do_method ( node , " add_child " , mi , true ) ;
2017-03-05 16:44:50 +01:00
ur - > add_do_method ( mi , " set_owner " , owner ) ;
2022-04-29 08:06:48 +02:00
ur - > add_do_method ( Node3DEditor : : get_singleton ( ) , " _request_gizmo " , mi ) ;
2016-05-23 22:10:26 +02:00
ur - > add_do_reference ( mi ) ;
2017-03-05 16:44:50 +01:00
ur - > add_undo_method ( node , " remove_child " , mi ) ;
2016-05-23 22:10:26 +02:00
ur - > commit_action ( ) ;
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditor : : _bind_methods ( ) {
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
MeshInstance3DEditor : : MeshInstance3DEditor ( ) {
2017-03-05 16:44:50 +01:00
options = memnew ( MenuButton ) ;
2019-04-25 15:27:33 +02:00
options - > set_switch_on_hover ( true ) ;
2020-03-26 22:49:16 +01:00
Node3DEditor : : get_singleton ( ) - > add_control_to_menu_panel ( options ) ;
2016-05-23 22:10:26 +02:00
2016-06-01 13:32:20 +02:00
options - > set_text ( TTR ( " Mesh " ) ) ;
2021-07-17 23:22:52 +02:00
options - > set_icon ( EditorNode : : get_singleton ( ) - > get_gui_base ( ) - > get_theme_icon ( SNAME ( " MeshInstance3D " ) , SNAME ( " EditorIcons " ) ) ) ;
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Trimesh Static Body " ) , MENU_OPTION_CREATE_STATIC_TRIMESH_BODY ) ;
2022-03-12 01:06:45 +01:00
options - > get_popup ( ) - > set_item_tooltip ( - 1 , TTR ( " Creates a StaticBody3D and assigns a polygon-based collision shape to it automatically. \n This is the most accurate (but slowest) option for collision detection. " ) ) ;
2016-05-23 22:10:26 +02:00
options - > get_popup ( ) - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Trimesh Collision Sibling " ) , MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE ) ;
2022-03-12 01:06:45 +01:00
options - > get_popup ( ) - > set_item_tooltip ( - 1 , TTR ( " Creates a polygon-based collision shape. \n This is the most accurate (but slowest) option for collision detection. " ) ) ;
2020-02-26 22:25:55 +01:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Single Convex Collision Sibling " ) , MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE ) ;
2022-03-12 01:06:45 +01:00
options - > get_popup ( ) - > set_item_tooltip ( - 1 , TTR ( " Creates a single convex collision shape. \n This is the fastest (but least accurate) option for collision detection. " ) ) ;
2021-07-07 21:14:12 +02:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Simplified Convex Collision Sibling " ) , MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE ) ;
2022-03-12 01:06:45 +01:00
options - > get_popup ( ) - > set_item_tooltip ( - 1 , TTR ( " Creates a simplified convex collision shape. \n This is similar to single collision shape, but can result in a simpler geometry in some cases, at the cost of accuracy. " ) ) ;
2020-01-31 16:30:27 +01:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Multiple Convex Collision Siblings " ) , MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES ) ;
2022-03-12 01:06:45 +01:00
options - > get_popup ( ) - > set_item_tooltip ( - 1 , TTR ( " Creates a polygon-based collision shape. \n This is a performance middle-ground between a single convex collision and a polygon-based collision. " ) ) ;
2016-05-23 22:10:26 +02:00
options - > get_popup ( ) - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Navigation Mesh " ) , MENU_OPTION_CREATE_NAVMESH ) ;
2016-05-23 22:10:26 +02:00
options - > get_popup ( ) - > add_separator ( ) ;
2018-04-22 19:36:01 +02:00
options - > get_popup ( ) - > add_item ( TTR ( " Create Outline Mesh... " ) , MENU_OPTION_CREATE_OUTLINE_MESH ) ;
2020-03-26 22:49:16 +01:00
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. \n This can be used instead of the StandardMaterial Grow property when using that property isn't possible. " ) ) ;
2017-12-09 18:11:26 +01:00
options - > get_popup ( ) - > add_separator ( ) ;
options - > get_popup ( ) - > add_item ( TTR ( " View UV1 " ) , MENU_OPTION_DEBUG_UV1 ) ;
options - > get_popup ( ) - > add_item ( TTR ( " View UV2 " ) , MENU_OPTION_DEBUG_UV2 ) ;
options - > get_popup ( ) - > add_item ( TTR ( " Unwrap UV2 for Lightmap/AO " ) , MENU_OPTION_CREATE_UV2 ) ;
2016-05-23 22:10:26 +02:00
2020-03-27 08:44:44 +01:00
options - > get_popup ( ) - > connect ( " id_pressed " , callable_mp ( this , & MeshInstance3DEditor : : _menu_option ) ) ;
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
outline_dialog = memnew ( ConfirmationDialog ) ;
2016-05-23 22:10:26 +02:00
outline_dialog - > set_title ( TTR ( " Create Outline Mesh " ) ) ;
2022-07-08 02:31:19 +02:00
outline_dialog - > set_ok_button_text ( TTR ( " Create " ) ) ;
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
VBoxContainer * outline_dialog_vbc = memnew ( VBoxContainer ) ;
2016-05-23 22:10:26 +02:00
outline_dialog - > add_child ( outline_dialog_vbc ) ;
2017-01-10 05:49:55 +01:00
//outline_dialog->set_child_rect(outline_dialog_vbc);
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
outline_size = memnew ( SpinBox ) ;
2016-05-23 22:10:26 +02:00
outline_size - > set_min ( 0.001 ) ;
outline_size - > set_max ( 1024 ) ;
outline_size - > set_step ( 0.001 ) ;
2017-01-04 05:16:14 +01:00
outline_size - > set_value ( 0.05 ) ;
2017-03-05 16:44:50 +01:00
outline_dialog_vbc - > add_margin_child ( TTR ( " Outline Size: " ) , outline_size ) ;
2016-05-23 22:10:26 +02:00
add_child ( outline_dialog ) ;
2020-03-27 08:44:44 +01:00
outline_dialog - > connect ( " confirmed " , callable_mp ( this , & MeshInstance3DEditor : : _create_outline_mesh ) ) ;
2016-05-23 22:10:26 +02:00
2017-03-05 16:44:50 +01:00
err_dialog = memnew ( AcceptDialog ) ;
2016-05-23 22:10:26 +02:00
add_child ( err_dialog ) ;
2017-12-09 18:11:26 +01:00
debug_uv_dialog = memnew ( AcceptDialog ) ;
2019-12-27 03:31:55 +01:00
debug_uv_dialog - > set_title ( TTR ( " UV Channel Debug " ) ) ;
2017-12-09 18:11:26 +01:00
add_child ( debug_uv_dialog ) ;
debug_uv = memnew ( Control ) ;
debug_uv - > set_custom_minimum_size ( Size2 ( 600 , 600 ) * EDSCALE ) ;
2020-03-27 08:44:44 +01:00
debug_uv - > connect ( " draw " , callable_mp ( this , & MeshInstance3DEditor : : _debug_uv_draw ) ) ;
2017-12-09 18:11:26 +01:00
debug_uv_dialog - > add_child ( debug_uv ) ;
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditorPlugin : : edit ( Object * p_object ) {
2020-03-26 22:49:16 +01:00
mesh_editor - > edit ( Object : : cast_to < MeshInstance3D > ( p_object ) ) ;
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
bool MeshInstance3DEditorPlugin : : handles ( Object * p_object ) const {
2020-03-26 22:49:16 +01:00
return p_object - > is_class ( " MeshInstance3D " ) ;
2016-05-23 22:10:26 +02:00
}
2020-03-27 08:44:44 +01:00
void MeshInstance3DEditorPlugin : : make_visible ( bool p_visible ) {
2016-05-23 22:10:26 +02:00
if ( p_visible ) {
mesh_editor - > options - > show ( ) ;
} else {
mesh_editor - > options - > hide ( ) ;
2020-04-02 01:20:12 +02:00
mesh_editor - > edit ( nullptr ) ;
2016-05-23 22:10:26 +02:00
}
}
2022-01-27 10:36:51 +01:00
MeshInstance3DEditorPlugin : : MeshInstance3DEditorPlugin ( ) {
2020-03-27 08:44:44 +01:00
mesh_editor = memnew ( MeshInstance3DEditor ) ;
2022-01-27 10:36:51 +01:00
EditorNode : : get_singleton ( ) - > get_main_control ( ) - > add_child ( mesh_editor ) ;
2016-05-23 22:10:26 +02:00
mesh_editor - > options - > hide ( ) ;
}
2020-03-27 08:44:44 +01:00
MeshInstance3DEditorPlugin : : ~ MeshInstance3DEditorPlugin ( ) {
2016-05-23 22:10:26 +02:00
}