initializeStaticResources static method
Prepares the rendering resources, such as textures and shaders, that are used to display models in this Scene.
This method ensures all necessary resources are loaded and ready to be used in the rendering pipeline.
Returns a Future that completes when the initialization is finished,
and that completes with the load's error when it fails. A failure also
resets the resources and leaves the scene not ready to render, so a
caller that awaits this without catching gets the error rather than a
misleading one from the first draw. SceneView reports it and stays on
its loading builder.
Implementation
static Future<void> initializeStaticResources() {
if (_initializeStaticResources != null) {
return _initializeStaticResources!;
}
listenForMemoryPressure();
_initializeStaticResources =
Future.wait([
loadBaseShaderLibrary(),
Material.initializeStaticResources(),
// The physical material shaders and the SMAA tables load on
// first use or through preload, so a scene using neither never
// pays for them.
])
// Needs the shader library, so it runs after the load and before
// rendering unblocks (environment radiance builds consult it).
.then((_) => probePlatformMipSampling())
.then((_) {
_readyToRender = true;
})
.onError<Object>((e, stacktrace) {
// Only a successful load marks the scene ready to render;
// rendering with these resources missing throws mid-frame.
// The memoized future is reset so a later call retries.
log(
'Failed to initialize static Flutter Scene resources',
error: e,
stackTrace: stacktrace,
);
_initializeStaticResources = null;
// Rethrow so an awaiting caller sees the real cause. Completing
// normally here left the failure visible only through
// `dart:developer` log(), which web does not surface, and sent
// the developer to the baseShaderLibrary getter's "await
// initializeStaticResources()" instead, the call they just made.
Error.throwWithStackTrace(e, stacktrace);
});
return _initializeStaticResources!;
}