fromPixels static method
Texture2D
fromPixels(
- Uint8List pixels,
- int width,
- int height, {
- TextureContent content = TextureContent.color,
- TextureSampling sampling = const TextureSampling(),
Builds a texture from RGBA8888 pixels (straight alpha, row-major) of
width x height.
Implementation
static Texture2D fromPixels(
Uint8List pixels,
int width,
int height, {
TextureContent content = TextureContent.color,
TextureSampling sampling = const TextureSampling(),
}) {
var levels = sampling.mipmaps
? generateMipChain(pixels, width, height, content)
: <MipLevel>[MipLevel(width, height, pixels)];
// The GPU allocator caps mip levels at fullMipCount (floor(log2(min(w, h)))),
// which is below the canonical chain length for non-square textures, so
// clamp before requesting the texture or creation throws a range error.
final maxLevels = gpu.Texture.fullMipCount(width, height);
final cap = sampling.maxMipmapLevels;
final limit = cap != null && cap >= 1 && cap < maxLevels ? cap : maxLevels;
if (limit < levels.length) {
levels = levels.sublist(0, limit);
}
final texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
width,
height,
mipLevelCount: levels.length,
);
for (var i = 0; i < levels.length; i++) {
texture.overwrite(ByteData.sublistView(levels[i].pixels), mipLevel: i);
}
return Texture2D._(texture, sampling.toSamplerOptions());
}