Build tools for Flutter GPU shader bundles/libraries.
Use native asset build hooks to import Flutter GPU shader bundle assets.
flutter config --enable-native-assets
shaders/my_cool_shader.vert
and shaders/my_cool_shader.frag
..shaderbundle.json
. For this example, we'll assume the following file is saved as my_cool_bundle.shaderbundle.json
:
{
"CoolVertex": {
"type": "vertex",
"file": "shaders/my_cool_shader.vert"
},
"CoolFragment": {
"type": "fragment",
"file": "shaders/my_cool_shader.frag"
}
}
buildShaderBundleJson
. The build hook must be named hook/build.dart
in your project; this script will be automatically invoked by Flutter when the "native assets" feature is enabled:
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:flutter_gpu_shaders/build.dart';
void main(List<String> args) async {
await build(args, (config, output) async {
await buildShaderBundleJson(
buildConfig: config,
buildOutput: output,
manifestFileName: 'my_cool_bundle.shaderbundle.json');
});
}
pubspec.yaml
, add an asset import rule to package the built shader bundles (this will become unnecessary once "native assets" supports DataAsset
in a future release of Flutter):
flutter:
assets:
- build/shaderbundles/*.shaderbundle.json
gpu.ShaderLibrary.fromAsset
in your project. For example:
import 'package:flutter_gpu/gpu.dart' as gpu;
final String _kBaseShaderBundlePath =
'packages/my_project/build/shaderbundles/my_cool_bundle.shaderbundle';
gpu.ShaderLibrary? _baseShaderLibrary = null;
gpu.ShaderLibrary get baseShaderLibrary {
if (_baseShaderLibrary != null) {
return _baseShaderLibrary!;
}
_baseShaderLibrary = gpu.ShaderLibrary.fromAsset(_kBaseShaderBundlePath);
if (_baseShaderLibrary != null) {
return _baseShaderLibrary!;
}
throw Exception(
"Failed to load base shader bundle! ($_kBaseShaderBundlePath)");
}