resource_importer_scene.h 21 KB

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