preload method

Future<void> preload()

Resolves the resources that need asynchronous work (external image assets, encoded image payloads, and fmat materials), caching them so the synchronous realize path finds them ready.

Await this before realizing a document that may reference such resources (the async loaders do). A resource that fails to load degrades to a placeholder (textures) or an unlit material (fmat) with a warning, rather than failing the whole scene.

Implementation

Future<void> preload() async {
  // Textures first: an fmat material's parameter overrides may reference a
  // texture resource, which must be decoded before the override resolves it.
  final textures = <Future<void>>[];
  for (final resource in document.resources.values) {
    if (resource is TextureResource && _needsAsyncTexture(resource)) {
      textures.add(_preloadTexture(resource));
    }
  }
  await Future.wait(textures);

  final materials = <Future<void>>[];
  for (final resource in document.resources.values) {
    if (resource is MaterialResource && resource.type == 'fmat') {
      materials.add(_preloadFmat(resource));
    }
  }
  await Future.wait(materials);
}