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

Cgltf importer plugin.

Imports glTF and binary glTF using the cgltf library.

This plugin provides the GltfImporter plugin.

Usage

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

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

set(WITH_ANYIMAGEIMPORTER ON CACHE BOOL "" FORCE)
add_subdirectory(magnum EXCLUDE_FROM_ALL)

set(WITH_CGLTFIMPORTER ON CACHE BOOL "" FORCE)
add_subdirectory(magnum-plugins EXCLUDE_FROM_ALL)

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

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

find_package(MagnumPlugins REQUIRED CgltfImporter)

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

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 supports ImporterFeature::OpenData and ImporterFeature::FileCallback features. All buffers are loaded on-demand and kept in memory for any later access. As a result, external file loading callbacks are called with InputFileCallbackPolicy::LoadPermanent. Resources returned from file callbacks can only be safely freed after closing the importer instance. In case of images, the files are loaded on-demand inside image2D() calls with InputFileCallbackPolicy::LoadTemporary and InputFileCallbackPolicy::Close is emitted right after the file is fully read.

Error reporting by cgltf is rather rudimentary, resulting in vague error messages and no line numbers for several classes of errors, including out-of-bounds indices and missing required attributes. If you need more detailed errors, consider using the glTF Validator.

The content of the global extensionsRequired array is checked against all extensions supported by the plugin. If a glTF file requires an unknown extension, the import will fail. This behaviour can be disabled with the ignoreRequiredExtensions configuration option.

Import of morph data is not supported at the moment.

Animation and skin import

  • Linear quaternion rotation tracks are postprocessed in order to make it possible to use the faster Math::lerp() / Math::slerp() functions instead of Math::lerpShortestPath() / Math::slerpShortestPath(). Can be disabled per-animation with the optimizeQuaternionShortestPath configuration option. This doesn't affect spline-interpolated rotation tracks.
  • If linear quaternion rotation tracks are not normalized, the importer prints a warning and normalizes them. Can be disabled per-animation with the normalizeQuaternions configuration option. This doesn't affect spline-interpolated rotation tracks.
  • Skin skeleton property is not imported
  • Morph targets are not supported
  • Animation tracks are always imported with Animation::Extrapolation::Constant, because glTF doesn't support anything else
  • It's possible to request all animation clips to be merged into one using the mergeAnimationClips option in order to for example preserve cinematic animations when using the Blender glTF exporter (as it otherwise outputs a separate clip for each object). When this option is enabled, animationCount() always report either 0 or 1 and the merged animation has no name. With this option enabled, however, it can happen that multiple conflicting tracks affecting the same node are merged in the same clip, causing the animation to misbehave.

Scene and object import

  • If no "scene" property is present and the file contains at least one scene, defaultScene() returns 0 instead of -1. According to the glTF 2.0 specification the importer is free to not render anything, but the suggested behavior would break even some official sample models.
  • In case object transformation is set via separate translation/rotation/scaling properties in the source file, ObjectData3D is created with ObjectFlag3D::HasTranslationRotationScaling and these separate properties accessible
  • If object rotation quaternion is not normalized, the importer prints a warning and normalizes it. Can be disabled per-object with the normalizeQuaternions configuration option.

Camera import

  • Cameras in glTF are specified with vertical FoV and vertical:horizontal aspect ratio, these values are recalculated for horizontal FoV and horizontal:vertical aspect ratio as is common in Magnum

Light import

Mesh import

Custom and unrecognized vertex attributes of allowed types are present in the imported meshes as well. Their mapping to/from a string can be queried using meshAttributeName() and meshAttributeForName(). Attributes with unsupported types (such as non-normalized integer matrices) cause the import to fail.

Material import

Texture and image import

  • Texture type is always Trade::TextureType::Texture2D, as glTF doesn't support anything else
  • Z coordinate of Trade::TextureData::wrapping() is always SamplerWrapping::Repeat, as glTF doesn't support 3D textures
  • glTF leaves the defaults of sampler properties to the application, the following defaults have been chosen for this importer:

  • The importer supports the following extensions for image types not defined in the core glTF 2.0 specification: MSFT_texture_dds for DirectDraw Surface images (*.dds), KHR_texture_basisu for Khronos Texture 2.0 images (*.ktx2) with Basis Universal supercompression, as well as the original provisional GOOGLE_texture_basis extension for referencing plain Basis Universal files (*.basis). There was no formal specification of the extension but the use is like below, equivalently to Basis own glTF example:

    {
        ...
        "textures": [
            {
                "extensions": {
                    "GOOGLE_texture_basis": {
                        "source": 0
                    }
                }
            }
        ],
        "images": [
            {
                "mimeType": "image/x-basis",
                "uri": "texture.basis"
            }
        ],
        "extensionsUsed": [
            "GOOGLE_texture_basis"
        ],
        "extensionsRequired": [
            "GOOGLE_texture_basis"
        ]
    }

    The MIME type (if one exists) is ignored by the importer. Delegation to the correct importer alias happens via AnyImageImporter which uses the file extension or buffer content to determine the image type.

Plugin-specific config

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

[configuration]

# Don't fail the import and only print a warning if an unknown or unsupported
# extension is listed in extensionsRequired. Some things might be missing or
# not get imported correctly if this is enabled.
ignoreRequiredExtensions=false

# Optimize imported linearly-interpolated quaternion animation tracks to
# ensure shortest path is always chosen. This can be controlled separately for
# each animation import.
optimizeQuaternionShortestPath=true

# Normalize transformation quaternions and linearly-interpolated quaternion
# animation tracks, if they are not already. Note that spline-interpolated
# quaternion animation tracks are not patched. This can be controlled
# separately for each object/animation import.
normalizeQuaternions=true

# Merge all animations into a single clip. Useful for preserving cinematic
# animations when using the Blender glTF exporter, as it exports animation of
# every object as a separate clip. See https://blender.stackexchange.com/q/5689
# and https://github.com/KhronosGroup/glTF-Blender-Exporter/pull/166 for more
# information.
mergeAnimationClips=false

# 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 opening a file, changing it during import
# will have undefined behavior.
textureCoordinateYFlipInMaterial=false

# The non-standard MeshAttribute::ObjectId is by default recognized under this
# name. Change if your file uses a different identifier.
objectIdAttribute=_OBJECT_ID

# Provide basic Phong material attributes even for PBR materials in order to be
# compatible with PhongMaterialData workflows from version 2020.06 and before.
# This option will eventually become disabled by default.
phongMaterialFallback=true

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

Base classes

class AbstractImporter
Base for importer plugins.

Constructors, destructors, conversion operators

CgltfImporter() explicit
Default constructor.
CgltfImporter(PluginManager::Manager<AbstractImporter>& manager) explicit
Constructor.
CgltfImporter(PluginManager::AbstractManager& manager, const std::string& plugin) explicit
Plugin manager constructor.

Function documentation

Magnum::Trade::CgltfImporter::CgltfImporter() explicit

Default constructor.

In case you want to open images, use CgltfImporter(PluginManager::Manager<AbstractImporter>&) instead.

Magnum::Trade::CgltfImporter::CgltfImporter(PluginManager::Manager<AbstractImporter>& manager) explicit

Constructor.

The plugin needs access to plugin manager for importing images.