godot icon indicating copy to clipboard operation
godot copied to clipboard

GLTF: Implement `KHR_animation_pointer` for animating custom properties

Open aaronfranke opened this issue 1 year ago • 4 comments

This PR implements the glTF Object Model and the KHR_animation_pointer extension. The object model allows accessing properties of the glTF JSON using JSON pointers, mapped to Godot properties. This can be used for things like engine-agnostic scripting systems, where simple behavior graphs can be defined inside of glTF files to manipulate properties.

KHR_animation_pointer is the first extension prominently making use of the object model, to allow for animations to control properties. With vanilla glTF animations, you can animate position, rotation, scale, and weights. However, animation pointer allows animating much more. For example, you could animate the color of a light, the FOV of a camera, the albedo color of a material, the UV offset of a material, and more.

https://github.com/godotengine/godot/assets/1646875/0d81e50d-4937-44c5-9ac2-c9a7498d69dc

Test project: gltf_khr_animation_pointer.zip

These test files were created by the Khronos group, not me. The source is here: https://github.com/KhronosGroup/glTF-Sample-Assets/pull/106

Both the object model and the KHR_animation_pointer extension are already final and ratified by the Khronos group. This must be core to Godot Engine because it requires a lot of low-level hooks to do it right, and other extensions need to be able to build on these features. See also https://github.com/godotengine/godot-proposals/issues/10164

For ease of reviewability, I have split this PR into 8 commits:

  • GLTF: Move the component type enum into GLTFAccessor
    • Also includes exposing and documenting the component types.
  • GLTF: Add more accessor component types
    • Not actually used in any code path when exporting, but allows importing more data types. This will become useful in the future for extensions like KHR_interactivity that can utilize data encoded with these types, or if new extensions use these data types for their buffers.
  • GLTF: Clean up animation code to make way for KHR_animation_pointer
    • This commit will be the most annoying to review. It overhauls the GLTF animation import and export code, so it's hard to get a detailed look at the changes from the diff.
    • Some specifics: Use double for animation times consistently (in the old code it was converting between double and float back and forth several times), rename Track to NodeTrack (to make way for pointer tracks), add static methods to GLTFAnimation to convert between Godot and GLTF interpolation types, move bone and Skeleton3D bone handling code to its own function _node_and_or_bone_to_gltf_node_index, overhaul and simplify _convert_animation.
  • GLTF: Prerequisite cleanups before KHR_animation_pointer
    • Add get_scene_node_path and has_additional_data to GLTFNode, fix code order in GLTFDocumentExtension, remove center of mass ignore warning in physics (it's supported now), rename d to mesh_dict in mesh import code.
  • GLTF: Implement the glTF Object Model to support JSON pointer properties
    • This is the exciting/interesting one, and is the one that exposes APIs for using the object model.
    • Includes support for handling object model pointers present in the core glTF spec and in a few extensions such as lights, texture transform, and emissive strength.
  • GLTF: Implement the glTF Object Model in physics extension
    • Allows for animating things like the mass of a physics body or the radius of a sphere collider.
    • I had to rework a bit of the export code to allow for shape indices to be known earlier in the process.
  • GLTF: Add functions to encode and decode Variants to/from accessors
    • Required to import pointer tracks as Variant arrays for use with Godot's animation system.
  • GLTF: Implement KHR_animation_pointer for animating custom properties
    • Bringing all of it together!

If desired, I can move some of these into a separate PR.

aaronfranke avatar Jul 10 '24 10:07 aaronfranke

Wow, I didn’t even know that GLTF could store such data, I was surprised by the waterfall, how does it work? UVs are recorded and animated?

JekSun97 avatar Jul 10 '24 14:07 JekSun97

Could you add this link when talking about JSON pointers in your intro? I had to search about the concept. https://datatracker.ietf.org/doc/html/rfc6901

adamscott avatar Jul 10 '24 14:07 adamscott

@JekSun97 The UV offset values can be stored in glTF in a KHR_texture_transform. This is then mapped to Godot's BaseMaterial3D uv1_offset. This PR adds support for importing and exporting animations that animate this property, but it's already possible to store a frozen non-animated version of this value before this PR.

aaronfranke avatar Jul 10 '24 16:07 aaronfranke

https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_animation_pointer is a ratified official extension so as soon as the Godot Engine portion is done, we can merge. Some of the other extensions are still in draft stage.

fire avatar Sep 17 '24 18:09 fire

It says in the queue that this is awaiting final touches, but that might be outdated. Is this ready for merge in your eyes @aaronfranke?

Repiteo avatar Oct 30 '24 00:10 Repiteo

@Repiteo There are some minor things, but they can all be resolved after merge. Every remaining problem pointed out above is actually a pre-existing problem, not something introduced by this PR.

I'd personally like to avoid making this PR too much larger, and it would be nice to avoid conflicting with myself too much, so merging this before fixing the other things makes sense. I did leave a note that I suggest merging https://github.com/godotengine/godot/pull/96748 first so that we can make UV map animations work correctly. I'll either need to update this PR or that PR.

aaronfranke avatar Oct 30 '24 06:10 aaronfranke

This one has more thorough review than #96748 & a handful of approvals to boot, so that PR can be expanded to handle the minor problems @lyuma mentioned after this is merged

Repiteo avatar Nov 05 '24 01:11 Repiteo

Thanks!

Repiteo avatar Nov 05 '24 03:11 Repiteo