loadModelData function

Future<ModelData> loadModelData(
  1. Uint8List bytes, {
  2. required String name,
  3. ModelFetch? fetch,
})

Reads a glTF model — .glb or .gltf — into ModelData.

The files it refers to (modelFilesOf) are fetched with fetch, all at once; reading the JSON and the accessors, decoding data: URIs and building vertices run in a background isolate. Throws UnsupportedError for a file this renderer can't show (Draco, meshopt, KTX2), FormatException for a malformed one, and StateError when it refers to files and no fetch was given.

Implementation

Future<ModelData> loadModelData(
  Uint8List bytes, {
  required String name,
  ModelFetch? fetch,
}) async {
  final paths = fetch == null
      ? const <String>[]
      // A `.glb`'s JSON chunk is small; a `.gltf` may embed megabytes of
      // base64 in its JSON, so it is read in the background.
      : isGlb(bytes)
      ? modelFilesOf(bytes)
      : await compute(modelFilesOf, bytes);
  final fetched = await Future.wait([for (final path in paths) fetch!(path)]);
  return compute(
    _build,
    _Job(bytes, {
      for (var i = 0; i < paths.length; i++) paths[i]: fetched[i],
    }, name),
  );
}