As participants in the Factshala Innovation Lab, we are building an in-classroom game about media manipulation. This came with a unique set of requirements :
We chose Flutter as the framework to implement this. One of the reasons for choosing Flutter was that flutter's APIs for working with shaders seemed very intuitive. Using the 1CustomPainter
class you can use dart to control drawing operations on a canvas. And to this same class you can pass a fragment shader to control the painting.
We went from 0 to loading shaders, working with textures and implementing interactivity. This guide will serve as a retracing of steps to share what we learnt.
1git clone https://github.com/dennyabrain/flutter_shader_demo.git
2git checkout basic-setup
Try Running this project. It should launch an app :
1var program = await FragmentProgram.fromAsset('shaders/shader_tex.frag');
2shader = program.fragmentShader();
1class ShaderPainter extends CustomPainter {
2 final FragmentShader shader;
3
4 ShaderPainter(FragmentShader fragmentShader) : shader = fragmentShader;
5
6
7 void paint(Canvas canvas, Size size) {
8 shader.setFloat(0, size.width);
9 shader.setFloat(1, size.height);
10
11 final paint = Paint();
12
13 paint.shader = shader;
14 canvas.drawRect(Offset.zero & size, paint);
15 }
16
17
18 bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
19}
Various milestones are pushed as tags here. You can always check out a particular tag to try out working stable code.
This is a work-in-progress post. Contact denny for feedback and clarifications.