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, although at the moment you will need a custom shader or a third-party add-on such as panda3d-simplepbr to render these materials correctly. glTF is also the format of choice when exporting models from Blender 2.80 and above, as explained here.

Installing the Plug-In

There are two ways to load glTF files. By default, Panda3D will load glTF files via the Assimp plug-in, but the quality of its converter is not very good. Instead, there is a high quality third-party plug-in for this purpose called panda3d-gltf. It can be installed by typing the following pip command:

python -m pip install -U panda3d-gltf

After installing this plug-in, no extra steps are needed. You can simply pass any filename with a .gltf extension to the loader.loadModel() call and it will be loaded using this plug-in. Of course, Panda’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.

Previewing glTF Models

Since the pview utility is written in C++, it cannot take advantage of the panda3d-gltf plug-in. If you try to load a glTF model in pview, it will always be loaded via the Assimp plug-in, which is usually not what you want.

You should instead use the gltf-viewer utility that is included with panda3d-gltf, since it ensures that the panda3d-gltf plug-in is being used. It also includes a default PBR shader that is able to render any PBR materials that are specified by the glTF asset.

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 Panda3D.

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 the panda3d-gltf and Assimp plug-ins support the following extensions, among others:

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(p3d_Normal, p3d_Tangent.xyz) * p3d_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().