How Unity Converts glTF to Prefabs and Meshes
Unity converts glTF (GL Transmission Format) and GLB files into
native engine assets through an import pipeline that parses the source
data, reconstructs 3D geometry into UnityEngine.Mesh
objects, translates PBR materials, and packages the resulting hierarchy
into a standard Unity Prefab. Because Unity does not support glTF
natively in its base install, this conversion relies on specialized
importer packages—such as Unity's official glTFast package or the
open-source UnityGLTF—leveraging the engine's
ScriptedImporter API to translate raw file buffers into
editor-ready assets seamlessly during import time or runtime.
The Role of ScriptedImporter
The conversion process begins with Unity's
ScriptedImporter framework. When a .gltf
(JSON-based) or .glb (binary) file is placed into the Unity
Project folder, the importer registers the file extension and triggers
an import task. The importer reads the underlying data structures,
isolates the components (geometry, textures, materials, and animations),
and declares what native Unity assets will be generated as main and
sub-assets inside the project database.
Parsing Structure and Binary Buffers
glTF assets define their scenes using a strict JSON schema coupled with binary blobs (buffers). Conversion happens in the following sequence:
- Reading Accessors and Buffer Views: The importer evaluates the JSON file to locate accessor objects, which describe how to interpret raw binary data.
- De-interleaving Vertex Data: The importer reads array buffers to extract vertex attributes including positions, vertex colors, UV coordinates, normals, and tangents.
- Index Parsing: Index buffers are read to assemble the polygon topology (typically triangle lists) required for rendering.
Converting Geometry to
UnityEngine.Mesh
Once raw vertex and index data are isolated, the importer constructs
one or more UnityEngine.Mesh instances:
- Vertex Allocation: Data is packed into native
memory buffers and assigned to the mesh via
Mesh.SetVertices(),Mesh.SetNormals(),Mesh.SetTangents(), andMesh.SetUVs(). For high-performance conversion, packages like glTFast write directly into Unity's vertex buffers usingMesh.SetVertexBufferDataand the C# Job System. - SubMeshes and Topologies: Each glTF primitive
corresponds to a sub-mesh in Unity. The importer maps primitive draw
modes to Unity's
MeshTopology.Triangles. - Skinning Data: If the model includes a skinned
mesh, the importer reads joint indices and weights, mapping them to
Unity's
BoneWeightstructs or native bone weight buffers, alongsidebindposes(inverse bind matrices).
Translating Materials and Textures
glTF utilizes a standard Metallic-Roughness or Specular-Glossiness Physically Based Rendering (PBR) workflow. Unity maps these definitions to corresponding shader properties:
- Texture Decoding: Images embedded or referenced by
the glTF file are decoded into
Texture2Dinstances. Normal maps and occlusion/roughness/metallic (ORM) packed textures are remapped to match the channel layouts expected by Unity's Standard, Universal Render Pipeline (URP), or High Definition Render Pipeline (HDRP) shaders. - Material Mapping: A native
UnityEngine.Materialis created for each glTF material definition. The importer assigns base color factors, metallic and roughness values, emission, and normal scale factors directly to the target shader's uniform parameters.
Hierarchy Construction and Prefab Generation
After generating meshes and materials, the importer constructs the object graph:
- Transform Nodes: glTF scenes are organized as
directed acyclic graphs of nodes. The importer iterates through these
nodes, instantiating a
GameObjectfor each and setting its local position, rotation, and scale. - Component Assignment: Nodes containing geometry are
assigned a
MeshFilter(referencing the newly createdMesh) and aMeshRenderer(referencing the generatedMaterial). Skinned nodes receive aSkinnedMeshRendererwith associated transform references for the bone rig. - Animation Clips: glTF animation channels
(translation, rotation, scale, and morph weights) are converted into
AnimationClipcurves mapped to the instantiated node paths. - Serialization: The entire root
GameObjecthierarchy, along with its dependent child meshes, materials, and animation clips, is passed to theAssetImportContext. Unity serializes this root object as a standard Prefab asset, making it directly usable in scenes or via script instantiation.