-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.dart
More file actions
47 lines (37 loc) · 1.24 KB
/
Copy pathutils.dart
File metadata and controls
47 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_gpu/gpu.dart' as gpu;
Future<gpu.Texture> loadTextureFromAsset(String assetPath) async {
final ByteData assetData = await rootBundle.load(assetPath);
final ui.Codec codec = await ui.instantiateImageCodec(
assetData.buffer.asUint8List(),
);
final ui.FrameInfo frame = await codec.getNextFrame();
final ui.Image image = frame.image;
final ByteData? rgbaBytes = await image.toByteData(
format: ui.ImageByteFormat.rawRgba,
);
if (rgbaBytes == null) {
throw Exception(
'An error occurred while converting $assetPath to RGBA ByteData');
}
final texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
image.width,
image.height,
format: gpu.PixelFormat.r8g8b8a8UNormInt,
sampleCount: 1,
coordinateSystem: gpu.TextureCoordinateSystem.uploadFromHost,
enableRenderTargetUsage: false,
enableShaderReadUsage: true,
enableShaderWriteUsage: false,
);
if (texture == null) {
throw Exception(
'An error occurred while creating a Texture for $assetPath');
}
if (!texture.overwrite(rgbaBytes)) {
throw Exception('texture.overwrite returned false');
}
return texture;
}