Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTF: Update OMI_physics_body, add OMI_physics_shape, keep OMI_collider #78967

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

aaronfranke
Copy link
Member

@aaronfranke aaronfranke commented Jul 3, 2023

This PR adds support for importing OMI_physics_shape, which supersedes OMI_collider. This PR also updates OMI_physics_body with recent OMI changes.

The change from OMI_collider to OMI_physics_shape includes not only a rename, but also a change in responsibility for where the behavior of a shape is defined, and of how the data is formatted. This is based on a combination of consensus between OMI, Microsoft, and Khronos. In the future the goal is to end up with a Khronos standard, which will likely end up looking very similar to OMI_physics_shape but with a name change.

The ability to load OMI_collider is kept for backwards compatibility, and uses mostly the same code. Note that for exporting new files, it will only use OMI_physics_shape. As such, do not backport to Godot 4.2.x. If desired I could backport only the import changes so that files exported in 4.3 will be loadable in 4.2.

The change in OMI_physics_body is similarly designed to bring the OMI spec closer to the consensus of what Khronos wants. The body definition is now inside of the motion sub-JSON, and collider/trigger shapes are now located inside properties with those names in OMI_physics_body.

Production edit: closes godotengine/godot-roadmap#43

@aaronfranke aaronfranke added this to the 4.2 milestone Jul 3, 2023
@aaronfranke aaronfranke requested a review from a team as a code owner July 3, 2023 07:20
@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from 3cd470e to 864375f Compare July 3, 2023 07:50
@aaronfranke aaronfranke modified the milestones: 4.2, 4.x Jul 28, 2023
@aaronfranke aaronfranke marked this pull request as draft July 28, 2023 04:02
@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from 864375f to 18820dd Compare August 11, 2023 08:28
@aaronfranke aaronfranke changed the title GLTF: Add support for OMI_physics_shape (and keep OMI_collider) GLTF: Update OMI_physics_body, add OMI_physics_shape, keep OMI_collider Aug 11, 2023
@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch 2 times, most recently from 5a31a74 to 58bcd0c Compare September 10, 2023 01:41
@aaronfranke aaronfranke marked this pull request as ready for review September 10, 2023 20:59
@fire
Copy link
Member

fire commented Sep 21, 2023

Comment from @aaronfranke about how to test.

Can you send me links?

@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from 58bcd0c to 0fe463d Compare September 29, 2023 15:40
@aaronfranke aaronfranke modified the milestones: 4.x, 4.3 Oct 11, 2023
@fire fire requested a review from a team November 30, 2023 22:55
@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from 0fe463d to 73d5b15 Compare November 30, 2023 22:56
Copy link
Contributor

@lyuma lyuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. I think the main blocker for now is indirect child collision shapes

Comment on lines 145 to 147
physics_body->body_type = "character";
} else if (cast_to<AnimatableBody3D>(p_body_node)) {
physics_body->body_type = "kinematic";
physics_body->body_type = "animatable";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand where the string animatable comes from.

I think we should stick for now with what is in the schema and add this as extras or a separate GODOT_ extension if needed
https://github.com/omigroup/gltf-extensions/blob/main/extensions/2.0/OMI_physics_body/schema/node.OMI_physics_body.motion.schema.json#L12..L16

Copy link
Member Author

@aaronfranke aaronfranke Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I have explaines before, the in-memory type list is closer to Godot's types for flexibility, and is expanded on import / flattened on export. We only ever deal with the types in the OMI schema at the level of glTF files.

This allows reading "dynamic" from OMI data, then setting it in memory as "rigid", then other extensions can change this to "vehicle". Or in this case, reading "kinematic", in memory "animatable", and other extensions can change this to "character".

This mechanism exists precisely to allow a separate extension to exist in the future, with code to support new types not needing to fight with the rest of the physics code. It gives full access to Godot's physics node types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, since these types are not GLTF types, I think it could be helpful to use an enum rather than string constants to help follow how these types are used in various points in the code (easier to search for uses of enum values).

The only reason I would be ok with strings here is so that future extensions can add additional physics types. But since there is an ERR_FAIL "Error converting GLTFPhysicsBody to a node: Body type X is unknown", I think this is not the reason... so in that light, and given that body_type was already exposed, I would prefer that this be addressed in one or two ways:

  1. in gltf_physics_body.cpp, ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_type"), "set_body_type", "get_body_type"); should be modified to use an enum hint with the list of possible body types
  2. in gltf_physics_body.cpp, set_body_type and get_body_type convert from string to enum value and vice versa, such that internally it becomes an enum.

modules/gltf/extensions/physics/gltf_physics_body.cpp Outdated Show resolved Hide resolved
Comment on lines +424 to +428
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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick but I think to make compile-time StringName, you're supposed to use SNAME

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comment above the SNAME macro "If in doubt, do not use":

/*
 * The SNAME macro is used to speed up StringName creation, as it allows caching it after the first usage in a very efficient way.
 * It should NOT be used everywhere, but instead in places where high performance is required and the creation of a StringName
 * can be costly. Places where it should be used are:
 * - Control::get_theme_*(<name> and Window::get_theme_*(<name> functions.
 * - emit_signal(<name>,..) function
 * - call_deferred(<name>,..) function
 * - Comparisons to a StringName in overridden _set and _get methods.
 *
 * Use in places that can be called hundreds of times per frame (or more) is recommended, but this situation is very rare. If in doubt, do not use.
 */

@fire
Copy link
Member

fire commented Dec 18, 2023

@aaronfranke I don't know this is the best place to write this, but do you know if you or anyone else has implemented OMI_physics_body and OMI_physics_shape in Blender?

@aaronfranke
Copy link
Member Author

@fire No, I have not implemented in Blender yet and nobody else has either.

@aaronfranke
Copy link
Member Author

@lyuma "I think the main blocker for now is indirect child collision shapes"

It's not a blocker, because that is not necessary for this PR. In master, indirect shapes do not work. In this PR, indirect shapes do not work. The behavior is unchanged. This PR does not require that PR.

@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from 73d5b15 to 39890fa Compare December 19, 2023 13:59
Copy link
Contributor

@lyuma lyuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this can be merged without resolving the discussion on indirect collision shapes.

I think my concern still remains about having a bunch of hardcoded strings in GLTFPhysicsBody which do not match one-to-one with gltf strings. I would rather these use an enum.

Comment on lines 145 to 147
physics_body->body_type = "character";
} else if (cast_to<AnimatableBody3D>(p_body_node)) {
physics_body->body_type = "kinematic";
physics_body->body_type = "animatable";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, since these types are not GLTF types, I think it could be helpful to use an enum rather than string constants to help follow how these types are used in various points in the code (easier to search for uses of enum values).

The only reason I would be ok with strings here is so that future extensions can add additional physics types. But since there is an ERR_FAIL "Error converting GLTFPhysicsBody to a node: Body type X is unknown", I think this is not the reason... so in that light, and given that body_type was already exposed, I would prefer that this be addressed in one or two ways:

  1. in gltf_physics_body.cpp, ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_type"), "set_body_type", "get_body_type"); should be modified to use an enum hint with the list of possible body types
  2. in gltf_physics_body.cpp, set_body_type and get_body_type convert from string to enum value and vice versa, such that internally it becomes an enum.

@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch 2 times, most recently from 63b13fe to dd0db3a Compare December 24, 2023 06:31
@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from dd0db3a to c4f5c1d Compare December 24, 2023 06:38
@fire
Copy link
Member

fire commented Jan 19, 2024

3d Asset Pipeline Meeting:

  1. Lyuma approved the pr and we think it should be merged.

@aaronfranke aaronfranke force-pushed the gltf-omi-physics-shape branch from c4f5c1d to b00e5ce Compare January 19, 2024 20:35
@YuriSizov YuriSizov merged commit c50b802 into godotengine:master Jan 22, 2024
16 checks passed
@YuriSizov
Copy link
Contributor

Thanks!

@aaronfranke aaronfranke deleted the gltf-omi-physics-shape branch January 27, 2024 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

5 participants