resource_importer_scene.h 23 KB

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