Merge pull request #72245 from JonqsGames/apply_root_scale_to_shape
Apply root scale to 3d shapes on import
This commit is contained in:
commit
78e7087d81
3 changed files with 19 additions and 14 deletions
|
@ -1085,10 +1085,10 @@ Node *ResourceImporterScene::_post_fix_animations(Node *p_node, Node *p_root, co
|
|||
return p_node;
|
||||
}
|
||||
|
||||
Node *ResourceImporterScene::_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) {
|
||||
Node *ResourceImporterScene::_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) {
|
||||
// children first
|
||||
for (int i = 0; i < p_node->get_child_count(); i++) {
|
||||
Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_occluder_arrays, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps);
|
||||
Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_occluder_arrays, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps, p_applied_root_scale);
|
||||
if (!r) {
|
||||
i--; //was erased
|
||||
}
|
||||
|
@ -1231,7 +1231,8 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
|
|||
} else {
|
||||
shapes = get_collision_shapes(
|
||||
m->get_mesh(),
|
||||
node_settings);
|
||||
node_settings,
|
||||
p_applied_root_scale);
|
||||
}
|
||||
|
||||
if (shapes.size()) {
|
||||
|
@ -1242,6 +1243,7 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
|
|||
p_node->add_child(col, true);
|
||||
col->set_owner(p_node->get_owner());
|
||||
col->set_transform(get_collision_shapes_transform(node_settings));
|
||||
col->set_position(p_applied_root_scale * col->get_position());
|
||||
base = col;
|
||||
} break;
|
||||
case MESH_PHYSICS_RIGID_BODY_AND_MESH: {
|
||||
|
@ -1249,6 +1251,7 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
|
|||
rigid_body->set_name(p_node->get_name());
|
||||
p_node->replace_by(rigid_body);
|
||||
rigid_body->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
|
||||
rigid_body->set_position(p_applied_root_scale * rigid_body->get_position());
|
||||
p_node = rigid_body;
|
||||
mi->set_transform(Transform3D());
|
||||
rigid_body->add_child(mi, true);
|
||||
|
@ -1258,6 +1261,7 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
|
|||
case MESH_PHYSICS_STATIC_COLLIDER_ONLY: {
|
||||
StaticBody3D *col = memnew(StaticBody3D);
|
||||
col->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
|
||||
col->set_position(p_applied_root_scale * col->get_position());
|
||||
col->set_name(p_node->get_name());
|
||||
p_node->replace_by(col);
|
||||
memdelete(p_node);
|
||||
|
@ -1267,6 +1271,7 @@ Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<
|
|||
case MESH_PHYSICS_AREA_ONLY: {
|
||||
Area3D *area = memnew(Area3D);
|
||||
area->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
|
||||
area->set_position(p_applied_root_scale * area->get_position());
|
||||
area->set_name(p_node->get_name());
|
||||
p_node->replace_by(area);
|
||||
memdelete(p_node);
|
||||
|
@ -2398,7 +2403,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
|
|||
fps = (float)p_options[SNAME("animation/fps")];
|
||||
}
|
||||
_pre_fix_animations(scene, scene, node_data, animation_data, fps);
|
||||
_post_fix_node(scene, scene, collision_map, occluder_arrays, scanned_meshes, node_data, material_data, animation_data, fps);
|
||||
_post_fix_node(scene, scene, collision_map, occluder_arrays, scanned_meshes, node_data, material_data, animation_data, fps, apply_root ? root_scale : 1.0);
|
||||
_post_fix_animations(scene, scene, node_data, animation_data, fps);
|
||||
|
||||
String root_type = p_options["nodes/root_type"];
|
||||
|
|
|
@ -279,7 +279,7 @@ public:
|
|||
|
||||
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);
|
||||
Node *_pre_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps);
|
||||
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);
|
||||
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);
|
||||
Node *_post_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps);
|
||||
|
||||
Ref<Animation> _save_animation_to_file(Ref<Animation> anim, bool p_save_to_file, String p_save_to_path, bool p_keep_custom_tracks);
|
||||
|
@ -298,7 +298,7 @@ public:
|
|||
ResourceImporterScene(bool p_animation_import = false);
|
||||
|
||||
template <class M>
|
||||
static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options);
|
||||
static Vector<Ref<Shape3D>> get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options, float p_applied_root_scale);
|
||||
|
||||
template <class M>
|
||||
static Transform3D get_collision_shapes_transform(const M &p_options);
|
||||
|
@ -314,7 +314,7 @@ public:
|
|||
};
|
||||
|
||||
template <class M>
|
||||
Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options) {
|
||||
Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh> &p_mesh, const M &p_options, float p_applied_root_scale) {
|
||||
ShapeType generate_shape_type = SHAPE_TYPE_DECOMPOSE_CONVEX;
|
||||
if (p_options.has(SNAME("physics/shape_type"))) {
|
||||
generate_shape_type = (ShapeType)p_options[SNAME("physics/shape_type")].operator int();
|
||||
|
@ -409,7 +409,7 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
|
|||
Ref<BoxShape3D> box;
|
||||
box.instantiate();
|
||||
if (p_options.has(SNAME("primitive/size"))) {
|
||||
box->set_size(p_options[SNAME("primitive/size")]);
|
||||
box->set_size(p_options[SNAME("primitive/size")].operator Vector3() * p_applied_root_scale);
|
||||
}
|
||||
|
||||
Vector<Ref<Shape3D>> shapes;
|
||||
|
@ -420,7 +420,7 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
|
|||
Ref<SphereShape3D> sphere;
|
||||
sphere.instantiate();
|
||||
if (p_options.has(SNAME("primitive/radius"))) {
|
||||
sphere->set_radius(p_options[SNAME("primitive/radius")]);
|
||||
sphere->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
|
||||
}
|
||||
|
||||
Vector<Ref<Shape3D>> shapes;
|
||||
|
@ -430,10 +430,10 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
|
|||
Ref<CylinderShape3D> cylinder;
|
||||
cylinder.instantiate();
|
||||
if (p_options.has(SNAME("primitive/height"))) {
|
||||
cylinder->set_height(p_options[SNAME("primitive/height")]);
|
||||
cylinder->set_height(p_options[SNAME("primitive/height")].operator float() * p_applied_root_scale);
|
||||
}
|
||||
if (p_options.has(SNAME("primitive/radius"))) {
|
||||
cylinder->set_radius(p_options[SNAME("primitive/radius")]);
|
||||
cylinder->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
|
||||
}
|
||||
|
||||
Vector<Ref<Shape3D>> shapes;
|
||||
|
@ -443,10 +443,10 @@ Vector<Ref<Shape3D>> ResourceImporterScene::get_collision_shapes(const Ref<Mesh>
|
|||
Ref<CapsuleShape3D> capsule;
|
||||
capsule.instantiate();
|
||||
if (p_options.has(SNAME("primitive/height"))) {
|
||||
capsule->set_height(p_options[SNAME("primitive/height")]);
|
||||
capsule->set_height(p_options[SNAME("primitive/height")].operator float() * p_applied_root_scale);
|
||||
}
|
||||
if (p_options.has(SNAME("primitive/radius"))) {
|
||||
capsule->set_radius(p_options[SNAME("primitive/radius")]);
|
||||
capsule->set_radius(p_options[SNAME("primitive/radius")].operator float() * p_applied_root_scale);
|
||||
}
|
||||
|
||||
Vector<Ref<Shape3D>> shapes;
|
||||
|
|
|
@ -441,7 +441,7 @@ void SceneImportSettings::_update_view_gizmos() {
|
|||
// This collider_view doesn't have a mesh so we need to generate a new one.
|
||||
|
||||
// Generate the mesh collider.
|
||||
Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh_node->get_mesh(), e.value.settings);
|
||||
Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh_node->get_mesh(), e.value.settings, 1.0);
|
||||
const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
|
||||
|
||||
Ref<ArrayMesh> collider_view_mesh;
|
||||
|
|
Loading…
Reference in a new issue