Skip to content

Instantly share code, notes, and snippets.

@ponnamkarthik
Last active February 9, 2023 09:26
Show Gist options
  • Save ponnamkarthik/e9938ffb1e40e30172ebc06a956d966c to your computer and use it in GitHub Desktop.
Save ponnamkarthik/e9938ffb1e40e30172ebc06a956d966c to your computer and use it in GitHub Desktop.
class ShaderHomePage extends StatefulWidget {
const ShaderHomePage({super.key});
@override
State<ShaderHomePage> createState() => _ShaderHomePageState();
}
class _ShaderHomePageState extends State<ShaderHomePage> {
late Timer timer;
double delta = 0;
FragmentShader? shader;
void loadMyShader() async {
var program = await FragmentProgram.fromAsset('shaders/shader.frag');
shader = program.fragmentShader();
setState(() {
// trigger a repaint
});
timer = Timer.periodic(const Duration(milliseconds: 16), (timer) {
setState(() {
delta += 1 / 60;
});
});
}
@override
void initState() {
super.initState();
loadMyShader();
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (shader == null) {
return const Center(child: CircularProgressIndicator());
} else {
return CustomPaint(painter: ShaderPainter(shader!, delta));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment