|
@@ -34,13 +34,26 @@
|
|
|
|
|
|
// Import process.
|
|
|
Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
|
|
|
- if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body")) {
|
|
|
+ if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body") && !p_extensions.has("OMI_physics_shape")) {
|
|
|
return ERR_SKIP;
|
|
|
}
|
|
|
Dictionary state_json = p_state->get_json();
|
|
|
if (state_json.has("extensions")) {
|
|
|
Dictionary state_extensions = state_json["extensions"];
|
|
|
- if (state_extensions.has("OMI_collider")) {
|
|
|
+ if (state_extensions.has("OMI_physics_shape")) {
|
|
|
+ Dictionary omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
|
|
|
+ if (omi_physics_shape_ext.has("shapes")) {
|
|
|
+ Array state_shape_dicts = omi_physics_shape_ext["shapes"];
|
|
|
+ if (state_shape_dicts.size() > 0) {
|
|
|
+ Array state_shapes;
|
|
|
+ for (int i = 0; i < state_shape_dicts.size(); i++) {
|
|
|
+ state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));
|
|
|
+ }
|
|
|
+ p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+#ifndef DISABLE_DEPRECATED
|
|
|
+ } else if (state_extensions.has("OMI_collider")) {
|
|
|
Dictionary omi_collider_ext = state_extensions["OMI_collider"];
|
|
|
if (omi_collider_ext.has("colliders")) {
|
|
|
Array state_collider_dicts = omi_collider_ext["colliders"];
|
|
@@ -49,9 +62,10 @@ Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vec
|
|
|
for (int i = 0; i < state_collider_dicts.size(); i++) {
|
|
|
state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));
|
|
|
}
|
|
|
- p_state->set_additional_data("GLTFPhysicsShapes", state_colliders);
|
|
|
+ p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);
|
|
|
}
|
|
|
}
|
|
|
+#endif // DISABLE_DEPRECATED
|
|
|
}
|
|
|
}
|
|
|
return OK;
|
|
@@ -61,49 +75,87 @@ Vector<String> GLTFDocumentExtensionPhysics::get_supported_extensions() {
|
|
|
Vector<String> ret;
|
|
|
ret.push_back("OMI_collider");
|
|
|
ret.push_back("OMI_physics_body");
|
|
|
+ ret.push_back("OMI_physics_shape");
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
|
|
|
+#ifndef DISABLE_DEPRECATED
|
|
|
if (p_extensions.has("OMI_collider")) {
|
|
|
Dictionary node_collider_ext = p_extensions["OMI_collider"];
|
|
|
if (node_collider_ext.has("collider")) {
|
|
|
// "collider" is the index of the collider in the state colliders array.
|
|
|
int node_collider_index = node_collider_ext["collider"];
|
|
|
- Array state_colliders = p_state->get_additional_data("GLTFPhysicsShapes");
|
|
|
+ Array state_colliders = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
|
|
ERR_FAIL_INDEX_V_MSG(node_collider_index, state_colliders.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the collider index " + itos(node_collider_index) + " is not in the state colliders (size: " + itos(state_colliders.size()) + ").");
|
|
|
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);
|
|
|
} else {
|
|
|
- p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(p_extensions["OMI_collider"]));
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));
|
|
|
}
|
|
|
}
|
|
|
+#endif // DISABLE_DEPRECATED
|
|
|
if (p_extensions.has("OMI_physics_body")) {
|
|
|
- p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(p_extensions["OMI_physics_body"]));
|
|
|
+ Dictionary physics_body_ext = p_extensions["OMI_physics_body"];
|
|
|
+ if (physics_body_ext.has("collider")) {
|
|
|
+ Dictionary node_collider = physics_body_ext["collider"];
|
|
|
+ // "shape" is the index of the shape in the state shapes array.
|
|
|
+ int node_shape_index = node_collider.get("shape", -1);
|
|
|
+ if (node_shape_index != -1) {
|
|
|
+ Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
|
|
+ ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), state_shapes[node_shape_index]);
|
|
|
+ } else {
|
|
|
+ // If this node is a collider but does not have a collider
|
|
|
+ // shape, then it only serves to combine together shapes.
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundCollider"), true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (physics_body_ext.has("trigger")) {
|
|
|
+ Dictionary node_trigger = physics_body_ext["trigger"];
|
|
|
+ // "shape" is the index of the shape in the state shapes array.
|
|
|
+ int node_shape_index = node_trigger.get("shape", -1);
|
|
|
+ if (node_shape_index != -1) {
|
|
|
+ Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
|
|
|
+ ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "GLTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), state_shapes[node_shape_index]);
|
|
|
+ } else {
|
|
|
+ // If this node is a trigger but does not have a trigger shape,
|
|
|
+ // then it's a trigger body, what Godot calls an Area3D node.
|
|
|
+ Ref<GLTFPhysicsBody> trigger_body;
|
|
|
+ trigger_body.instantiate();
|
|
|
+ trigger_body->set_body_type("trigger");
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), trigger_body);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));
|
|
|
+ }
|
|
|
}
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
-void _setup_collider_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_collider) {
|
|
|
- GLTFMeshIndex collider_mesh_index = p_collider->get_mesh_index();
|
|
|
- if (collider_mesh_index == -1) {
|
|
|
- return; // No mesh for this collider.
|
|
|
+void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_gltf_shape) {
|
|
|
+ GLTFMeshIndex shape_mesh_index = p_gltf_shape->get_mesh_index();
|
|
|
+ if (shape_mesh_index == -1) {
|
|
|
+ return; // No mesh for this shape.
|
|
|
}
|
|
|
- Ref<ImporterMesh> importer_mesh = p_collider->get_importer_mesh();
|
|
|
+ Ref<ImporterMesh> importer_mesh = p_gltf_shape->get_importer_mesh();
|
|
|
if (importer_mesh.is_valid()) {
|
|
|
return; // The mesh resource is already set up.
|
|
|
}
|
|
|
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
|
|
|
- ERR_FAIL_INDEX_MSG(collider_mesh_index, state_meshes.size(), "GLTF Physics: When importing '" + p_state->get_scene_name() + "', the collider mesh index " + itos(collider_mesh_index) + " is not in the state meshes (size: " + itos(state_meshes.size()) + ").");
|
|
|
- Ref<GLTFMesh> gltf_mesh = state_meshes[collider_mesh_index];
|
|
|
+ ERR_FAIL_INDEX_MSG(shape_mesh_index, state_meshes.size(), "GLTF Physics: When importing '" + p_state->get_scene_name() + "', the shape mesh index " + itos(shape_mesh_index) + " is not in the state meshes (size: " + itos(state_meshes.size()) + ").");
|
|
|
+ Ref<GLTFMesh> gltf_mesh = state_meshes[shape_mesh_index];
|
|
|
ERR_FAIL_COND(gltf_mesh.is_null());
|
|
|
importer_mesh = gltf_mesh->get_mesh();
|
|
|
ERR_FAIL_COND(importer_mesh.is_null());
|
|
|
- p_collider->set_importer_mesh(importer_mesh);
|
|
|
+ p_gltf_shape->set_importer_mesh(importer_mesh);
|
|
|
}
|
|
|
|
|
|
-CollisionObject3D *_generate_collision_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_collider, Ref<GLTFPhysicsBody> p_physics_body) {
|
|
|
- print_verbose("glTF: Creating collision for: " + p_gltf_node->get_name());
|
|
|
- bool is_trigger = p_collider->get_is_trigger();
|
|
|
+#ifndef DISABLE_DEPRECATED
|
|
|
+CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, Ref<GLTFPhysicsBody> p_physics_body) {
|
|
|
+ print_verbose("glTF: Creating shape with body for: " + p_gltf_node->get_name());
|
|
|
+ bool is_trigger = p_physics_shape->get_is_trigger();
|
|
|
// This method is used for the case where we must generate a parent body.
|
|
|
// This is can happen for multiple reasons. One possibility is that this
|
|
|
// GLTF file is using OMI_collider but not OMI_physics_body, or at least
|
|
@@ -113,10 +165,10 @@ CollisionObject3D *_generate_collision_with_body(Ref<GLTFState> p_state, Ref<GLT
|
|
|
if (p_physics_body.is_valid()) {
|
|
|
// This code is run when the physics body is on the same GLTF node.
|
|
|
body = p_physics_body->to_node();
|
|
|
- if (is_trigger != (p_physics_body->get_body_type() == "trigger")) {
|
|
|
+ if (is_trigger && (p_physics_body->get_body_type() != "trigger")) {
|
|
|
// Edge case: If the body's trigger and the collider's trigger
|
|
|
// are in disagreement, we need to create another new body.
|
|
|
- CollisionObject3D *child = _generate_collision_with_body(p_state, p_gltf_node, p_collider, nullptr);
|
|
|
+ CollisionObject3D *child = _generate_shape_with_body(p_state, p_gltf_node, p_physics_shape, nullptr);
|
|
|
child->set_name(p_gltf_node->get_name() + (is_trigger ? String("Trigger") : String("Solid")));
|
|
|
body->add_child(child);
|
|
|
return body;
|
|
@@ -126,33 +178,131 @@ CollisionObject3D *_generate_collision_with_body(Ref<GLTFState> p_state, Ref<GLT
|
|
|
} else {
|
|
|
body = memnew(StaticBody3D);
|
|
|
}
|
|
|
- CollisionShape3D *shape = p_collider->to_node();
|
|
|
+ CollisionShape3D *shape = p_physics_shape->to_node();
|
|
|
shape->set_name(p_gltf_node->get_name() + "Shape");
|
|
|
body->add_child(shape);
|
|
|
return body;
|
|
|
}
|
|
|
+#endif // DISABLE_DEPRECATED
|
|
|
+
|
|
|
+CollisionObject3D *_get_ancestor_collision_object(Node *p_scene_parent) {
|
|
|
+ // Note: Despite the name of the method, at the moment this only checks
|
|
|
+ // the direct parent. Only check more later if Godot adds support for it.
|
|
|
+ if (p_scene_parent) {
|
|
|
+ CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_scene_parent);
|
|
|
+ if (likely(co)) {
|
|
|
+ return co;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nullptr;
|
|
|
+}
|
|
|
+
|
|
|
+Node3D *_generate_shape_node_and_body_if_needed(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, CollisionObject3D *p_col_object, bool p_is_trigger) {
|
|
|
+ // If we need to generate a body node, do so.
|
|
|
+ CollisionObject3D *body_node = nullptr;
|
|
|
+ if (p_is_trigger || p_physics_shape->get_is_trigger()) {
|
|
|
+ // If the shape wants to be a trigger but it doesn't
|
|
|
+ // have an Area3D parent, we need to make one.
|
|
|
+ if (!Object::cast_to<Area3D>(p_col_object)) {
|
|
|
+ body_node = memnew(Area3D);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!Object::cast_to<PhysicsBody3D>(p_col_object)) {
|
|
|
+ body_node = memnew(StaticBody3D);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Generate the shape node.
|
|
|
+ _setup_shape_mesh_resource_from_index_if_needed(p_state, p_physics_shape);
|
|
|
+ CollisionShape3D *shape_node = p_physics_shape->to_node(true);
|
|
|
+ if (body_node) {
|
|
|
+ shape_node->set_name(p_gltf_node->get_name() + "Shape");
|
|
|
+ body_node->add_child(shape_node);
|
|
|
+ return body_node;
|
|
|
+ }
|
|
|
+ return shape_node;
|
|
|
+}
|
|
|
+
|
|
|
+// Either add the child to the parent, or return the child if there is no parent.
|
|
|
+Node3D *_add_physics_node_to_given_node(Node3D *p_current_node, Node3D *p_child, Ref<GLTFNode> p_gltf_node) {
|
|
|
+ if (!p_current_node) {
|
|
|
+ return p_child;
|
|
|
+ }
|
|
|
+ String suffix;
|
|
|
+ if (Object::cast_to<CollisionShape3D>(p_child)) {
|
|
|
+ suffix = "Shape";
|
|
|
+ } else if (Object::cast_to<Area3D>(p_child)) {
|
|
|
+ suffix = "Trigger";
|
|
|
+ } else {
|
|
|
+ suffix = "Collider";
|
|
|
+ }
|
|
|
+ p_child->set_name(p_gltf_node->get_name() + suffix);
|
|
|
+ p_current_node->add_child(p_child);
|
|
|
+ return p_current_node;
|
|
|
+}
|
|
|
|
|
|
Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
|
|
|
- Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
|
|
- Ref<GLTFPhysicsShape> collider = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
|
|
|
- if (collider.is_valid()) {
|
|
|
- _setup_collider_mesh_resource_from_index_if_needed(p_state, collider);
|
|
|
- // If the collider has the correct type of parent, we just return one node.
|
|
|
- if (collider->get_is_trigger()) {
|
|
|
- if (Object::cast_to<Area3D>(p_scene_parent)) {
|
|
|
- return collider->to_node(true);
|
|
|
+ Ref<GLTFPhysicsBody> gltf_physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
|
|
+#ifndef DISABLE_DEPRECATED
|
|
|
+ // This deprecated code handles OMI_collider (which we internally name "GLTFPhysicsShape").
|
|
|
+ Ref<GLTFPhysicsShape> gltf_physics_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
|
|
|
+ if (gltf_physics_shape.is_valid()) {
|
|
|
+ _setup_shape_mesh_resource_from_index_if_needed(p_state, gltf_physics_shape);
|
|
|
+ // If this GLTF node specifies both a shape and a body, generate both.
|
|
|
+ if (gltf_physics_body.is_valid()) {
|
|
|
+ return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, gltf_physics_body);
|
|
|
+ }
|
|
|
+ CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
|
|
|
+ if (gltf_physics_shape->get_is_trigger()) {
|
|
|
+ // If the shape wants to be a trigger and it already has a
|
|
|
+ // trigger parent, we only need to make the shape node.
|
|
|
+ if (Object::cast_to<Area3D>(ancestor_col_obj)) {
|
|
|
+ return gltf_physics_shape->to_node(true);
|
|
|
}
|
|
|
- } else {
|
|
|
- if (Object::cast_to<PhysicsBody3D>(p_scene_parent)) {
|
|
|
- return collider->to_node(true);
|
|
|
+ } else if (ancestor_col_obj != nullptr) {
|
|
|
+ // If the shape has a valid parent, only make the shape node.
|
|
|
+ return gltf_physics_shape->to_node(true);
|
|
|
+ }
|
|
|
+ // Otherwise, we need to create a new body.
|
|
|
+ return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, nullptr);
|
|
|
+ }
|
|
|
+#endif // DISABLE_DEPRECATED
|
|
|
+ Node3D *ret = nullptr;
|
|
|
+ CollisionObject3D *ancestor_col_obj = nullptr;
|
|
|
+ if (gltf_physics_body.is_valid()) {
|
|
|
+ ancestor_col_obj = gltf_physics_body->to_node();
|
|
|
+ ret = ancestor_col_obj;
|
|
|
+ } else {
|
|
|
+ ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
|
|
|
+ if (!Object::cast_to<PhysicsBody3D>(ancestor_col_obj)) {
|
|
|
+ if (p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundCollider"))) {
|
|
|
+ // If the GLTF file wants this node to group solid shapes together,
|
|
|
+ // and there is no parent body, we need to create a static body.
|
|
|
+ ancestor_col_obj = memnew(StaticBody3D);
|
|
|
+ ret = ancestor_col_obj;
|
|
|
}
|
|
|
}
|
|
|
- return _generate_collision_with_body(p_state, p_gltf_node, collider, physics_body);
|
|
|
}
|
|
|
- if (physics_body.is_valid()) {
|
|
|
- return physics_body->to_node();
|
|
|
+ // Add the shapes to the tree. When an ancestor body is present, use it.
|
|
|
+ // If an explicit body was specified, it has already been generated and
|
|
|
+ // set above. If there is no ancestor body, we will either generate an
|
|
|
+ // Area3D or StaticBody3D implicitly, so prefer an Area3D as the base
|
|
|
+ // node for best compatibility with signal connections to this node.
|
|
|
+ Ref<GLTFPhysicsShape> gltf_physics_collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
|
|
+ Ref<GLTFPhysicsShape> gltf_physics_trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
|
|
+ bool is_ancestor_col_obj_solid = Object::cast_to<PhysicsBody3D>(ancestor_col_obj);
|
|
|
+ if (is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
|
|
|
+ Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
|
|
|
+ ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
|
|
}
|
|
|
- return nullptr;
|
|
|
+ if (gltf_physics_trigger_shape.is_valid()) {
|
|
|
+ Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_trigger_shape, ancestor_col_obj, true);
|
|
|
+ ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
|
|
+ }
|
|
|
+ if (!is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
|
|
|
+ Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
|
|
|
+ ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
// Export process.
|
|
@@ -202,22 +352,26 @@ GLTFMeshIndex _get_or_insert_mesh_in_state(Ref<GLTFState> p_state, Ref<ImporterM
|
|
|
|
|
|
void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
|
|
|
if (cast_to<CollisionShape3D>(p_scene_node)) {
|
|
|
- CollisionShape3D *shape = Object::cast_to<CollisionShape3D>(p_scene_node);
|
|
|
- Ref<GLTFPhysicsShape> collider = GLTFPhysicsShape::from_node(shape);
|
|
|
+ CollisionShape3D *godot_shape = Object::cast_to<CollisionShape3D>(p_scene_node);
|
|
|
+ Ref<GLTFPhysicsShape> gltf_shape = GLTFPhysicsShape::from_node(godot_shape);
|
|
|
{
|
|
|
- Ref<ImporterMesh> importer_mesh = collider->get_importer_mesh();
|
|
|
+ Ref<ImporterMesh> importer_mesh = gltf_shape->get_importer_mesh();
|
|
|
if (importer_mesh.is_valid()) {
|
|
|
- collider->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
|
|
|
+ gltf_shape->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
|
|
|
}
|
|
|
}
|
|
|
- p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), collider);
|
|
|
+ if (cast_to<Area3D>(_get_ancestor_collision_object(p_scene_node))) {
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), gltf_shape);
|
|
|
+ } else {
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), gltf_shape);
|
|
|
+ }
|
|
|
} else if (cast_to<CollisionObject3D>(p_scene_node)) {
|
|
|
- CollisionObject3D *body = Object::cast_to<CollisionObject3D>(p_scene_node);
|
|
|
- p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(body));
|
|
|
+ CollisionObject3D *godot_body = Object::cast_to<CollisionObject3D>(p_scene_node);
|
|
|
+ p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(godot_body));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-Array _get_or_create_state_colliders_in_state(Ref<GLTFState> p_state) {
|
|
|
+Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
|
|
|
Dictionary state_json = p_state->get_json();
|
|
|
Dictionary state_extensions;
|
|
|
if (state_json.has("extensions")) {
|
|
@@ -225,48 +379,60 @@ Array _get_or_create_state_colliders_in_state(Ref<GLTFState> p_state) {
|
|
|
} else {
|
|
|
state_json["extensions"] = state_extensions;
|
|
|
}
|
|
|
- Dictionary omi_collider_ext;
|
|
|
- if (state_extensions.has("OMI_collider")) {
|
|
|
- omi_collider_ext = state_extensions["OMI_collider"];
|
|
|
+ Dictionary omi_physics_shape_ext;
|
|
|
+ if (state_extensions.has("OMI_physics_shape")) {
|
|
|
+ omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
|
|
|
} else {
|
|
|
- state_extensions["OMI_collider"] = omi_collider_ext;
|
|
|
- p_state->add_used_extension("OMI_collider");
|
|
|
+ state_extensions["OMI_physics_shape"] = omi_physics_shape_ext;
|
|
|
+ p_state->add_used_extension("OMI_physics_shape");
|
|
|
}
|
|
|
- Array state_colliders;
|
|
|
- if (omi_collider_ext.has("colliders")) {
|
|
|
- state_colliders = omi_collider_ext["colliders"];
|
|
|
+ Array state_shapes;
|
|
|
+ if (omi_physics_shape_ext.has("shapes")) {
|
|
|
+ state_shapes = omi_physics_shape_ext["shapes"];
|
|
|
} else {
|
|
|
- omi_collider_ext["colliders"] = state_colliders;
|
|
|
+ omi_physics_shape_ext["shapes"] = state_shapes;
|
|
|
+ }
|
|
|
+ return state_shapes;
|
|
|
+}
|
|
|
+
|
|
|
+Dictionary _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
|
|
|
+ Array state_shapes = _get_or_create_state_shapes_in_state(p_state);
|
|
|
+ int size = state_shapes.size();
|
|
|
+ Dictionary shape_property;
|
|
|
+ Dictionary shape_dict = p_physics_shape->to_dictionary();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ Dictionary other = state_shapes[i];
|
|
|
+ if (other == shape_dict) {
|
|
|
+ // De-duplication: If we already have an identical shape,
|
|
|
+ // set the shape index to the existing one and return.
|
|
|
+ shape_property["shape"] = i;
|
|
|
+ return shape_property;
|
|
|
+ }
|
|
|
}
|
|
|
- return state_colliders;
|
|
|
+ // If we don't have an identical shape, add it to the array.
|
|
|
+ state_shapes.push_back(shape_dict);
|
|
|
+ shape_property["shape"] = size;
|
|
|
+ return shape_property;
|
|
|
}
|
|
|
|
|
|
Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_node) {
|
|
|
- Dictionary node_extensions = r_node_json["extensions"];
|
|
|
+ Dictionary physics_body_ext;
|
|
|
Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
|
|
|
if (physics_body.is_valid()) {
|
|
|
- node_extensions["OMI_physics_body"] = physics_body->to_dictionary();
|
|
|
- p_state->add_used_extension("OMI_physics_body");
|
|
|
+ physics_body_ext = physics_body->to_dictionary();
|
|
|
}
|
|
|
- Ref<GLTFPhysicsShape> collider = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
|
|
|
- if (collider.is_valid()) {
|
|
|
- Array state_colliders = _get_or_create_state_colliders_in_state(p_state);
|
|
|
- int size = state_colliders.size();
|
|
|
- Dictionary omi_collider_ext;
|
|
|
- node_extensions["OMI_collider"] = omi_collider_ext;
|
|
|
- Dictionary collider_dict = collider->to_dictionary();
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- Dictionary other = state_colliders[i];
|
|
|
- if (other == collider_dict) {
|
|
|
- // De-duplication: If we already have an identical collider,
|
|
|
- // set the collider index to the existing one and return.
|
|
|
- omi_collider_ext["collider"] = i;
|
|
|
- return OK;
|
|
|
- }
|
|
|
- }
|
|
|
- // If we don't have an identical collider, add it to the array.
|
|
|
- state_colliders.push_back(collider_dict);
|
|
|
- omi_collider_ext["collider"] = size;
|
|
|
+ Ref<GLTFPhysicsShape> collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
|
|
|
+ if (collider_shape.is_valid()) {
|
|
|
+ physics_body_ext["collider"] = _export_node_shape(p_state, collider_shape);
|
|
|
+ }
|
|
|
+ Ref<GLTFPhysicsShape> trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
|
|
|
+ if (trigger_shape.is_valid()) {
|
|
|
+ physics_body_ext["trigger"] = _export_node_shape(p_state, trigger_shape);
|
|
|
+ }
|
|
|
+ if (!physics_body_ext.is_empty()) {
|
|
|
+ Dictionary node_extensions = r_node_json["extensions"];
|
|
|
+ node_extensions["OMI_physics_body"] = physics_body_ext;
|
|
|
+ p_state->add_used_extension("OMI_physics_body");
|
|
|
}
|
|
|
return OK;
|
|
|
}
|