fromEncodedBytes static method

Future<Texture2D> fromEncodedBytes(
  1. Uint8List bytes, {
  2. TextureContent content = TextureContent.color,
  3. TextureSampling sampling = const TextureSampling(),
  4. int? maxSize,
})

Decodes and uploads an encoded image (PNG, JPEG, and whatever else the platform reads) from bytes.

Where the backend decodes for itself (the web, whose browser decodes off the main thread and whose mip chain is built on the GPU) the pixels never pass through Dart; elsewhere the platform codec decodes them. maxSize caps the longest side, scaling a larger image down as it decodes, so an app can bound texture memory without holding the full-size pixels.

Implementation

static Future<Texture2D> fromEncodedBytes(
  Uint8List bytes, {
  TextureContent content = TextureContent.color,
  TextureSampling sampling = const TextureSampling(),
  int? maxSize,
}) async {
  final direct = await gpuTextureFromEncodedBytes(
    bytes,
    content: content,
    mipmaps: sampling.mipmaps,
    maxMipmapLevels: sampling.maxMipmapLevels,
    maxSize: maxSize,
  );
  if (direct != null) return Texture2D._(direct, sampling.toSamplerOptions());
  final image = await decodeEncodedImage(bytes, maxSize: maxSize);
  try {
    return await fromImage(image, content: content, sampling: sampling);
  } finally {
    image.dispose();
  }
}