Debian Codegraph @

editor/import/3d/resource_importer_scene.h

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