gpuTextureFromImage function
Uploads a decoded dart:ui ui.Image to a Flutter GPU texture.
The image is read as raw RGBA bytes and copied into a host-visible GPU texture matching the image's dimensions. The returned texture is suitable for binding to materials such as UnlitMaterial.baseColorTexture or PhysicallyBasedMaterial.baseColorTexture, or for building an EnvironmentMap.
Throws if the image can't be read as RGBA.
Implementation
Future<gpu.Texture> gpuTextureFromImage(ui.Image image) async {
final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
if (byteData == null) {
throw Exception('Failed to get RGBA data from image.');
}
// Upload the RGBA image to a Flutter GPU texture.
final texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
image.width,
image.height,
);
texture.overwrite(byteData);
return texture;
}