-
Notifications
You must be signed in to change notification settings - Fork 0
/
madelbrot_factal_shader_page.dart
106 lines (91 loc) · 2.73 KB
/
madelbrot_factal_shader_page.dart
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import 'dart:ui';
import 'package:flutter/material.dart';
void main(List<String> args) {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: MandelbrotFractalPage(),
));
}
class MandelbrotFractalPage extends StatefulWidget {
const MandelbrotFractalPage({super.key});
@override
State<MandelbrotFractalPage> createState() => _MandelbrotFractalPageState();
}
class _MandelbrotFractalPageState extends State<MandelbrotFractalPage> {
Future<FragmentProgram> loadMyShader() async =>
await FragmentProgram.fromAsset('assets/shaders/mandelbrot_fractal.frag');
late final Future<FragmentProgram> _program = loadMyShader();
double time = 0.0;
final controller = TransformationController();
@override
void dispose() {
controller.dispose();
super.dispose();
}
bool colorize = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
floatingActionButton: Row(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(onPressed: () {
controller.value = Matrix4.identity();
}),
FloatingActionButton(onPressed: () {
colorize = !colorize;
setState(() {});
}),
],
),
body: Center(
child: FutureBuilder<FragmentProgram>(
future: _program,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("error loading shader: ${snapshot.error}");
}
if (snapshot.hasData == false) {
return const Text("loading shader...");
}
final shader = snapshot.data!;
return InteractiveViewer(
transformationController: controller,
minScale: 1,
maxScale: 150000,
child: CustomPaint(
painter: MandelbrotFractalPainter(
shader,
showColor: colorize,
),
child: const SizedBox.expand(),
),
);
},
),
),
);
}
}
class MandelbrotFractalPainter extends CustomPainter {
const MandelbrotFractalPainter(
this.program, {
this.showColor = false,
});
final FragmentProgram program;
final bool showColor;
@override
void paint(Canvas canvas, Size size) {
final shader = program.fragmentShader()
..setFloat(0, size.width)
..setFloat(1, size.height)
..setFloat(2, showColor ? .1 : 0);
canvas.drawRect(
Rect.fromLTWH(0, 0, size.width, size.height),
Paint()..shader = shader,
);
}
@override
bool shouldRepaint(MandelbrotFractalPainter oldDelegate) => oldDelegate.showColor != showColor;
}