Magnum::Trade::GltfSceneConverter class new in Git master

glTF converter plugin

Exports full scenes to either *.gltf files with an external *.bin buffer or to a self-contained *.glb. You can use GltfImporter to import scenes in this format.


This plugin depends on the Trade library and is built if WITH_GLTFSCENECONVERTER is enabled when building Magnum Plugins. To use as a dynamic plugin, load "GltfSceneConverter" via Corrade::PluginManager::Manager.

Additionally, if you're using Magnum as a CMake subproject, bundle the magnum-plugins repository and do the following:

add_subdirectory(magnum-plugins EXCLUDE_FROM_ALL)

# So the dynamically loaded plugin gets built implicitly
add_dependencies(your-app MagnumPlugins::GltfSceneConverter)

To use as a static plugin or as a dependency of another plugin with CMake, put FindMagnumPlugins.cmake into your modules/ directory, request the GltfSceneConverter component of the MagnumPlugins package and link to the MagnumPlugins::GltfSceneConverter target:

find_package(MagnumPlugins REQUIRED GltfSceneConverter)

# ...
target_link_libraries(your-app PRIVATE MagnumPlugins::GltfSceneConverter)

See Downloading and building plugins, Plugin usage with CMake, Loading and using plugins and File format support for more information.

Behavior and limitations

The plugin recognizes SceneConverterFlag::Quiet, which will cause all conversion warnings to be suppressed. Both SceneConverterFlag::Quiet and SceneConverterFlag::Verbose are also translated to corresponding ImageConverterFlags and propagated to image converter plugins the converter delegates to.

Mesh export

Image and texture export

  • Images are converted using a converter specified in the imageConverter configuration option, propagating flags set via setFlags(). An AbstractImageConverter plugin manager has to be registered using PluginManager::Manager::registerExternalManager() for image conversion to work. Options for a particular converter are meant to be set globally via PluginManager::Manager::metadata() through the image converter manager.
  • By default, images are saved as external files for a *.gltf output and embedded into the buffer for a *.glb output. This behavior can be overriden using the bundleImages configuration option on a per-image basis.
  • Core glTF supports only JPEG and PNG file formats, other formats are available via extensions:
    • WebP can be saved with the EXT_texture_webp extension
    • Basis-encoded KTX2 files can be saved with the KHR_texture_basisu extension by setting imageConverter=BasisKtxImageConverter The MSFT_texture_dds extension is not exported because there's currently no image converter capable of saving DDS files. Other formats (such as TGA, OpenEXR...) are not supported by the spec but GltfImporter supports them and they can be exported if the strict configuration option is disabled. Such images are then referenced directly without any extension.
  • If the experimentalKhrTextureKtx configuration option is enabled, generic KTX2 images can be saved with the proposed KHR_texture_ktx extension.
  • Image and texture names, if passed, are saved into the file. Additionally the buffer views referenced by embedded images will be annotated with image ID and name if the accessorNames configuration option is enabled.
  • The texture is required to only be added after all images it references
  • At the moment, there's no support for exporting multi-level images even though the KTX2 container is capable of storing these.

2D array texture export

If the experimentalKhrTextureKtx configuration option is enabled, the plugin supports also 3D images and 2D array textures using a proposed KHR_texture_ktx extension.

  • Only KTX2 images are supported for 3D, i.e. with either imageConverter=KtxImageConverter or imageConverter=BasisKtxImageConverter. They need to have ImageFlag3D::Array set.
  • Use a TextureType::Texture2DArray texture to reference the 3D images. Due to how the extension is designed, the resulting glTF then has the texture duplicated for each layer of the image; GltfImporter then undoes the duplication again on import.
  • Due to how the extension is designed, the presence of the texture referencing the 3D image is essential for properly recognizing the image as 3D on import. Without it, the image gets recognized as 2D and the import will subsequently fail due to the image file not actually being 2D.
  • A material referencing a 2D array texture implicitly uses the first (0) layer. Use the *TextureLayer attributes (such as MaterialAttribute::BaseColorTextureLayer for a MaterialAttribute::BaseColorTexture) to specify a layer. The layer index has to be smaller than the Z dimension of the image.

Material export

