API Reference · Model and texture loading

GltfAsset

Namespace: Ava3D

public sealed class GltfAsset

A glTF file, read once. Immutable and safe to share: every copy of the model on screen is an instance of this, and the geometry and textures are decoded and uploaded once however many there are.

This is the whole of the model pipeline. GltfLoader.LoadModel is load-then-instantiate in one call, which is right for a viewer with one model in it; a game with a crowd loads the asset once and instantiates it per character. A .glb loads from bytes alone; a .gltf with external buffers and images needs a GltfLoadOptions.Resolver, or a Uri whose scheme picks one.

Properties

MemberDescription
IReadOnlyList<AnimationClip> Clips { get; }

The clips the file declared, in its own order, bound to the asset's own template nodes. An instance gets its own copies bound to its own nodes; these are for looking at names and durations.

IReadOnlyList<GltfDiagnostic> Diagnostics { get; }

Everything the reader could not carry over, and why. Empty for a file that loaded whole. Worth logging once per asset: it is the only way to learn that a texture is missing because it was WebP, or that a rig stands still because one joint is outside the scene.

Nullable<Text.Json.JsonElement> Extras { get; }

The file's root-level extras, when it has any.

IReadOnlyList<Material> Materials { get; }

The materials as read, one per glTF material that a drawn primitive uses (two, when one material covers both a primitive with texture coordinates and one without). Instances wear these or copies of them, by InstantiateOptions.Materials.

IReadOnlyList<Mesh> Meshes { get; }

Every mesh in the file that is drawn, one per glTF primitive, however many nodes draw it. Shared by every instance; the geometry is uploaded once.

string Name { get; }

The name given at load, which every instance's root node takes unless told otherwise.

int NodeCount { get; }

How many glTF nodes are in the scene that was read.

IReadOnlyList<Skin> Skins { get; }

The skins the file declared, bound to the template nodes. Instances get their own.

IReadOnlyList<Texture> Textures { get; }

Every texture, one per image-and-wrap-mode the file's materials use. Shared by every instance.

Methods

MemberDescription
Nullable<BoundingBox> AuthoredBounds(Mesh mesh)

The bounds the file authored for a mesh — its POSITION accessor's min and max — or null when it wrote none. The same box as Mesh.Bounds computes, but available without touching the vertices, which is what a residency decision wants.

Nullable<Text.Json.JsonElement> ExtrasOf(Material material)

The glTF material's extras, or null when it has none or material is not from this asset.

Nullable<Text.Json.JsonElement> ExtrasOf(Mesh mesh)

The glTF mesh's extras, or null when it has none or mesh is not from this asset.

GltfInstance Instantiate(InstantiateOptions options = default)

A fresh copy of the model: its own node tree, skins bound to its own joints, its own animator and morph state, sharing this asset's meshes and textures — and its materials, unless options asks for copies. Safe to call from any thread, and from several at once.

static GltfAsset Load(ReadOnlyMemory<byte> bytes, GltfLoadOptions options = null)

Reads a .glb or .gltf from bytes, on the calling thread.

External resources, if the file has any, go through GltfLoadOptions.Resolver and this waits for each — fine for a file on disk, and the reason GltfAsset.LoadAsync exists for anything that is not.

static Threading.Tasks.Task<GltfAsset> LoadAsync(ReadOnlyMemory<byte> bytes, GltfLoadOptions options = null, Threading.CancellationToken cancellationToken = default)

Reads a .glb or .gltf from bytes, fetching any external resources through GltfLoadOptions.Resolver without blocking. The parsing itself still runs on the calling thread; wrap the call in Task.Run to keep a UI responsive through a large file.

static Threading.Tasks.Task<GltfAsset> LoadAsync(Uri source, GltfLoadOptions options = null, Threading.CancellationToken cancellationToken = default)

Reads a file by address. A file:// URI is read from disk and its directory becomes the base for relative resources; avares:// is read from the application's resources through GltfAvaloniaResolver; http:// and https:// are fetched through GltfHttpResolver. Any other scheme needs GltfLoadOptions.Resolver, which also overrides the choice for the three above and is handed the URI as its first request.

Nullable<ValueTuple<int, int>> SourceOf(Mesh mesh)

Which glTF mesh and primitive a Mesh came from, or null when it is not from this asset.

See also