glTF Files — Panda3D Manual (2024)

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 theKhronos Group, with contributors across the industry. It is widely supported asan 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 texteditor, 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 topack the textual JSON tree together with any external binary data into the samefile efficiently.

At the time of writing, there are two versions of the glTF standard, version 1.0and version 2.0, with substantial differences between them. It is recommended touse version 2.0, which is much more prevalent than 1.0.

A particular advantage of this format is its support for PBR (physically-basedrendering) 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-onsuch as panda3d-simplepbr torender these materials correctly. glTF is also the format of choice whenexporting models from Blender 2.80 and above, as explainedhere.

Installing the Plug-In

There are two ways to load glTF files. By default, Panda3D will load glTF filesvia the Assimp plug-in, but the quality of its converteris not very good. Instead, there is a high quality third-party plug-in for thispurpose 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 theloader.loadModel() calland it will be loaded using this plug-in. Of course, Panda’s model cacheensures that the conversion step is only run the first time a particularmodel is loaded, or whenever it is modified on disk.

Previewing glTF Models

Since the pview utility is written in C++, it cannot takeadvantage 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 youwant.

You should instead use the gltf-viewer utility that is included withpanda3d-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 materialsthat 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 modelis required to be in a binary form. This concerns such things as vertex andanimation data, which would use considerably more space if it were written outa 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 bythe main .gltf file. This is the most typical approach, and will work formost 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 packedtogether into a .glb file. This is a more efficient manner of embeddingbinary data, but the file is no longer human-readable.

The choice of these options is usually available in the exporter plug-in forthe modelling program. Which option you choose is up to you; all three aresupported by Panda3D.

Extensions

The glTF format is defined as an extensible format. A glTF asset can opt-in toa number of externally defined extensions that add additional functionality notprovided 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 tobe present for normal mapping. Instead, binormal vectors are intended to bederived from the cross product of the normal and tangent vectors.

The cross product is not sufficient to indicate the direction of the binormalvector, so the tangent is stored as a 4-component value, with the w componentindicating the sign of the binormal vector. This component always containseither the value 1 or -1.

With this information, the binormal vector can be reconstructed as follows inthe vertex shader:

binormal = cross(p3d_Normal, p3d_Tangent.xyz) * p3d_Tangent.w

Distributing glTF Models

When building your application, the plug-insresponsible for loading glTF models are not distributed along by default.It is much better to convert the model to the optimized .bam format for thispurpose. As of Panda3D 1.10.13, this can be done automatically, by adding theextensions to the bam_model_extensions list:

options = { 'build_apps': { ... 'include_patterns': [ # Make sure the gltf/glb file is being found '**/*.gltf', '**/*.glb', '**/*.jpg', ... ], # Models with these extensions are converted to .bam automatically 'bam_model_extensions': ['.gltf', '.glb', '.egg'], ...

External Links

For more information on panda3d-gltf, or to report issues, see the project’sGitHub page.

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:

https://github.com/KhronosGroup/glTF-Sample-Models

glTF Files — Panda3D Manual (2024)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6792

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.