Unity3D library for importing and exporting glTF 2.0 assets.
Editor | Runtime | |
---|---|---|
Import | ✅ | ✅ |
Export | ✅ | ✅ |
UnityGLTF doesn't have any native dependencies (pure C#) and thus works on all platforms that Unity supports. This includes WebGL import and export.
The library is designed to be easy to extend with additional extensions to the glTF specification. Both import and export allow attaching custom plugins and callbacks and can be heavily modified to fit into specific pipelines. Many glTF extensions are supported.
You can install this package from the Needle Package Registry with a one-click installer:
You can also install this package from git, compatible with UPM (Unity Package Manager).
Window > Package Manager
https://github.com/KhronosGroup/UnityGLTF.git
Note: If you want to target a specific version, append
#release/<some-tag>
or a specific commit to the URL above. Example:https://github.com/KhronosGroup/UnityGLTF.git#release/2.9.0-rc
.
Please use Long-Term Support versions of Unity (2021.3+, 2022.3+, 2023.3+).
Recommended:
Tested:
Legacy:
These configurations have been working in the past. They will not be updated with material extensions or new features. Also, issues in these configurations will most likely not be addressed if they're not also happening on later versions.
HDRP:
Note: Issues on non-LTS Unity versions (not on 2020.3, 2021.3, 2022.3, ...) will most likely not be addressed. Please use LTS (Long-Term Support) versions where possible.
A separate glTF implementation for Unity, glTFast, is available from the Unity Registry.
glTFast being supported by Unity means, in a nutshell, that it sticks to standards pretty strictly and can't easily ship non-ratified extensions or experimental features that work for the majority, but not all, of users.
KHR_animation_pointer
, KHR_audio
, and KHR_materials_variants
.glTFast and UnityGLTF can coexist in the same project; you can for example use glTFast for import and UnityGLTF for export.
For imported assets, you can choose which importer to use with a dropdown.
glTFast import has precedence if both are in the same project. See also Default Importer Selection.
The lists below are non-conclusive and in no particular order. Note that there are gaps where features could easily be supported for im- and export but currently aren't. PRs welcome!
com.unity.cloud.draco
)com.unity.cloud.ktx
)com.unity.meshopt.decompress
)To leverage the extended material model of glTF in Unity, use the UnityGLTF/PBRGraph
material.
It allows the use of various glTF material extensions for import, export, and inside Unity.
This includes features that URP is missing, such as transmission, volume, and per-texture UV control.
UnityGLTF contains helpers to make converting to UnityGLTF/PBRGraph easy.
When you switch a material from any shader to PBRGraph, an automatic conversion can run to bring the properties over.
Some shaders already come with automatic conversions, please see the table below.
When a shader doesn't have a converter yet, UnityGLTF will ask if you want to create a Conversion Script. These scripts contain all properties of the source shader and the target shader, but no specified mapping yet (as that depends on the intent of the shader author).
After the conversion script has been created, you can edit it to correctly map from the source shader's properties to PBRGraph properties.
When you switch such a shader to PBRGraph the next time, your conversion script will run and automatically translate the materials in the specified way.
Note: Currently, conversion scripts aren't used automatically on glTF export. Convert materials at edit time for best results.
If you want to design for glTF export, it's recommended to use Unity 2021.3+ with URP and the UnityGLTF/PBRGraph material. It comes with support for modern material extensions like refraction and iridescence, and allows for perfect roundtrips. Great for building glTF pipelines in and out of Unity.
Render Pipeline | Shader | Notes | Source |
---|---|---|---|
URP on 2020.3+ Built-In on 2021.3+ |
UnityGLTF/PBRGraph ☝️ Use this if you're not sure |
Perfect roundtrip, Material Extensions | UnityGLTF |
UnityGLTF/UnlitGraph | Perfect roundtrip | UnityGLTF | |
ShaderGraphs/glTF-pbrMetallicRoughness | glTFast | ||
ShaderGraphs/glTF-unlit | glTFast | ||
URP | URP/Lit | Unity | |
URP/Unlit | Unity | ||
Built-In | Standard | Unity | |
GLTF/PbrMetallicRoughness | UnityGLTF (legacy) | ||
GLTF/Unlit | UnityGLTF (legacy) | ||
glTF/PbrMetallicRoughness | glTFast (legacy) | ||
glTF/Unlit | glTFast (legacy) | ||
HDRP (limited support) | HDRP/Lit | Unity | |
HDRP/Unlit | Unity |
Transmission and Volume allow rendering materials like glass, that are fully transparent but still show reflections, as well as volume attenuation (e.g. colored jelly) and rough refraction (e.g. brushed glass). To use these material features, you need to do some setup on the material and your render pipeline.
To see transmissive and volume materials correctly in Unity,
To see transmissive and volume materials correctly in Unity,
HDRP has its own rough refraction support. There's currently no automatic import / export support to convert to that. Use glTFast if you need this.
Note: Fully metallic materials are never transparent. See the KHR_materials_transmission spec for more info.
To export an object from a scene or your project,
Assets > UnityGLTF > Export selected as GLB
/ Export selected as glTF
to exportTip: You can set shortcuts for quick export in Unity's Shortcut Manager. For example, Ctrl + Space for GLB export and Ctrl + Shift + Space for glTF export allow for very fast iteration.
The various glTF viewers in existence have varying feature sets. Only a select few have full coverage of the glTF spec, most only support a subset.
Notable features with limited support:
textureCoord
per texture.textureRotation
. Many viewers simply ignore it.To view your glTF files, here's a number of tools you can use:
Name | Notes |
---|---|
Khronos glTF Sample Viewer | Full support for ratified extensions |
gltf.report | Inspect file size, meshes, textures |
model-viewer | Support for KHR_materials_variants with custom code |
Gestaltor | Full glTF Spec Compliance Support for KHR_animation_pointer Support for KHR_audio Support for KHR_materials_variants |
Needle Viewer | Support for KHR_animation_pointer Inspect hierarchy, textures, cameras, lights, warnings |
Babylon.js Sandbox | Support for KHR_animation_pointer |
UnityGLTF (this project!) |
Simply drop the exported glb file back into Unity. |
glTFast | Add the glTFast package to your project. You can switch the used importer on glTF files between glTFast and UnityGLTF. |
To further process files after exporting them with UnityGLTF, you can use:
Name | Notes |
---|---|
gltf-transform | Compress meshes with draco or meshopt Compress textures to ktx2 Optimize Files Convert between .gltf and .glb |
Blender | Import/export glTF files with good feature coverage |
Animations can be exported both in the Editor and at runtime. Editor export works with Animator (multiple clips), Animation (multiple clips), and Timeline (GLTFRecorderTrack).
You can export entire Animators and their clips as glTF files with multiple animations.
Animation clips will be named after each Motion State in the Animator Controller.
The "speed" property of each Motion will be baked into the exported clip. Ensure the speed is 1 when you want to export unchanged.
Any number of Animators in a hierarchy is supported, as is any number of clips in those.
Both Humanoid and Generic animations will be exported. Humanoid animations are baked onto the target rig at export time.
Note: Animator export only works in the Editor. For runtime export, use the GLTFRecorder capabilities or the Timeline Recorder.
For creating and/or recording animations at runtime, you can use the GLTFRecorder API. It allows to capture the state of entire hierarchies and complex animations and export them directly as glTF file,
optionally with KHR_animation_pointer support for complex material and property animations.
See GLTFRecorderComponent
for an example implementation.
Timelines or sections of them can be recorded with a GltfRecorderTrack
and one or more GltfRecorderClips
.
Timeline recording uses the GLTFRecorder API under the hood.
Note: Animation Component export only works in the Editor. For runtime export, use the GLTFRecorder capabilities.
Animation components and their legacy clips can also be exported.
UnityGLTF supports importing and exporting animations with the KHR_animation_pointer extension. The core glTF spec only allows animation node transforms and blend shape weights, while this extension allows animating arbitrary properties in the glTF file – including material and object properties, even in custom extensions and script components.
Exporting with KHR_animation_pointer can be turned on in Project Settings > UnityGLTF
by enabling the KHR_animation_pointer plugin.
Note: The exported files can be viewed with Gestaltor, Babylon Sandbox, and Needle Engine, but currently not with three.js / model-viewer. See https://github.com/mrdoob/three.js/pull/24108.
Morph Targets / Blend Shapes / Shape Keys are supported, including animations.
To create smaller files for complex blend shape animations (e.g. faces with dozens of shapes), export with the "Sparse Accessors" setting enabled.
For importing .gltf
or .glb
files in the editor, place them in your project as usual (Assets or Packages).
Make sure to bring .bin/textures along for .gltf
files with the correct relative paths;.glb
is usually self-contained.
When moving .gltf
files inside Unity, make sure to move their .bin/texture files as well, to not break the path references between them.
UnityGLTF will register itself as the default importer for the .gltf
and .glb
extensions.
If the glTFast package is also present in a project, glTFast gets precedence and UnityGLTF is available as Importer Override, which can be selected from a dropdown on each glTF asset.
UnityGLTF uses Unity's ScriptedImporter
system. For any given file format (file extension) there has to be one default importer and there can be additional, alternative importers.
You can make UnityGLTF the default importer and de-prioritize glTFast by adding the following settings to your project's Scripting Defines
:
GLTFAST_FORCE_DEFAULT_IMPORTER_OFF
UNITYGLTF_FORCE_DEFAULT_IMPORTER_ON
Care has been taken to align glTFast's and UnityGLTF's importers, so that in most cases you can switch between them without breaking prefab references. That being said, switching between importers can change material references, mesh references etc., so some manual adjustments may be needed. You may have to adjust the root stripping settings of each importer (glTFast and UnityGLTF do that slightly differently).
Animations can be imported both in the Editor and at runtime.
On the importer, you can choose between "Legacy", "Mecanim" or "Humanoid" clips.
At runtime, if you're importing "Mecanim" clips, you need to make sure to add them to a playable graph via script (e.g. Animator Controller or Timeline) to play them back.
UnityGLTF has import and export plugins. These have callbacks for modifying node structures, extension data, materials and more as part of the regular export and import process. They are used both in the Editor and at runtime.
You can make your own plugins and enable them in the Project Settings > UnityGLTF
menu.
Plugins are ScriptableObjects that can have settings; they're serialized as part of the GLTFSettings asset.
Plugins create concrete instances of import/export handlers.
To create a plugin, follow these steps:
GLTFImportPlugin
or GLTFExportPlugin
. This is the ScriptableObject that contains plugin settings.GLTFImportPluginContext
or GLTFExportPluginContext
. This class has the actual callbacks.CreateInstance
in your plugin to return a new instance of your plugin context.If your plugin reads/writes custom extension data, you need to also implement GLTF.Schema.IExtension
for serialization and deserialization.
🏗️ Under construction. You can take a look at
MaterialVariantsPlugin.cs
for an example.
Note: As of 20240129 the default branch of this repository has been renamed from
master
tomain
.
UnityGLTF is an open-source project. Well-tested PRs are welcome.
It is currently maintained by