|
| 1 | +--- |
| 2 | +title: "Userspace glTF Extension Handling" |
| 3 | +authors: ["@christopherbiscardi"] |
| 4 | +pull_requests: [22106] |
| 5 | +--- |
| 6 | + |
| 7 | +Prior to 0.18, the code to handle extensions like `KHR_lights_punctual` was hardcoded into Bevy's glTF loader. |
| 8 | +In 0.18, users may implement the `GltfExtensionHandler` trait to do stateful processing of glTF data as it loads. |
| 9 | +Processing _extension_ data is only half the story here because to process extension data you also have to be able to process the non-extension data like meshes, materials, animations, and more. |
| 10 | + |
| 11 | +Extension handlers can be written for wide variety of use cases, including: |
| 12 | + |
| 13 | +- Insert Bevy Component data on entities |
| 14 | +- Convert all `Mesh3d` components to `Mesh2d` |
| 15 | +- Build `AnimationGraph`s and insert them on animation roots |
| 16 | +- Replace `StandardMaterial` with custom materials |
| 17 | +- Insert lightmaps |
| 18 | + |
| 19 | +## Extras vs Extensions |
| 20 | + |
| 21 | +glTF has two mechanisms for extending glTF files with additional user data: Extras and Extensions. |
| 22 | + |
| 23 | +**Extras** are meant to be arbitrary application-specific data, often authored by users directly in tools like Blender's custom properties. |
| 24 | +Extras are historically well supported by Bevy; If you add a custom property in Blender that data will end up in one of the `GltfExtras` components on the relevant `Entity`. |
| 25 | + |
| 26 | +**Extensions** are meant for data that can be shared across applications. |
| 27 | +They are more flexible, allowing for new data in more places inside a glTF file, and more powerful as a result. |
| 28 | +Extensions can add new object types, such as `lights` from the `KHR_lights_punctual` extension, as well as arbitrary buffers, data that is at the root of the glTF file, and more. |
| 29 | + |
| 30 | +More examples of extensions can be found in the [KhronosGroup git repo](https://github.com/KhronosGroup/glTF/blob/7bbd90978cad06389eee3a36882c5ef2f2039faf/extensions/README.md) |
| 31 | + |
| 32 | +## Case Study |
| 33 | + |
| 34 | +Extensions typically require an application that is _producing_ the data as well as _consuming_ the data. |
| 35 | + |
| 36 | +For example: [Skein](https://github.com/rust-adventure/skein) defines a glTF extension that allows adding Bevy Components to glTF objects. |
| 37 | +This is most commonly produced by Blender and consumed by Skein's `GltfExtensionHandler` in Bevy. |
| 38 | +These components are then inserted on entities in a scene at the same time built-in components like `Transform` and `Mesh3d` are. |
| 39 | + |
| 40 | +Using glTF Extensions for this data means that other level editors like Trenchbroom can also write the same format to glTF files. |
| 41 | +Any third party software that writes component data into a glTF file can use Skein's `GltfExtensionHandler`, resulting in components being "ready-to-go" when spawning `Scene`s. |
| 42 | + |
| 43 | +## New Examples |
| 44 | + |
| 45 | +Two new examples show off use cases: |
| 46 | + |
| 47 | +- The first builds an `AnimationGraph` and inserts it onto the animation root in a Scene, which means it is now accessible to play animations using the `AnimationPlayer` on the same `Entity` later when that Scene is spawned. |
| 48 | +- The second uses a `GltfExtensionHandler` to switch the 3d Mesh and Material components for their 2d counterparts. This is useful if you're using software like Blender to build 2d worlds. |
| 49 | + |
| 50 | +```shell |
| 51 | +cargo run --example gltf_extension_animation_graph |
| 52 | +cargo run --example gltf_extension_mesh_2d |
| 53 | +``` |
0 commit comments