glTF Files¶
The glTF format is the industry standard file format for delivering 3D assets (models and scenes) to 3D engines. It is an open standard maintained by the Khronos Group, with contributors across the industry. It is widely supported as an export format by 3D modelling programs and an import format by game engines.
The format is based around JSON, a common text-based data interchange format. Like .egg, a file with the .gltf extension can be inspected using a text editor, or it can be programmatically manipulated by any JSON parsing library. There is also a binary variant of the format, .glb, which is used if you wish to pack the textual JSON tree together with any external binary data into the same file efficiently.
At the time of writing, there are two versions of the glTF standard, version 1.0 and version 2.0, with substantial differences between them. It is recommended to use version 2.0, which is much more prevalent than 1.0.
A particular advantage of this format is its support for PBR (physically-based
rendering) materials, which are better supported with glTF than with Egg. To
render these materials correctly, use the engine’s PBR shader, set up by
acron.pbr.init() (see Materials and PBR), or a shader of your
own. glTF is also the format of choice when
exporting models from Blender 2.80 and above, as explained
here.
The glTF Loader¶
Acron comes with a glTF loader, acron.gltf; there is nothing to install.
Without it, glTF files would go through the
Assimp plug-in, whose converter is not nearly as good.
You can simply pass any filename with a .gltf or .glb extension to the
loader.loadModel() call
and it will be loaded with acron.gltf. Acron’s model cache ensures that
the conversion step is only run the first time a particular model is
loaded, or whenever it is modified on disk. When a game is packaged with
build_apps, its glTF models are converted to .bam (see
Building Python Games).
The loader is configured with PRC variables, such as
gltf-collision-shapes bullet to build Bullet shapes instead of
collision solids, gltf-skip-animations and gltf-no-srgb.
Converting to Bam¶
python -m acron.gltf.cli source.gltf output.bam converts a model ahead of
time; -h lists its options.
Previewing glTF Models¶
Since the pview utility is written in C++, it cannot use the Python glTF loader, and loads glTF files via the Assimp plug-in instead, which is usually not what you want. Use the glTF viewer that comes with the loader instead; it renders the model’s PBR materials as well:
python -m acron.gltf.viewer model.glb
Embedding Binary Data¶
While the glTF format is a text-based format, some data that is part of a model is required to be in a binary form. This concerns such things as vertex and animation data, which would use considerably more space if it were written out a text form. The glTF standard provides three ways to store this binary data:
- Separate .bin file
The binary data can be stored in a separate .bin file that is referred to by the main .gltf file. This is the most typical approach, and will work for most purposes, but having a separate .bin file can be inconvenient for some.
- Embedded
The binary data is encoded as base64 and embedded in the .gltf file. This method keeps the JSON structure of the file intact, at the cost of a (roughly) 33 % increase in file size due to the inefficiency of base64.
- Binary glTF (.glb)
Instead of a .gltf file, both the JSON and the binary data are packed together into a .glb file. This is a more efficient manner of embedding binary data, but the file is no longer human-readable.
The choice of these options is usually available in the exporter plug-in for the modelling program. Which option you choose is up to you; all three are supported by Acron.
Extensions¶
The glTF format is defined as an extensible format. A glTF asset can opt-in to a number of externally defined extensions that add additional functionality not provided by the base standard.
Both Acron’s glTF loader and the Assimp plug-in support the following extensions, among others:
KHR_lights_punctual - adds support for light sources
KHR_texture_transform - adds support for texture transformations
KHR_materials_ior - makes it possible to specify the index of refraction of a material
Tangent and Binormal Vectors¶
glTF files do not contain binormal vectors, even if normal mapping is used. This may be an issue when using custom shaders that expect a binormal vector to be present for normal mapping. Instead, binormal vectors are intended to be derived from the cross product of the normal and tangent vectors.
The cross product is not sufficient to indicate the direction of the binormal vector, so the tangent is stored as a 4-component value, with the w component indicating the sign of the binormal vector. This component always contains either the value 1 or -1.
With this information, the binormal vector can be reconstructed as follows in the vertex shader:
binormal = cross(acron_Normal, acron_Tangent.xyz) * acron_Tangent.w
Distributing glTF Models¶
When building your application, glTF models with the
.gltf or .glb models are converted to .bam by default, assuming that they are
referenced in the include_patterns list in setup.py. The .bam extension is
appended after the existing extension, like model.gltf.bam. This will
allow the model loader to automatically look for model.gltf.bam when
trying to load model.gltf, so that you do not need to change your calls
to loader.loadModel().
External Links¶
The full text of the glTF specification can be found on the Khronos website:
https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
Sample models in the glTF format are available from this GitHub repository: