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