fromAsset static method

Future<ShaderLibrary?> fromAsset(
  1. String assetName
)

Loads the shader bundle at assetName and returns its ShaderLibrary, or null if the bundle could not be parsed.

This is async so it can work on every platform. Some platforms can only read assets asynchronously, so loading is async everywhere for a consistent API. On platforms that can read synchronously the returned Future still completes in the same turn. A load failure is reported through the Future rather than thrown synchronously.

The library is cached by assetName, so repeated loads of the same asset return the same instance. The cache also backs hot reload (see reinitialize), which looks the library up by asset path.

Implementation

static Future<ShaderLibrary?> fromAsset(String assetName) async {
  final cached = _registry[assetName];
  if (cached != null) {
    return cached;
  }
  final lib = ShaderLibrary._();
  final error = lib._initializeWithAsset(assetName);
  if (error != null) {
    throw Exception("Failed to initialize ShaderLibrary: ${error}");
  }
  _registry[assetName] = lib;
  assert(() {
    _ensureHooksRegistered();
    return true;
  }());
  return lib;
}