Scene export

  • Only 3D scenes are supported
  • Only objects with a SceneField::Parent entry are exported, all other objects are ignored with a warning. This also implies that object IDs are not preserved, as otherwise the glTF would contain a lot of empty unreferenced node objects.
  • The SceneField::Parent hierarchy is required to be acyclical
  • To satisfy glTF requirements, if both SceneField::Transformation and at least one of Translation, Rotation and Scaling is present, only the TRS component(s) are saved into the file and not the matrix, assuming the transformation matrix is equivalent to them
  • Object and scene names, if passed, are saved into the file
  • The scene is required to only be added after all meshes and materials it references
  • Multiple SceneField::Mesh and SceneField::MeshMaterial entries per object are turned into multi-mesh primitives. Other duplicate builtin fields are ignored with a warning.
  • Custom SceneFieldType::Float, UnsignedInt, Int, Bit and string fields are exported if a name is set for them via setSceneFieldName(). Custom fields of other types and without a name assigned are ignored with a warning. Fields that have SceneFieldFlag::MultiEntry set are exported as arrays, otherwise duplicate entries for the same object are ignored with a warning.
  • At the moment, only SceneField::Parent, Transformation, Translation, Rotation, Scaling, Mesh and MeshMaterial is exported, other builtin fields are ignored with a warning
  • At the moment, only a single scene can be exported. Default scene index is however only written if setDefaultScene() is called.

Plugin-specific config

It's possible to tune various output options through configuration(). See below for all options and their default values:

# Copyright and generator name, written into the asset object. If empty, no
# value is written. The {0} placeholder, if present, will be replaced with
# Corrade, Magnum and Magnum Plugins version info including Git commit hashes
# if the plugin is built in Release from a non-sparse Git clone.
generator=Magnum GltfSceneConverter {0}

# Add one or more extensionUsed and/or extensionRequired values to populate
# the extension usage and requirement arrays.

# Whether to write a *.gltf or a *.glb file. If empty, detected automatically
# based on filename extension, conversion to data defaults to a binary file.
# If a text file is selected for conversion to data, converting anything that
# involves binary buffers will currently fail.

# Name all buffer views and accessors to see what they belong to. Useful for
# debugging purposes. The option can be also enabled just for a particular
# add() operation and then disabled again to reduce the impact on file sizes.

# Allow only strictly valid glTF files. Disallows:
# - Meshes with zero vertices, zero indices or zero attributes
# - Meshes with 32-bit integer attributes
# - Image formats that don't have a corresponding glTF extension
# Magnum's GltfImporter will be able to open files with this option unset
# (unless it's own strict option is enabled), but other libraries may fail.
# The option can be also disabled just for a particular add() operation and
# then enabled again to not loosen up validation for everything at once.

# Perform Y-flip for texture coordinates in a material texture transform. By
# default texture coordinates are Y-flipped directly in the mesh data to
# avoid the need to supply texture transformation matrix to a shader,
# enabling this will cause all texture coordinate data to be unchanged and
# instead all materials will have a Y-flipping texture transformation
# present. Note that this flag has to be enabled before beginning a file,
# changing it during conversion will have undefined behavior.

# The non-standard MeshAttribute::ObjectId is by default recognized under
# this name. Change if you want to export it under a different identifier.

# Implicitly, only material attributes that differ from glTF material
# defaults are written. Enable to unconditionally save all attributes present
# in given MaterialData. Attributes that are not present in given
# MaterialData are saved only if the Magnum default differs from the glTF
# default and this option doesn't affect them.

# Whether to bundle images in buffers. If empty, images are bundled for *.glb
# files and saved externally for *.gltf files. Can be set differently for
# each add() operation.

# Experimental KHR_texture_ktx support. The extension is not stabilized yet,
# thus the implementation may not reflect latest changes to the proposal.

# Image converter to use. Can be set differently for each add() operation in
# order to export different images in different formats. Formats that don't
# have any corresponding glTF image extension are allowed only with the
# strict option unset.

# Configuration options to propagate to the image converter. Obsolete, prefer
# to set the converter options directly through the plugin manager.

See Editing plugin-specific configuration for more information and an example showing how to edit the configuration values.

Base classes

class AbstractSceneConverter new in 2020.06
Base for scene converter plugins.

Constructors, destructors, conversion operators

GltfSceneConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin) explicit
Plugin manager constructor.