resource_importer_scene.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**************************************************************************/
  2. /* resource_importer_scene.h */
  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. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/io/resource_importer.h"
  33. #include "core/variant/dictionary.h"
  34. #include "scene/3d/importer_mesh_instance_3d.h"
  35. #include "scene/resources/3d/box_shape_3d.h"
  36. #include "scene/resources/3d/capsule_shape_3d.h"
  37. #include "scene/resources/3d/cylinder_shape_3d.h"
  38. #include "scene/resources/3d/importer_mesh.h"
  39. #include "scene/resources/3d/sphere_shape_3d.h"
  40. #include "scene/resources/animation.h"
  41. #include "scene/resources/mesh.h"
  42. class AnimationPlayer;
  43. class ImporterMesh;
  44. class Material;
  45. class EditorSceneFormatImporter : public RefCounted {
  46. GDCLASS(EditorSceneFormatImporter, RefCounted);
  47. List<ResourceImporter::ImportOption> *current_option_list = nullptr;
  48. protected:
  49. static void _bind_methods();
  50. Node *import_scene_wrapper(const String &p_path, uint32_t p_flags, const Dictionary &p_options);
  51. Ref<Animation> import_animation_wrapper(const String &p_path, uint32_t p_flags, const Dictionary &p_options);
  52. GDVIRTUAL0RC(Vector<String>, _get_extensions)
  53. GDVIRTUAL3R(Object *, _import_scene, String, uint32_t, Dictionary)
  54. GDVIRTUAL1(_get_import_options, String)
  55. GDVIRTUAL3RC(Variant, _get_option_visibility, String, bool, String)
  56. public:
  57. enum ImportFlags {
  58. IMPORT_SCENE = 1,
  59. IMPORT_ANIMATION = 2,
  60. IMPORT_FAIL_ON_MISSING_DEPENDENCIES = 4,
  61. IMPORT_GENERATE_TANGENT_ARRAYS = 8,
  62. IMPORT_USE_NAMED_SKIN_BINDS = 16,
  63. IMPORT_DISCARD_MESHES_AND_MATERIALS = 32, //used for optimizing animation import
  64. IMPORT_FORCE_DISABLE_MESH_COMPRESSION = 64,
  65. };
  66. void add_import_option(const String &p_name, const Variant &p_default_value);
  67. void add_import_option_advanced(Variant::Type p_type, const String &p_name, const Variant &p_default_value, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = String(), int p_usage_flags = PROPERTY_USAGE_DEFAULT);
  68. virtual void get_extensions(List<String> *r_extensions) const;
  69. virtual Node *import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err = nullptr);
  70. virtual void get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options);
  71. virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options);
  72. virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {}
  73. };
  74. class EditorScenePostImport : public RefCounted {
  75. GDCLASS(EditorScenePostImport, RefCounted);
  76. String source_file;
  77. protected:
  78. static void _bind_methods();
  79. GDVIRTUAL1R(Object *, _post_import, Node *)
  80. public:
  81. String get_source_file() const;
  82. virtual Node *post_import(Node *p_scene);
  83. virtual void init(const String &p_source_file);
  84. };
  85. class EditorScenePostImportPlugin : public RefCounted {
  86. GDCLASS(EditorScenePostImportPlugin, RefCounted);
  87. public:
  88. enum InternalImportCategory {
  89. INTERNAL_IMPORT_CATEGORY_NODE,
  90. INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE,
  91. INTERNAL_IMPORT_CATEGORY_MESH,
  92. INTERNAL_IMPORT_CATEGORY_MATERIAL,
  93. INTERNAL_IMPORT_CATEGORY_ANIMATION,
  94. INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE,
  95. INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE,
  96. INTERNAL_IMPORT_CATEGORY_MAX
  97. };
  98. private:
  99. mutable const HashMap<StringName, Variant> *current_options = nullptr;
  100. mutable const Dictionary *current_options_dict = nullptr;
  101. List<ResourceImporter::ImportOption> *current_option_list = nullptr;
  102. InternalImportCategory current_category = INTERNAL_IMPORT_CATEGORY_MAX;
  103. protected:
  104. GDVIRTUAL1(_get_internal_import_options, int)
  105. GDVIRTUAL3RC(Variant, _get_internal_option_visibility, int, bool, String)
  106. GDVIRTUAL2RC(Variant, _get_internal_option_update_view_required, int, String)
  107. GDVIRTUAL4(_internal_process, int, Node *, Node *, Ref<Resource>)
  108. GDVIRTUAL1(_get_import_options, String)
  109. GDVIRTUAL3RC(Variant, _get_option_visibility, String, bool, String)
  110. GDVIRTUAL1(_pre_process, Node *)
  111. GDVIRTUAL1(_post_process, Node *)
  112. static void _bind_methods();
  113. public:
  114. Variant get_option_value(const StringName &p_name) const;
  115. void add_import_option(const String &p_name, const Variant &p_default_value);
  116. void add_import_option_advanced(Variant::Type p_type, const String &p_name, const Variant &p_default_value, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = String(), int p_usage_flags = PROPERTY_USAGE_DEFAULT);
  117. virtual void get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options);
  118. virtual Variant get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
  119. virtual Variant get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
  120. virtual void internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options);
  121. virtual void get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options);
  122. virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
  123. virtual void pre_process(Node *p_scene, const HashMap<StringName, Variant> &p_options);
  124. virtual void post_process(Node *p_scene, const HashMap<StringName, Variant> &p_options);
  125. };
  126. VARIANT_ENUM_CAST(EditorScenePostImportPlugin::InternalImportCategory)
  127. class ResourceImporterScene : public ResourceImporter {
  128. GDCLASS(ResourceImporterScene, ResourceImporter);
  129. static Vector<Ref<EditorSceneFormatImporter>> scene_importers;
  130. static Vector<Ref<EditorScenePostImportPlugin>> post_importer_plugins;
  131. static ResourceImporterScene *scene_singleton;
  132. static ResourceImporterScene *animation_singleton;
  133. enum LightBakeMode {
  134. LIGHT_BAKE_DISABLED,
  135. LIGHT_BAKE_STATIC,
  136. LIGHT_BAKE_STATIC_LIGHTMAPS,
  137. LIGHT_BAKE_DYNAMIC,
  138. };
  139. enum MeshPhysicsMode {
  140. MESH_PHYSICS_DISABLED,
  141. MESH_PHYSICS_MESH_AND_STATIC_COLLIDER,
  142. MESH_PHYSICS_RIGID_BODY_AND_MESH,
  143. MESH_PHYSICS_STATIC_COLLIDER_ONLY,
  144. MESH_PHYSICS_AREA_ONLY,
  145. };
  146. enum NavMeshMode {
  147. NAVMESH_DISABLED,
  148. NAVMESH_MESH_AND_NAVMESH,
  149. NAVMESH_NAVMESH_ONLY,
  150. };
  151. enum OccluderMode {
  152. OCCLUDER_DISABLED,
  153. OCCLUDER_MESH_AND_OCCLUDER,
  154. OCCLUDER_OCCLUDER_ONLY,
  155. };
  156. enum MeshOverride {
  157. MESH_OVERRIDE_DEFAULT,
  158. MESH_OVERRIDE_ENABLE,
  159. MESH_OVERRIDE_DISABLE,
  160. };
  161. enum BodyType {
  162. BODY_TYPE_STATIC,
  163. BODY_TYPE_DYNAMIC,
  164. BODY_TYPE_AREA
  165. };
  166. enum ShapeType {
  167. SHAPE_TYPE_DECOMPOSE_CONVEX,
  168. SHAPE_TYPE_SIMPLE_CONVEX,
  169. SHAPE_TYPE_TRIMESH,
  170. SHAPE_TYPE_BOX,
  171. SHAPE_TYPE_SPHERE,
  172. SHAPE_TYPE_CYLINDER,
  173. SHAPE_TYPE_CAPSULE,
  174. SHAPE_TYPE_AUTOMATIC,
  175. };
  176. static Error _check_resource_save_paths(ResourceUID::ID p_source_id, const String &p_hash_suffix, const Dictionary &p_data);
  177. Array _get_skinned_pose_transforms(ImporterMeshInstance3D *p_src_mesh_node);
  178. void _replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner);
  179. Node *_generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches);
  180. void _add_shapes(Node *p_node, const Vector<Ref<Shape3D>> &p_shapes);
  181. void _copy_meta(Object *p_src_object, Object *p_dst_object);
  182. Node *_replace_node_with_type_and_script(Node *p_node, String p_node_type, Ref<Script> p_script);
  183. enum AnimationImportTracks {
  184. ANIMATION_IMPORT_TRACKS_IF_PRESENT,
  185. ANIMATION_IMPORT_TRACKS_IF_PRESENT_FOR_ALL,
  186. ANIMATION_IMPORT_TRACKS_NEVER,
  187. };
  188. enum TrackChannel {
  189. TRACK_CHANNEL_POSITION,
  190. TRACK_CHANNEL_ROTATION,
  191. TRACK_CHANNEL_SCALE,
  192. TRACK_CHANNEL_BLEND_SHAPE,
  193. TRACK_CHANNEL_MAX
  194. };
  195. void _optimize_track_usage(AnimationPlayer *p_player, AnimationImportTracks *p_track_actions);
  196. void _generate_editor_preview_for_scene(const String &p_path, Node *p_scene);
  197. String _scene_import_type = "PackedScene";
  198. public:
  199. static const String material_extension[3];
  200. static ResourceImporterScene *get_scene_singleton() { return scene_singleton; }
  201. static ResourceImporterScene *get_animation_singleton() { return animation_singleton; }
  202. static void add_post_importer_plugin(const Ref<EditorScenePostImportPlugin> &p_plugin, bool p_first_priority = false);
  203. static void remove_post_importer_plugin(const Ref<EditorScenePostImportPlugin> &p_plugin);
  204. const Vector<Ref<EditorSceneFormatImporter>> &get_scene_importers() const { return scene_importers; }
  205. static void add_scene_importer(Ref<EditorSceneFormatImporter> p_importer, bool p_first_priority = false);
  206. static void remove_scene_importer(Ref<EditorSceneFormatImporter> p_importer);
  207. static void get_scene_importer_extensions(List<String> *p_extensions);
  208. static void clean_up_importer_plugins();
  209. String get_scene_import_type() const { return _scene_import_type; }
  210. void set_scene_import_type(const String &p_type) { _scene_import_type = p_type; }
  211. virtual String get_importer_name() const override;
  212. virtual String get_visible_name() const override;
  213. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  214. virtual String get_save_extension() const override;
  215. virtual String get_resource_type() const override;
  216. virtual int get_format_version() const override;
  217. virtual int get_preset_count() const override;
  218. virtual String get_preset_name(int p_idx) const override;
  219. enum InternalImportCategory {
  220. INTERNAL_IMPORT_CATEGORY_NODE = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_NODE,
  221. INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE,
  222. INTERNAL_IMPORT_CATEGORY_MESH = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH,
  223. INTERNAL_IMPORT_CATEGORY_MATERIAL = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MATERIAL,
  224. INTERNAL_IMPORT_CATEGORY_ANIMATION = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_ANIMATION,
  225. INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE,
  226. INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE,
  227. INTERNAL_IMPORT_CATEGORY_MAX = EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MAX
  228. };
  229. void get_internal_import_options(InternalImportCategory p_category, List<ImportOption> *r_options) const;
  230. bool get_internal_option_visibility(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
  231. bool get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
  232. virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
  233. virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
  234. virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
  235. // Import scenes *after* everything else (such as textures).
  236. virtual int get_import_order() const override { return ResourceImporter::IMPORT_ORDER_SCENE; }
  237. void _pre_fix_global(Node *p_scene, const HashMap<StringName, Variant> &p_options) const;
  238. Node *_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames, const HashMap<StringName, Variant> &p_options);
  239. Node *_pre_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps);
  240. Node *_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps, float p_applied_root_scale, const String &p_source_file, const HashMap<StringName, Variant> &p_options);
  241. Node *_post_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps, bool p_remove_immutable_tracks);
  242. Ref<Animation> _save_animation_to_file(Ref<Animation> anim, bool p_save_to_file, const String &p_save_to_path, bool p_keep_custom_tracks);
  243. void _create_slices(AnimationPlayer *ap, Ref<Animation> anim, const Array &p_clips, bool p_bake_all);
  244. void _optimize_animations(AnimationPlayer *anim, float p_max_vel_error, float p_max_ang_error, int p_prc_error);
  245. void _compress_animations(AnimationPlayer *anim, int p_page_size_kb);
  246. Node *pre_import(const String &p_source_file, const HashMap<StringName, Variant> &p_options);
  247. virtual Error import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
  248. virtual bool has_advanced_options() const override;
  249. virtual void show_advanced_options(const String &p_path) override;
  250. ResourceImporterScene(const String &p_scene_import_type = "PackedScene", bool p_singleton = false);
  251. ~ResourceImporterScene();
  252. template <typename M>
  253. static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<ImporterMesh> &p_mesh, const M &p_options, float p_applied_root_scale);
  254. template <typename M>
  255. static Transform3D get_collision_shapes_transform(const M &p_options);
  256. };
  257. class EditorSceneFormatImporterESCN : public EditorSceneFormatImporter {
  258. GDCLASS(EditorSceneFormatImporterESCN, EditorSceneFormatImporter);
  259. public:
  260. virtual void get_extensions(List<String> *r_extensions) const override;
  261. virtual Node *import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err = nullptr) override;
  262. };
  263. template <typename M>
  264. Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<ImporterMesh> &p_mesh, const M &p_options, float p_applied_root_scale) {
  265. ERR_FAIL_COND_V(p_mesh.is_null(), Vector<Ref<Shape3D>>());
  266. ShapeType generate_shape_type = SHAPE_TYPE_AUTOMATIC;
  267. if (p_options.has(SNAME("physics/shape_type"))) {
  268. generate_shape_type = (ShapeType)p_options[SNAME("physics/shape_type")].operator int();
  269. }
  270. if (generate_shape_type == SHAPE_TYPE_AUTOMATIC) {
  271. BodyType body_type = BODY_TYPE_STATIC;
  272. if (p_options.has(SNAME("physics/body_type"))) {
  273. body_type = (BodyType)p_options[SNAME("physics/body_type")].operator int();
  274. }
  275. generate_shape_type = body_type == BODY_TYPE_DYNAMIC ? SHAPE_TYPE_DECOMPOSE_CONVEX : SHAPE_TYPE_TRIMESH;
  276. }
  277. if (generate_shape_type == SHAPE_TYPE_DECOMPOSE_CONVEX) {
  278. Ref<MeshConvexDecompositionSettings> decomposition_settings = Ref<MeshConvexDecompositionSettings>();
  279. decomposition_settings.instantiate();
  280. bool advanced = false;
  281. if (p_options.has(SNAME("decomposition/advanced"))) {
  282. advanced = p_options[SNAME("decomposition/advanced")];
  283. }
  284. if (advanced) {
  285. if (p_options.has(SNAME("decomposition/max_concavity"))) {
  286. decomposition_settings->set_max_concavity(p_options[SNAME("decomposition/max_concavity")]);
  287. }
  288. if (p_options.has(SNAME("decomposition/symmetry_planes_clipping_bias"))) {
  289. decomposition_settings->set_symmetry_planes_clipping_bias(p_options[SNAME("decomposition/symmetry_planes_clipping_bias")]);
  290. }
  291. if (p_options.has(SNAME("decomposition/revolution_axes_clipping_bias"))) {
  292. decomposition_settings->set_revolution_axes_clipping_bias(p_options[SNAME("decomposition/revolution_axes_clipping_bias")]);
  293. }
  294. if (p_options.has(SNAME("decomposition/min_volume_per_convex_hull"))) {
  295. decomposition_settings->set_min_volume_per_convex_hull(p_options[SNAME("decomposition/min_volume_per_convex_hull")]);
  296. }
  297. if (p_options.has(SNAME("decomposition/resolution"))) {
  298. decomposition_settings->set_resolution(p_options[SNAME("decomposition/resolution")]);
  299. }
  300. if (p_options.has(SNAME("decomposition/max_num_vertices_per_convex_hull"))) {
  301. decomposition_settings->set_max_num_vertices_per_convex_hull(p_options[SNAME("decomposition/max_num_vertices_per_convex_hull")]);
  302. }
  303. if (p_options.has(SNAME("decomposition/plane_downsampling"))) {
  304. decomposition_settings->set_plane_downsampling(p_options[SNAME("decomposition/plane_downsampling")]);
  305. }
  306. if (p_options.has(SNAME("decomposition/convexhull_downsampling"))) {
  307. decomposition_settings->set_convex_hull_downsampling(p_options[SNAME("decomposition/convexhull_downsampling")]);
  308. }
  309. if (p_options.has(SNAME("decomposition/normalize_mesh"))) {
  310. decomposition_settings->set_normalize_mesh(p_options[SNAME("decomposition/normalize_mesh")]);
  311. }
  312. if (p_options.has(SNAME("decomposition/mode"))) {
  313. decomposition_settings->set_mode((MeshConvexDecompositionSettings::Mode)p_options[SNAME("decomposition/mode")].operator int());
  314. }
  315. if (p_options.has(SNAME("decomposition/convexhull_approximation"))) {
  316. decomposition_settings->set_convex_hull_approximation(p_options[SNAME("decomposition/convexhull_approximation")]);
  317. }
  318. if (p_options.has(SNAME("decomposition/max_convex_hulls"))) {
  319. decomposition_settings->set_max_convex_hulls(MAX(1, (int)p_options[SNAME("decomposition/max_convex_hulls")]));
  320. }
  321. if (p_options.has(SNAME("decomposition/project_hull_vertices"))) {
  322. decomposition_settings->set_project_hull_vertices(p_options[SNAME("decomposition/project_hull_vertices")]);
  323. }
  324. } else {
  325. int precision_level = 5;
  326. if (p_options.has(SNAME("decomposition/precision"))) {
  327. precision_level = p_options[SNAME("decomposition/precision")];
  328. }
  329. const real_t precision = real_t(precision_level - 1) / 9.0;
  330. decomposition_settings->set_max_concavity(Math::lerp(real_t(1.0), real_t(0.001), precision));
  331. decomposition_settings->set_min_volume_per_convex_hull(Math::lerp(real_t(0.01), real_t(0.0001), precision));
  332. decomposition_settings->set_resolution(Math::lerp(10'000, 100'000, precision));
  333. decomposition_settings->set_max_num_vertices_per_convex_hull(Math::lerp(32, 64, precision));
  334. decomposition_settings->set_plane_downsampling(Math::lerp(3, 16, precision));
  335. decomposition_settings->set_convex_hull_downsampling(Math::lerp(3, 16, precision));
  336. decomposition_settings->set_max_convex_hulls(Math::lerp(1, 32, precision));
  337. }
  338. return p_mesh->convex_decompose(decomposition_settings);
  339. } else if (generate_shape_type == SHAPE_TYPE_SIMPLE_CONVEX) {
  340. Vector<Ref<Shape3D>> shapes;
  341. shapes.push_back(p_mesh->create_convex_shape(true, /*Passing false, otherwise VHACD will be used to simplify (Decompose) the Mesh.*/ false));
  342. return shapes;
  343. } else if (generate_shape_type == SHAPE_TYPE_TRIMESH) {
  344. Vector<Ref<Shape3D>> shapes;
  345. shapes.push_back(p_mesh->create_trimesh_shape());
  346. return shapes;
  347. } else if (generate_shape_type == SHAPE_TYPE_BOX) {
  348. Ref<BoxShape3D> box;
  349. box.instantiate();
  350. if (p_options.has(SNAME("primitive/size"))) {
  351. box->set_size(p_options[SNAME("primitive/size")].operator Vector3() * p_applied_root_scale);
  352. } else {
  353. box->set_size(Vector3(2, 2, 2) * p_applied_root_scale);
  354. }
  355. Vector<Ref<Shape3D>> shapes;
  356. shapes.push_back(box);
  357. return shapes;
  358. } else if (generate_shape_type == SHAPE_TYPE_SPHERE) {
  359. Ref<SphereShape3D> sphere;
  360. sphere.instantiate();
  361. if (p_options.has(SNAME("primitive/radius"))) {
  362. sphere->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
  363. } else {
  364. sphere->set_radius(1.0f * p_applied_root_scale);
  365. }
  366. Vector<Ref<Shape3D>> shapes;
  367. shapes.push_back(sphere);
  368. return shapes;
  369. } else if (generate_shape_type == SHAPE_TYPE_CYLINDER) {
  370. Ref<CylinderShape3D> cylinder;
  371. cylinder.instantiate();
  372. if (p_options.has(SNAME("primitive/height"))) {
  373. cylinder->set_height(p_options[SNAME("primitive/height")].operator float() * p_applied_root_scale);
  374. } else {
  375. cylinder->set_height(1.0f * p_applied_root_scale);
  376. }
  377. if (p_options.has(SNAME("primitive/radius"))) {
  378. cylinder->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
  379. } else {
  380. cylinder->set_radius(1.0f * p_applied_root_scale);
  381. }
  382. Vector<Ref<Shape3D>> shapes;
  383. shapes.push_back(cylinder);
  384. return shapes;
  385. } else if (generate_shape_type == SHAPE_TYPE_CAPSULE) {
  386. Ref<CapsuleShape3D> capsule;
  387. capsule.instantiate();
  388. if (p_options.has(SNAME("primitive/height"))) {
  389. capsule->set_height(p_options[SNAME("primitive/height")].operator float() * p_applied_root_scale);
  390. } else {
  391. capsule->set_height(1.0f * p_applied_root_scale);
  392. }
  393. if (p_options.has(SNAME("primitive/radius"))) {
  394. capsule->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
  395. } else {
  396. capsule->set_radius(1.0f * p_applied_root_scale);
  397. }
  398. Vector<Ref<Shape3D>> shapes;
  399. shapes.push_back(capsule);
  400. return shapes;
  401. }
  402. return Vector<Ref<Shape3D>>();
  403. }
  404. template <typename M>
  405. Transform3D ResourceImporterScene::get_collision_shapes_transform(const M &p_options) {
  406. Transform3D transform;
  407. ShapeType generate_shape_type = SHAPE_TYPE_AUTOMATIC;
  408. if (p_options.has(SNAME("physics/shape_type"))) {
  409. generate_shape_type = (ShapeType)p_options[SNAME("physics/shape_type")].operator int();
  410. }
  411. if (generate_shape_type == SHAPE_TYPE_AUTOMATIC) {
  412. BodyType body_type = BODY_TYPE_STATIC;
  413. if (p_options.has(SNAME("physics/body_type"))) {
  414. body_type = (BodyType)p_options[SNAME("physics/body_type")].operator int();
  415. }
  416. generate_shape_type = body_type == BODY_TYPE_DYNAMIC ? SHAPE_TYPE_DECOMPOSE_CONVEX : SHAPE_TYPE_TRIMESH;
  417. }
  418. if (generate_shape_type == SHAPE_TYPE_BOX ||
  419. generate_shape_type == SHAPE_TYPE_SPHERE ||
  420. generate_shape_type == SHAPE_TYPE_CYLINDER ||
  421. generate_shape_type == SHAPE_TYPE_CAPSULE) {
  422. if (p_options.has(SNAME("primitive/position"))) {
  423. transform.origin = p_options[SNAME("primitive/position")];
  424. }
  425. if (p_options.has(SNAME("primitive/rotation"))) {
  426. transform.basis = Basis::from_euler(p_options[SNAME("primitive/rotation")].operator Vector3() * (Math::PI / 180.0));
  427. }
  428. }
  429. return transform;
  430. }